int i=0; // State Counter is incremented sequentially to determine the next state of the winding energizing sequence
int t=0;
int j=0;
int k=0;
unsigned int motor1;
unsigned int motor2;
unsigned char hundred;
unsigned char tense;
unsigned char onece;
const float x=0.1;
float a;
float b;
float m;
float n;
float u;
float v;
// Lcd pinout settings
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;
// Pin direction
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
void decode(int var_1)
{
hundred=(var_1/100)+48;
tense=(var_1%100)/10 + 48;
onece=(var_1%10)+48;
}
float w;
char txt[10];
void main()
{
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_out(1,1,"SYSTEM ON");
TRISC = 0X00;
PORTC = 0X00;
TRISD = 0XFF;
motor1=0;
motor2=0;
a=sin(u);
b=cos(u);
m=sin(v);
n=cos(v);
u = ((pi/1800)*motor1);
v = ((pi/1800)*motor2);
while (1)
{
// Forward Rotation - Full Step
if(RD0_bit==0 && i==0)
{
PORTC = 0B00000111; // 0001 - Energize winding D
i = 1;
Delay_ms(1200);
motor1++;
};
if(RD0_bit==0 && i==1)
{
PORTC = 0B00001110; // 0100 - Energize winding B
Delay_ms(1200);
i = 2;
motor1++;
}
if(RD0_bit==0 && i==2)
{
PORTC = 0B00001011; // 0010 - Energize winding C
Delay_ms(1200);
i = 3;
motor1++;
}
if(RD0_bit==0 && i==3)
{
PORTC = 0B00001101; // 1000 - Energize winding A
Delay_ms(1200);
i = 0;
motor1++;
}
// Backward Rotation - Full Step
if(RD1_bit==0 && j==0)
{
PORTC = 0b00001101; // 1000 - Energize winding A
j = 1;
Delay_ms(1200);
motor1--;
};
if(RD1_bit==0 && j==1)
{
PORTC = 0b00001011; // 0010 - Energize winding C
Delay_ms(1200);
j = 2;
motor1--;
}
if(RD1_bit==0 && j==2)
{
PORTC = 0b00001110; // 0100 - Energize winding B
Delay_ms(1200);
j = 3;
motor1--;
}
if(RD1_bit==0 && j==3)
{
PORTC = 0b00000111; // 0001 - Energize winding A
Delay_ms(1200);
j = 0;
motor1--;
}
// Forward Rotation - stepper motor2
if(RD2_bit==0 && t==0)
{
PORTC = 0B01110000; // 1000 - Energize winding A
t = 1;
Delay_ms(1200);
motor2++;
};
if(RD2_bit==0 && t==1)
{
PORTC = 0b11100000; // 0101 - Energize windings B & D
Delay_ms(1200);
t = 2;
motor2++;
}
if(RD2_bit==0 && t==2)
{
PORTC = 0b10110000; // 0100 - Energize winding B
Delay_ms(1200);
t = 3;
motor2++;
}
if(RD2_bit==0 && t==3)
{
PORTC = 0b11010000; // 0110 -Energize windings B & C
Delay_ms(1200);
t = 0;
motor2++;
}
if(RD3_bit==0 && k==0)
{
k=1;
PORTC = 0b11010000;
Delay_ms(1200);
motor2--;
};
if(RD3_bit==0 && k==1)
{
PORTC = 0b10110000; // 0101 - Energize windings B & D
Delay_ms(1200);
k = 2;
motor2--;
}
if(RD3_bit==0 && k==2)
{
PORTC = 0b11100000; // 0100 - Energize winding B
Delay_ms(1200);
k = 3;
motor2--;
}
if(RD3_bit==0 && k==3)
{
PORTC = 0b01110000; // 0110 -Energize windings B & C
Delay_ms(1200);
k = 0;
motor2--;
}
if(RD4_bit==0)
{
Lcd_Cmd(_LCD_CLEAR);
decode(motor1);
Lcd_out(1,1,"ANGLE-1=");
Lcd_chr_cp(hundred);
Lcd_chr_cp(tense);
Lcd_chr_cp('.');
Lcd_chr_cp(onece);
}
if(RD5_bit==0)
{
Lcd_Cmd(_LCD_CLEAR);
decode(motor2);
Lcd_out(1,1,"ANGLE-2=");
Lcd_chr_cp(hundred);
Lcd_chr_cp(tense);
Lcd_chr_cp('.');
Lcd_chr_cp(onece);
}
if(RD6_bit==0)
{
w=(a*m)/((b*m)+(a*n));
Lcd_Cmd(_LCD_CLEAR);
floattostr(d,txt);
lcd_out(2,1,txt);
}
}
}