' Dag van de Wetenschap nov. 2019 Energyville / UBA ' PIC12LF1552 microcontroller: see datasheet on www.microchip.com. ' MCP9808 i2c-sensor: see datasheet on www.microchip.com. ' Programmed with mikroBasic for PIC v6.*.* www.mikroe.com ' Coded by on4ssb@amsat.org / paul.meugens@kuleuven.be ' Code is optimized to maximize battery life. ' (uC and sensor are sleeping when not needed.) ' Code was debugged using a software serial port output on pin RA2. ' (Debug code marked out in the finalized version.) program MyProject dim upperbyte,lowerbyte as byte dim i as byte dim temperature as word dim strtemperature as string[8] dim index as integer dim flikdigit1,flikdigit2,flikdigit3 as byte dim rest as word dim negflag as bit ' Software I2C connections dim Soft_I2C_Scl as sbit at RA5_bit Soft_I2C_Sda as sbit at RA4_bit Soft_I2C_Scl_Direction as sbit at TRISA5_bit Soft_I2C_Sda_Direction as sbit at TRISA4_bit ' End Software I2C connections sub procedure zerotemp ' een nul tonen door beide leds aan te doen. lata.1 = 0 ' oranje led aan lata.0 = 0 ' blue led aan delay_ms(2000) lata.1 = 1 ' oranje led uit lata.0 = 1 ' blue led uit delay_ms(1000) end sub sub procedure show(dim value as byte) ' leds aandoen met 'value' TRISA.0 = 0 ' set ra0 output red led TRISA.1 = 0 'set ra1 output blue led for i = 1 to value if negflag = 0 then lata.0 = 0 ' oranje led aan delay_ms(200) lata.0 = 1 ' oranje led uit delay_ms(200) end if ' end if negflag = 0 if negflag = 1 then lata.1 = 0 ' blue led aan delay_ms(200) lata.1 = 1 ' blue led uit delay_ms(200) end if ' end if negflag = 1 next i delay_ms(1000) end sub main: ' Actual program begins here ... ANSELA = 0 ' turn off ADC's, otherwise nothing goes ... TRISA.0 = 0 ' set ra0 output red led TRISA.1 = 0 'set ra1 output blue led osccon = $70 '8mc pllen = 0 /ircf = 111 scs=00 ' asm clrwdt ' clear watchdog ' end asm wdtcon = $1a ' 8sec watchdog , enable watchdog ' wdtcon = $1c ' 16sec watchdog , enable watchdog strtemperature ="" ' welcome toggle of leds loop: for i = 1 to 5 lata.1 = 1 lata.0 = 0 delay_ms(150) lata.1 = 0 lata.0 = 1 delay_ms(150) next i ' end of welcome toggle lata.1 = 1 ' both leds off lata.0 = 1 delay_ms(2000) '-------------------- ' goto loop ' asm clrwdt ' clear watchdog ' end asm ' wdtcon = $1a ' 8sec watchdog , enable watchdog ' wdtcon = $1c ' 16sec watchdog , enable watchdog soft_uart_init(porta,1,2,9600,0) ' rx-pin ra1, tx-pin ra2 'soft_I2C_Init() ' Initialize Soft I2C communication 'opn: ' porta.0 = 1 ' delay_ms(100) ' porta.0 = 0 ' delay_ms(100) ' goto opn soft_I2C_Init() ' Initialize Soft I2C communication opn: delay_ms(10) ' lees config register soft_i2c_start() soft_i2c_write($30) '0011 0000 write command soft_i2c_write($01) 'adddress config regsiter is $01 delay_ms(10) soft_i2c_start() soft_i2c_write($31) '0011 0001 read command upperbyte = soft_i2c_read(1) lowerbyte = soft_i2c_read(0) soft_i2c_stop() 'soft_UART_Write(upperbyte) 'soft_UART_Write($20) ' space 'soft_UART_Write(lowerbyte) 'soft_UART_Write($0D) 'soft_UART_Write($0A) delay_ms(20) ' einde lees config register ' lees Ta register soft_i2c_start() soft_i2c_write($30) '0011 0000 write command soft_i2c_write($05) 'address Ta register is $05 soft_i2c_start() soft_i2c_write($31) '0011 0001 read command upperbyte = soft_i2c_read(1) lowerbyte = soft_i2c_read(0) soft_i2c_stop() upperbyte = upperbyte and $1F ' discard flag bits if ((upperbyte and $10) = $10) then ' Ta < O negative temp upperbyte = upperbyte and $0F ' clear sign lowerbyte = lowerbyte div 16 temperature = 256 - ((upperbyte * 16) + lowerbyte ) NEGFLAG = 1 else ' upperbyte = upperbyte and $0F ' clear sign lowerbyte = lowerbyte div 16 temperature = ((upperbyte * 16) + lowerbyte ) NEGFLAG = 0 end if ' convert to decimal 'soft_UART_Write(hi(temperature)) 'soft_UART_Write($20) ' space 'soft_UART_Write(lo(temperature)) 'soft_UART_Write($0D) 'soft_UART_Write($0A) 'for i = 1 to 10 'porta.0 = 0 'delay_ms(50) 'porta.0 = 1 'delay_ms(50) 'next i 'delay_ms(500) ' einde lees Ta register '--------- WordToStr(temperature, strtemperature) ltrim(strtemperature) 'get ride of the leading space if negflag = 1 then Soft_Uart_write("-") end if if negflag = 0 then Soft_Uart_write("+") end if FOR index = 0 to length(strtemperature) - 1 Soft_UART_Write(strtemperature[index]) Next index ' ' Soft_UART_Write(13) 'carriage return Soft_UART_Write(10) 'line feed ' delay_ms(500) 'goto opn '---------------- led flikker flikdigit1 = temperature div 100 rest = temperature - (flikdigit1 * 100) flikdigit2 = rest div 10 rest = rest - (flikdigit2 * 10) flikdigit3 = rest Soft_UART_Write(flikdigit1) Soft_UART_Write(flikdigit2) Soft_UART_Write(flikdigit3) ' Soft_UART_Write($ff) 'carriage return if temperature =0 then zerotemp goto jumpover end if if (flikdigit1>0) then show(flikdigit1) end if if (flikdigit2 > 0) then show(flikdigit2) end if ' geval x0x if (flikdigit2= 0) and (flikdigit1 > 0) then ' zerotemp ' show 0 flok digit 2 end if ' geval xx0 if (flikdigit3= 0) and ((flikdigit1 > 0) or (flikdigit2 > 0)) then ' zerotemp ' show zero flikdigit3 end if if (flikdigit3 > 0) then show(flikdigit3) end if '-------------- ' sleep temp chip soft_i2c_start() soft_i2c_write($30) '0011 0000 write command soft_i2c_write($01) 'adddress config regsiter is $01 soft_i2c_write($01) 'shutdown msb soft_i2c_write($00) ' lsb soft_i2c_stop() delay_ms(10) '------------------------------- jumpover: ' put pic12lf1552 to sleep and wait for watchdog timeout to wake up. asm clrwdt sleep nop clrwdt nop end asm 'asm clrwdt 'end asm ' delay_ms(2000) soft_i2c_start() soft_i2c_write($30) '0011 0000 write command soft_i2c_write($01) 'adddress config register is $01 soft_i2c_write($00) 'wake up soft_i2c_write($00) ' lsb soft_i2c_stop() delay_ms(10) goto opn end.