uart1_write(128);
delay_ms(500);
uart1_write(127);
delay_ms(500);
while (1) {
uart1_write(counter);
counter++;
delay_ms(50);
}
...I can expect some error (0.16% with a baud of 9600) but this is a little much
Can you show us the code to set up your UART?
Perhaps check that you are using the same data format on each end? e.g. 1 start bit, 8 data bits and 1 stop bit.
//set up alternate pin config
APFCON0.B7 = 1; //RX is on RB2
APFCON1.B0 = 1; //TX is on RB5
RCSTA.B7 = 1; //serial port enabled
RCSTA.B6 = 0; //8 bit data reception on
TXSTA.B2 = 1; //BRGH = 1
TXSTA.B6 = 0; //select 8 bit transmission
TXSTA.B5 = 1; //transmit enabled
TXSTA.B4 = 0; //asynch mode on
//other
baudcon.sckp = 0; //ENSURE DO NOT INVERT DATA
baudcon.b3 = 0; //8 BIT TIMER SELECTED (BRG16 = 0)
SPBRG = 25;
UART1_Init(9600);
because I've set up both sides to use 8 bit data, one stop bit, no flow control
Only done one test (I'll do a few more to verify) but it looks like a replacement PIC has done the trick.
unsigned char counter = 0;
void main() {
//configure chip
APFCON0.B7 = 1; //set alternate pin config so tx and rx go to right pin
APFCON1.B0 = 1; //as schematic was originally made for 16f88 (virtually pin compatible)
ANSELB.B2 = 0; //set pin digital - just to be sure
ANSELB.B5 = 0; //and again, just one less thing to worry about
TRISB.B2 = 1; //RX is input
TRISB.B5 = 0; //TX is output
TRISA.B3 = 0; //LED is output
SPBRG = 25; //set up baud rate generator
BAUDCON.BRG16 = 1; //^^
TXSTA.BRGH = 0; // ^^
TXSTA.SYNC = 0;
RCSTA.SPEN = 1; //serial port enabled
TXSTA.TXEN = 1; //transmit enabled
TXSTA.TX9 = 0; //8 bit transmission
RCSTA.CREN = 1; //enable receiver
RCSTA.RX9 = 0; //8 bit reception enabled
//light up led temporarily just to show it works
LATA.B3 = 1;
delay_ms(750);
lata.b3 = 0;
delay_ms(750);
LATA.B3 = 1;
delay_ms(750);
lata.b3 = 0;
//enter main loop
while (1){
if (Pir1.rcif == 1) { //if data has been received
counter = RCREG; //grab data
LATA.B3 = 1; //put led on to show data has been received
delay_ms(750); //hold led on a short while
lata.b3 = 0; //disable led, we're done
}
}
}
unsigned char RX_Data = 0;
void main() {
//configure chip
TRISB.B2 = 1; //RX is input (RB")
TRISB.B5 = 0; //TX is output (RB5)
TRISA.B3 = 0; //POWER LED Pin is output
APFCON1.B0 = 1; //alternate pin configuration - TX is now on RB5
APFCON0.B7 = 1; //alternate pin configuration - RX is now on RB2
ANSELB.B2 = 0; //rb2 is digital
ANSELB.B5 = 0; //rb5 is digital
//set up EUSART
//FOR BAUD 9600 WITH 4MHZ CRYSTAL - SYNC =0, BRGH = 1, BRG16 = 0;
SPBRGH = 0;
SPBRGL = 25; //SPBRG = 25
TXSTA.BRGH = 1; //BRGH SET
BAUDCON.BRG16 = 0; //BRG16 IS 0
RCSTA.SYNC = 0; //SYNC CLEARED
RCSTA.SPEN = 1; //SERIAL PORT ENABLED
RCSTA.CREN = 1; //CONTINUOUS RECEIVE ENABLED
TXSTA.B6 = 0; //8 BIT TRANSMISSION SELECTED
TXSTA.B5 =1; //TRANSMIT ENABLE BIT SET
TXSTA.B4 = 0; //ASYNCHRONOUS MODE SET
//should be ready to roll?
delay_ms(100); //allow settling time
uart1_write_text("Hello: "); //test transmission
//now test reception:
while (1){
if (uart1_data_ready() == 1){
RX_Data = uart1_read();
uart1_write(RX_Data);
}
}
}
if(RCIF){
RX_DATA = RCREG;
Send(RX_DATA);
RCIF=0;
}
Send(unsigned char dat){
while(!TXIF);
TXREG=dat;
}
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?