SgtRedrum
Newbie level 6
- Joined
- Oct 10, 2012
- Messages
- 11
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Location
- Bayan Lepas, Penang
- Activity points
- 1,620
Hello there, I need help I have a code in order to receive stuff from visual basic 6 (laptop) to my microcontroller PIC16F877A. I need help regarding the coding as there has been an error. My project is basically to use the laptop to control the microcontroller.
Here's the code:
Here's the code:
Code:
// ===== SMART GROUP ======
// ===== LIM KIA YING T18157 =====
// LCD display with the motor
// LCD display is to show the direction of the motor
// PORT B and PORT C is for motor
// PORT D is for LCD display
#include <pic.h>
__CONFIG(0x3f32);
//=========== define of the motor ports ============
#define L293D_E RA0 //Enable of L293D
#define L293D_A RB1 //Positive of motor
#define L293D_B RA2 //Negative of motor
#define L293D_C RA3 //Positive of motor
#define L293D_D RB2 //Negative of motor
#define L293D_G RA5 //Positive of motor
#define L293D_H RE0 //Negative of motor
#define L293D_J RE1 //Positive of motor
#define L293D_K RE2 //Negative of motor
//=========== define the LCD ports =================
#define rs RB7 //RS pin of the LCD display
#define e RB6 //E pin of the LCD display
#define lcd_data PORTD //LCD 8-bit data PORT
//=========== define LED Signal Light =============
#define signal_left RB5 //Left side LED
#define signal_right RB4 //Right side LED
//=========== define Buzzer ================
#define buzzer RB3 //Buzzer
//=========== define switch ===============
#define switch1 RC0 //Switch 1 for LENG KABIAN
#define switch2 RC1 //Switch 2 for LENG KABIAN
#define switch3 RC2 //Switch 3 for LENG KABIAN
#define switch4 RC3 //Switch 4 for LENG KABIAN
#define switch5 RC4 //Switch 5 for LENG KABIAN
#define switch6 RC5 //Switch 6 for LENG KABIAN
#define switch7 RC6 //Switch 7 for LENG KABIAN
#define switch8 RC7 //Switch 8 for LENG KABIAN
//========== Motor Function Prototypes ===================
void rotate_forward_LCD_1(void); //Forward run function
void rotate_backward_LCD_1(void); //Backward run function
void rotate_leftside_LCD_1(void); //Leftside run function
void rotate_rightside_LCD_1(void); //Rightside run function
void rotate_forward_LCD_2(void); //Forward run function
void rotate_backward_LCD_2(void); //Backward run function
void rotate_leftside_LCD_2(void); //Leftside run function
void rotate_rightside_LCD_2(void); //Rightside run function
void breaks_1(void); //Motor stop function
void breaks_2(void); //Motor stop function
void breaks_3(void); //Motor stop function
void breaks_4(void); //Motor stop function
void delay_motor(void); //Some delay (Motor)
void delay_led(void); //Some delay (LED)
void delay_buzzer(void); //Some delay (Buzzer)
void command_error(void); //Command error
//======== LCD Function Prototypes =========
void delay_lcd(unsigned long data);
void send_config(unsigned char data);
void send_char(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);
unsigned char uart_rec(void); //receive uart value
void uart_send(unsigned char data);
void uart_str(const char *s);
void send_cmd(unsigned char num, unsigned int data, unsigned int ramp);
//======= LED Signal LED ==========
void led_off_l(void);
void led_off_r(void);
void led_signal_left(void);
void led_signal_right(void);
void led_signal_sos(void);
void led_sos_off(void);
//========== Buzzer Function Prototypes ======
void buzzer_stop(void);
// ========== Program Start ==============
void main(){ //Our main function
unsigned char input;
TRISA=0x00; //One side of Motor
TRISB=0x00; //LCD and Buzzer
TRISD=0x00; //LCD display
TRISE=0x00; //One side of Motor
PORTA=0; //Switch
PORTB=0;
PORTD=0;
PORTE=0;
TRISC=0xFF; //Switch
PORTC=0; //Switch
//Configure UART
SPBRG=129; //set baud rate as 9600 baud
BRGH=1; //baud rate high speed option
TXEN=0; //disable transmission
TX9 =0; //8-bit transmission
RX9 =0; //8-bit reception
CREN=1; //enable reception
SPEN=1; //enable serial port
while(1){ //Infinite loop
rotate_forward_LCD_1(); //Run forward
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_1(); //Stop
delay_motor(); //Some delay
rotate_backward_LCD_1(); //Run Backwards
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_1(); //Stop
delay_motor(); //Some delay
rotate_leftside_LCD_1(); //Run left
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_2(); //Stop
delay_motor(); //Some delay
rotate_rightside_LCD_1(); //Run right
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_2(); //Stop
delay_motor(); //Some delay
rotate_forward_LCD_2(); //Run forward
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_3(); //Stop
delay_motor(); //Some delay
rotate_backward_LCD_2(); //Run Backwards
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_3(); //Stop
delay_motor(); //Some delay
rotate_leftside_LCD_2(); //Run left
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_4(); //Stop
delay_motor(); //Some delay
rotate_rightside_LCD_2(); //Run right
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_4(); //Stop
delay_motor(); //Some delay
command_error(); //Command error
delay_buzzer(); //Some delay
lcd_clr(); //clear lcd
buzzer_stop(); //
delay_motor(); //Some delay
led_signal_left(); //LED Signal light turn left
delay_led(); //Some delay
lcd_clr(); //clear lcd
led_off_l(); //LED Signal light off
delay_motor(); //Some delay
led_signal_right(); //LED Signal light turn right
delay_led(); //Some delay
lcd_clr(); //clear lcd
led_off_r(); //LED Signal light off
delay_motor(); //Some delay
led_signal_sos(); //Emergency light on
delay_led(); //Some delay
lcd_clr(); //clear lcd
led_sos_off(); //Emergency light off
delay_motor(); //Some delay
while(1) // infinite loop
{
if(SW1==0)
{
uart_str("============== SK40C UART Test Program =============="); // display on HyperTerminal
uart_send(10); // new Line
uart_send(13); // cursor return to new line
uart_send(10); // new Line
uart_send(13); // cursor return to new line
uart_str(" - type O to switch ON LED2, no need to press Enter"); // display on HyperTerminal
uart_send(10); // new Line
uart_send(13); // cursor return to new line
uart_str(" - type N to switch ON LED1, no need to press Enter"); // display on HyperTerminal
uart_send(10); // new Line
uart_send(13); // cursor return to new line
uart_send(10); // new Line
uart_send(13); // cursor return to new line
while(1) // Infinite loop
{
a = uart_rec(); // wait data receive
if(a == 'O') // if data received, ON LED2
{
LED1=0;
LED2=1;
}
else if(a == 'N') // if data received, ON LED1
{
LED1=1;
LED2=0;
}
}
}
}
}
//Do this infinitely
// =========== MOTOR CODING ============
void rotate_forward_LCD_1(){
L293D_A = 1; //Make positive of motor 1
L293D_B = 0; //Make negative of motor 0
L293D_E = 1; //Enable L293D
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("PSM.L"); //display "Cytron Tech."
lcd_goto(27); //set the lcd cursor to location 20
send_string("UP"); //display "SK40C"
}
void rotate_backward_LCD_1(){
L293D_A = 0; //Make positive of motor 0
L293D_B = 1; //Make negative of motor 1
L293D_E = 1; //Enable L293D
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("PSM.L"); //display "Cytron Tech."
lcd_goto(25); //set the lcd cursor to location 20
send_string("DOWN"); //display "SK40C"
}
void rotate_leftside_LCD_1(){
L293D_C = 1; //Make positive of motor 1
L293D_D = 0; //Make negative of motor 0
L293D_E = 1; //Enable L293D
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("PSM.L"); //display "Cytron Tech."
lcd_goto(25); //set the lcd cursor to location 20
send_string("LEFT"); //display "SK40C"
}
void rotate_rightside_LCD_1(){
L293D_C = 0; //Make positive of motor 0
L293D_D = 1; //Make negative of motor 1
L293D_E = 1; //Enable L293D
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("PSM.L"); //display "Cytron Tech."
lcd_goto(25); //set the lcd cursor to location 20
send_string("RIGHT"); //display "SK40C"
}
void rotate_forward_LCD_2(){
L293D_G = 1; //Make positive of motor 1
L293D_H = 0; //Make negative of motor 0
L293D_E = 1; //Enable L293D
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("PSM.R"); //display "Cytron Tech."
lcd_goto(27); //set the lcd cursor to location 20
send_string("UP"); //display "SK40C"
}
void rotate_backward_LCD_2(){
L293D_G = 0; //Make positive of motor 0
L293D_H = 1; //Make negative of motor 1
L293D_E = 1; //Enable L293D
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("PSM.R"); //display "Cytron Tech."
lcd_goto(25); //set the lcd cursor to location 20
send_string("DOWN"); //display "SK40C"
}
void rotate_leftside_LCD_2(){
L293D_J = 1; //Make positive of motor 1
L293D_K = 0; //Make negative of motor 0
L293D_E = 1; //Enable L293D
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("PSM.R"); //display "Cytron Tech."
lcd_goto(25); //set the lcd cursor to location 20
send_string("LEFT"); //display "SK40C"
}
void rotate_rightside_LCD_2(){
L293D_J = 0; //Make positive of motor 0
L293D_K = 1; //Make negative of motor 1
L293D_E = 1; //Enable L293D
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("PSM.R"); //display "Cytron Tech."
lcd_goto(25); //set the lcd cursor to location 20
send_string("RIGHT"); //display "SK40C"
}
void command_error(){
buzzer=1;
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("COMMAND"); //display "Cytron Tech."
lcd_goto(25); //set the lcd cursor to location 20
send_string("ERROR"); //display "SK40C"
}
void breaks_1(){
L293D_A = 0; //Make positive of motor 0
L293D_B = 0; //Make negative of motor 0
L293D_E = 0; //Disable L293D
}
void breaks_2(){
L293D_C = 0; //Make positive of motor 0
L293D_D = 0; //Make negative of motor 0
L293D_E = 0; //Disable L293D
}
void breaks_3(){
L293D_G = 0; //Make positive of motor 0
L293D_H = 0; //Make negative of motor 0
L293D_E = 0; //Disable L293D
}
void breaks_4(){
L293D_J = 0; //Make positive of motor 0
L293D_K = 0; //Make negative of motor 0
L293D_E = 0; //Disable L293D
}
void buzzer_stop(){
buzzer=0;
}
// ========== LCD CODING ========
void delay_lcd(unsigned long data) //delay function, the delay time
{ //depend on the given value
for( ;data>0;data--);
}
void send_config(unsigned char data) //send lcd configuration
{
rs=0; //set lcd to configuration mode
lcd_data=data; //lcd data port = data
e=1; //pulse e to confirm the data
delay_lcd(50);
e=0;
delay_lcd(50);
}
void send_char(unsigned char data) //send lcd character
{
rs=1; //set lcd to display mode
lcd_data=data; //lcd data port = data
e=1; //pulse e to confirm the data
delay_lcd(10);
e=0;
delay_lcd(10);
}
void lcd_goto(unsigned char data) //set the location of the lcd cursor
{ //if the given value is (0-15) the
if(data<16) //cursor will be at the upper line
{ //if the given value is (20-35) the
send_config(0x80+data); //cursor will be at the lower line
} //location of the lcd cursor(2X16):
else // -----------------------------------------------------
{ // | |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15| |
data=data-20; // | |20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35| |
send_config(0xc0+data); // -----------------------------------------------------
}
}
void lcd_clr(void) //clear the lcd
{
send_config(0x01);
delay_lcd(600);
}
void send_string(const char *s) //send a string to display in the lcd
{
while (s && *s)send_char (*s++);
}
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 data received
}
void uart_send(unsigned char data)
{
while(TXIF==0); //only send the new data after
TXREG=data; //the previous data finish sent
}
void uart_str(const char *s)
{
while(*s)uart_send(*s++);
}
// ======== LED Signal Light ==========
void led_signal_left()
{
signal_left=1;
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("SIGNAL"); //display "Cytron Tech."
lcd_goto(25); //set the lcd cursor to location 20
send_string("LEFT"); //display "SK40C"
}
void led_signal_right()
{
signal_right=1;
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(5); //set the lcd cursor to location 0
send_string("SIGNAL"); //display "Cytron Tech."
lcd_goto(25); //set the lcd cursor to location 20
send_string("RIGHT"); //display "SK40C"
}
void led_signal_sos()
{
signal_right=1;
signal_left=1;
unsigned long delay_time=5000;
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//display startup message
lcd_clr(); //clear lcd
lcd_goto(3); //set the lcd cursor to location 0
send_string("EMERGENCY"); //display "Cytron Tech."
lcd_goto(23); //set the lcd cursor to location 20
send_string("EMERGENCY"); //display "SK40C"
}
void led_off_l()
{
signal_left=0;
}
void led_off_r()
{
signal_right=0;
}
void led_sos_off()
{
signal_left=0;
signal_right=0;
}
// ========= TIME DELAY ============
void delay_motor(){ //delay for motor
unsigned char i,j,k;
for(i=0;i<0x20;i++)
for(j=0;j<100;j++)
for(k=0;k<100;k++);
}
void delay_led(){
unsigned char i,j,k;
for(i=0;i<0x20;i++)
for(j=0;j<255;j++)
for(k=0;k<255;k++);
}
void delay_buzzer(){
unsigned char i,j,k;
for(i=0;i<0x20;i++)
for(j=0;j<150;j++)
for(k=0;k<150;k++);
}
Last edited by a moderator: