Can anyone help me to check this program. I'm already test and running these program using MPLAB,
the result is ********** Build successful! **********
The problem is I'm no sure about the correct output for rotate stepper motor depend on increment number or decrement number received on the receiver board. Please help me..newbie
- CODE: SELECT_ALL_CODE
// Project : Wireless Stepper Motor (RF Receiver)
// Project description : The receiver will receive the data(0->9) from transmitter,
// display the data on 7-segment display and rotate stepper motor.
//===========================================================================
// include
//===========================================================================
#include <pic.h>
//===========================================================================
// configuration
//============================================================================
__CONFIG (0x3F32);
//============================================================================
// define
//============================================================================
#define display PORTB
#define HEADER 0xaa
#define pulse RC3 //STEPPER MOTOR DRIVER
#define direction RC4 //STEPPER MOTOR DRIVER
#define en RC5 //STEPPER MOTOR DRIVER
//=============================================================================
// function prototype
//=============================================================================
unsigned char uart_rec(void);
unsigned char read_packet(void);
void delay (unsigned long data); //STEPPER MOTOR
void rotate (void); //STEPPER MOTOR
unsigned char cw=0, ccw=0, run=0; //STEPPER MOTOR
//============================================================================
// main function
//============================================================================
void main(void)
{
//assign variable
unsigned char num,no;
// 7 segment display
unsigned char _7seg[10]={0b01111111,0b00001101,0b10110111,0b10011111, //0,1,2,3
0b11001101,0b11011011,0b11111011,0b00001111, //4,5,6,7
0b11111111,0b11011111}; //8,9,
//set I/O input output
TRISB = 0b00000000; //configure PORTB as output
TRISC = 0b00000000; //STEPPER MOTOR (set PORTC as output)
PORTC = 0;
//setup USART
BRGH = 0; //baud rate low speed option
SPBRG = 255; //set boud rate to 1200bps for 20Mhz crystal
SPEN = 1; //enable serial port
RX9 = 0; //8-bit reception
CREN = 1; //enable reception
num=0;
display=_7seg[num];
while(1) //infinity loop
{
CREN=1; //enable continuos receive
if(OERR==0) no=read_packet(); //receive sata if overrun error free
else CREN=0; //if overrun error, disable continuos receive
if(no <= 0x09) num=no; //only accept the value from 0 to 9
display=_7seg[num];
if(ccw) //STEPPER MOTOR
{ if(num>0) //STEPPER MOTOR
{ //STEPPER MOTOR
rotate(); //STEPPER MOTOR
} //STEPPER MOTOR
else //STEPPER MOTOR
{ //STEPPER MOTOR
pulse=0; //STEPPER MOTOR
} //STEPPER MOTOR
} //STEPPER MOTOR
else //STEPPER MOTOR
{ //STEPPER MOTOR
if(num<9) //STEPPER MOTOR
{ //STEPPER MOTOR
rotate(); //STEPPER MOTOR
} //STEPPER MOTOR
else //STEPPER MOTOR
{ //STEPPER MOTOR
pulse=0; //STEPPER MOTOR
} //STEPPER MOTOR
} //STEPPER MOTOR
}
}
//==================================================================================
// functions
//===================================================================================
unsigned char uart_rec(void) //receive uart value
{
unsigned char rec_data;
while(RCIF==0); //wait for data
rec_data = RCREG;
return rec_data; //return the received data
}
void delay(unsigned long data) //STEPPER MOTOR
{ //STEPPER MOTOR
for( ;data>0;data-=1); //STEPPER MOTOR
} //STEPPER MOTOR
void rotate(void) //STEPPER MOTOR
{ //STEPPER MOTOR
unsigned char i=0; //STEPPER MOTOR
ADGO=1; //STEPPER MOTOR
while(ADGO==1) continue; //STEPPER MOTOR
if(ADRESH==0) //STEPPER MOTOR
{ //STEPPER MOTOR
pulse=0; //STEPPER MOTOR
} //STEPPER MOTOR
else if((255-ADRESH)<=6) //STEPPER MOTOR
{ //STEPPER MOTOR
i=6; //STEPPER MOTOR
pulse=1; //STEPPER MOTOR
delay(i); //STEPPER MOTOR
pulse=0; //STEPPER MOTOR
delay(i); //STEPPER MOTOR
} //STEPPER MOTOR
else //STEPPER MOTOR
{ //STEPPER MOTOR
i=255-ADRESH; //STEPPER MOTOR
pulse=1; //STEPPER MOTOR
delay(i); //STEPPER MOTOR
pulse=0; //STEPPER MOTOR
delay(i); //STEPPER MOTOR
} //STEPPER MOTOR
} //STEPPER MOTOR
unsigned char read_packet(void)
{
// Buffer for received byte.
unsigned char received_byte;
// Counter to indicate the current position of the received data packet.
static unsigned char counter = 0;
// Buffers for the data and checksum.
unsigned char data;
unsigned char checksum;
// We loop until the checksum is correct.
do {
// We will ignore the sync data and assume the header byte is the start of packet.
// Keep reading until the header byte is received.
while (uart_rec() != HEADER);
// The following byte shoulde be the data byte.
data = uart_rec();
// Then the last byte is the checksum.
checksum = uart_rec();
} while (checksum != (unsigned char)(HEADER + data));
// If the checksum is correct, return the data.
return data;
}