Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[HELP] AtMega644 - help with coding needed

Status
Not open for further replies.

scdvom

Member level 1
Joined
Feb 8, 2010
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,524
[HELP] AtMega644

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;
 

Hi,

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

Code:
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:
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:
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.
 

Hi Tahmid. thx 4 ur reply and helping.really appreciated it.XD
erm..i think i get wat u means in d 1st part.bt nt really understand on 2nd part.
hw do u knw its should be 3800 instead of 38000?and hw do u calculated the on time =95%?and wat actually is "on time"?wat d meaning and purpose of "on time"?can u explain to me?thx
and in Timer1,the purpose is to sets motor speed to zero.then y should v write d code in such way (OCR1A = 38000) and (TCCR1B = 1)?

actually im dealing my project wif PIC18F4550, can u help me translate all these code into it?pls..
i dl d datasheet of PIC18F4550 d,n I've check fr it,really no idea on hw 2 transform to.

eg.
T0CON = 0xC5

in this case,i jz can enable the interrupt,timer0 configure as 8-bit,use 64 as prescale value.am I right?bt hw do I make the Interrupt occurs in 1ms time base???

ur reply n helping are greatly appreciated!!! thx!!!
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top