Hello,
We are attempting to use a MCP7940n RTC with the Diablo16, the actual circuit board is actually a PiFace RTC module for the Raspberri Pi we had lying around. I've searched the forums for implementations of RTCs with the Diablo16 and certainly there are a few, but all of them use the common DS1307 RTC. I figured it should be doable to change the base code found in the old 4D site to make it run with the 7940N, but I haven't managed to make it work so here we are.
Microchip was kind enough to create a small migration guide from DS1307 to MCP7940n, so I went on and changed the device address from 0xD0 (DS1307) to 0xDE (7940N), unfortunately that is not enough. The migration guide mentions that the oscillator bit has reversed polarity in the MCP, which I assume is the part in the code that reads
Unfortunately, I'm not very electronics-savvy so it all reads very weird to me. If I understood the code correctly, this first reads the address for the seconds and retrieves the value in bit 7 (I assume that's what the '& 0x7F' is there for), then rewrites that same value in the same address? I'm assuming that (according to the migration guide) this value should read '1' indicating that the oscillator is enabled, so I tried hard-writing a 1 but it only changes the 'seconds' value to 1.
I've made sure (I think?) that the board is properly wired - the pins corresponding to SDA and SCL are wired to the corresponding Diablo16 I²C PAs set in Designer code; ground is wired to one of the Diablo16 GND pins and the board's Vcc pin is wired to the Diablo16's 3V3 out which should be enough, the I²C bus should work as long as voltage don't dips under 1V8 as per MCP7940N's datasheet.
A few months ago this same RTC board was used on a Raspberri Pi and it worked, so it doesn't seem like it's a hardware problem. We tested the board continuity and it was all right, so I guess I'm skipping something on the conversion from DS1307 to MCP7940 that I can't quite see. Any help is appreciated.
Below is the code I'm currently testing. This is *not* the one from the old 4D site, it was posted by a forum member that was also having issues with a RTC (in his case, DS1307).
We are attempting to use a MCP7940n RTC with the Diablo16, the actual circuit board is actually a PiFace RTC module for the Raspberri Pi we had lying around. I've searched the forums for implementations of RTCs with the Diablo16 and certainly there are a few, but all of them use the common DS1307 RTC. I figured it should be doable to change the base code found in the old 4D site to make it run with the 7940N, but I haven't managed to make it work so here we are.
Microchip was kind enough to create a small migration guide from DS1307 to MCP7940n, so I went on and changed the device address from 0xD0 (DS1307) to 0xDE (7940N), unfortunately that is not enough. The migration guide mentions that the oscillator bit has reversed polarity in the MCP, which I assume is the part in the code that reads
Code:
n:=readbyte(0) & 0x7F; // ensure CH bit is clear else clock wont run writebyte(0, n);
I've made sure (I think?) that the board is properly wired - the pins corresponding to SDA and SCL are wired to the corresponding Diablo16 I²C PAs set in Designer code; ground is wired to one of the Diablo16 GND pins and the board's Vcc pin is wired to the Diablo16's 3V3 out which should be enough, the I²C bus should work as long as voltage don't dips under 1V8 as per MCP7940N's datasheet.
A few months ago this same RTC board was used on a Raspberri Pi and it worked, so it doesn't seem like it's a hardware problem. We tested the board continuity and it was all right, so I guess I'm skipping something on the conversion from DS1307 to MCP7940 that I can't quite see. Any help is appreciated.
Below is the code I'm currently testing. This is *not* the one from the old 4D site, it was posted by a forum member that was also having issues with a RTC (in his case, DS1307).
Code:
#platform "uLCD-70DT" #inherit "4DGL_16bitColours.fnc" #constant SECONDS 0 //seconds register address #constant MINUTES 1 //minutes register address #constant HOURS 2 //hours register adress #constant DATE 4 //date register address #constant MONTH 5 //month register address #constant YEAR 6 //year register address #constant DS1307 0xDE #constant WR 1 //Write or Read bit // global variables var seconds, minutes, hours, date, month, year; // Convert binary coded decimal to normal decimal numbers func bcdToDec(var val) return( (val/16*10) + (val%16) ); endfunc // read a single byte from the required register func readbyte(var address) var b; I2C1_Start(); // Generate Start condition I2C1_Write(DS1307); // send slave address I2C1_Write(LObyte(address)); // Send register address I2C1_Restart(); // Generate Restart I2C1_Write(DS1307+WR); // send control byte for Read b := I2C1_Read(); // read the byte I2C1_Nack(); // finished reading, send Not Ack I2C1_Stop(); // Send Stop Condition return b; // return the register value endfunc // write a single byte to the required register func writebyte(var register, var value) I2C1_Start(); // Generate Start condition I2C1_Write(DS1307); // send slave address I2C1_Write(register); // select the register I2C1_Write(value); // save the value in selected register I2C1_Stop(); // finished with bus endfunc func main() var n, k; I2C1_Open(I2C_SLOW, PA4, PA5); // (100khz, SCL Pin, SDA Pin) pause(10); n := readbyte(0) & 0x7F; // ensure CH bit is clear else clock wont run writebyte(0, n); // print time and date every second repeat seconds := bcdToDec(readbyte(SECONDS)); // read xloxk xhip, convert to minutes := bcdToDec(readbyte(MINUTES)); hours := bcdToDec(readbyte(HOURS)); date := bcdToDec(readbyte(DATE)); month := bcdToDec(readbyte(MONTH)); year := bcdToDec(readbyte(YEAR)) + 2000; gfx_MoveTo(0, 0); print ("Time: ", hours, ":", minutes, ":", seconds, "\n"); print ("Date: ", date, ":", month, ":", year, "\n"); print ("\nCycles: ", ++k); pause(1000); forever endfunc
Comment