// I2C BV4237 is a PCB that contains an RTC, Temperature // sensor and analog in, digital out // // The following are the default addresses, RXC can be // changed if required. constant RXC 0x62 // i2c default bv4237 address constant TEMP 0x90 // i2c address of temp sensor constant RTC 0xd0 // real time clock // ******************************************************************* // Checks device to see if it is connected and // all devices are responding, also opens the I2C channel // return 3 if all okay, any less and there is a fault // ******************************************************************* function i2_37_start dim rv=0 i2copen 100000 // modist 100k rv=i2ctest(RXC) rv=rv+i2ctest(TEMP) rv=rv+i2ctest(RTC) result rv endf // ===================== temperature ================================= // ******************************************************************* // Gets the temperature as a string, assumes set up as 9 bit // mode and so only one cecimal place // This can be improved by reading the temperature a few times over // sa a period of 1-2 seconds and then avaeraging out the results // ******************************************************************* function i2_37_temp$ // gets temperature as a string dim t[3], dpl, t$[:8] i2cread TEMP 0, t[1];2 if t[2] > 128 then dpl=5 endif t$=str$(t[1])+"."+str$(dpl) endf // ===================== RTC ========================================= // ******************************************************************* // starts clock after power down // ******************************************************************* function i2_37_startrtc i2cwrite RTC 0x0c 0 endf // ******************************************************************* // set time on i2c clock, enter numbers as decimal, example // i2_37_settime(14,34,0) // ******************************************************************* function i2_37_settime(h%, m%, s%) s%=dec2bcd(s%,2):m%=dec2bcd(m%,2):h%=dec2bcd(h%,2) i2cwrite RTC 0 0 s% m% h% endf // ******************************************************************* // set date on i2c clock, enter numbers as decimal, example // i2_37_setdate(29,4,2012,2) // ******************************************************************* function i2_setdate(dom%, mon%, yr%, dow%) dom%=dec2bcd(dom%,2):mon%=dec2bcd(mon%,2):dow%=dec2bcd(dow%,2) yr%=dec2bcd(yr%,4)// year is 4 digits yr1%=and(yr%,0xff) // but only 2 used // NOTE rtc MT1T81, registers start from 0 i2cwrite RTC 4 dow% dom% mon% yr1% endf // ******************************************************************* // time returns time on i2c clock as a string, use this before // i2_37_startrtc to find out when it was last powered // ******************************************************************* function i2_37_time$ dim t[8], t$[:50] i2cread RTC 0, t[1];8 // time t$=bcd2dec(t[4],2)+":"+bcd2dec(t[3],2)+":"+bcd2dec(t[2],2) result t$ endf // ******************************************************************* // time returns date on i2c clock as a string, use this before // i2_37_startrtc to find out when it was last powered // ******************************************************************* function i2_37_date$ dim t[8], t$[:50] i2cread RTC 0, t[1];8 // date t$=bcd2dec(t[6],2)+"/"+bcd2dec(t[7],2)+"/" yr=2000+bcd2dec(t[8],2) t$=t$+yr+" "+bcd2dec(t[5],2) result t$ endf // ================== Analogue & Digital ============================= // The BV4237 has 5 analogue inputs and 8 digital outputs all with // various options, some of these are used here. // ******************************************************************* // Read an analogue input channel and return the value which // will be in the range 0-1023 (10 bits), enter cahnnel number // as 0 to 4 // ******************************************************************* function i2_37_read(channel) dim t[11], rv if channel > 4 then print "channel error" exitf // exit function else channel=channel*2 endif i2cread RXC 2 0,t[1];10 // read all channels rv=t[channel+1]*256 // high byte rv=rv+t[channel+2] // low byte result rv endf // ******************************************************************* // output to digital port, set channel number 1-8 and power // level 0 to 8 // ******************************************************************* function i2_37_out(chan, power) // could do some checking here i2cwrite RXC chan+0x11 power endf