My microcontroller project has a small design mistake: I'm driving a small
> buzzer but I accidentally connected it to the wrong pin. It should have
> been connected to the OC0 pin which allows me to simply set the Timer0
> register and it will produce a PWM output with a certain frequency.
> Connecting the buzzer to the wrong pin forces me drive the buzzer 'manually.'
>
> So I'm planning to generate an timer interrupt and toggle the pin there
> but this will mean that the number of interrupts will equal the buzzer
> frequency, about 4000 hz in my case. The ISR (interrupt service routine)
> should be very small but I'm wondering if the AVR will be up to the task.
> Has anyone done something similar? BTW I'm driving the Mega-AVR with a
> 8Mhz crystal but I can upgrade it to 16Mhz if needed.
I once did a 10000 Hz ISR that does an update for 4 stepper motors, and sends the drive signals out on a 32 bit SPI chain. This was on an atmega128 using a 16MHz xtal. All code written in C. So, 4000 Hz with a simpler ISR should be fairly easy.. assuming of course you don't have too many other things going on.
On 14/03/2006 the venerable George Orwell etched in runes:
Actually to produce a 4kHz signal you're looking at an 8kHz ISR rate. One ISR for each transition. With an 8MHz crystal that's 1000 clock cycles per ISR.
If you can't change the pin then you might be able to change the buzzer to a self-oscillating one that only needs to be switched on and off. Seems like a better solution to me.
I've also had 10 kHz interrupt routines on an AVR, running at 8 MHz. But I think the tightest challenge was running a 38.4 kbaud software UART with 4 times sampling (153.6 kHz interrupts) on a 7.37 MHz AVR, giving precisely 48 clock ticks between each interrupt.
So 4000 Hz is no problem. If you want to minimise the overhead, write the ISR in assembly. Figure out how many (if any) registers you need, and reserve them from use by your compiler. That way you don't need any context saving or restoring, except perhaps for the status register, and all your ISR-critical data is in registers when the code starts.
I've done a 2kHz piezo using a 4MHz '05 (1MHz bus rate) (interrupt every 250usec), with enough time left over to do 32-bit PID control,
15-bit slope ADC and scan displays/keyboard, so I don't think you'll have any problems with an 8Mhz AVR 'less'n you do something silly like write the ISR in C.
Best regards, Spehro Pefhany
--
"it's the network..." "The Journey is the reward"
speff@interlog.com Info for manufacturers: http://www.trexon.com
The HPC has more than just a few MIPs - can't they run at up to 40 MHz? (Our HPC apps never used more than 10 MHz.) About 15 years ago, they were one of the fastest microcontrollers available. Still, at 250,000 context switches a second, there can't have been much left cpu time left after the context switch overhead?
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.