serial communication

Thread Starter

ect_09

Joined May 6, 2012
180
Rich (BB code):
#include <P18F458.h>


unsigned char RX(void);
void TX(unsigned char x);
void init_comms(void);


#define BAUD 9600      
#define FOSC 4000000
#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))

/* configuration words for pic */
/* fuses of pic */

#pragma config PWRT  =  OFF
#pragma config BOR   =  OFF
#pragma config DEBUG =  OFF
#pragma config OSC 	 =   HS 	  // 4MHz Crystal, (HS oscillator)
#pragma config WDT 	 =   OFF   // watch dog timer off
#pragma config LVP	 =   OFF   // Low voltage program off

		
void main(void)
  {
	  unsigned char x;
	  TRISD=0;
	  PORTD=255;
	 
init_comms();
   
while(1)
{
	x=RX();
	if(x=='A')
	PORTD=0;
	else if(x=='B')
	PORTD=10;
	
TX(x);
}
  }


void init_comms(void)
{
TRISCbits.TRISC6=0;
TRISCbits.TRISC7=1;
TXSTAbits.BRGH=1;     // high speed uart
SPBRG = DIVIDER;
RCSTAbits.CREN=1;     // continous recieve enable bit (asynchronous mode)
RCSTAbits.SPEN=1;     // serial port enable
TXSTAbits.TXEN=1;
}

void TX(unsigned char x) //send data to serial port
{
while(PIR1bits.TXIF==0); // wait here till transmit complete
TXREG=x; 
}

unsigned char RX(void) {
	/* retrieve one byte */
	while(PIR1bits.RCIF==0);	/* set when register is not empty */
	return RCREG;	
}

this is the code for serial communication.in proteous 7.8 when i write A nd B in virtual terminal then port D has '0',&'10' is transfered to PortD.
i want that when i write A or B then after pressing enter then data should be transfer to PORTD.
please help me!!!
 

upand_at_them

Joined May 15, 2010
939
"Pressing Enter" causes two characters to be transmitted: 13 and 10. So if you're want to detect the "Enter" you'll need to detect that character sequence.
 

osx-addict

Joined Feb 9, 2012
122
"Pressing Enter" causes two characters to be transmitted: 13 and 10. So if you're want to detect the "Enter" you'll need to detect that character sequence.
I believe that's true if you're using Windoze -- but if using any unix box it should only see LF's (10's) whenever the enter key is pressed..
 

Thread Starter

ect_09

Joined May 6, 2012
180
"Pressing Enter" causes two characters to be transmitted: 13 and 10. So if you're want to detect the "Enter" you'll need to detect that character sequence.
how to transmit it...............
 
Top