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