I am having trouble with writing data into DS1307 using I2C.
The problem is that the program gets stuck in the Wire.endTransmission() call, if the sqw1() is called from the main().But it works fine, if the call is made from loop()
Here are the two programs, first displays nothing in the lcd, but the second returns 0(success) in the lcd.
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 4, 5, 6, 7);
/*
DS1307 Square-wave machine
Used to demonstrate the four different square-wave outputs from Maxim DS1307
See page nine of data sheet for more information
John Boxall - tronixstuff.com
*/
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 // each I2C object has a unique bus address, the DS1307 is 0x68
void sqw1() // set to 1Hz
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary)
Wire.endTransmission();
lcd.clear();
lcd.print(Wire.endTransmission(),DEC);
bitSet(PORTB,5);
}
int main()
{
lcd.begin(16,2);
Wire.begin();
sqw1();
//bitSet(DDRB,5);
}
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 4, 5, 6, 7);
/*
DS1307 Square-wave machine
Used to demonstrate the four different square-wave outputs from Maxim DS1307
See page nine of data sheet for more information
John Boxall - tronixstuff.com
*/
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 // each I2C object has a unique bus address, the DS1307 is 0x68
void setup()
{
lcd.begin(16,2);
Wire.begin();
bitSet(DDRB,5);
}
void sqw1() // set to 1Hz
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0x07); // move pointer to SQW address
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary)
Wire.endTransmission();
lcd.clear();
lcd.print(Wire.endTransmission(),DEC);
//bitSet(PORTB,5);
}
void loop()
{
sqw1();
while(1);
// sqwOff();
}