I display the RTC clock to a hyperterminal but managed to get all 0. The clock doesnt increment and also doesnt display the preset values. Anyone can please help? Thanks in advance
**broken link removed**
**broken link removed**
**broken link removed**
**broken link removed**
Code:
#include <htc.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include "../peripheral/sw_i2c.h"
#include "../hardware/display.h"
#include "../hardware/buttons.h"
#include "../hardware/resistors.h"
#include "../main/settings.h"
#include "../utility/delay.h"
#include "../main/isr.h"
#include "../modes/ds1307.h"
#include "../serial_lib.h"
//==========================
// initial DS1307
//==========================
void init_DS1307()
{
DS1307_SCL = 0;
DS1307_SDA = 0;
//Set Time
write_DS1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
write_DS1307(1,0x10); //write min 27
write_DS1307(2,0x01); //write hour 14
write_DS1307(3,0x02); //write day of week 2:Monday
write_DS1307(4,0x05); // write date 17
write_DS1307(5,0x01); // write month 6 June
write_DS1307(6,0x09); // write year 8 --> 2008
write_DS1307(7,0x10); //SQWE output at 1 Hz
write_DS1307(0,0x00); //Reset second to 0 sec. and start Oscillator
}
//==========================
// write data one byte to
// DS1307
//==========================
void write_DS1307(unsigned short address, unsigned short data)
{
short int status;
di();
SWStartI2C();
SWWriteI2C(0xd0);
SWWriteI2C(address);
SWWriteI2C(data);
SWStopI2C();
SWRestartI2C();
status=SWWriteI2C(0xd0);
while(status==1)
{
SWStartI2C();
status=SWWriteI2C(0xd0);
}
ei();
}
//==========================
// read data one byte from D3107
//==========================
unsigned short read_DS1307(unsigned short address)
{
unsigned short data;
di();
SWStartI2C();
SWWriteI2C(0xd0);
SWAckI2C();
SWWriteI2C(address);
SWAckI2C();
SWRestartI2C();
SWWriteI2C(0xd1);
SWAckI2C();
data = SWGetsI2C(0,0);
SWStopI2C();
ei();
return(data);
}
unsigned short myBcd2Dec(unsigned short bcd){
return ((bcd >> 4)*10+(bcd & 0x0F));
}
void ds1307(void) {
unsigned short sec,min,hour,day,date,month,year;
init_comms();
SYNC = 0;
BRGH = 0;
SPBRG = 32;//9600
display_clear();
//DelayMs(50);
init_DS1307(); // initial DS1307
//write_DS1307(0, sec); //SQWE output at 1 Hz
//sec=read_DS1307(0);
//write_DS1307(0,sec & 0x7F); // enable oscillator(bit 7 =0)
while (BUTTON_1 || BUTTON_2) {
if (!BUTTON_1 && BUTTON_2) {
}
while (BUTTON_1 && !BUTTON_2) {
sec=read_DS1307(0); // read second
min=read_DS1307(1); // read minute
hour=read_DS1307(2); // read hour
day=read_DS1307(3); // read day
date=read_DS1307(4); // read date
month=read_DS1307(5); // read month
year=read_DS1307(6); // read year
putch(0x0c);
printf("Time : %02d:%02d:%02d\r\n",hour,min,sec);
printf("Day : %02d\r\n",day);
printf("Date : %02d/%02d/20%02d\r\n",date,month,year);
DelayMs(1000);
}
}
}