1. We will be in Read Only mode (no new threads, replies, registration) starting at 9:00 EDT for a number of hours as we migrate the forums to upgraded software.

About PIC18F8722 UART

Discussion in 'Embedded Systems and Microcontrollers' started by ahgan84, Mar 27, 2012.

  1. ahgan84

    Thread Starter Member

    Dec 19, 2011
    55
    0
    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:
    Code ( (Unknown Language)):
    1. void UART_Init(void)
    2. {
    3.     RCSTAbits.SPEN = 0;
    4.  
    5.     TXSTAbits.TXEN =0;
    6.  
    7.     RCSTAbits.CREN =0;
    8.  
    9.     TXSTAbits.SYNC = 0;
    10.  
    11.     TRISCbits.TRISC6 = 0;        
    12.     TRISCbits.TRISC7 = 1;        
    13.  
    14.     SPBRG = 31;    
    15.    
    16.     TXSTAbits.TXEN =1;
    17.     RCSTAbits.CREN =1;
    18.     PIE1bits.RCIE = 1;
    19.     RCSTAbits.SPEN = 1;
    20. }
    Below is my UART interrupt function:
    Code ( (Unknown Language)):
    1. #pragma interrupt InterruptServiceHigh
    2. void InterruptServiceHigh(void)
    3. {
    4.      if(PIR1bits.TX1IF == 1 && PIE1bits.TX1IE ==1)
    5.      {
    6.          UARTSend();
    7.      }
    8. }
    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:
    Code ( (Unknown Language)):
    1.  
    2. unsigned char UARTTrasnmitString(unsigned char *ucString, unsigned char ucBytesToSend)
    3. {
    4.     unsigned char ucIterator;
    5.     if(ucBytesToSend > MAX_UART_BUFFER)
    6.     {
    7.         return FALSE;
    8.     }
    9.     for(ucIterator=0; ucIterator < ucBytesToSend; ucIterator++)
    10.     {
    11.         ucUARTTxBuffer[ucIterator]= *ucString++;    
    12.            
    13.     }
    14.     ucUARTTxProd=0;
    15.     ucUARTBytesToSend=ucIterator;
    16.     PIR1bits.TXIF =0;
    17.     PIE1bits.TXIE = 1;        
    18.    
    19.     return TRUE;
    20.  
    21. }
    22. void UARTSend(void)
    23.  {
    24.     PIR1bits.TXIF =0;
    25.     if(ucUARTTxProd < ucUARTBytesToSend)
    26.     {
    27.         TXREG = ucUARTTxBuffer[ucUARTTxProd++];
    28.     }
    29.    
    30.     if(ucUARTTxProd == ucUARTBytesToSend)
    31.     {
    32.         PIE1bits.TXIE = 0;      
    33.     }
    34.   }
    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: Mar 27, 2012
  2. johngoh2007

    New Member

    Mar 27, 2012
    4
    0
    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
     
  3. ahgan84

    Thread Starter Member

    Dec 19, 2011
    55
    0
    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: Mar 27, 2012
  4. ahgan84

    Thread Starter Member

    Dec 19, 2011
    55
    0
    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?
     
Loading...