Well, this is an addition to this page, this time we are using a firmware created in Pic Micro Pascal 1.6.0.86 of Philippe Paternotte. I said everything on that page about this project but here is something interesting to mention: the resulted code produced by the compiler is far better than the one produced by the Mikroelektronika mikroPascal PRO 4.60 compiler and this is a great performance. More details on this page.
So, the code is:
program adc_test; {$PROCESSOR PIC12F675 } {$FREQUENCY 4 MHZ } {$OPTIMIZE MEMORY } {$CONFIG BODEN = OFF, MCLRE = OFF, OSC = INTRC_OSC_NOCLKOUT, WDT = OFF } var Volt: word; tmp1: dword; realvolt: word; function ADC_read(channel: byte): word; var Result_Lo: byte @Result + 0; Result_Hi: byte @Result + 1; begin ADCON0.CHS := Channel; ADCON0.ADON := true; { Ensure AD is activated } delay(50); ADCON0.GO := true; { Start conversion } while ADCON0.GO_DONE do; Result_Lo := ADRESL; Result_Hi := ADRESH; end; // main program begin //Initialization part INTCON := 0b00000000; IOC := 0; T1CON := 0b00000001; OPTION_REG := 0b10000001; VRCON := 0; // vref OFF CMCON := 0b00000111; // comparators OFF asm BSF STATUS, RP0 CALL 0x3FF MOVWF OSCCAL ;BCF STATUS, RP0 end; ANSEL := 0b01011000; // FOSC/16 and AN3 as analog TRISIO := 0b00111000; ADCON0.ADFM := 1; // 0 = left justified, 1 = right justified ADCON0.VCFG := 0; // voltage ref is VDD ADCON0.ADON := 1; // turn on ADC module while true do begin Volt := ADC_Read(3); // Let's do the math for the real voltage value: // (The resistive divider values are: // R1 = 10K and R2 = 1K) tmp1 := dword(537) * (Volt + 1); realvolt := tmp1 div 1000; if realvolt > 136 then //-- 13.6V GPIO.GP1 := 0; //charging off if realvolt < 130 then //-- 13.0V GPIO.GP1 := 1; //charging on if realvolt > 129 then //-- 12.9V GPIO.GP2 := 0; // consumer on if realvolt < 120 then //-- 12.0V GPIO.GP2 := 1; // consumer off //Wait for 2 seconds. This way, your charging relay will //not be afected if you have a defective battery (if your //battery can't hold the charge and is discharging fast bellow //the charged value, your relay will "flash" fast between //"charge/don't charge" - with that delay, this will not happen) delay_ms(1000); delay_ms(1000); {//test gpio.gp1 := not gpio.gp1; delay_ms(volt);} end; // while end.
At the end of all tests made with different firmwares on this controller, I chose to let this code active so, now it is in use until other changes will occur.