Connect with us

Adc Value Conversion

Discussion in 'Microcontrollers, Programming and IoT' started by Joshsinclair, Feb 28, 2015.

  1. Joshsinclair

    Joshsinclair

    3
    0
    Feb 28, 2015
    Hi, I am new to this site and have been learning to program

    I am currently working on a weigh scale project where I need to convert a adc value(eg0-1024) into a weight. Im wanting to do this buy setting a point for the 0kg and a point for a known weight. This would need to be able to be calibrated through the circuit.
    At the moment I'm using a Pic18f45k22 connected to a Lcd display, I have the adc value 0-1024 displaying on the screen. I have a potentiometer connected to RA2 so I can change the value.

    I'm having trouble working out the math and procedure in setting for example 2.5v input to 2000kg output on the screen, as well as at the same time setting 1v to 0kg output.
    Im using Mikroc for programming

    Is there a function in the C programming language that is able to perform this.
    Any help would be appreciated.

    This is the code I currently have.


    // Glcd module connections
    char GLCD_DataPort at PORTD;

    sbit GLCD_CS1 at LATB0_bit;
    sbit GLCD_CS2 at LATB1_bit;
    sbit GLCD_RS at LATB2_bit;
    sbit GLCD_RW at LATB3_bit;
    sbit GLCD_EN at LATB4_bit;
    sbit GLCD_RST at LATB5_bit;

    sbit GLCD_CS1_Direction at TRISB0_bit;
    sbit GLCD_CS2_Direction at TRISB1_bit;
    sbit GLCD_RS_Direction at TRISB2_bit;
    sbit GLCD_RW_Direction at TRISB3_bit;
    sbit GLCD_EN_Direction at TRISB4_bit;
    sbit GLCD_RST_Direction at TRISB5_bit;
    // End Glcd module connections

    #include <built_in.h>

    void main() {
    char Weight[14];
    int adc_rd = 0;
    ANSELB = 0; // Configure PORTB pins as digital I/O
    ANSELD = 0; // Configure PORTD pins as digital I/O
    ANSELA = 0x04; // Configure RA2 pin as analog
    TRISA = 0x04; // Set RA2 pin as input
    Glcd_Init(); // Initialize GLCD
    Glcd_Fill(0x00); // Clear GLCD
    ADC_Init(); // Initialize ADC


    while (1){
    adc_rd = ADC_Get_Sample(2); //Read ADC and store in adc_rd int
    IntToStr(adc_rd, Weight); //Convert int to string

    Glcd_Write_Text("WEIGH SCALES", 25, 3, 2); // Write Text(Weigh Scales)
    Glcd_Write_Text(Weight , 25, 4, 2); // Write weight(ADC Value)
    Glcd_Write_Text("KG", 65, 4, 2); // Write Text(KG)
    Delay_ms(50); // Delay 50ms
    Glcd_Fill(0x00); // Clear GLCD

    }
    }
     
  2. BobK

    BobK

    7,645
    1,662
    Jan 5, 2010
    Let Mc = the calibration mass
    Let Az = the ADC reading at 0 gm
    Let Ac = the ADC reading at calibration mass
    Let M = the unknown mass
    Let Am = the ADC reading of an unknown mass

    Then the linear approximation would be:

    M = Mc * ( (Am - Az) / (Ac - Az) )

    Whether or not this linear approximation is good and within what limits is unknown.

    Edit: Do all of this math with floats.

    Bob
     
  3. Joshsinclair

    Joshsinclair

    3
    0
    Feb 28, 2015
    Thank you Bob for your reply.

    I tried this in my code using these values below for a example and it displays values 0kg, 1000kg, 2000kg so it goes up in 1000kg steps as the potentiometer is moved im needing this to go up in say 5kg increments but with the scale based on the known weight. is this due to the math equation or the variables not being floats.
    also when I changed the variables from eg, int MC to float MC. it would not display anything on the screen, what would be the reason behind that. thanks


    // Glcd module connections
    char GLCD_DataPort at PORTD;

    sbit GLCD_CS1 at LATB0_bit;
    sbit GLCD_CS2 at LATB1_bit;
    sbit GLCD_RS at LATB2_bit;
    sbit GLCD_RW at LATB3_bit;
    sbit GLCD_EN at LATB4_bit;
    sbit GLCD_RST at LATB5_bit;

    sbit GLCD_CS1_Direction at TRISB0_bit;
    sbit GLCD_CS2_Direction at TRISB1_bit;
    sbit GLCD_RS_Direction at TRISB2_bit;
    sbit GLCD_RW_Direction at TRISB3_bit;
    sbit GLCD_EN_Direction at TRISB4_bit;
    sbit GLCD_RST_Direction at TRISB5_bit;
    // End Glcd module connections

    #include <built_in.h>

    void main() {


    int MC = 1000; //Calibration Mass eg 1000kg
    int AZ = 200; //ADC reading at zero kg eg200
    int AC = 600; //ADC reading at 1000kg eg 600
    int M; //Unknown mass on scales eg 4000kg
    int AM; //ADC reading of unknown mass

    char Weight[14];

    ANSELB = 0; // Configure PORTB pins as digital I/O
    ANSELD = 0; // Configure PORTD pins as digital I/O
    ANSELA = 0x04; // Configure RA2 pin as analog
    TRISA = 0x04; // Set RA2 pin as input
    Glcd_Init(); // Initialize GLCD
    Glcd_Fill(0x00); // Clear GLCD
    ADC_Init(); // Initialize ADC


    while (1){

    AM = ADC_Get_Sample(2); //Get adc sample and store into AM
    M = MC*((AM-AZ)/(AC-AZ));
    IntToStr(M, Weight);


    Glcd_Write_Text("WEIGH SCALES", 25, 3, 2); // Write Text(Weigh Scales)
    Glcd_Write_Text(Weight , 25, 4, 2); // Write weight(ADC Value)
    Glcd_Write_Text("KG", 65, 4, 2); // Write Text(KG)
    Delay_ms(50); // Delay 50ms
    Glcd_Fill(0x00); // Clear GLCD

    }
    }
     
  4. BobK

    BobK

    7,645
    1,662
    Jan 5, 2010
    Well, I told you to use float variables for the calculation, didn't I?

    Do you understand the difference between int and float?

    Bob
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-