6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Apr 28, 2024 10:44 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Thu Sep 01, 2022 10:08 am 
Offline
User avatar

Joined: Sat Jul 24, 2021 1:37 pm
Posts: 282
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.

_________________
BB816 Computer YouTube series


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 01, 2022 4:10 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
Make sure you make PB7 an output. Otherwise it looks ok. I think what will happen when you stop it though is that T1 will keep counting down, but with no effect on anything. If you want to make sure the first half cycle is (approximately) the right length when you re-start the beep, you'll need to write to the counter before setting the ACR to 11xxxxxx. (BTW, I would do LDA ACR, ORA #%11000000, STA ACR, so other things you have going there are not affected.)

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 01, 2022 9:52 pm 
Offline
User avatar

Joined: Sat Jul 24, 2021 1:37 pm
Posts: 282
Thanks, Garth, that's helpful! Below is my new code.
I ended up switching the order of setting the latches and enabling the counter to account for this, as well as making sure I reset the latches to zero at the end, just to be in a known state. I also hear you for using ORA, but nothing else is touching the ACR, so I left it as is for now.
Finally, I had the formula to compute the counter values wrong, as you need to subtract 2 from the value to get an accurate number of clock cycles. Indeed, storing zero in the counter results in PB7 toggling every two clock cycles.

Code:
!macro play_tone_hz_ms .frequency_hz, .duration_ms {

  ; cycles for half period = CPU_SPEED_MHZ * 1000000 / .frequency_hz / 2
  ; storing 0 in the counter results in PB7 toggling every two cycles, storing 1 results in toggling every three cycles.
  ; So we need to substract 2 from the value to get the accurate frequency.
  ; example: CPU_SPEED_MHZ=3.33MHz, .frequency_hz = 555kHz => cycles should be 1
  @cycles = int( CPU_SPEED_MHZ * 1000000.0 / .frequency_hz / 2 ) - 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

  ; Setup T1 for continuous operation, PB7 toggling
  ; T1 interrupts are disabled in 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

  +delay_ms .duration_ms

  ; disable the counter and reset the values
  stz IO_0_VIA_ACR
  stz IO_0_VIA_T1CL
  stz IO_0_VIA_T1CH

}

_________________
BB816 Computer YouTube series


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 07, 2022 8:50 pm 
Offline
User avatar

Joined: Wed Mar 01, 2017 8:54 pm
Posts: 660
Location: North-Germany
I once programmed my SYM-1 to play a few notes of "Pour Elise" using two T1's on two VIAs.
To implement "silence" stopping T1 wasn't helpful, it frequently causes an audible glitch. I solved this by switching to ultrasonic. To save my tweeters I added a simple RC low pass ....

Good luck.


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 07, 2022 8:56 pm 
Offline
User avatar

Joined: Sat Jul 24, 2021 1:37 pm
Posts: 282
I might just make it play a song as well :-)

Are they vulnerable to high frequencies?

_________________
BB816 Computer YouTube series


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 07, 2022 9:13 pm 
Offline
User avatar

Joined: Wed Mar 01, 2017 8:54 pm
Posts: 660
Location: North-Germany
In regular music the amount of high frequencies and their volume is usually low. Many speakers do not have high power tweeters. The speakers I had then could be used up to 20 W or so, but the tweeters were specified for 5W max only.

Even some amplifiers could suffer from ultrasonic signals. So it is better to use a low pass filter to stay save :)


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 07, 2022 9:14 pm 
Offline
User avatar

Joined: Sat Jul 24, 2021 1:37 pm
Posts: 282
Gotcha! I'm using a piezzo buzzer, not sure what power it is rated for, I'll have to look that up

_________________
BB816 Computer YouTube series


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 07, 2022 9:29 pm 
Offline
User avatar

Joined: Wed Mar 01, 2017 8:54 pm
Posts: 660
Location: North-Germany
Piezzos are tough. But perhaps your neighbours canary not :shock:


Top
 Profile  
Reply with quote  
PostPosted: Wed Sep 07, 2022 9:37 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
The piezoelectric transducers that are around .015" (0.38mm) thick and can be taped or soldered to a surface for a sounding board are mostly like a .02uF capacitor, and at audio frequencies, they can handle far more voltage than you'll ever give them with a 5V circuit.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 22 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: