PDA

View Full Version : help me with timers



look4kannan
03-18-2008, 04:56 PM
I am using ATMEGA32.

I cant run timer0(unfortunately 8 bit timer) for 2mins.

Anyone help me to solve this.

I cant use delay function, because it will stop motor for declared time.

#include<avr/io.h>
#include <avr/interrupt.h>

void port_init(void) //intialize ports
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
DDRC = 0xFF;
DDRD = 0xEF;
}


void timer0(void) //intiallize timer0 to run for 2mins and stop
{
int i=0;
int j=0;
TCCR0 |= ((1 << CS00) | (1 << CS02));

if (TCNT0 == 256) //if counter reaches 256 counts its .01 secs
{i++;

if(i=10) //if i reaches 10 its 1 sec
{j++;

if(j=120) //if j reaches 120 its 2 mins
{return(0);}
}}
}

void timer1(void) //intialize servo motor
{
TCCR1B = 0x00; //stop timer
TCNT1H = 0xB1; //PWM mode 8
TCNT1L = 0xE1; //prescaler 8 for 16 MHz crystal
ICR1 = 20000; // used for TOP, makes for 50 hz
OCR1A = 2000; //intially servo at right position
OCR1B = 2000;
TCCR1A = 0xA2;
TCCR1B = 0x1A; //start timer

}



void init_devices(void)
{
//stop errant interrupts until set up
cli(); //disable all interrupts
port_init();
timer1();

sei(); //re-enable interrupts
//all peripherals are now initialized
}

int main()
{
init_devices();

while(1)
{

if(bit_is_set(PIND,3))
{

OCR1A=1500; //servo to centre
PORTC=0x80; //dc motor CW direction
timer0(); // for 2 min



OCR1A=2000; //servo to right
PORTC=0x40; //dc motor CCW direction
timer0(); // for 2 min


PORTC=0x00; //dc motor off
}

else
{
OCR1A=2000; //servo to right
PORTC=0x00; //dc motor off
}

}
}


My application requires
1..DC motor running at very slow speed(Using PWM) in CW direction and Servo should be in left position.
2..after 't' mins CCW direction at full speed and servo should be in centre position for 't1' mins
3- as soon external interrupt pin is on step 1 should be followed again.

elecrom
03-19-2008, 07:06 AM
:lol: !!!

8 bit Counter never reaches to 256 !!!


if (TCNT0 == 256) //if counter reaches 256 counts its .01 secs

is not correct . Counter register TCNT0 counts from 0 to 255 and then rolls back to ZERO, so modify your code accordingly.

avinash_g
03-20-2008, 12:54 PM
Hey


if (TCNT0 == 256) //if counter reaches 256 counts its .01 secs
{i++;

if(i=10) //if i reaches 10 its 1 sec [ERROR 1]
{j++;

if(j=120) //if j reaches 120 its 2 mins [ERROR 2]
{return(0);} [ERROR 3]
}}
}


I GET NO LOGIC IN THIS CODE !!!

u start the timer and immediatly check if its 255 or anythin but its not then what, u simly re3turn!!! and by the way the way u r comaring i=10 is WRONG it should be i==0.
the code to increament i and j should be in timer ISR .

the code is in correctable !!!

i can help ya more wid that if u wish :D

request me in my forum
forum.extremeelectronics.co.in

allbits
03-20-2008, 02:55 PM
use flags.

init();


while (1){

readFlags();
updateDCmotorposition(); //according to flag status
driveServo(); //again according to servo position register

if (timer overflow)
{
inc counter;
if (n= req count for 2 min){
//update flag status here
n=0;
//other blah blah
}

if (PIND3 ==whatever ){
//change/update your flag here
}

}//while end


}

I would suggest you draw a flow chart/write algo on a piece of paper before you start typing on a computer. It might look childish, but let me tell you, it will drastically reduce your coding errors and coding time. I still spend more time with paper than a computer, when it comes to implementing an algo. believe me, it works well.




request me in my forum
forum.extremeelectronics.co.in

is it in line with the etiquette to ask a user to post a question in a personal forum?? cant you help him here itself??