I have just started another project and I need a bit of help. This time I need to generate square waves at a few frequencies (1 Hz and 100 Hz to name a few). A button will be used to change between the frequencies. The hardware is very simple, just an ATTiny2313 MCU with an 8 MHz external crystal (with 2 x 22pF caps on its legs) and a led connected via a 1K resistor to pin 9 of the chip. Software wise, I thought of using 16-bit Timer1 and generate the pulses via software.
First, I tried to toggle a LED at 1 Hz by modifying an example from the manual (code below). The problem is that the LED seems to stay on for 2 seconds and off for 2 seconds so the frequency is about 0.5 Hz instead of 1 Hz. Any idea what I'm doing wrong?
#option explicit#chip tiny2313, 8#config OSC = EXT#define LED PORTD.5DirLEDOUTInittimer1OSC,PS_256Starttimer1Settimer1,34286;preloadtimerwith65536-(8MHz/256/1Hz)OnInterruptTimer1OverflowCallFlash_LEDDo'Wait for interruptloopSubFlash_LEDSettimer1,34286'Preload timerLED=!LEDEndSub
Thanks!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Simply put, the frequency is equal to 1/2 the period of the waveform. To put it another way, with a 50% duty cycle you would expect the timer to be high 1/2 the time and low the other half. So assuming the above equation is correct then it should be 8Mhz / (2* 256 * 1).
Check out the data sheet if you want to automate that procedure using the Clear Timer on Compare (CTC) mode. It will give a 50% duty cycle without using interrupts. Six to one, half a dozen to the other, both will work fine.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I have just started another project and I need a bit of help. This time I need to generate square waves at a few frequencies (1 Hz and 100 Hz to name a few). A button will be used to change between the frequencies. The hardware is very simple, just an ATTiny2313 MCU with an 8 MHz external crystal (with 2 x 22pF caps on its legs) and a led connected via a 1K resistor to pin 9 of the chip. Software wise, I thought of using 16-bit Timer1 and generate the pulses via software.
First, I tried to toggle a LED at 1 Hz by modifying an example from the manual (code below). The problem is that the LED seems to stay on for 2 seconds and off for 2 seconds so the frequency is about 0.5 Hz instead of 1 Hz. Any idea what I'm doing wrong?
Thanks!
Simply put, the frequency is equal to 1/2 the period of the waveform. To put it another way, with a 50% duty cycle you would expect the timer to be high 1/2 the time and low the other half. So assuming the above equation is correct then it should be 8Mhz / (2* 256 * 1).
Check out the data sheet if you want to automate that procedure using the Clear Timer on Compare (CTC) mode. It will give a 50% duty cycle without using interrupts. Six to one, half a dozen to the other, both will work fine.
Thanks for the input, it got me going in the right direction. I really need to be a bit more thorough when learning. Cheers!