Real Time Clock Backup
The PIC32 does not have a means of keeping the RTC information once the power has been removed. It may just be possible to organise the main oscillator to switch off and then power from a battery but there is an easier way.
![]() |
![]() |
|
BV4236 |
BV4237 |
The above devices will provide a battery backed up RTC (the silver thing is the battery holder). The BV4236 has an RTC and a digital temperature sensor and the BV4237 has in addition 8 digital outputs and 5 analogue inputs. Both are available from http://www.byvac.co.uk/bv/I2C.htm
BV4236
For the purpose of this interface exercise we will use the simpler BV4236. Something to bare in mind is that all I2C devices require a pair of pull up resistors per bus and the BV513 or the BV4236 do not provide these so arrangements must be made. The device will actually plug straight into JP8 on the BV513, if you want to do this then solder a pair of pull up resistors to the back of the BV4236. An alternative is to use some ribbon cable and 10 way IDC connectors, this is very useful for multiple I2C and IASI devices.

![]() |
![]() |
![]() |
The IDC connectors are very easy to fit onto the ribbon cable, no soldering is required and the cable makes an ideal bus.

Two 5k6 resistors are soldered to a pinhead connector before being pushed into the IDC socket. The resistor values are not that important, anything between 3k and 10k would probably do. Using a cable in this way gives the opportunity to add other I2C devices.
The software (code is in downloads)
Reading and writing to I2C devices has been covered elseware so this text will not go into that again. There are two devices mounted on the BV4236: The M41T81 which is the Real Time Clock and the MCP9802 which is the temperature sensor. The RTC has an address of 0xd0 and the digital temperature sensor has an address of 0x90.
Just digressing form the RTC slightly the code for the temperature sensor is here:
dim t[3], dpl=0
i2cread I2_TMP 0, t[1];2
if t[2] > 128 then
dpl=5
endif
result str$(t[1])+"."+str$(dpl)
endf
I2_RTC and I2_TMP have been defined as constants, this simply reads the temperature sensor and returns it as a string. The default is 9 bits, 8, including the sign bit are used for the temperature in whole numbers (to the left of the decimal) and 1 bit is used for the 1/2 degree resolution.
The RTC
In common with most RTC devices, including the one in the PIC32, the date and time are stored in BCD (Binary Coded Decimal) format. This means some conversion will be needed to store and fetch data. PIC32-Basic provides words for converting between BCD and back. The functionss i2_settime, i2_setdate, i2_time$ and i2_date$ get and retrieve dates. What is less obvious is that the M41T81 will keep the time at power down, this needs to be started again with i2_startrtc:
function i2_startrtc
i2cwrite I2_RTC 0x0c 0
endf
This can be quite useful as it indicates when power was removed form the system.
dim t[2], t$, d$
print "Time at shutdown ";i2_date$;" ";i2_time$
i2_startrtc
i2cread I2_RTC 0, t[1];1 // compare values
wait 20
i2cread I2_RTC 0, t[2];1 // compare values
if t[2]>t[1] then
print "Clock running so setting system time"
settime i2_time$
setdate i2_date$
endif
print "Current time ";i2_date$;" ";i2_time$
endf
The purpose of i2_clock is to set the system time provided the I2C RTC is running, this is determined by taking two readings from it 20ms apart. If it is running the i2_time$ and i2_date$ return a format that is the exact same format required for setting the system data and time.
There is a further function 'i2_time' in the i2ctime.bas code which looks to see if there is an RTC connected, if so it uses i2c_clock, if not it asks the user for the correct time and date. This could be used as a start up routine in a user interactive system.
The i2ctest() function does not work in all circumstances, it may not be possible to detect the presence of an I2C device without causing a system error.




