Hey all,
I'm working on code to drive a piezo buzzer from the VIA's Timer 1. I have hooked up the buzzer to PB7, and I'm using the timer with continuous toggling and no interrupts. The length of each tone is controlled by a delay loop.
Here is the code at the moment (acme assembler):
Code:
; to have the buzzer play an A4 note for 500ms, do the following:
; +macro play_tone_hz_ms 440, 500
!macro play_tone_hz_ms .frequency_hz, .duration_ms {
; Setup T1 for continuous operation, PB7 toggling
; T1 interrupts are disabled on the IER
lda # (VIA_ACR_T1_CONTINUOUS << VIA_ACR_T1_CONTROL_INT) | (VIA_ACR_T1_PB7_ON << VIA_ACR_T1_CONTROL_PB7)
sta IO_0_VIA_ACR
; cycles for half period = CPU_SPEED_MHZ * 1000000 / .frequency_hz / 2
@cycles = int( CPU_SPEED_MHZ * 1000000.0 / .frequency_hz / 2 )
@low_order_byte = @cycles & 255
@high_order_byte = (@cycles >> 8) & 255
lda # @low_order_byte
sta IO_0_VIA_T1CL
lda # @high_order_byte
sta IO_0_VIA_T1CH
+delay_ms .duration_ms
; disable the counter
stz IO_0_VIA_ACR
}
I'm wondering if I'm using the registers correctly. For each invocation of the macro, I am:
1. Enabling the counter in the ACR
2. Storing the desired counts
3. Waiting for the note to play
4. Disabling the counter in the ACR
However, this means that the counter values stay in the registers when I exit the macro. On the next use of this counter (in this or another macro), will the counter resume counting with old values before I have time to store my desired count? Should I store zero in the counter registers before disabling it? The datasheet does not really say what happens when you write zero to it.