[HELP] AtMega644

Thread Starter

scdvom

Joined Feb 22, 2010
1
can any1 tell me wat does t44he following code doing? thx.
i'm a newbie to AtMega6 and microcontroller chip, and i need it urgently for my Final Year Project. thx 4 helping...


void initialize(void)
begin

/************** TIMER INITIALIZAIONS ********************/
//TIMER0
//set up timer 0 for 1 mSec timebase
TIMSK0= (1<<OCIE0A); //turn on timer 0 cmp match ISR
OCR0A = 249; //set the compare re to 250 time ticks
//set prescalar to divide by 64
TCCR0B= 3; //0b00001011;
// turn on clear-on-match
TCCR0A= (1<<WGM01) ;

//TIMER1
//sets motor speed to zero
OCR1A = 38000;
//turns on interrupt vectors for timer0
TIMSK1 = (1<<OCIE1A)|(1<<TOIE1) ; //turn on ISR
// timer 0 prescalar to 64
TCCR1B = 1;
 

Tahmid

Joined Jul 2, 2008
343
Hi,

First, since you are new to this, you should download the datasheet of the ATmega644 chip.

www.atmel.com/dyn/resources/prod_documents/doc2593.pdf

Then look up the Timer/Counter 0 and Timer/Counter 1 parts, which explain most of these stuff.

In this part:
Rich (BB code):
//TIMER0
//set up timer 0 for 1 mSec timebase
TIMSK0= (1<<OCIE0A); //turn on timer 0 cmp match ISR
OCR0A = 249; //set the compare re to 250 time ticks
//set prescalar to divide by 64
TCCR0B= 3; //0b00001011;
// turn on clear-on-match
TCCR0A= (1<<WGM01) ;
It is set such that, when a compare even occurs, an interrupt will be generated.
The OCR0A register is loaded with 249. Timer/Counter 0 is an 8-bit counter, so it counts from 0 to 255 max. The value is incremented every instruction cycle, which for 8MHz would be every 125 nanoseconds, if no prescaler is assigned. But you set TCCR0B to 3, so prescaler = 64. So your timer value is incremented every (0.125us x 64) = every 8 microseconds.
The timer is set to CTC (Clear Timer on Compare) mode, where, when a compare match occurs, the timer is cleared to 0.
So when timer 0 counts from 0 to 249, compare match occurs, timer is reset to 0.
When the counter reaches value 249, a compare match occurs and an interrupt request is generated.
So, an interrupt is generated every (0.125 * 64 * 250) = 2000 us = 2 ms for 8MHz clock. But since your comment says 1ms time base, I assume a 4MHz clock is being used.

In this part:
Rich (BB code):
//TIMER1
//sets motor speed to zero
OCR1A = 38000;
//turns on interrupt vectors for timer0
TIMSK1 = (1<<OCIE1A)|(1<<TOIE1) ; //turn on ISR
// timer 0 prescalar to 64
TCCR1B = 1;
Timer/Counter 1 is a 16-bit counter which counts from 0 to 65535. The prescaler is set as 1 (the comment is given wrongly). If prescaler is to be set to 64, TCCR1B is to be loaded with 3.
Interrupts for Compare Match and Timer Overflow are enabled.
Compare match occurs when timer counts from 0 to 38000.

I think there is a mistake and OCR1A should be 3800 instead. If it is to be, then this program is for motor control, as Timer 0 is used to provide the time base of 1ms and Timer 1 the on time. If OCR1A = 3800, then on time = 95%

Of course, you need to set and clear the PWM pin in the interrupt service routine.

If your intention is motor control, then instead of this method, you can use the PWM mode of the timers instead. It would be much easier.

Hope this helps.
Tahmid.
 
Top