Servo Motor Control

/************************************************************************************************
*************************************************************************************************
*********************************** Servo Motor Control****************************************
*************************************************************************************************
		Subeer Kumar Sarkar
		Electrical & Electronic Engineer
************************************************************************************************
************************************************************************************************
***********************************************************************************************/
#include<avr/io.h>
#include<util/delay.h>
int main(void)
{
DDRB|=(1<<DDB1)|(1<<DDB2);									//OC1B & OC1A connected
/***********************Fast PWM TOP=ICR1 non-inverting mode************************************/
TCCR1A|=(1<<WGM11)|(1<<COM1B1)|(1<<COM1A1);
TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<CS10);		    //prescalling 64
ICR1=2499;													//time preiod 50Hz
/***********************************************************************************************/
while(1)
{
	for(int i=125;i<=249;i++)
	{
		OCR1B=i;
		_delay_ms(80);
	}

}
return 0;
}
/*************** Interrupt Enable ****************************/
#include<avr/interrupt.h>
sei();	//Global Interrupt Enable
TIMSK|=(1<<OCIE1A);  //Timer1, Output Compare A Match Interrupt Enable
/*********************   Set logic  ****************************/
ISR(TIMER1_COMPA_vect)
{
   //set I/O port
}
if(TCNT1>=140 && TCNT1<=249)
  {
   if(TCNT1>=140) PORTx &=~(1<<PINx);
   if(TCNT1>=180) PORTx &=~(1<<PINx);
-------------------------------------------------------------
  }
/****************************************************************************************************
*****************************************************************************************************
***************************** Multiple Servo Motor Control ****************************************** 
*****************************************************************************************************
		Subeer Kumar Sarkar
		Electrical & Electronic Engineer
******************************************************************************************************
******************************************************************************************************
*****************************************************************************************************/
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
int main(void)
{
DDRD|=0x0f;
/********************************Fast PWM  prescalling 64*********************************************/
TCCR1A|=(1<<WGM11);            						   //Normal port operation, OC1A/OC1B disconnected
TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS10)|(1<<CS11);
TIMSK|=(1<<OCIE1A);									   //Timer1, Output Compare A Match Interrupt Enable						
sei();													   //Globel Interrupt Enable
ICR1=2499;										           //Time preiod 50Hz
/******************************************************************************************************/
while(1)
{
/**************************************** Clear Logic *************************************************/
if(TCNT1>=140 && TCNT1<=249)
{	if(TCNT1>=140) PORTD &=~(1<<PIND0);
	if(TCNT1>=180) PORTD &=~(1<<PIND1);
	if(TCNT1>=200) PORTD &=~(1<<PIND2);
	if(TCNT1>=240) PORTD &=~(1<<PIND3);
}
/*******************************************************************************************************/
}
return 0;
}
/*************************************** Set Logic *****************************************************/
ISR(TIMER1_COMPA_vect)
{
PORTD=0x0f;
}
/*******************************************************************************************************/

1 thought on “Servo Motor Control

Comments are closed.