Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem with using ADC values in code for LCD (MikroC)

Status
Not open for further replies.

frustrated1

New Member
Hi I'm trying to build a force meter using a force resistive sensor (FSR). Basically I'll get a signal in the form of voltage and have to convert it to force using a parabolic equation (something like this: y = 0.6452x^2 + 0.4591x + 0.015; y being force in kg and x being voltage).

Anyway, I've gotten the hardware set up and it's running fine. I also have no problems getting the voltage readout on my LCD.The trouble comes when I try to convert the voltage values into force values using the aforementioned equation.

I'm using a PIC16F877A and a HD44780 LCD display. Here is my code. Please bear with me as I only stared programming PICs a few months ago and only have verybasic knowledge of C.

Code:
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char Message1[] = "kg";
char Message2[] = "mmHg";

unsigned long int ADC_Value,  Kg, Voltage, Newton, Pa, mmHg;
char *display_v = "0.000";
char *display_kg = "0.00";
char *display_mmhg = "000";
char *display_n = "00.0";

void main() {

  ADCON0 = 0b00001000; // Analog channel select @ AN2
  ADCON1 = 0x00;   // Analog input
  CMCON = 0 ; // Disbale comparators
  TRISB = 0b00000000; // PORTB All Outputs
  TRISA = 0b00001100; // PORTA All Outputs, Except RA3 and RA2
  TRISD = 0b00000000; // PORTA ALL Outputs for status LED
  delay_ms(200);        // Delay to prevent black box error
  Lcd_Init();        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);             // CLEAR display
  Lcd_Cmd(_LCD_CURSOR_OFF);        // Cursor off

  Lcd_Out(1,5,Message1);
  Lcd_Out(1,11,Message2);
  Lcd_Chr(2,6,'N');
  Lcd_Chr(2,13,'V');

do {
   PORTD = 0b00001111;//turn status LED ON
   ADC_Value = ADC_Read(2);         //take value from AN2
   Voltage = 5000 * ADC_Value / 1023;    //change back to decimal
   Kg = (6452/10000) * ((5 * ADC_Value / 1023)^2) + (4591/10000) * (5 * ADC_Value / 1023) + (15/1000);
   Newton = Kg * 981/100;
   Pa = Newton / (44488/100000000);
   mmHg = Pa * (7500617/1000000000);  // I get errors when I use decimals

   display_kg[0] = Kg + 48;
   display_kg[2] = (Kg * 10) % 10 + 48;
   display_kg[3] = (Kg * 100) % 10 + 48;
   Lcd_Out(1,1,display_kg);
   
   display_mmhg[0] = mmHg / 100 + 48;
   display_mmhg[1] = (mmHg / 10) % 10 + 48;
   display_mmhg[2] = mmHg % 10 + 48;
   Lcd_Out(1,8,display_mmhg);
   
   display_n[0] = (Newton / 10) % 10 + 48;
   display_n[1] = Newton % 10 + 48;
   display_n[3] = (Newton * 10) % 10 + 48;
   display_n[4] = (Newton *100) % 10 + 48;
   Lcd_Out(2,1,display_n);

   display_v[0] = Voltage /1000 +48 ; //add 48 to get the ASCII char value
   display_v[2] = (Voltage /100)%10 +48;
   display_v[3] = (Voltage /10)%10 +48;
   display_v[4] = Voltage %10 +48;
   Lcd_Out(2,8,display_v);

   delay_ms(500);
   PORTD = 0x00; //turn off status LEDs
   delay_ms(500);
  } while(1);

}

Note that there is some problem with my positioning of characters on the LCD. Causes flickering in the display and the program to hang (my status led stops blinking). I guess this can be fixed in time. But my main concern is why are my kg, mmHg, and Newton outputs still at the default zero values, but my Voltage is being displayed?
 
Last edited:
When you use a "long" in C it cannot handle floating point numbers

Kg = (6452/10000) * ((5 * ADC_Value / 1023)^2) + (4591/10000) * (5 * ADC_Value / 1023) + (15/1000);

You will need to transpose this formula... The 6452/10000 will ALWAYS equal 0... therefore kg will NEVER equal anything else

ie.. take 0.4591x rewite as x * 4591 / 10000 you get the same answer.... But +0.0015... This is one time where you are going to need floats

Trouble is floats are harder to output to LCD..... So its up to you to decide floating point or fixed point..
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top