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
#chip16f873A,8;12f675,4#optionexplicit;#config_INTRC_OSC_NOCLKOUT;SoftwareI2CForLCD#defineI2C_MODEMaster#defineI2C_DATAportb.3;gpio.0'SDA #define I2C_CLOCK portb.4 ;gpio.4 'SCL#defineI2C_DISABLE_INTERRUPTSON#defineLCD_IO10#defineLCD_I2C_Address_10x4E;0x4edefaultto0x4E#defineLCD_SPEEDFAST//resistormeasure=Rx#DEFINEanalogPinAN0#definech2Kportc.4;int#DEFINEch20Kportc.5;int#DEFINEch200Kportc.6;int#DEFINEch1Mportc.7;int#DEFINEapply_voltageportb.0//ROTRYSWITCCONFIINPUT#DEFINEScale2kportc.0;=9;int#DEFINEScale20kportc.1;=12;int#DEFINEScale200kportc.2;=11;int#DEFINEScale1Mportc.3;=10;intdimbuffer,Vin,R2,asbytedimV_measured,VoutaswordV_measured=0;intVin=5;intVout=0;floatR2=0;floatbuffer=0;float//voidsetup()//directions//dirV_measuredIN//diranalogPinINdirapply_voltageOUT//Detecttherangemode(0-1kK;10k-100k;100k-1M)RESISTANCEdirScale2kINdirScale20kindirScale200kINdirScale1MIN//Wesetthispinsasinputfornow.dirch2KINdirch20KINdirch200KINdirch1MIN//voidloop()do///////////////////-2k-///////////////////////if(digitalRead(Scale2k))ifScale2k=1then//digitalWrite(apply_voltage,HIGH);letapply_voltage=1dirch2KOUTdirch20KINdirch200KINdirch1MIN//digitalWrite(ch2K,LOW);SETch2K=0;LOWdimR1asbyteR1=2;//SetthisvaluestothevalueoftheusedresistorinKohmsV_measured=readAD(AN1)//in8bitsbuffer=V_measured*VinVout=(buffer)/1024;//involts1024.0buffer=[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 ifcls 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 IFloop
////////////////////////////////////////////
Last edit: Anobium 2023-11-02
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
////////////////////////////////////////////
Last edit: 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.
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.