Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include <htc.h>
#include <pic.h>
#define stepcnt 100 //max number of steps
#define _XTAL_FREQ 4000000
void Run_Motor1(void);
void Run_Motor2(void);
void Accel_Motor1(void);
void Deccel_Motor1(void);
void Accel_Motor2(void);
void Deccel_Motor2(void);
void main(void)
{
TRISA = 0xFF; //SET PORT A AS INPUT
PORTA = 0; //INITIALISE PORTA AS 0
TRISB = 0xFF; //SET PORT B AS INPUT PORT
PORTB = 0; //INITIALISE PORT B AS 0
TRISC = 0xFF; //SET PORT C AS AN OUTPUT PORT
Run_Motor1();
}
void Run_Motor1(void) //function to run motor 1 at constant speed
{
int b;
PORTC = 0x32; //light up led to indicate motor 1 is active and wait for the step signal
__delay_us(150);
for(b=0;b<=stepcnt;b++) //loop to step 100 times running motor1
{
PORTC = 0x32;
__delay_us(150);
PORTC= 0x33;
__delay_us(50);
}
}
void Run_Motor2(void) //function to run motor 2 at constant speed
{
int d;
PORTC = 0x52; //turn on led to indicate motor 2 is about to start running and wait for the step signal
__delay_us(15);
for(d=0;d<=stepcnt;d++) //loop to run motor 2 for 100 steps
{
PORTC = 0x52;
__delay_us(15);
PORTC = 0x56;
__delay_us(5);
}
}
void Accel_Motor1(void) //function to accelerate motor 1
{
int x;
PORTC= 0x32; //delay tables to accelerate motor 1
__delay_us(25);
PORTC = 0x33;
__delay_us(5);
PORTC = 0x32;
__delay_us(20);
PORTC = 0x33;
__delay_us(5);
PORTC = 0x32;
__delay_us(15);
PORTC = 0x33;
__delay_us(5);
PORTC = 0x32;
__delay_us(10);
PORTC = 0x33;
__delay_us(5);
PORTC = 0x32;
__delay_us(5);
PORTC = 0x33;
for(x=0;x<=stepcnt;x++) //loop to maintain final speeed after delay table implementation for 100 steps
{
PORTC = 0x32;
__delay_us(5);
PORTC = 0x33;
__delay_us(5);
}
}
void Deccel_Motor1(void) //function to decelerate motor 1
{
int y;
PORTC = 0x32; //delay tables to decelerate motor 1
__delay_us(5);
PORTC = 0x33;
__delay_us(5);
PORTC = 0x32;
__delay_us(10);
PORTC = 0x33;
__delay_us(5);
PORTC= 0x32;
__delay_us(15);
PORTC = 0x33;
__delay_us(5);
PORTC= 0x32;
__delay_us(20);
PORTC= 0x33;
__delay_us(5);
PORTC= 0x32;
__delay_us(25);
PORTC = 0x33;
for(y=0;y<=stepcnt;y++) //loop to maintain final speed after deceleration table implentation
{
PORTC = 0x32;
__delay_us(25);
PORTC = 0x33;
__delay_us(25);
}
}
void Accel_Motor2(void) //function to accelerate motor 2
{
int m;
PORTC= 0x52; //delay tables to accelerate motor two
__delay_us(25);
PORTC = 0x56;
__delay_us(5);
PORTC = 0x52;
__delay_us(20);
PORTC = 0x56;
__delay_us(5);
PORTC = 0x52;
__delay_us(15);
PORTC = 0x56;
__delay_us(5);
PORTC = 0x52;
__delay_us(10);
PORTC = 0x56;
__delay_us(5);
PORTC = 0x52;
for(m=0;m<=stepcnt;m++) //loop to maintain final speeed after delay table implementation for 100 steps
{
PORTC = 0x32;
__delay_us(5);
PORTC = 0x33;
__delay_us(5);
}
}
void Deccel_Motor2(void)
{
int n;
PORTC = 0x52; //delay tables to decelerate motor 2
__delay_us(5);
PORTC = 0x56;
__delay_us(5);
PORTC = 0x52;
__delay_us(10);
PORTC = 0x56;
__delay_us(5);
PORTC= 0x52;
__delay_us(15);
PORTC = 0x56;
__delay_us(5);
PORTC= 0x52;
__delay_us(20);
PORTC= 0x56;
__delay_us(5);
PORTC= 0x52;
__delay_us(25);
PORTC = 0x56;
for(n=0;n<=stepcnt;n++) //loop to maintain final speed after deceleration table implentation
{
PORTC = 0x32;
__delay_us(25);
PORTC = 0x33;
__delay_us(25);
}
}