Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

UART Coding to receive from Microsoft Visual Basic 6

Status
Not open for further replies.

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:

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:

Joined
Jul 25, 2012
Messages
1,192
Helped
170
Reputation
340
Reaction score
161
Trophy points
1,343
Activity points
0
Yes. You have three while(1) loops. You have to remove one if you don't have break; statements to get out of the loop. Do you have break; statements anywhere in your code? Edit your post and wrap the code in code tags. Use the # button to get code tags.

According to your code, your program will be looping in the below code i.e., the third while(1) loop inside the if(SW==0) statement all the time if
Code:
 if(SW1==0)
. and before that it will be looping in the
Code:
 while(1)	 						// infinite loop
	{

}
just above it. So it is not doing anything. It is just looping in a empty while(1) lop till SW is not 0. When SW == 0 then it loops in another while loop mentioned below and the looping never ends. And depending upon wheather a == 'O' or a == 'N', it will turn ON/OFF LED1 and LED2

Code:
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;
				}
			}
--------------------Update---------------------

Better way to understand

According to your code, your code performs these codes in the beginning

Code:
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();	 			//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
then it enters the second while(1) loop and it will be looping there without doing anything until SW==1. When the moment SW becomes 0 it prints the following 3 messages on uart

"============== SK40C UART Test Program =============="
" - type O to switch ON LED2, no need to press Enter"
" - type N to switch ON LED1, no need to press Enter"

and then enters the third while(1) loop and then it performs

Code:
 a = uart_rec(); infinetly.
until a is not equal to 'O' or 'N'.

If a becomes equal to 'O' or 'N' it will turn OFF/ON LED1 and LED2 and then again it will be looping in the third while(1) loop doing a = uart_rec() infinitly. After that the program doesn't proceed.
 
Last edited:

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
I roughly do understand what you're trying to say. It's like my program runs the first while loop coding once then proceed with the 2nd then 3rd while loop right? In which it will only keeps going on and on in the 3rd while loop without being able to go back to the first and 2nd loop. Am I right? I'm quite confused with how to use switch case for Hi-Tech C programming. Any idea? I would like to use switch case to get the output characters from my visual basic via serial communication. After detecting the characters/alphabets I would be able to execute the above program based on the alphabet/characters.

Thanks a lot for willing to spend your time helping.
 

Joined
Jul 25, 2012
Messages
1,192
Helped
170
Reputation
340
Reaction score
161
Trophy points
1,343
Activity points
0
Yes, you are right.

Switch case is very easy. For eg., if there is a integer variable count which increments on some button presss. You can use switch case statement like mentioned below.

Code:
switch(count) {

	case 1:	//If count is 1

		//do something
		break;
	case 2:	//If count is 2
		//do some other thing
		break;

	case 3:	//If count is 3
		//do still someother thing
		break;
	default:
		break;


};
Code:
unsigned char uart_received_char;

switch(uart_received_char) {

	case 'A':	//If uart_received_char = 'A'

		//do something
		break;
	case 'B':	//If uart_received_char = 'B'
		//do some other thing
		break;

	case 'C'3:	//If uart_received_char = 'C'
		//do still someother thing
		break;
	default:
		break;


};
 

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
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);
void uart_send(unsigned char data);
unsigned string uart_rec(void);            //receive uart value
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 ==============

void main(){                 		  //Our main function
	unsigned long delay_time=5000;
    unsigned int a=0;

	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

					a = uart_rec();						//wait data receive
					if (a == 'LU') 						//if data received, turn left mirror up
					{
 	             	rotate_forward_LCD_1();       	    	//Run forward
                	delay_motor();             		  		//Some delay
					lcd_clr();								//clear lcd
                	breaks_1();                   			//Stop
                	delay_motor();                      	//Some delay
					}
					else if (a == 'LD')						//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 (a == 'LL')						//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 (a == 'LR')						//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 (a == 'RU')						//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 (a == 'RD')						//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 (a == 'RL')						//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 (a == 'RR')						//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 (a == 'LS')						//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 (a == 'RS')						//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 (a == 'SOS')					//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
					{
					command_error();						//Command error
					delay_buzzer();             		  		//Some delay
					lcd_clr();								//clear lcd
                	buzzer_stop();							//
                	delay_motor();             		  		//Some delay
					}
	}
} 			                   				   			//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++);
}

// ======== 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++);
}


Here's my new coding that I have amended but apparently it seems as though I am facing the problem saying that I am using char. Is char supposed to be having variables like an alphabetic letter only? If I were to want to put a few alphabetically letter what variable function should I use? Thanks
 
