View previous topic :: View next topic |
Author |
Message |
CrazyIgels
Joined: 26 Oct 2007 Posts: 79
|
Posted: Mon Feb 04, 2008 8:35 am Post subject: 5 kHz square wave with TIMER0 - troubles again :( |
|
|
Hello All.
I have trouble with 5kHz generation... Again
I use code:
$regfile = "m64def.dat"
$crystal = 7372800
Config Timer0 = Timer , Prescale = 1
On Ovf0 Tim0_isr
Enable Timer0
Enable Interrupts
Stop Timer0
Dim Dt As Byte
Dim K As Byte
Start Timer0
K = 0
Dt = 10
Cycle:
Input "Dt=" , Dt
Goto Cycle
Tim0_isr:
K = K + 1
If K = Dt Then
Portc.3 = Not Portc.3
K = 0
End If
Return
END OF CODE
Now - i have square wave at pin 3 of port c. But if i set Dt variable to 3
the out frequency is 4.78 kHz. If i set Dt to 4 i have 3.62 kHz.
I need posiblity to set frequency precisely.
Like this: I send 3.5 to my board and output frequency set to 3.5kHz.
total range of frequency start from 2 kHz and go up to 10 kHz with step of 0.1 kHz
Please HELP!!!!
And again: sorry my bad english.
THANKS ALL!!! |
|
Back to top |
|
|
CrazyIgels
Joined: 26 Oct 2007 Posts: 79
|
Posted: Mon Feb 04, 2008 8:39 am Post subject: |
|
|
This is output signal with Dt = 3 |
|
Back to top |
|
|
mattcro
Joined: 03 Oct 2007 Posts: 327 Location: Scotland
|
Posted: Mon Feb 04, 2008 11:35 am Post subject: |
|
|
You're trying to implement a software timer there. To get the sort of timings you require, you'll have to use the hardware 16bit timer in PWM mode and set the TOP value (probably register ICR1). You can choose the period of the timer quite accurately, and then set the duty cycle (with output compare register) to be 50%. With careful selection of prescaler and TOP value, you should be able to get a square wave on an output compare pin at the desired frequency or close to it.
You'll need to set some timer register bits manually, because Bascom doesn't have a suitable config command for this mode. I don't have an example handy (search the forum), but you should be able to work it out from examples and the datasheet. _________________ If all else fails, read the manual. Even better: read the manual before something fails. If you can't find it in the manual, search the forum.
BascomAVR 2.0.8.5 |
|
Back to top |
|
|
yah996
Joined: 24 Sep 2006 Posts: 31 Location: Bergkirchen
|
Posted: Mon Feb 04, 2008 10:23 pm Post subject: |
|
|
You can preload the timer0 in the tim0_isr: to change the frequency closer to the hardware, like:
Tim0_isr:
Timer0 = 164 (this value should be changed in your program: like: timer=var_pre_scaler)
Toggle portc.3
Return
prescale=8 timerpreload=164 should generate a frequency about 10017kHz, ~ 5kHz output frequency.
I would use the toggle command like: toggle portc.3
Better to use a 16bit timer, if possible.
Be aware of the clock prescaler. If the prescale value is 1 on the 8bit timer, after 256 system clocks, an interrupt is fired up, each time. (A lot of overhead)
If you use an external oscillator, for driving the timer, your output should be more accurate. |
|
Back to top |
|
|
|