mrlooneytoon
New Member
Hello
I've decided to play around with some serial communication on this PIC and am trying to work off examples from datasheet/app notes but am not very good with C.
My program simply reads a value that a user types on HyperTerminal and displays what he/she typed.
Big surprise, my code doesn't compile , I've highlighted in my code in ALL CAPS where the errors occur. From reading the data sheet RCREG is a 1 byte register and char is 1 byte as well so I don't see why there's a mismatch. Any help please? I know this is a simple fix. Thanks!
Also, I have no idea if this will work, I haven't tested it out on hardware yet but will soon. If anyone has the HW to test this for now, it would be much appreciated![Smile :) :)](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
I've decided to play around with some serial communication on this PIC and am trying to work off examples from datasheet/app notes but am not very good with C.
My program simply reads a value that a user types on HyperTerminal and displays what he/she typed.
Big surprise, my code doesn't compile , I've highlighted in my code in ALL CAPS where the errors occur. From reading the data sheet RCREG is a 1 byte register and char is 1 byte as well so I don't see why there's a mismatch. Any help please? I know this is a simple fix. Thanks!
Also, I have no idea if this will work, I haven't tested it out on hardware yet but will soon. If anyone has the HW to test this for now, it would be much appreciated
Code:
// Include Files
#include <p18f2525.h> // Include PIC18F2525 registers
#include <usart.h> // Include EUSART functions
#include <delays.h> // Delay functions
// Global function prototyping
void initialize(void);
void ReceiveSerial(void);
// Main function
void main (void) {
// Declare variable for the received byte
unsigned char chByte;
// Initialize EUSART
initialize();
// Write a header
putrsUSART("\nEnter any character into Hyperterminal\n\n");
// Process a keyboard character and display it onto the terminal
while (!DataRdyUSART()) {
chByte = ReceiveSerial(); /******ERROR: TYPE MISMATCH IN ASSIGNMENT*******/
putrsUSART ((const far rom char *)"\r\n You typed: ");
WriteUSART (chByte);
putrsUSART ((const far rom char *)"\r\n");
}
}
// EUSART and PORT initializations
void initialize(void) {
TRISC = 0xC0; // Set PORTC bits for RX and TX
RCSTA = 0b10010000; // Enable serial port & continuous receive
TXSTA = 0b00100100; // Enable transmit, BRGH = 1, BRG16 = 0
SPBRG = 0x19; // dec -> 25, 9600 Baud @ 4MHz, Actual = 9615 Baud, Error: 0.16%
Delay10TCYx(10); // Create a delay after initializing to ensure EUSART is set
}
// RX Function
void ReceiveSerial(void) {
unsigned char chByteReceived;
if( PIR1bits.RCIF == 1) { // If RCREG is full with a byte then RCIF = 1
chByteReceived = RCREG;
if(RCSTAbits.OERR == 1) { // Check overrun error bit
TXSTAbits.TXEN=0;
TXSTAbits.TXEN=1;
RCSTAbits.CREN=0; // Simply clear RCREG and renable reception
RCSTAbits.CREN=1;
}
else if(RCSTAbits.FERR == 1) { // Check framing error bit
unsigned char chDummy;
chDummy = RCREG; // Discard the wrong byte
TXSTAbits.TXEN=0;
TXSTAbits.TXEN=1;
}
else // If message is recieved correctly then return the byte
return chByteReceived; /******ERROR: UNEXPECTED RETURN VALUE*******/
}
}