Last edited by a moderator:

Joined
Jul 25, 2012
Messages
1,192
Helped
170
Reputation
340
Reaction score
161
Trophy points
1,343
Activity points
0
If you use characters like'A', 'X', etc., you have to use
Code:
 unsigned char myCharVariable;
if you want to store strings like "Hello!" you have to use
Code:
 unsigned char myStringVariable[string_length + 1];
Please edit your last post and wrap your code in code tags.

Your usage of int variable a is wrong. You have to assign integer values to a. If you want to assign characters like 'A' to variable a then make it
Code:
 unsigned char a
If you want to store strings in like "SOS" in varaiable a make it
Code:
 unsigned char a[10] = "SOS";
Also you cannot use 'SOS'. Single quotes is used for characters like 'S' and double quotes is used for stings like "SOS".


Which PIC are you using? I think you have to configre ADCON1 and CMCON registers to make pins digital i/o.

You are using UART. Where is your interrupt routine for UART receive? Where are the timer and interrupt initialization for UART?

See these links for UART and Software UART

http://saeedsolutions.blogspot.in/2012/05/interfacing-with-uart-of-pic.html
http://saeedsolutions.blogspot.in/2012/07/pic12f675-software-uart-bit-banging.html
 
Last edited:

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
Erm I'm not too sure about UART interrupts. I'm currently using PIC16F877A and just want to receive from the output given by Visual Basic 6. Can you please enlighten me about the interrupts? I'm still new to this. Thanks. I hope my project really goes well. Regarding the configuration. If I just want to receive and not transmit it's possible right? Because I want to receive from my visual basic 6 and perform the action in microcontroller PIC16F877A thats all.

Thanks a bunch! :D
 

Joined
Jul 25, 2012
Messages
1,192
Helped
170
Reputation
340
Reaction score
161
Trophy points
1,343
Activity points
0
I know about hardware UART. See the first link in my last post. I don't know about Software UART. If you use hardware uart you have to use interrupt routine for uart_receive() finction. Yes, If you want just to receive from UART, you can do that. UART transmit doesn't need interrupts.

If you send character 'A' from Visual Basic application and receive it i.e., read it using uart and store it in a char variable a, then the value of variable will be 65 decimal or 41 hex .i.e, 65d or 0x41.

Are you using Proteus to Simulate?

You send commands like LU, LD for Left motor UP and left motor DOWN from VB App.
You read uart and assign "LU", "LU",... to char variable a.
Then you check if a equals to something like "LU", "LD,...
You can also use switch case statement to check the value of variable a.
Then depending upon that you do some action. Am I right?

This is very easy. The problem in your code is UART receive.

Which Compiler are you using? MPLAB C18 or Hi-Tech C?

UART Code

Code:
unsigned char UART_Receive(void)   //Gets a character from the serial port
{
	if(OERR) //if over run error, then reset the receiver
	{
		CREN = 0;
		CREN = 1;
	}
	
	while(!RCIF);  //wait for transmission to receive
	
	return RCREG;
}


a = UART_Receive();
You have to include these settigs to make ANx pins digital

ADCON1 = 0b10000111;
CMCON = 0b00000111;
CVRENbits.CVROE = 0;

You have to configure register shown in image for UART receive function.
 

Attachments

Last edited:

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
I am using MPLAB IDE. Erm I used the technique that you taught me earlier
Code:
unsigned char a[10] ;
Together with the
Code:
	a = uart_rec();						//wait data receive
					if (a == "LU") 						//if data received, turn left mirror up
					{
 	             	rotate_forward_LCD_1();       	    	//Run forward
                	delay_motor();             		  		//Some delay
					lcd_clr();								//clear lcd
                	breaks_1();                   			//Stop
                	delay_motor();                      	//Some delay
					}
MPLAB.jpg

But it seems there's an error like this.... Any reason why?
 

Joined
Jul 25, 2012
Messages
1,192
Helped
170
Reputation
340
Reaction score
161
Trophy points
1,343
Activity points
0
There is a space between the variable and ;

Make this
Code:
 unsigned char a[10] ;
to
Code:
 unsigned char a[10];
Read my last post it is updated.


a = UART_Receive() is true if it is receiving characters like 'A',..........

UART_Receive() returns an unsigned char and not a string or pointer. So you should declare like
Code:
 unsigned char a;

Use these codes

Code:
unsigned char myChar;

unsigned char UART_Receive_Char(void)   //Gets a character from the serial port
{
	if(OERR) //if over run error, then reset the receiver
	{
		CREN = 0;
		CREN = 1;
	}
	
	while(!RCIF);  //wait for transmission to receive
	
	return RCREG;
}

