Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics 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.

communication between usart and pic 18f452 using C++ language

Status
Not open for further replies.

tylim

Newbie level 4
Joined
Apr 7, 2011
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,312
hi, good afternoon

i want to make a communication between my pc and pic18f4520

i have the rs232 now

i am doing some object tracking with image processing

i planning to send a character to my pic every time my camera catch a new frame

i have checked the data send out from my camera, yes, it is something that i desired, a character of S,R,L,M (stop, right, left, mid) is sent

now is the problem with my pic, i think something is mistake in my code but i really don know why, can someone tell me why

here is my code

Code:
//*********************************************/ 
//*          Include Header                   */
//*********************************************/
#include <p18f452.h>
#include <delays.h>
#include <usart.h>

#pragma config OSC=HSPLL
#pragma config OSCS=OFF
#pragma config PWRT=OFF
#pragma config BOR=OFF
#pragma config WDT=OFF
#pragma config CCP2MUX=ON
#pragma config STVR=OFF
#pragma config LVP=OFF
#pragma config DEBUG=OFF
//*********************************************/

//*********************************************/ 
//*          Define                           */
//*********************************************/
#define ENB			LATCbits.LATC1
#define ENA			LATCbits.LATC2
#define IN1			LATAbits.LATA1
#define IN2			LATAbits.LATA2
#define IN3			LATAbits.LATA3
#define IN4			LATEbits.LATE1
#define Startled  		LATEbits.LATE0



#define IR			PORTAbits.RA5
#define ChAR		PORTBbits.RB1
#define ChBR		PORTBbits.RB0
#define Start		PORTAbits.RA0

#define PWM1		CCPR1L
#define PWM2		CCPR2L

//*********************************************/

//*********************************************/
//*          Function Prototype               */
//*********************************************/
void Init(void);
void Delay(unsigned long uldelay);
void Goright(void);
void Goleft(void);
void Gomid(void);
void Stop(void);

//*********************************************/

//*********************************************/
//*          Variable                         */
//*********************************************/
char temp[];
char usart=0;
void rx_handler (void);

//*********************************************/

//*********************************************/
//setting interrupt vector
//*********************************************/
#pragma code rx_interrupt = 0x8
void rx_int (void)
{
_asm goto rx_handler _endasm
}

//interrupt subroutine
//=========================================================
#pragma code
#pragma interrupt rx_handler
void rx_handler (void)
{
	while (!DataRdyUSART());
	temp[0]=RCREG;
switch(temp[0])
		{
			case 'R': Goright();
				  break;
			case 'L': Goleft();
				  break;
			case 'M': Gomid();
				  break;
			case 'S': Stop();
				  break;
		}
	usart=1;
	//clear the flag bit
	PIR1bits.RCIF = 0;
}

                 
//*********************************************/
//*          Main Function                    */
//*********************************************/
void Init(void)
{
	TRISA 	= 0b00100001;
	TRISB 	= 0b00000000;
	TRISC 	= 0b00000000;
	TRISD 	= 0b00110000;
	TRISE 	= 0b00000000;




//	PWM
	T2CON	= 0b00000101;        	//timer2 used for pwm
	PR2		= 0xFF;    				//set up PWM
	CCP1CON = 0b00001100;			//PWM
	CCP2CON = 0b00001100;			//PWM

//	UART setting through library
	OpenUSART( USART_TX_INT_OFF &
		USART_RX_INT_OFF &
		USART_ASYNCH_MODE &
		USART_EIGHT_BIT &
		USART_CONT_RX &
		USART_BRGH_LOW, 1);

//	Interrupt
	RCONbits.IPEN = 1;
	IPR1bits.RCIP = 1;
	INTCONbits.GIEH = 1;
}

void main(void)
{

	
	while(1)
	{
	Delay(200000);

		temp[0]=RCREG;
		switch(temp[0])
		{
			case 'R': Goright();
				  break;
			case 'L': Goleft();
				  break;
			case 'M': Gomid();
				  break;
			case 'S': Stop();
				  break;
		}
	}


	CloseUSART();
}

void Delay(unsigned long uldelay)
{
	for( ; uldelay > 0; uldelay--);
}
void Goright()
{
PWM1=150;
PWM2=255;
IN1 = 1;
IN2 = 0;
IN3 = 1;
IN4 = 0;
}
void Goleft()
{
PWM1=255;
PWM2=150;
IN1 = 1;
IN2 = 0;
IN3 = 1;
IN4 = 0;
}
void Gomid()
{
PWM1=255;
PWM2=255;
IN1 = 1;
IN2 = 0;
IN3 = 1;
IN4 = 0;

}
void Stop()
{
IN1 = 0;
IN2 = 0;
IN3 = 0;
IN4 = 0;
PWM1=0;
PWM2=0;
}
 

if you are checking temp[0] in main program then why are you doing the same thing in receive ISR..?
you should not perform your checking and setting operation in the ISR. just set a flag in ISR and leave it as soon as possible then check for that flag in the main function...
 

if you are checking temp[0] in main program then why are you doing the same thing in receive ISR..?
you should not perform your checking and setting operation in the ISR. just set a flag in ISR and leave it as soon as possible then check for that flag in the main function...

i am sorry, i dont quite understand(i am a newbie)

can you show me the correct code?
 

ok first tell me what is the function of this line

while (!DataRdyUSART());

are you waiting for incoming byte of data...?
because the RX interrupt is only called when a byte is completely received...
 

ok first tell me what is the function of this line

while (!DataRdyUSART());

are you waiting for incoming byte of data...?
because the RX interrupt is only called when a byte is completely received...

hmm, actually i get this code by referring to my senior thesis, i am not also not understand about it

i just need pic to send output signal as soon as the pic receive the character from my computer
 

Dear check the direction of your PORTC it shoud be 0b10000000 or 0x80 because RXD pin must be set as input pin. First try it if not solved then I will have a further look.

Regards
Amir Rashid
 

Dear check the direction of your PORTC it shoud be 0b10000000 or 0x80 because RXD pin must be set as input pin. First try it if not solved then I will have a further look.

Regards
Amir Rashid

i changed TRISC = 0b00000000; to TRISC = 0b10000000;

and it still not working....
 

have you used max232 converter between PC and pic..?
have you checked the baud rate and other settings are correct..?
first simulate it on Proteus or some other software to check that your code is correct...
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top