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:
How do I make sure the MCU read the data as it is (UART). At this moment I just want to read incoming data.
Does this has something to do with oscillator? I'm using XTAL 4Mhz.
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
}
}
UART1_Data_Ready()
andUART1_Read()
functions, but, unfortunately, you're probably prohibited from sharing that... \$\endgroup\$