Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Lab No.

servo motor
Home Task:

Continuously rotate a servo motor from 45o to 135o. Consider the minimum and
maximum angle of motor 0o and 180o
.

CODE:#define F_CPU 100000UL


#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
unsigned int x;
//Configure Timer1 registers
TCCR1A |= (1<<COM1A1)|(1<<WGM11);

//Non-Inverted

PWM
TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS11);//Prescaler=16 Mode
14(Fast PWM)
ICR1=2499;
//Freq PWM=50Hz, Time Period = 20ms
DDRD = 0xFF;
//PWM generation on PortD5
while(1)
{
x = 124;
//1ms = 5% of 20ms pulse
OCR1A = x;
//0 degree rotation initially
_delay_ms(1000);

while(x<=249)
//2ms = 10% of 20ms Pulse
{
OCR1A=x;

_delay_ms(50);
x++;
}
}
}

You might also like