Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

TIMERS AND SQUARE WAVE GENERATOR (CLK/ 16 BIT NUMBER)

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Various
View previous topic :: View next topic  
Author Message
ALJ-1108

Bascom Member



Joined: 08 Nov 2005
Posts: 7
Location: USA RICHMOND,TEXAS

usa.gif
PostPosted: Tue Aug 25, 2009 8:32 pm    Post subject: TIMERS AND SQUARE WAVE GENERATOR (CLK/ 16 BIT NUMBER) Reply with quote

Hello All,
I've been playing with this Atmega 2561 on a Kanda board for the better part of a week now. Wondering if anyone has anything on setting up a the timer1 for a simple Clk divided by a 16 bit number to give you a known frequency output????? I'm so darn use to the 82c54 its ridiculous but I want to know if the timers in the mpu itself are of any use...
So far all I got is:

'TIMER FOR FREQUENCY OUTPUT
Config Timer1 = Timer,Prescale = 1,Compare A = Toggle,Clear Timer = 1
Timer1 = 0
Config Portb = Output
Start Timer0
Strt:
Compare1a = 0
Goto Strt

Which on a 16mhz clock is 8mhz but when playing with the Compare1a the values are screwed...

_________________
Willing to help if I can...
Back to top
View user's profile Yahoo Messenger
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Wed Aug 26, 2009 12:17 am    Post subject: Reply with quote

You start timer1 at 0, then set the compare1 reg to 0 in the loop. So you get an immediate toggle every time the timer starts from 0. Maybe what you want, maybe not.
But its slightly better this way:
Code:

Config Timer1 = Timer,Prescale = 1,Compare A = Toggle,Clear Timer = 1
Timer1 = 0
Config Portb = Output
Start Timer0 '??? should be timer 1
Compare1a = xxxx 'whatever value you want
Strt:
wait 1 'waste some time
Goto Strt
 

Setting the value of xxxx higher will give you longer time intervals before the timer equals the compare, and then the timer resets. You dont need to reset the compare1a register unless you want to change the interval within the loop.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
ALJ-1108

Bascom Member



Joined: 08 Nov 2005
Posts: 7
Location: USA RICHMOND,TEXAS

usa.gif
PostPosted: Wed Aug 26, 2009 12:59 am    Post subject: timer and things Reply with quote

Yea I screwed up on the cutting and posting....
It was like this
'TIMER FOR FREQUENCY OUTPUT
Config Timer1 = Timer,Prescale = 1,Compare A = Toggle,Clear Timer = 1
Timer1 = 0
Config Portb = Output
Start Timer1
Strt:
Compare1a = 1143 ' which 1143 is the count of 7Khz
Goto Strt
' The wierd is that the toggle divides it by 2
' So basically (16MHZ * 1 * 7Khz)/2
' Where 16mhz is the base clk , 1 is the prescale , 7Khz is the frequecy out

_________________
Willing to help if I can...
Back to top
View user's profile Yahoo Messenger
mattcro

Bascom Member



Joined: 03 Oct 2007
Posts: 327
Location: Scotland

uk.gif
PostPosted: Wed Aug 26, 2009 10:53 am    Post subject: Reply with quote

The AVR timers are pretty complicated and powerful - there are several ways to do what you want, but this is probably the simplest to understand (honest!). It takes a fair bit of reading through the data sheet and examples to get a good understanding of what's going on under the hood.

What you have in your latest code is a counter counting up from 0 to (in your case) 1143 at 16MHz, then resetting (rolling over) to 0 again, i.e. 14000 times a second. As it resets, it flips the state of (toggles) the Compare A output pin (OC1A) to produce a square wave.

The frequency of this square wave is half of the counter rollover frequency since the output pin only toggles once for every 1143 counts, and it takes two rollovers to produce one cycle of the output square wave.

You just have to take account of this in your frequency calculations.

_________________
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
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Various 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