combining two 8 bit reg

Discussion in 'Programmer's Corner' started by karthi keyan 9, Apr 23, 2018.

  1. karthi keyan 9

    Thread Starter New Member

    Apr 17, 2018
    29
    0
    I want to calculate the value of forumal
    for that i need to take value from two 8bit reg in that of 16bit i want the data of 12MSB. 12MSB i placed in the formula to get the data

    My code:
    /
    #include<pic.h>

    #define _XTAL_FREQ 11059200

    __CONFIG (0xFF02);
    int i,x,y,r;
    char data1,data2;
    data1=0x07; // 8-bit data from reg 1
    data2=0x56; // 8-bit data from reg 2
    uint16_t x,y,d3,d4,z;
    d3=(data1<<8)|data2; // Combining reg1 and reg2 = 16bit
    d4=d3>>4; //Shifting 4 bit left side

    void main()
    {

    r=0.000000000004
    x=0x3000;
    y=0xA000;
    for( i=0;i<255;i++); //delay
    while(1)
    {
    // formula = (((12bit data)- 0x3000)/0xA000)*4pico
    z=((d4-x)/y)r; // Forumal
    for(i=0;i<255;i++); //delay
    }
    }

    i this code i am getting multiple error i solved few of them rest all i am finding difficulty pls help to solve it
     
  2. bogosort

    Active Member

    Sep 24, 2011
    170
    83
    Obvious problems:
    • You declare x and y twice (first as int, then as uint16_t).
    • You declare r as an int, but define it as a float (and you forget semicolon).
    • You declare z as an int, but define it with a division and scale it by a tiny float (also missing '*' symbol in the multiply).
    • You have code outside of main that's not part of a function (d3 and d4 definitions).
    Note that multiplying by r may exceed the precision of your hardware. You could leave the result unscaled and post-process it outside of the program, or treat it as implicitly scaled.
     
    karthi keyan 9 likes this.
  3. karthi keyan 9

    Thread Starter New Member

    Apr 17, 2018
    29
    0
    Thank you for your reply. I corrected the some mistakes but still only one error existing in the code.

    modified code:
    #include<pic.h>
    #include<stdio.h>

    #define _XTAL_FREQ 11059200

    __CONFIG (0xFF02);
    int i;
    float r;
    uint16_t x,y,d3,d4,z;
    char d1=0x07; // 8-bit data from reg 1
    char d2=0x56; // 8-bit data from reg 2


    void main()
    {
    d3=(d1<<8)|d2; // Combining reg1 and reg2 = 16bit
    d4=d3>>4; //Shifting 4 bit left side
    r=0.000000000004;
    x=0x3000;
    y=0xA000;
    for( i=0;i<255;i++); //delay
    while(1)
    {
    // formula = (((12bit data)- 0x3000)/0xA000)*4pico
    z=((d4-x)/y)*r; // Formula
    for(i=0;i<255;i++); //delay
    }
    }


    Error:
    1. uint16_t x,y,d3,d4,z; ( "," expected )

    please explain on this
     
  4. bogosort

    Active Member

    Sep 24, 2011
    170
    83
    The line as written is syntactically correct; maybe you have a typo in your actual source code?

    The bigger problem is the multiplication by a tiny float r into a short integer z; you're going to find that z is always 0. Consider what happens when you multiply the largest possible unsigned short int by r:

    0xFFFF * 4e-9 = 0.000262

    As an integer, that equals zero.
     
    karthi keyan 9 likes this.
  5. karthi keyan 9

    Thread Starter New Member

    Apr 17, 2018
    29
    0
    Yeah your correct I want measure capacitance in pico farads. It's should be less right. Then what is the solution to calculate those things?
     
  6. bogosort

    Active Member

    Sep 24, 2011
    170
    83
    Don't scale the result. In other words, leave z as the capacitance in picoFarads. For example, if you get z = 102, then you know that you have 102 pF of capacitance. The only thing you have to consider is if the range of possible capacitances isn't too large for your 16-bit arithmetic.
     
  7. Ian Rogers

    Active Member

    Dec 12, 2012
    416
    105
    I can't see your include for stdint... uint16_t will be defined in a library.

    Short cut
    typedef unsigned int uint16_t ;
     
    karthi keyan 9 likes this.
  8. karthi keyan 9

    Thread Starter New Member

    Apr 17, 2018
    29
    0
    Yeah its working..
    Thank you
     
Loading...