Now before going to the coding part first we have to get an idea of the code by seeing the Hardware Circuit.
This is the Hardware Circuit. If we start simulation, first the Stepper motor is rotate in Half wave mode in Clockwise direction. Clock pulse is assign to pin P2.4 of 8051, here I use 1 sec time period. If we press the full wave button then Stepper motor starts run in Full wave mode. Similarly If we press the Anti-Clockwise button the stepper motor runs in anti-clockwise direction either in Full wave or Half wave mode. Now we are going to Coding part.
Code :-
#include <regx51.h>
sbit En=P2^0; // Enable pin is set to P2.0
sbit Rs=P2^1; // Reset pin is set to P2.1
sbit H_F=P2^2; // Half / Full pin is set to P2.2
sbit Cw_Ccw=P2^3; // Clockwise / Counter CW pin is set to P2.3
sbit Clock=P2^4; // Clock pin is set to P2.4
sbit Stop_Button=P1^0; // To stop the Stepper motor
sbit F_Button = P1^2; // Half wave Button
sbit Ccw_Button = P1^4; // Anti Clockwise Button
void delay() // 500ms Delay function using Timers
{
int count=0;
while(count!=500) // This loop Counts 500 times
{
TMOD = 0x01; // Timer0 mode1
TH0 = 0xFC; //initial value for 1ms in TH0 and TL0
TL0 = 0x66;
TR0 = 1; // To Enable the Timer-0
while(!TF0); // check overflow condition
TR0 = 0; // Timer-0 Disable
TF0 = 0; // Clear flag
count++;
}
}
void clock_pulse() // Using Delay we Generate Clock pulse
{
Clock = 1; // This clock pulse assign to P2.4
delay();
Clock = 0;
delay();
}
void Stop() // Stop Function
{
En = 1; //To stop the Stepper motor we need this
Rs = 0;
}
void Half_wave() // Half wave function
{
clock_pulse(); // Used to run the S-motor in Half wave mode
H_F = 1;
}
void Full_wave() // Full wave function
{
clock_pulse(); // Used to run the S-motor in Full-wave mode
H_F = 0;
}
void main() // Main Function
{
En = 0; // Initailly all pins set to 0
Rs = 1; // Except Reset pin
H_F = 0;
Cw_Ccw = 0;
while(1)
{
if(Stop_Button==0) // Condition for Stop the S-motor
Stop();
else if(Ccw_Button==0) // Condition for S-motor runs in Anti-Clock wise direction
{
En = 1;
Rs = 1;
Cw_Ccw = 0;
if(F_Button==0) // Condition for Full wave mode
Full_wave();
else
Half_wave(); // Condition for Half wave mode
}
else
{ // Initial this condition is active.
En = 1;
Rs = 1;
Cw_Ccw = 1; // Used to run S-motor in Clockwise direction
if(F_Button==0)
Full_wave();
else
Half_wave();
}
}
}
See the code carefully this code is very simple to understand. See the below video it shows how it works.
No comments:
Post a Comment