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!!!