Problem with Wire library call from main()

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();
}

The arduino IDE builds a main function for you that initializes the hardware. Your providing your own main overrides it. Unless you really intended to do this and are aware of the consequences, better to stick to the model in sketch two.

Wildbill thanks for the reply :), Is there any workaround to achieve the same using the first, Actually this is just a part of a larger program which uses main().

You can do some research, and look at the code provided with the IDE, or look at the .cpp file created from your .ino file to see what main() actually does when the IDE creates it.

One thing it does that your main doesn’t is that it calls init() before setup().