Electronic – PIC UART to work with USART

picpickituart

I face a road block with my PIC UART/USART. Previously I was working with UART in PIC18F452. But when bring my code to use with PIC16F1824, it doesn't work. I guess this is because PIC16F1824 is using Enhanced USART. Technically it should support UART. But I don't how to make it work.

I'm using MikroC Pro. It detect the incoming data using UART1_Data_Ready(), when I read the data using UART1_Read(), it seems the data is garbage.

Question:

  1. How do I make sure the MCU read the data as it is (UART). At this moment I just want to read incoming data.

  2. Does this has something to do with oscillator? I'm using XTAL 4Mhz.

  3. I'm using PICKIT 3. Is there a way I can see the the incoming data?

Useful links:

Code

void main()

{

ANSELA = 0; //Digital I/O for PORTA
TRISA = 0b00100001;
PORTA = 0;
APFCON0.RXDTSEL = 1;  //0:RX is on RC5 1:RX is on RA1
TRISC = 0;
PORTC = 0;

RCSTA.RX9D = 0;
RCSTA.ADDEN = 0;
RCSTA.CREN = 1; // eneble reception.
RCSTA.SPEN = 1; //Serial port enable
RCSTA.RX9 = 0;

T1CON = 1;          // Turn on timer TMR1
PIR1.TMR1IF = 0;    // Reset the TMR1IF bit
TMR1H = 0xFC;       // TMR1H and TMR1L timer registers are returned
TMR1L = 0x18;       // their initial values
PIE1.TMR1IE = 1;    // Enable an interrupt on overflow
INTCON = 0xC0;      // Enable interrupt (bits GIE and PEIE)


PWM1_Init(4000);    //initialize PWM at 5Khz

UART1_Init(9600);

while(1)
{
  if (UART1_Data_Ready())
  {
    //Blink the LED, indication data is coming in.
     PORTA = 4;
     delay_ms(50);
     PORTA = 0;

     sVal = UART1_Read();

    //My debug code to see what character coming in
     EEPROM_Write(0x00+ii, sVal);
     ii++;

      if (sVal == '3')
         motor_run_fast();
      else
          motor_stop();

  } //UART ready
}

}

Best Answer

Things to check / try:

  1. Make sure you have the right baud rate set for your clock speed. The calculation is made somewhat more tricky by the newer 16-bit baud rate generator option.

  2. Make sure you are actually running at the clock speed you are expecting to be running at.

  3. Check you have the clock polarity set right - try different settings:

bit 4 SCKP: Synchronous Clock Polarity Select bit

  • Asynchronous mode:
    • 1 = Transmit inverted data to the TX/CK pin
    • 0 = Transmit non-inverted data to the TX/CK pin

There is a similar setting for the receive clock somewhere, but I can't find it at the moment.