![]() |
Forum Index : Microcontroller and PC projects : Just another datalogger with MX170
Author | Message | ||||
Volhout Guru ![]() Joined: 05/03/2018 Location: NetherlandsPosts: 5089 |
This thread is not intended to show anything new or exciting. Since it is quite warm in Netherlands, I decided to grab in my parts bin and put together a temperature logger. I found a Texas Instruments HDC2080, and an 24C32 EEPROM. Soldered these on the MUP-V3 board (an MX170 experimenter board) at the I2C pins. A small piece of code to measure every minute, and store the data in EEPROM. After some hours dump the eeprom and make a nice graph in excell. Nothing exciting, just a small tool. '---------------------------------------------------------------------------- '! HDC2080 READ '! '! read temperature and humidity sensor TI HDC2080 and converts into C and % '! write data into a 24C32 eeprom every minute. Dump eeprom into csv format '! '! Aug 2020 Volhout '---------------------------------------------------------------------------- HDC=&h40 'I2C address of the hdc2080 sensor NVM=&h57 'I2C address of the EEPROM (CAT24C32) I2C open 100,100 Dim ar(4) 'ar(0..3) is a copy of hdc2080 registers (0..3) Dim ad(2) 'ad() is the write pointer in nvm (lsb first) Dim an(4) 'an() is used for writing pointer (ar(0..1)=0, ar(2..3)=pointer) Dim av(6) 'av() combines ad() and an() for single I2C send (ad()=swapped) Dim b 'just a byte variable start: identify init_hdc main: Do Input "log(l), clearlog(cl) or dump(d): ";a$ Select Case a$ Case "l","L" SetTick 60000,do_log Print "pres any key to stop logging" Do a$=Inkey$ Loop Until a$<>"" SetTick 0,0 Case "cl" clearlog Case "d" 'dump datapoints in CSV format to terminal readpointer datapoints = (ad(0)+256*ad(1))/4 - 1 'set start of data array to adress &h0004 I2C write nvm,0,2,0,4 Print "temperature (C),humidity (%)" For i=1 To datapoints I2C read nvm,0,4,ar() convert Print Left$(Str$(temperature),5);" , ";Left$(Str$(humidity),5) Next i Case Else End End Select Loop ' ---------------- timed interrupt sub --------------- Sub do_log startmeasure readsensor add_log 'calculate values for information and show convert 'send to terminal Print "temperature = ";Left$(Str$(temperature),5);" C ", Print "humidity = ";Left$(Str$(humidity),5);" %" End Sub '-----------------------subs sensor------------------- Sub convert temperature = 165*(ar(0)+256*ar(1))/65536 - 40 humidity = 100*(ar(2)+256*ar(3))/65536 End Sub Sub identify 'identify sensor device from register &hfc...&hff I2C write hdc,0,1,&hfc I2C read hdc,0,4,ar() 'Print Hex$(ar(0)),Hex$(ar(1)),Hex$(ar(2)),Hex$(ar(3)) If (ar(0)=&h49 And ar(1)=&h54 And ar(2)=&hd0 And ar(3)=&h07) Then Print "hdc2080 found" End If End Sub Sub init_hdc 'set measurement configuration in registers &h0e and &h0f I2C write hdc,0,1,&h0e 'register &h0e ar(0)=&b00000000 'triggered measure every second 'register &h0f 'ar(1)=&b10100000 '9 bit resolution for fast measuremen ar(1)=&b01010000 '11 bit resolution for normal measuremen 'ar(1)=&b00000000 '14 bit resolution for accurate measuremen I2C write hdc,0,2,ar() trigger=ar(1)+1 'set start bit, auto reset when measurement complete End Sub Sub startmeasure 'for triggered measurements through register 0x0f, write "trigger" to 0x0f ar(0)=&h0f ar(1)=trigger I2C write hdc,0,2,ar() 'check if measurement finished (read 0x0f and check value bit 0 = 0) Do I2C write hdc,0,1,ar(0) I2C read hdc,0,1,b Pause 10 'Print "wait" Loop Until (b = trigger - 1) End Sub Sub readsensor 'read temperature and humidity from registers 0/1 and 2/3 I2C write hdc,0,1,&h0 I2C read hdc,0,4,ar() End Sub '---------------------- subs nvm --------------------- Sub writepointer 'write pointer at NVM address 0 (2 bytes) 'an() contains nvm adress (2 bytes = 00) and pointer (2 bytes) an(0)=0:an(1)=0 I2C write nvm,0,4,an() Pause 20 End Sub Sub readpointer 'read pointer at NVM address 0 (2 bytes) 'set nvm adress to &h0000 (2 bytes) I2C write nvm,0,2,0,0 'read the current data pointer into ad() I2C read nvm,0,2,ad() End Sub Sub incr_pointer_write 'increment the data pointer to the next datapair (+ 4 bytes) ad(0)=ad(0)+4 'check if page boundary crossed If ad(0)>255 Then ad(0)=ad(0)-256 ad(1)=ad(1)+1 End If 'update pointer write array and write an(2)=ad(0):an(3)=ad(1) writepointer End Sub Sub add_log 'add a temperature an humidity pair to the nvm log readpointer 'move pointer and temp and humidity into 1 array (av()) for sending 'I2C adresses are MSB first av(0)=ad(1):av(1)=ad(0) 'the data written is a copy of the hdc2080 registers 0,1,2,3 av(2)=ar(0):av(3)=ar(1):av(4)=ar(2):av(5)=ar(3) 'send to nvm I2C write nvm,0,6,av() Pause 20 'increment data pointer incr_pointer_write readpointer 'Print ad(0),ad(1) 'debug: this is the new pointer End Sub Sub clearlog ad(0)=0:ad(1)=0 'pointer adress to zero + increment = adress 4 incr_pointer_write End Sub PicomiteVGA PETSCII ROBOTS |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |