jayanth.devarayanadurga
Banned
- Joined
- Dec 4, 2012
- Messages
- 4,280
- Helped
- 822
- Reputation
- 1,654
- Reaction score
- 791
- Trophy points
- 1,393
- Location
- Bangalore, India
- Activity points
- 0
// ===== SMART GROUP ======
// ===== =====
// 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>
#include <string.h>
#include <htc.h>
__CONFIG(0x3f32); //3FFB
#define _XTAL_FREQ 20000000
//=========== 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);
// ======= UART Function Prototypes =======
void uart_send(unsigned char data);
unsigned char UART_Receive_Char(void); //receive uart value
unsigned char UART_Receive_String(void);
void uart_str(const char *s);
//======= 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 ==============
unsigned int count = 0, count1 = 0;
unsigned char myString[4];
void interrupt ISR(void)
{
if(RCIF) //If UART Rx Interrupt
{
if(OERR) //if over run error, then reset the receiver
{
CREN = 0;
CREN = 1;
}
/*
if((RCREG == 'L') || (RCREG == 'R') || (RCREG == 'U') || (RCREG == 'D') || (RCREG == 'X') || (RCREG == 'B')){
myString[count] = RCREG;
count++;
if(count == 2) count = 0;
count1 = 0;
}
else if((RCREG == 'S') || (RCREG == 'O')){
myString[count1] = RCREG;
count1++;
if(count1 == 3) count1 = 0;
count = 0;
}
*/
if((RCREG == 'L') || (RCREG == 'R') || (RCREG == 'U') || (RCREG == 'D') || (RCREG == 'X')){
myString[count] = RCREG;
count++;
if(count == 2) count = 0;
count1 = 0;
}
if((RCREG == 'S') || (RCREG == 'O')){
if(RCREG == 'S'){
//if((myString[0] == 'L') || (myString[0] == 'R')){
if(count == 1) {
myString[count] = RCREG;
count = 0;
count1 = 0;
}//}
else if((count1 == 0) || (count1 == 2)) {
myString[count1] = RCREG;
count1++;
if(count1 == 3) count1 = 0;
count = 0;
}
}
else if(RCREG == 'O'){
if(count1 == 1) {
myString[count1] = RCREG;
count1++;
count = 0;
}
}
}
RCIF = 0;
}
}
// =========== 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;
}
// ======== 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++);
}
void main(){ //Our main function
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
ADCON1 = 0b10000111;
CMCON = 0b00000111;
CVROE = 0;
//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
SYNC = 0;
SPEN = 1;
RX9 = 0;
CREN = 1;
FERR = 0;
OERR = 0;
TXIE = 0;
RCIE = 1;
//Include these settings
GIE = 1; //Enable global interrupts
PEIE = 1; //Enable Peripheral Interrupts
count = 0;
count1 = 0;
//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();
while(1) { //Do this infinitely
if((myString[0] == 'L') && (myString[1] == 'U'))
{
rotate_forward_LCD_1(); //Run forward
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_1(); //Stop
delay_motor(); //Some delay
}
else if((myString[0] == 'L') && (myString[1] == 'D')) //if data received, turn left mirror down
{
rotate_backward_LCD_1(); //Run Backwards
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_1(); //Stop
delay_motor(); //Some delay
}
else if((myString[0] == 'L') && (myString[1] == 'L')) //if data received, turn left mirror left
{
rotate_leftside_LCD_1(); //Run left
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_2(); //Stop
delay_motor(); //Some delay
}
else if((myString[0] == 'L') && (myString[1] == 'R')) //if data received, turn left mirror right
{
rotate_rightside_LCD_1(); //Run right
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_2(); //Stop
delay_motor(); //Some delay
}
else if((myString[0] == 'R') && (myString[1] == 'U')) //if data received, turn right mirror up
{
rotate_forward_LCD_2(); //Run forward
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_3(); //Stop
delay_motor(); //Some delay
}
else if((myString[0] == 'R') && (myString[1] == 'D')) //if data received, turn right mirror down
{
rotate_backward_LCD_2(); //Run Backwards
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_3(); //Stop
delay_motor(); //Some delay
}
else if ((myString[0] == 'R') && (myString[1] == 'L')) //if data received, turn right mirror left
{
rotate_leftside_LCD_2(); //Run left
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_4(); //Stop
delay_motor(); //Some delay
}
else if((myString[0] == 'R') && (myString[1] == 'R')) //if data received, turn right mirror right
{
rotate_rightside_LCD_2(); //Run right
delay_motor(); //Some delay
lcd_clr(); //clear lcd
breaks_4(); //Stop
delay_motor(); //Some delay
}
else if ((myString[0] == 'L') && (myString[1] == 'S')) //if data received, left signal is lighted up
{
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
}
else if((myString[0] == 'R') && (myString[1] == 'S')) //if data received, right signal is lighted up
{
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
}
else if((myString[0] == 'S') && (myString[1] == 'O') && (myString[2] == 'S')) //if data received, emergency signal light will be lighted up
{
led_signal_sos(); //Emergency light on
delay_led(); //Some delay
lcd_clr(); //clear lcd
led_sos_off(); //Emergency light off
delay_motor(); //Some delay
}
else if ((myString[0] != 'L') && (myString[1] != 'U') &&
(myString[0] != 'L') && (myString[1] != 'D') &&
(myString[0] != 'L') && (myString[1] != 'R') &&
(myString[0] != 'L') && (myString[1] != 'L') &&
(myString[0] != 'R') && (myString[1] != 'U') &&
(myString[0] != 'R') && (myString[1] != 'D') &&
(myString[0] != 'R') && (myString[1] != 'R') &&
(myString[0] != 'R') && (myString[1] != 'L') &&
(myString[0] != 'L') && (myString[1] != 'S') &&
(myString[0] != 'R') && (myString[1] != 'S') &&
(myString[0] != 'X') && (myString[1] != 'X') &&
(myString[0] != 'S') && (myString[1] != 'O') && (myString[2] != 'S'))
{
command_error(); //Command error
delay_buzzer(); //Some delay
lcd_clr(); //clear lcd
buzzer_stop(); //
delay_motor(); //Some delay
}
else if((myString[0] == 'X') && (myString[1] == 'X'))
{
lcd_goto(3); //set the lcd cursor to location 0
send_string("Welcome to");
lcd_goto(25);
send_string("SMART");
}
else {
;
}
}
}
// ========== 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++);
}
void delay_lcd(unsigned long data) //delay function, the delay time
{ //depend on the given value
for( ;data>0;data--);
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?