Hi guys, I'm currently having problem with the UART of the PIC18F8722. It keep transmitting wrong value. It keep transmitting 0x00 no matter what I send.
Below is my UART init:
Below is my UART interrupt function:
I'm using MAX232 to transmit RS232 signal to my pc. I'm trying to transmit a string of data. I've declared UARTTrasnmitString("Test",4) in main.c. The problem is, it always transmit out 0x00 to my pc:
My settings is for baud rate 9600. The oscillator frequency I use is 20MHz. So, my SPBRG value should be 31 correct?
Do you guys know what is missing? I've been trying out a whole day already. Please help!
Below is my UART init:
Rich (BB code):
void UART_Init(void)
{
RCSTAbits.SPEN = 0;
TXSTAbits.TXEN =0;
RCSTAbits.CREN =0;
TXSTAbits.SYNC = 0;
TRISCbits.TRISC6 = 0;
TRISCbits.TRISC7 = 1;
SPBRG = 31;
TXSTAbits.TXEN =1;
RCSTAbits.CREN =1;
PIE1bits.RCIE = 1;
RCSTAbits.SPEN = 1;
}
Rich (BB code):
#pragma interrupt InterruptServiceHigh
void InterruptServiceHigh(void)
{
if(PIR1bits.TX1IF == 1 && PIE1bits.TX1IE ==1)
{
UARTSend();
}
}
Rich (BB code):
unsigned char UARTTrasnmitString(unsigned char *ucString, unsigned char ucBytesToSend)
{
unsigned char ucIterator;
if(ucBytesToSend > MAX_UART_BUFFER)
{
return FALSE;
}
for(ucIterator=0; ucIterator < ucBytesToSend; ucIterator++)
{
ucUARTTxBuffer[ucIterator]= *ucString++;
}
ucUARTTxProd=0;
ucUARTBytesToSend=ucIterator;
PIR1bits.TXIF =0;
PIE1bits.TXIE = 1;
return TRUE;
}
void UARTSend(void)
{
PIR1bits.TXIF =0;
if(ucUARTTxProd < ucUARTBytesToSend)
{
TXREG = ucUARTTxBuffer[ucUARTTxProd++];
}
if(ucUARTTxProd == ucUARTBytesToSend)
{
PIE1bits.TXIE = 0;
}
}
Do you guys know what is missing? I've been trying out a whole day already. Please help!
Last edited: