1

I am running a microcontroller in Proteus with its integrated ADC (10bit output) and a LCD display, yet when inputing a signal of 5V, it will only display up to 4092mV, even though the LCD is perfectly fine because I tried outputing other chars.

Here is the code and a picture of Proteus.

int tension;
int valeurnum;
char valeurchar[10];

void main() {
    ADCON1=14;
    TRISA=1;TRISB=0;
    Lcd_Init();
    while(1){
            tension=ADC_Read(0);
            valeurnum=(5000/1023)*tension;
            Lcd_Cmd(_LCD_CLEAR);
            Lcd_Cmd(_LCD_CURSOR_OFF);
            IntToStr(valeurnum,valeurchar);
            Lcd_Out(1,1,valeurchar);
            delay_ms(1000);;
            }
}

If you lads hav any idea about what I could do or check, or if you need any more infos, please tell me, thanks a lot in advance and have a great day!

Proteus Layout

Community
  • 1
  • 1
Téo ORIOL
  • 57
  • 7
  • 1
    This is probably an issue with your electronics, maybe the reference voltage for the ADC is not good enough – Ctx Feb 24 '17 at 15:26
  • Weird because 5V is going in the potentiometer (and sorry for beeing a beginner) – Téo ORIOL Feb 24 '17 at 15:32
  • Are you sure the conversion formula is correct (`valeurnum=(5000/1023)*tension;`) ? – Jabberwocky Feb 24 '17 at 15:37
  • 1
    Whenever you have integer math, first multiply then divide to avoid rounding down the intermediate result, which is what happened in this case. Had you done `valeurnum=5000*tension/1023;` you would have gotten the correct result. – tonypdmtr Feb 25 '17 at 13:34

1 Answers1

2

Apparently I made a mistake with formulas, I was loosing too much accuracy with the number itself when dividing.

float valeurnum;
char valeurchar[20];
void main() {
    ADCON1=14;
    TRISA=1;TRISB=0;
    Lcd_Init();
    while(1){
            valeurnum=ADC_Read(0);
            valeurnum=valeurnum*5000;
            valeurnum=valeurnum/1024;
            Lcd_Cmd(_LCD_CLEAR);
            Lcd_Cmd(_LCD_CURSOR_OFF);
            FloatToStr(valeurnum,valeurchar);
            Lcd_Out(1,1,valeurchar);
            delay_ms(1000);;
            }
}
Téo ORIOL
  • 57
  • 7