Close or Esc Key

Arduino Projects   |   Raspberry Pi   |   Electronic Circuits   |   AVR   |   PIC   |   8051   |   Electronic Projects

Servo Program Using TIMER0

8 posts / 0 new
Last post
BhanuKiran Chaluvadi
BhanuKiran Chaluvadi's picture
Offline
Last seen: 5 years 6 months ago
Joined: 22/11/2012 - 09:47
Servo Program Using TIMER0

Hello sir , I have programmed servo using Timer0, Its not Working.I am unable to sort out mistake, Could u please Help me

 

 

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
 
volatile uint8_t tot_overflow;
 
void timer0_init()
{
TCCR0  |= (1<<WGM01) | (1<<WGM00) | (1<<CS12) ; // Fast PWM CTC mode, Prescaling of 256.
 
TCNT0 = 0 ;
OCR0 = 249;     //This gives us a time period of 4ms.
TIMSK  |= (1<<OCIE0) ;
sei();
 
}
ISR (TIMER0_COMPA_vect)
{
tot_overflow++;
if(tot_overflow==5)  //5 waves each of time period 4ms= 5*4=20ms
{
PORTA |= (1<<PA0) | (1<<PA1) | (1<<PA2) ;
tot_overflow=0;
}
 
}
 
int main (void)
{
DDRA |= (1<<PA0) | (1<<PA1) | (1<<PA2) ;
timer0_init();
  tot_overflow=0;
while(1)
{
if (TCNT0>=15 && TCNT0<=145 && tot_overflow==0 )
{
 
 
if (TCNT0 >= 23 && bit_is_set(PORTA, PINA0)  ) PORTA &= ~(1<<PINA0);
if (TCNT0 >= 78 && bit_is_set(PORTA, PINA1)  ) PORTA &= ~(1<<PINA1);
if (TCNT0 >= 133 && bit_is_set(PORTA, PINA2) ) PORTA&= ~(1<<PINA2);
 
}
}
 
}
zaib Akhter
zaib Akhter's picture
Offline
Last seen: 5 years 6 days ago
Joined: 27/03/2013 - 19:29
where from this TCNT1

where from this TCNT1 came?????????????

 

initialized only timer0 and using tcnt1 in main .........

zaib Akhter
zaib Akhter's picture
Offline
Last seen: 5 years 6 days ago
Joined: 27/03/2013 - 19:29
TCNT1 is register of

TCNT1 is register of TIMER1

 

so you should either initialize timer1

 

OR

 

use TCNT0.....................................

BhanuKiran Chaluvadi
BhanuKiran Chaluvadi's picture
Offline
Last seen: 5 years 6 months ago
Joined: 22/11/2012 - 09:47
Unfortunately, i posted it

Unfortunately, i posted it wrongly sir . In the actual program i  used TCNT0 every where. Now i corrected  the program posted above . Could u please help me in finding mistake sir .

zaib Akhter
zaib Akhter's picture
Offline
Last seen: 5 years 6 days ago
Joined: 27/03/2013 - 19:29
for Timer0 ..................

for Timer0 .................. ISR (TIMER0_COMP_vect)

if you are having problem with result then tell what you want... add some discription.

 

as it is producing some output and able to rotate motor in one direction

(i have not really work with servo so don't know much)

BhanuKiran Chaluvadi
BhanuKiran Chaluvadi's picture
Offline
Last seen: 5 years 6 months ago
Joined: 22/11/2012 - 09:47
 Thanks for u r  help sir

 

Thanks for u r  help sir ...I made 2 mistakes

 

1.Using CS12 for Prescaling which has to be CS02 ;

 

2. It has to be  ISR(TIMER0_COMP_vect)   not  ISR(TIMER0_COMPA_vect) ;

BhanuKiran Chaluvadi
BhanuKiran Chaluvadi's picture
Offline
Last seen: 5 years 6 months ago
Joined: 22/11/2012 - 09:47
/* Here is the actual Working

/* Here is the actual Working Progam, Controlling Servo Using TIMER0 . SO that we can  use Timer1 for other Purpose */

 

 

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
 
volatile uint8_t tot_overflow;
 
void timer0_init()
{
TCCR0  |= (1<<WGM01) | (1<<WGM00) | (1<<CS02) ; // Fast PWM CTC mode, Prescaling of 256
 
TCNT0 = 0 ;
OCR0 = 249;
TIMSK  |= (1<<OCIE0) ;
sei();
}
 
ISR (TIMER0_COMP_vect)
{
tot_overflow++;
if(tot_overflow==5)
{
PORTA |= (1<<PA0) | (1<<PA1) | (1<<PA2) ;
tot_overflow=0;
}
 
}
 
int main (void)
{
DDRA |= (1<<PA0) | (1<<PA1) | (1<<PA2) ;
timer0_init();
  tot_overflow=0;
while(1)
{
if (TCNT0>=15 && TCNT0<=145 && tot_overflow==0)
{
if (TCNT0 >= 23 && bit_is_set(PORTA, PINA0)  ) PORTA &= ~(1<<PINA0);
if (TCNT0 >= 78 && bit_is_set(PORTA, PINA1)  ) PORTA &= ~(1<<PINA1);
if (TCNT0 >= 133 && bit_is_set(PORTA, PINA2) ) PORTA &= ~(1<<PINA2);
 
}
}
 
}
Anumolu
Anumolu's picture
Offline
Last seen: 1 month 1 day ago
Joined: 31/10/2018 - 19:29
hardware connections

Hi Bhanu

In above Program you have used three pins among them which pin used  for servo motor.