First post from a newbie!
I'm working on a logger (16f873a). To save external EEPROM space, the 10 bytes data from RTC, sensors etc are compressed to an 8-byte array that is sent (byte by byte) to the EEPROM at the end of each logged event. The byte compression is done by culling the unused bits in the data bytes and combining the essential data from 2 bytes into one. The following code snippet works, but there must be a more elegant and economical way of doing it. Any suggestions?
Thanks!
Month var byte 'formatted data from RTC, only 4 bits neccessary
Day var byte 'formatted data from RTC, only 5 bits neccesary
Hours var byte 'formatted data from RTC
Mins var byte 'formatted data from RTC
Secs var byte 'formatted data from RTC
logdata var byte [8] '8 byte long array, containing data from RTC, incident direction,
' 'incident duration, temperature and humidity.
This array is sent to the external EEPROM.
'
logdir var byte 'incident direction byte, only 2 bits neccessary
modir var byte 'temporary byte used to compress Month and Direction data to
' 'same byte in array to save eeprom space
daytra var byte ''temporary byte used to compress Day and part of Duration data
' 'to same byte in array to save eeprom space
transwrite var byte 'temporary byte for part of flittime data
flittime var word 'Event duration word, 11 bits neccessary
thermo var byte 'Temperature byte
humi var byte 'Humidity byte
'
logdata[1] = Hours 'the full 8 bits of each of these 3 data bytes is transferred
' 'to the corresponding byte in the array
logdata[2] = Mins
logdata[3] = Secs
'
modir.0=Month.0 'here we strip the bottom 4 bits from the Month byte and put them
' 'in the corresponding bits of the modir byte
modir.1=Month.1
modir.2=Month.2
modir.3=Month.3
'
daytra.0=Day.0 'and the same with the bottom 5 bits from the Day variable,
' 'going to the daytra byte
daytra.1=Day.1
daytra.2=Day.2
daytra.3=Day.3
daytra.4=Day.4
modir.4=logdir.0 'here we strip the bottom four bits from the logdir variable
' 'and put them in the top 4 bits of the modir byte
modir.5=logdir.1
modir.6=logdir.2
modir.7=logdir.3
'
logdata[4] = modir 'so this byte in the array contains both Month and Direction data
'
transwrite=flittime.lowbyte 'Since flittime is a word variable (with 11 bits of valid data)
' 'we need to distribute it over 2 bytes.The low byte goes to transwrite.
logdata[5] = transwrite
'
daytra.5=flittime.8 'The 3 unused bits of the daytra byte are filled with 3 more bits from
' 'the flittime variable.
daytra.6=flittime.9
daytra.7=flittime.10
'
logdata[0] = daytra 'Byte 0 of the logdata string contains both the 3 high bits of the transition
' 'time count and 5 bits Day data
logdata[6] = thermo 'puts Temperature data in byte 7 of the array
logdata[7] = humi 'puts humidity data in byte 8 of the array
Bookmarks