Hello
I have been gone through page https://exploreembedded.com/wiki/Serial_Communication_with_PIC16F877A
sample code
I wonder for TXREG and RCREG, what is that in code. they haven't defined or declare anywhere. Is it variable ?
I have been gone through page https://exploreembedded.com/wiki/Serial_Communication_with_PIC16F877A
sample code
Code:
#include<pic16f877a.h>
#define SBIT_TXEN 5
#define SBIT_SPEN 7
#define SBIT_CREN 4
void UART_Init(int baudRate)
{
TRISC=0x80; // Configure Rx pin as input and Tx as output
TXSTA=(1<<SBIT_TXEN); // Asynchronous mode, 8-bit data & enable transmitter
RCSTA=(1<<SBIT_SPEN) | (1<<SBIT_CREN); // Enable Serial Port and 8-bit continuous receive
SPBRG = (20000000UL/(long)(64UL*baudRate))-1; // baud rate @20Mhz Clock
}
void UART_TxChar(char ch)
{
while(TXIF==0); // Wait till the transmitter register becomes empty
TXIF=0; // Clear transmitter flag
TXREG=ch; // load the char to be transmitted into transmit reg
}
char UART_RxChar()
{
while(RCIF==0); // Wait till the data is received
RCIF=0; // Clear receiver flag
return(RCREG); // Return the received data to calling function
}
int main()
{
char i,a[]={"Welcome to Pic Serial Comm, Type the char to be echoed: "};
char ch;
UART_Init(9600); //Initialize the UART module with 9600 baud rate
for(i=0;a[i]!=0;i++)
{
UART_TxChar(a[i]); // Transmit predefined string
}
while(1)
{
ch = UART_RxChar(); // Receive a char from serial port
UART_TxChar(ch); // Transmit the received char
}
}
I wonder for TXREG and RCREG, what is that in code. they haven't defined or declare anywhere. Is it variable ?