View previous topic :: View next topic |
Author |
Message |
neochrome32
Joined: 09 Jun 2013 Posts: 153
|
MCP7940N Clock cant get the time to start |
Posted: Fri Sep 05, 2014 1:40 am |
|
|
Code: |
sec &= 0x7F;
hr &= 0x3F;
i2c_start();
i2c_write(0xDE); // I2C write address
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_write(bin2bcd(sec) | 0x80); // REG 0// enable external clock(apepars to work but doesn't increment the time)
i2c_write(bin2bcd(min)); // REG 1
i2c_write(bin2bcd(hr)); // REG 2
i2c_write(bin2bcd(dow) | 0b1000); // REG 3//enable vat (which now works)
i2c_write(bin2bcd(day)); // REG 4
i2c_write(bin2bcd(mth)); // REG 5
i2c_write(bin2bcd(year)); // REG 6
i2c_write(0x80); // REG 7 - Disable squarewave output pin
i2c_stop();
|
this is the code im using to set the time and date.
as you'll see at reg3, according to datasheet i need to enable this bit to make use of the battery.
TWO things..
the battery doesn't get used when i cut the power... AND the clock doesn't start counting!
i've checked the circuit for missing components, and i've made sure i got the two 6pf and 100pf on the crystal and vbat!
oddly i can write the time and then read them out later
but the clock doesn't keep the time when no power, and doesn't even start counting |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Fri Sep 05, 2014 2:03 am |
|
|
Why not just modify the DS1307 driver?.
This chip is basically identical to the 1307, except for using a different I2C address, and having one extra bit that needs to be set (plus a couple of minor things in configuration).
It is that extra bit, that is the core problem....
Code: |
i2c_write(bin2bcd(sec) | 0x80); // REG 0
|
Bit 7 of the 'second' register, is the 'ST' bit. This starts the oscillator, and is the _opposite polarity_ to the bit on the 1307. Needs to be set to '1' for the oscillator to run. |
|
|
neochrome32
Joined: 09 Jun 2013 Posts: 153
|
|
Posted: Fri Sep 05, 2014 2:15 am |
|
|
that code is from the 1307, and in reg 0 (seconds) i did set bit7, but the clock doens't appear to move...
it sets ok, and i can cut power and the last set time re-appears..
so the circuit appears ok.
but the clock doens't start up lol |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Fri Sep 05, 2014 4:22 am |
|
|
Have you run PCM_programmer's I2C test program (in the code library), and verified that the chip is seen as acknowledging?.
Do this. It'll test that you are genuinely talking to the chip, and that your bus is working. |
|
|
neochrome32
Joined: 09 Jun 2013 Posts: 153
|
|
Posted: Fri Sep 05, 2014 8:35 pm |
|
|
i got it to work, turns out i forgot to change the spi clock speed, and yes the chip was working on the PCM so knew it wasn't the chip
i am using the SDCARD and the clock on the same spi line ... not a good move really so i've moved it off that line and ..... well it works now
Thanks for the tip on the ST bit
with the minor changes, it works well.. |
|
|
|