About PIC18F8722 UART

Thread Starter

ahgan84

Joined Dec 19, 2011
55
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:
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;
}
Below is my UART interrupt function:
Rich (BB code):
#pragma interrupt InterruptServiceHigh
void InterruptServiceHigh(void)
{
     if(PIR1bits.TX1IF == 1 && PIE1bits.TX1IE ==1)
     {
         UARTSend();
     }
}
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:
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;      
    }
  }
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!
 
Last edited:

Thread Starter

ahgan84

Joined Dec 19, 2011
55
Did you check ucUARTTxProd whether it is incremented everytime it enters the interrupt?

you need to make sure that the following assignment is not empty
ucUARTTxBuffer[ucIterator]= *ucString++;

http://www.myplogger.com
Yup. It got increment.
The problem is, even if I hardcode it like this: TXREG = 0x41, it will still transmit out 0x00. I don't understand. Anything I miss? Is the SPBRG value really correct? According to my calculation SPBRG = (20MHz /9600/64) - 1 = 31
Or is there any hardware problem I need to check like the MAX232 ic problem?
 
Last edited:

Thread Starter

ahgan84

Joined Dec 19, 2011
55
Can the TX1 pin and RX1 pin on PIC18F8722 be pull high with a 10k resistor? This is what I saw from my circuit. Does this the cause?
 
Top