Menu

PLEASE I NEED A HELP, AM TRYING TO READ OHM METER WHICH I SAW THE CODE IN ARDUINO SO I MANAGED TO TRANSLATE IT TO GCB IT COMPILED BUT NOT SHOWING ACCURATE ON THE LCD

Help
samco
2023-11-02
2023-11-02
  • samco

    samco - 2023-11-02
      #chip 16f873A, 8 ;12f675, 4
      #option explicit
      ; #config  _INTRC_OSC_NOCLKOUT
      ;Software I2C For LCD
    
    
      #define I2C_MODE Master
      #define I2C_DATA  portb.3  ;gpio.0 'SDA
      #define I2C_CLOCK  portb.4  ;gpio.4 'SCL
      #define I2C_DISABLE_INTERRUPTS ON
    
      #define LCD_IO 10
      #define LCD_I2C_Address_1 0x4E ; 0x4e default to 0x4E
      #define LCD_SPEED FAST
    
    
    
    // resistor measure =Rx
    #DEFINE analogPin AN0
    #define ch2K portc.4  ;int
    #DEFINE ch20K portc.5 ;int
    #DEFINE ch200K portc.6 ;int
    #DEFINE  ch1M portc.7 ;int
    #DEFINE  apply_voltage portb.0
    //ROTRY SWITC CONFI INPUT
    #DEFINE Scale2k         portc.0 ; =9;int
    #DEFINE Scale20k        portc.1 ; =12;int
    #DEFINE Scale200k       portc.2 ; =11;int
    #DEFINE Scale1M         portc.3 ; =10;int
    
    dim  buffer, Vin, R2,  as byte
    
    dim V_measured, Vout as word
    
    V_measured= 0;int
    Vin= 5   ;int
    Vout= 0    ;float
    R2= 0; float
    buffer= 0;float
    
    
    //void setup()
    // directions
    //dir  V_measured IN
    //dir  analogPin IN
    dir  apply_voltage OUT
    
      //Detect the range mode (0-1kK; 10k-100k; 100k-1M) RESISTANCE
    dir Scale2k   IN
    dir Scale20k  in
    dir Scale200k IN
    dir Scale1M   IN
    
    //We set this pins as input for now.
    dir ch2K   IN
    dir ch20K  IN
    dir ch200K IN
    dir ch1M   IN
    
    
    
    //void loop()
     do
    ///////////////////-2k-/////////////////////
    //if (digitalRead(Scale2k))
      if Scale2k=1 then
      //digitalWrite(apply_voltage,HIGH);
      let apply_voltage=1
    
        dir ch2K OUT
        dir ch20K IN
        dir ch200K IN
        dir ch1M IN
    
        //digitalWrite(ch2K,LOW);
    
         SET ch2K=0  ;LOW
         dim R1 as byte
          R1= 2; // Set this values to the value of the used resistor in K ohms
    
          V_measured= readAD(AN1) //in 8bits
    
          buffer= V_measured * Vin
          Vout= (buffer)/1024;  //in volts 1024.0
          buffer= [WORD]((Vin/Vout) -1);
          R2= R1 * buffer*1000; //*1000 because we express it in ohms
    
           // if  R2 > 2000 then  //20.00
    
          if R2>2000 then
    
    
            Locate 0,0
            print "Increase scale"
            //lcd.clear();
            //lcd.setCursor(0,0);
            print " Increase scale"
            //lcd.setCursor(0,1);
            Locate 1,0
            print "or insert resist"
            //delay(1000);
            Wait 1000 ms
          end if
    
    cls
          if R2 < 2000 then
    
    
           Locate 0,0
           print "SCALE:    0 - 2K"
           Locate 1,0
            print R2
            Locate 1,5
            PRINT V_measured
           // lcd.setCursor(10,1);
            Locate 1,10
            print "  ohms"
            //delay(1000);
            Wait 1000 ms
          end if
    
      end if
    //LOOP
    
    
    
    ///////////////////-20k-/////////////////////
    
       if Scale20k=1 then
        //digitalWrite(apply_voltage,HIGH);
         let apply_voltage=1
    
        dir ch2K in
        dir ch20K out
        dir ch200K IN
        dir ch1M IN
        let  ch20K=0 ; LOW
    
           R1= 20; //  float  Set this values to the value of the used resistor in K ohms
          V_measured= readAD(an1) //in 8bits
    
          buffer= V_measured * Vin;
          Vout= (buffer)/1024.0;  //in volts
          buffer= [WORD](Vin/Vout) -1;
          R2= R1 * buffer;
    
    
           if R2>20 then     ;//if (R2 > 20)
    
    
            Locate 0,0
            print "Increase scale"
            //lcd.clear();
            //lcd.setCursor(0,0);
            print " Increase scale"
            //lcd.setCursor(0,1);
            Locate 1,0
            print "or insert resist"
            //delay(1000);
            Wait 1000 ms
          end if
    
    
          if (R2 < 20) then
    
    
    
            cls
           Locate 0,0
           print "SCALE:   2 - 20K"
            Locate 1,0
            print(R2)
            Locate 1,10
            print "K ohms"
            Wait 1000 ms
            //delay(1000);
    
        end if
        end if
    
    ///////////////////-200k-/////////////////////
    //if (digitalRead(Scale200k))
    
     if Scale200k=1 then
        //digitalWrite(apply_voltage,HIGH);
         let apply_voltage=1
    
        dir ch2K in
        dir ch20K in
        dir ch200K out
        dir ch1M IN
        let  ch200K=0 ; LOW
    
           R1= 220; //  float  Set this values to the value of the used resistor in K ohms
          V_measured= readAD(an1) //in 8bits
    
          buffer= V_measured * Vin;
          Vout= (buffer)/1024.0;  //in volts
          buffer= (Vin/Vout) -1;
          R2= R1 * buffer;
    
    
           if R2>200 then     ;//if (R2 > 20)
    
    
            Locate 0,0
            print "Increase scale"
            //lcd.clear();
            //lcd.setCursor(0,0);
            print " Increase scale"
            //lcd.setCursor(0,1);
            Locate 1,0
            print "or insert resist"
            //delay(1000);
            Wait 1000 ms
          end if
    
    
          if (R2 < 200) then
    
    
    
            cls
           Locate 0,0
           print "SCALE: 20 - 200K"
            Locate 1,0
            print(R2)
            Locate 1,10
            print "K ohms"
            Wait 1000 ms
            //delay(1000);
    
        end if
        end if
    
    
    
    ///////////////////-1M-/////////////////////
    //if (digitalRead(Scale1M))
    
     if Scale1M=1 then
        //digitalWrite(apply_voltage,HIGH);
         let apply_voltage=1
    
        dir ch2K in
        dir ch20K in
        dir ch200K IN
        dir ch1M OUT
        let  ch1M=0 ; LOW
    
           R1= 220; //  float  Set this values to the value of the used resistor in K ohms
          V_measured= readAD(an1) //in 8bits
    
          buffer= V_measured * Vin;
          Vout= (buffer)/1024.0;  //in volts
          buffer= (Vin/Vout) -1;
          R2= R1 * buffer;
    
    
           if R2>2 then     ;//if (R2 > 20)
    
    
            Locate 0,0
            print "Increase scale"
            //lcd.clear();
            //lcd.setCursor(0,0);
            print " Increase scale"
            //lcd.setCursor(0,1);
            Locate 1,0
            print "or insert resist"
            //delay(1000);
            Wait 1000 ms
          end if
    
    
          if (R2 < 2) then
    
    
    
            cls
           Locate 0,0
           print "SCALE:  200 - 1M"
            Locate 1,0
            print(R2)
            Locate 1,10
            print "M ohms"
            Wait 1000 ms
            //delay(1000);
    
        end if
        end if
    
      if         Scale2k=0 AND  Scale20k=0  AND  Scale200k=0   AND  Scale1M=0 THEN
    
        CLS
        Locate 0,0
        print("Select scale and")
       Locate 1,0
        print("insert  resistor")
       WAIT 1000 MS
        //delay(1000);
    
      END IF
    
    
    loop
    

    ////////////////////////////////////////////

     

    Last edit: Anobium 2023-11-02
  • Anobium

    Anobium - 2023-11-02

    The issue is likely to be the division(s) you are doing.

    I have annotated your program with the following. This will show up as RED in GCCODE.

    //! This division is intended to do what?
    //! If the original used real number and GCBASIC uses integers what is the impact?
    

    To resolve. Factor the variable ( buffer as an example) by 100, then, do the divide. You know the number is 100 times larger and the MOD 100 of the result gives you the decimal component. Then, divide 100 to get the integer result.


    Hope this helps.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.