Timers :( Newbie needs help

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK, Timers have done me in. Timers, plus trying to edit a large post here, typing it twice now, rather than doing it offline and pasting it, and losing it..... Aaaarghhhh. Well, I've learnt my lesson on that one. Here's one I prepared earlier.

Back to the real reason of my post. Timers. My little project is coming along ok. Many of the sub-tasks that I have set myself are coming together, but sound is proving a little more challenging. I guess I'm at an impasse at the moment. (for the record: ATMEGA32, 16MHz external crystal, C programming in WinAVR GCC, AVR Studio for debugging where possible. I've searched high and low for code snippets and guidance from previous forum posts, but a lot of it seems based on problems that aren't the same as mine. I've read the data sheet for MEGA32, but the timers section didn't shed any light on things, it perhaps confused me more as I'm not sure what timer mode I need.)

Here's what I need to have: the ability to create two particular frequency sounds using a buzzer attached to PD4 of my Mega32 Futurlec controller board. This has been prototyped, using a square wave pin off/pin on program that simply used a wait calculation. This has alllowed me to test different frequencies and I've now settled on the two I need - 4kHz and 1.6kHz. Now the tricky part has come up..... I need to be able to continue driving a 6-digit 7-seg LED display at the same time as I make these sounds, so a home-grown wait is not going to work..... As the board I'm using conveniently uses PD4 as the buzzer output, it probably means that using Timer1 and OCR1B output would make sense. But I'm lost figuring out exactly what mode I need, and what I have to set to enable that mode. :-S

The next challenge is that for the 4kHz sound, it needs to run for about 300msec, while the 1.6kHz needs to run for about 700msec. (I say "about", as some msec either way won't be audibly different for the application I'm using it for). So I reckon I need to use another timer. For this one I could *probably* use an 8-bit timer, and use that to "tick" over in an interrupt routine and catch it in MAIN to stop the buzzer, but perhaps there's another more efficient way? Not that I'm worried about CPU cycles, well at the moment anyway....

So here are my questions:
How do I configure timer1 for outputting a frequency on PD4? I *think* I want CTC mode, but I'm really not sure??
Does anyone have a suggestion about dealing with the long delay timing of 300msec / 700msec?
Is there a better way than using Timer0 or Timer2 (8-bit) and "ticks"?

I'd really appreciate some assistance here, or if someone has code samples that would be great as well.

Thanks in advance
Brett

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I haven't done this before, so no code samples, but it's fairly straightforward.

Quote:
How do I configure timer1 for outputting a frequency on PD4? I *think* I want CTC mode, but I'm really not sure??

Yes, to get a simple square wave you want CTC1 (WGM12) mode + the "toggle" setting for COM1Bx. With a prescale of /1
4kHz is a period of (250*16)=4000 ticks and 1.6kHz (625*16)=10000 ticks (but check my arithmetic).
Quote:
Does anyone have a suggestion about dealing with the long delay timing of 300msec / 700msec?

My vote would be to use the timer1 Output Compare interrupt (set OC1A==OC1B) and just count the cycles. 300ms@4kHz is 1200*2 interrupts, and
700ms@1.6kHz is 1120*2 interrupts. The duration-counts of these two are pretty close, though my suggestion would still be to
parameterize each "beep" using (OCR1B_value,duration_count) for the (inevitable) late-date specification change. Side-note:
read up on FOC1B -- you'll probably need it to kill the beep at the end.
Quote:
Is there a better way than using Timer0 or Timer2 (8-bit) and "ticks"?

As it happens, this also gets you out of using another timer.

Edit: Forgot to mention OC1A

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Poundy

This looks like an orienteering start clock application. Did you get it to go. You can see my solutions on finchhouse.org look for Clock project and KitST pages.
TF - Scotland

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, orienteering start clock, good guess :) Now moved on; never went to fruition as SportIdent got a much bigger step forward and its no longer relevant to me/us. Thanks for the link to your project though, seems similar to what I was going to end up with.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Indeed - but actually organising a start for a few 1000 runners needs a good layout and timing - hence the project. Originally for Scottish 6 Days event.