ADC and UART on 18F24k50

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Bonca
Posts: 319
Joined: 18 Mar 2009 07:55

ADC and UART on 18F24k50

#1 Post by Bonca » 20 Jul 2022 10:01

Hi,

I have an analog-digital reading issue on AN0 with 18F24k50. The ADC values in the serial terminal are not showing however other UART_Write is OK. I am not sure if the config of the controller is correct on the analog input. Please check the code and the schematics and if you have any suggestion I would appreciate. Thank you in advance.

Code: Select all

unsigned int adc_result;
char text;

void main() {

     TRISA = 0b00000001;   // AD conversion pdf 293
     ANSELA = 0b00000001;  // AN0

     //ADCON1 = 0;         // pdf 295
     //ADCON0 = 1;

     //CM1CON0.C1ON = 0;   // pdf 307
     //CM2CON0.C2ON = 0;

     LATA = 0;
     PORTA = 0;

     LATB = 0;
     TRISB = 0;
     PORTB = 0;
     LATC = 0;
     TRISC = 0;
     PORTC = 0;
     
     UART1_Init(9600);
     Delay_ms(100);
     UART1_Write_Text("UART init");
     UART1_Write('\r');

     ADC_Init();
     UART1_Write_Text("ADC init");
     UART1_Write('\r');
     delay_ms(10);

     adc_result = ADC_Read(0);
     UART1_Write_Text("ADC0 read");
     UART1_Write('\r');
     
     IntToStr(adc_result, text);
     UART1_Write_Text(text);
     UART1_Write('\r');
     
     UART1_Write_Text("ADC0 value send to terminal");
     UART1_Write('\r');


}
schematics.jpg
schematics.jpg (32.29 KiB) Viewed 502 times
termial.JPG
termial.JPG (11.99 KiB) Viewed 502 times

Bonca

hexreader
Posts: 1785
Joined: 27 Jun 2010 12:07
Location: England

Re: ADC and UART on 18F24k50

#2 Post by hexreader » 20 Jul 2022 11:14

Check the help utility for IntToStr function

A 7 character buffer is required. You have only provided a one character buffer

Code: Select all

char text[7];
You should be using WordToStr rather than IntToStr, since ADC value is unsigned, not signed
Start every day with a smile...... (get it over with) :)

Bonca
Posts: 319
Joined: 18 Mar 2009 07:55

Re: ADC and UART on 18F24k50

#3 Post by Bonca » 20 Jul 2022 11:41

Oh... Blind me... Thank you hexreader :)

Bonca

Post Reply

Return to “mikroC PRO for PIC General”