Stap 4: Software-implementatie
Voor het programmeren van CBDBv2 Evo Board en uploaden van de bestuurders en de software zullen we blijven gebruiken de LuaUploader zoals voorheen.
1. de gebruikte GPIO pinnen en variabelen definiëren:
ADC_SRC = 5 -- GPIO14 - select Voltage Divider / Current Input sda=2 -- GPIO4 - SDA scl=1 -- GPIO5 - SCL gpio.mode(ADC_SRC,gpio.OUTPUT, gpio.PULLUP) gpio.write(ADC_SRC,1) -- Voltage Measurement - Voltage Divider Source selected gpio.write(ADC_SRC,0) -- Current Measurement - Current Shunt Monitor output selected voltdiv= 0.00412 -- Voltage reading calibration dival = 0.00096 -- ADC volt/div value - CALIBRATE !! resdiv = 4.31447 -- Voltage Divider Ratio - CALIBRATE!! divalI = 0.9425 -- Current volt/div ratio - CALIBRATE!! cpct = 0 -- Calculated Delivered Energy adcI = 0 -- ADC readings - Curent adcV = 0 -- ADC readings - Voltage pwr = 0 -- Calculate Power t=0 -- time
2. Lees ADC - Voltage
function readADC_Voltage() adcV = 0 advr = 0 advr=adc.read(0) print("\nADCV Step : " ..string.format("%g",advr).." steps") adcV=advr*dival*resdiv print("Voltage : " ..string.format("%g",adcV).." V") return adcV end
3. Lees ADC - stroom
function readADC_Current() adcI = 0 adcr = 0 adcr=adc.read(0) adcI=adcr*divalI print("ADCI Step : " ..string.format("%g",adcr).." steps") print("Current : " ..string.format("%g",adcI).." mA") return adcI end
4. Lees ADC proces functie en momentane vermogen consumtion berekening
function readUI() gpio.write(ADC_SRC,1) --select source adcV = readADC_Voltage() tmr.delay(10000) gpio.write(ADC_SRC,0) --select source adcI = readADC_Current() pwr = adcI*adcV print("Power : " ..string.format("%g",pwr).." mW") end
5. number format-functie voor goede LCD afdrukken
nr_format = function (fnr,unit) if (fnr > 1000) then fnri=fnr/1000 uniti=string.sub(unit, 2) nrf=string.format(" %.3f%s ",fnri, uniti) else if (fnr < 100) then if (fnr < 10) then nrf = string.format(" %.1f%s ",fnr, unit) else nrf = string.format(" %.1f%s ",fnr,unit) end else nrf = string.format("%.1f%s ",fnr,unit) end end return nrf end
6. LCD Print - > spanning / huidige / energie / Power
Voor meer informatie over het stuurprogramma van de LCD I2C, neem een kijkje op de ST7032i LCD Driver artikel
function readUI() gpio.write(ADC_SRC,1) --select source adcV = readADC_Voltage() tmr.delay(10000) gpio.write(ADC_SRC,0) --select source adcI = readADC_Current() pwr = adcI*adcV print("Power : " ..string.format("%g",pwr).." mW") end
7. de hoofdprogramma
require('st7032i') st7032i:init_i2c(sda,scl) st7032i:init_LCD() st7032i:lcd_clear() st7032i:lcd_print(1,1,string.format("Battery Monitor")) st7032i:lcd_print(3,2,string.format("Starting ...")) tmr.alarm(0, 10000, 1, function() readUI() LCDout() tmr.delay(1000) end)