I am trying to create a program that contains UART using the PIC18f25j50 device. I have attached my creation so far which is a simple program that outputs a single character repeatedly to the console of a PC. The problem is the console displays a different character (þ~) to the selected one. This character stays the same despite the character that is selected changing (e.g “M” -> “þ~”, “g” -> “þ~”).
My thinking is that it something to do with the clock or the baud rate but I can’t see any error. I have a 8MHz crystal connected and the fuses are set so that there is no PLL. The baud rate equation is correct given that I desire the peripheral to be asynchronous, 8-bit and low speed (P.327 of the linked datasheet).
Would anyone have any idea where the mistake is or have ideas to try to fix this?
#include <xc.h>
#pragma config WDTEN = OFF // Watchdog Timer (Disabled - Controlled by SWDTEN bit)
#pragma config PLLDIV = 2 // PLL Prescaler Selection bits (Divide by 2 (8 MHz oscillator input))
#pragma config STVREN = ON // Stack Overflow/Underflow Reset (Enabled)
#pragma config XINST = OFF // Extended Instruction Set (Disabled)
#pragma config CPUDIV = OSC1 // CPU System Clock Postscaler (No CPU system clock divide)
#pragma config CP0 = OFF // Code Protect (Program memory is not code-protected)
#pragma config OSC = HS // HS
#pragma config T1DIG = ON // T1OSCEN Enforcement (Secondary Oscillator clock source may be selected)
#pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator (High-power operation)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
#pragma config WDTPS = 1 // Watchdog Postscaler (1:1)
#pragma config DSWDTOSC = INTOSCREF// DSWDT Clock Select (DSWDT uses INTRC)
#pragma config RTCOSC = T1OSCREF// RTCC Clock Select (RTCC uses T1OSC/T1CKI)
#pragma config DSBOREN = OFF // Deep Sleep BOR (Disabled)
#pragma config DSWDTEN = OFF // Deep Sleep Watchdog Timer (Disabled)
#pragma config DSWDTPS = G2 // Deep Sleep Watchdog Postscaler (1:2,147,483,648 (25.7 days))
void tx_data(unsigned char);
#define FREQ 8000000 // Frequency = 8MHz
#define baud 9600
#define spbrg_value (((FREQ/64)/baud)-1)
void main()
{
TRISCbits.TRISC6 = 0; // TX pin set as output
TRISCbits.TRISC7 = 1; // RX pin set as input
TXSTAbits.TX9=0;
RCSTAbits.RX9=0;
TXSTAbits.SYNC=0;
TXSTAbits.BRGH=0;
BAUDCONbits.BRG16=0;
SPBRG=spbrg_value; // Fill the SPBRG register to set the Baud Rate
RCSTAbits.SPEN=1; // To activate Serial port (TX and RX pins)
TXSTAbits.TXEN=1; // To enable transmission
RCSTAbits.CREN=1; // To enable continuous reception
while(1)
{
tx_data('m'); // Transmit the same data back to PC
}
}
void tx_data(unsigned char data1)
{
TXREG=data1;
while(TXSTAbits.TRMT==0);
}