# PIC UART to work with USART

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
}


}

• The interesting stuff is probably the contents of the UART1_Data_Ready() and UART1_Read() functions, but, unfortunately, you're probably prohibited from sharing that... – Kevin Vermeer Jun 21 '11 at 12:25
• The main issue I had when switching from UART to AUART was the inversion. By default (I think) the AUART is already inverted and doesn't need to go through a driver. If you put it through a driver there is a flag to set to change the inversion of the signal. – Majenko Jun 21 '11 at 12:35
• @Kevin, UART1_Data_Ready() and UART1_Read() is a built in function (library) from MikroC Pro. – mlam Jun 21 '11 at 14:36
• Right, so we can't see their source to know if the library is compatible with or different on the PIC16. – Kevin Vermeer Jun 21 '11 at 20:21
• @mlam - Can you accept an answer (even if it's your own), so that the question doesn't pop up anymore without any answers added. – stevenvh Jul 22 '11 at 12:29

## 2 Answers

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.

Thanks to @Leon Heller. The issue is with my Oscillator. I have to set OSCCON. I managed to get my XTAL 4 Mhz works. I added OSCCON = 1101; And my USART works! Thanks guys.

But I haven't find a way to use internal Oscillator.

• Like I also said in your other question: this should be a comment. Comment to the pertaining answer, or, lacking that to your question itself. This area is reserved for answers. – stevenvh Jun 22 '11 at 11:40
• @mlam Since this answer describes how you fixed your problem you should accept it as the answer. – mjh2007 Jul 6 '11 at 18:50