Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

5 kHz square wave with TIMER0 - troubles again :(

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
CrazyIgels

Bascom Member



Joined: 26 Oct 2007
Posts: 79

russia.gif
PostPosted: Mon Feb 04, 2008 8:35 am    Post subject: 5 kHz square wave with TIMER0 - troubles again :( Reply with quote

Hello All.

I have trouble with 5kHz generation... Again Sad
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
View user's profile
CrazyIgels

Bascom Member



Joined: 26 Oct 2007
Posts: 79

russia.gif
PostPosted: Mon Feb 04, 2008 8:39 am    Post subject: Reply with quote

This is output signal with Dt = 3
Back to top
View user's profile
mattcro

Bascom Member



Joined: 03 Oct 2007
Posts: 324
Location: Scotland

uk.gif
PostPosted: Mon Feb 04, 2008 11:35 am    Post subject: Reply with quote

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.2
Back to top
View user's profile
yah996

Bascom Member



Joined: 24 Sep 2006
Posts: 31
Location: Bergkirchen

germany.gif
PostPosted: Mon Feb 04, 2008 10:23 pm    Post subject: Reply with quote

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
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum