Thứ Sáu, 14 tháng 12, 2018

Stepper Motor Angle Control using AVR Microcontroller

There are many applications in which it is required to set the position of an object at a desire angle. Some of the examples are
1.      Satellite Dish Antenna positioning 
The Satellite Dish Antenna should be in straight alignment with Satellite in Space to receive signal with maximum strength. So it’s required to position the Dish Antenna at a certain angle
2.      Aileron, Elevator and Rudder positioning in Aircraft
In an Aircraft it is required to position Aileron, Elevator and Rudder at a desired angle to increase or decrease the lift, to change direction and to increase or decrease the speed
3.      Rudder Positioning in Ship
To move ship in a specific direction it is required to set an angle of Rudder located in front of the Propeller
4.      Tank Gun or Howitzer Positioning
To hit the bull’s eye (Target) the Tank Gun (or Howitzer) must be positioned to specific angle 

This project demonstrates how any device or object can be positioned to desired angle. The circuit presented here demonstrates how to position Stepper Motor at a specific Angle using AVR microcontroller. The desired Angle Position is entered by user and when He presses the button to rotate motor, the motor starts rotating and rotates till it reaches that angle. The angle can be entered in step of 15o between 0o to 360o. User can increment or decrement angle value in step of 15o and set the desire angle. Based on set angle motor rotates forward (CCW) or reverse (CW). Like if current motor angle is 60o and user enters 90o then motor rotates CCW and if user enters 30o motor rotates CW.

System Circuit Diagram
As shown in figure the circuit is build using AVR microcontroller ATMega32, Current Driver Chip ULN2003A, LCD and few push buttons. 3 push buttons (SW1, SW2 and SW3) are connected to PORTA pins PA0 – PA2 such that when any buttons is pressed logic 0 is given as input to that pin. Normally all 3 pins are given logic 1 by connecting them to Vcc through pull-up resistors. PORTB pins PB0 – PB3 are connected to input pins of ULN2003A. These pins drive stepper motor. The output of ULN drives four coils of unipolar stepper motor. The common terminal of motor coils and ULN chip is connected to Vcc. PORTC drives data pins of LCD and two LCD control pins RS and En are driven by PORTD pins PD0 and PD1 respectively. Control pin RW is connected to ground. A 1K pot is connected to brightness control pin Vee (3) as shown. It can be used to adjust LCD brightness. A 16 MHz crystal is connected to crystal input pins of ATMega32 along with two 22pf capacitors as shown.

System Operation
·         When circuit is switched on the LCD displays set angle as 0 degree. The motor also rests at 0 degree
·         By pressing SW1 the angle can be increased in step of 15 degree. Similarly to decrease angle press SW2.
·         After selecting desire angle when SW3 is pressed, the stepper motor rotates till desire angle and waits for next angle value
·         The angle value can be increased or decreased by pressing SW1 or SW2
·         If new selected angle value is less than previous angle value then motor rotate reverse. E.g. if motor is at an angle 75and now new set angle value is 30then motor rotates reverse
·         But if new selected angle value is higher, then motor rotates forward
Complete Circuit Operation is due to the software program embedded into AVR microcontroller. First let us see the logic behind the program.

Software Logic & Program

Software Logic
The software logic is based on how to rotate stepper motor at set angle every time. Motor rotation directly depends upon its step resolution. The motor step resolution is 3.75o/ pulse. That means for each pulse applied – motor rotates for 3.75only. But it is required to give 4 pulses in sequence to all four motor coils. So motor will rotate 3.75× 4 = 15o. Thus we can rotate motor in multiples of 15o like 30o, 45o, 60o, 75o ......
 The logic is to find out how many number of pulses should be applied to motor so that it can rotate till desire angle value. So to do this, every time when user selects rotation angle, the number of pulses to be applied to motor is found from the equation given below
Number of pulses = 4 × (new set angle value – previous set angle value) / 15
e.g. lets take previously set angle value was 45o and new set angle value is 75o. That means actually the motor has to rotate for 30o. Putting values in above equation
Number of pulses =  4 × (75-45) / 15 = 8
So motor rotates for 8 × 3.75o = 30o
If new set angle value is lesser than previous set angle value then subtraction will be reversed and motor also rotates in reverse (CW) direction.

Software Program
Program is written in C language. It’s compiled and simulated using AVR studio software. Complete program is made up of different functions.
Senddata() function sends 1 byte data to LCD on its data bus
sendcommand() function sends 1 byte command to LCD on its data bus
printstr() function displays message or string on LCD
inc_angle() function increments angle by 15and displays on LCD
dec_angle() function decrements angle by 15and displays on LCD
display_value() function takes integer angle value as input. Convert its all digits into ASCII and displays it as angle value on LCD
lcd_init() function configures and initializes LCD
rotate_motor function rotates stepper motor CW or CCW as per entered angle value till motor reaches that angle position. Please refer the complete program in the code with necessary comments.
.
Circuit:

Circuit Diagram of AVR ATMega32 based Stepper Motor Angle Control
Code:
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>

//////////////// variable declarations////////////////////////////

unsigned int angle=0,new_angle,set_angle=0,num_of_pulses, rotate_reverse_flag=0;

///////////// function to send data to LCD//////////////////////////

void senddata(unsigned char data)
{
  _delay_ms(2);   // wait for LCD to be ready
PORTD=(1<<PD0);   // for data rs=1
PORTC=data;    // put data byte on data-bus
PORTD=(1<<PD0)|(1<<PD1); // give strobe to En pin
PORTD=(1<<PD0)|(0<<PD1);
}

//////////////////// function to send command to LCD////////////////

void sendcmd(unsigned char cmd)
{
_delay_ms(2);   // wait for LCD to be ready
PORTD=(0<<PD0);   // for data rs=0
PORTC=cmd;    // put command byte on data-bus
PORTD=(1<<PD1);   // give strobe to En pin
PORTD=(0<<PD1);
}

///////////// function to send string to LCD //////////////////////
void printstr(unsigned char *s)
{
uint8_t l,i;
l = strlen(s);   // get the length of string
for(i=0;i<l;i++)
{
  senddata(*s);  // write every char one by one
  s++; 
}
}

///////////////// function to display angle value on LCD///////////////

void display_value(unsigned int t)
  {
   unsigned int t1,a;
unsigned char asci[3];
if(t>=100)    // for angle in 3 digit
      {
    a=2;
    while(t>=10)  // separate all 3 digits
    { 
      t1=t%10;  // one by one
      asci[a]=t1+0x30; // convert them into ASCII
      t=t/10;  // store them in array
      a--;
      }
    asci[0]=t+0x30;  // first digit of angle
      }
  else     // for angle in2 digits
      {
    t1=t%10;   // separate both digits
    asci[2]=t1+0x30;  // convert them in to ASCII
    t=t/10;   
    asci[1]=t+0x30;
    asci[0]=0x20;  // take first digit as space
      }
sendcmd(0xC0);
senddata(asci[0]);  // send all digits one by one to
  senddata(asci[1]);  // LCD
  senddata(asci[2]);
printstr(" deg "); 
  }

//////////////// function to initialize LCD /////////////////////////
void lcd_init()
  {
    sendcmd(0x3E);   // configure LCD dot pattern
  sendcmd(0x0E);   // LCD screen on cursor on
  sendcmd(0x01);   // clear LCD memory and home cursor
  sendcmd(0x80);   // set cursor to 1st line 1st column
  printstr("Set Motor Angle:");  // display message
  }

//////////////// function to increase angle by 15 //////////////////
void inc_angle()
  {
   if(angle<345) angle+=15; // increase angle till 345 degree
display_value(angle);  // display new angle value
new_angle = angle;  // update new angle value
  }

////////////// function to decrease angle by 15 ////////////////
void dec_angle()
  {
   if(angle>0) angle-=15;  // decrease angle till 0 degree
display_value(angle);  // display new angle value
new_angle = angle;  // update new angle value
  } 

///////////////// function to rotate motor till desire angle //////////
void rotate_motor()
  {
   int i;
if(new_angle>set_angle)  // if new entered angle is larger
   {
  num_of_pulses = (new_angle-set_angle) / 15; // set number of
  rotate_reverse_flag=0; // pulses and clear flag to rotate
      // motor CCW
   }
else     // if new entered angle is smaller
   {
  num_of_pulses = (set_angle-new_angle) / 30; // set number
  rotate_reverse_flag=1; // of pulses and set flag to rotate
      // motor CW
   }
if(rotate_reverse_flag==0) // check if flag is clear
   {
    for(i=0;i<num_of_pulses;i++) // generate pulse sequence to
    {     // rotate motor CCW
     PORTB = 0x01;
   _delay_ms(100);
   PORTB = 0x02;
   _delay_ms(100);
   PORTB = 0x04;
   _delay_ms(100);
   PORTB = 0x08;
   _delay_ms(100);  
    } 
   }
  else      // else if flag is set
   {
    for(i=0;i<num_of_pulses;i++) // generate pulse sequence to
    {
     PORTB = 0x08;  // rotate motor CW
   _delay_ms(100);
   PORTB = 0x04;
   _delay_ms(100);
   PORTB = 0x02;
   _delay_ms(100);
   PORTB = 0x01;
   _delay_ms(100);
    } 
   }  
   set_angle = new_angle;  // save angle value

  }

/////////////////// main function starts here /////////////////////////
int main(void)
{
DDRC=0xFF;    // PORTB, PORTC and PORTD as
DDRD=0xFF;    // an output ports
DDRB=0x0F;
DDRA=0x00;    // PORT A as an input port
PORTC=0x00;
PORTD=0x00;
PORTB=0x0F;
lcd_init();    // initialize LCD
display_value(angle);  // display angle as 0 degree
loop:while(PINA==0xF0);  // loop until no button is pressed
switch(PINA)   // when button is pressed
   {
   case 0xF1:   // if 1st button is pressed
   inc_angle(); // increment angle by 15 degree
   _delay_ms(200); // key debounce delay
   break;  // get out of loop
  case 0xF2:   // same as above
   dec_angle();
   _delay_ms(200);   
   break;
  case 0xF4:   // for 3rd key
   rotate_motor(); // rotate motor   
   break;   
   }       
      goto loop;    // continuous loop
}

Không có nhận xét nào:

Đăng nhận xét

Bài đăng mới nhất

Hướng dẫn sử dụng Cân điện tử Fujihatsu FTC-01

Hướng dẫn sử dụng Cân điện tử Fujihatsu FTC-01 # candientu ,  # fujihatsu ,  # candientufujihatsu  #candientu,  # candientufujhatsu , #fuji...

Bài đăng phổ biến