myChar = UART_Receive_Char();

//-------------------------------------------------------------------------------------

unsigned char myString[10];

unsigned char UART_Receive_String(void)   	//Gets a character from the serial port
{
	unsigned int count = 0;

	if(OERR) 				//if over run error, then reset the receiver
	{
		CREN = 0;
		CREN = 1;
	}
	
	while(!RCIF);  				//wait for transmission to receive
	
	for(count=0;count<10;count++) {
		myString[count] = RCREG;
	}
	
	
}

myString = UART_Receive_String();
Use this code which has interrupt routine

Code:
//Include these settings
GIE  = 1;  		//Enable global interrupts
PEIE = 1;  		//Enable Peripheral Interrupts


//Global Variables
unsigned int count, uart_receive_flag = 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;
		}
		
		while(!RCIF);
		uart_receive_flag = 1;
		
	}
}

void main() {



	while(1) {
		
		if(uart_receive_flag == 1) {
			
			uart_receive_flag = 0;
			
			myString[count] = RCREG;
			count++;
		}
		myString[3] = '\0';


		if(myString == "SOS") {

			//do something
		}
			

	}

}
 
Last edited:

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
There is a space between the variable and ;

Make this
Code:
 unsigned char a[10] ;
to
Code:
 unsigned char a[10];
Read my last post it is updated.


a = UART_Receive() is true if it is receiving characters like 'A',..........

UART_Receive() returns an unsigned char and not a string or pointer. So you should declare like
Code:
 unsigned char a;

Use these codes

Code:
unsigned char myChar;

unsigned char UART_Receive_Char(void)   //Gets a character from the serial port
{
	if(OERR) //if over run error, then reset the receiver
	{
		CREN = 0;
		CREN = 1;
	}
	
	while(!RCIF);  //wait for transmission to receive
	
	return RCREG;
}

myChar = UART_Receive_Char();

//-------------------------------------------------------------------------------------

unsigned char myString[10];

unsigned char UART_Receive_String(void)   	//Gets a character from the serial port
{
	unsigned int count = 0;

	if(OERR) 				//if over run error, then reset the receiver
	{
		CREN = 0;
		CREN = 1;
	}
	
	while(!RCIF);  				//wait for transmission to receive
	
	for(count=0;count<10;count++) {
		myString[count] = RCREG;
	}
	
	
}

myString = UART_Receive_String();
Use this code which has interrupt routine

Code:
//Include these settings
GIE  = 1;  		//Enable global interrupts
PEIE = 1;  		//Enable Peripheral Interrupts


//Global Variables
unsigned int count, uart_receive_flag = 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;
		}
		
		while(!RCIF);
		uart_receive_flag = 1;
		
	}
}

void main() {



	while(1) {
		
		if(uart_receive_flag == 1) {
			
			uart_receive_flag = 0;
			
			myString[count] = RCREG;
			count++;
		}
		myString[3] = '\0';


		if(myString == "SOS") {

			//do something
		}
			

	}

}
I pretty much know what you're trying to say but the ADCON configuration you were mentioning I should assign in under the function prototype specifically just for UART? Because if I'm right the other programs that I am using to configure for my LCD has to do with ADCON which I understand that it is used to assign Digital Input & Output pins right? Btw back then you were using the USB to UART Converter that is ready made or you use it via RS232 serial converter with Max232 IC pins? As I know the USB to UART Converter is a ready made device in which you just plug in and connect the 4 pins to the starter kit right? So for UART receiving basically the interrupt function is very very important? I didn't know I had to do so many configurations. I thought it was pretty much straight forward.

Btw the unsigned char a[10]; I tried but it seems like there's still problem. Errr do you have an email or something? I think its easier to communicate there. Thanks for guiding me through this. My project only problem is the UART in which this combines both my hardware and software project. Thats why i'm in need of real help now. Thanks a lot!
 

Joined
Jul 25, 2012
Messages
1,192
Helped
170
Reputation
340
Reaction score
161
Trophy points
1,343
Activity points
0
ADCON setting is to make pins as digital or analog I/O type. Pins used for LCD, Switches, etc., should be digital type.

You put the ADCON1, CMCON, GIE, PIE, TXSTA, RXSTA, and other register settings below TRISx and PORTx settings.

You got that error shown in image because you have used unsigned char a; or unsigned int a; and then assigning string like "LR" to variable a.

You can assign string like "LR" to variable a if it is declared as unsigned char a[10];
 
Last edited:

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top