Dear Friends,
I having trying very hardly to get the RTC DS1307 to communicate with PIC 18F4520. The problem that i having is that my LCD display out is the register address not the value that i set from the register.The below attach is my program.Anyone can check for me where i did it wrong in my program???
LCD Display Show:
02:01:00
04/05/06
Parameter:
PIC OSC = 4MHz
I2C Speed = 100KHz
SDA & SCL pull-up resistor = 4.7Kohm
Thank You
I having trying very hardly to get the RTC DS1307 to communicate with PIC 18F4520. The problem that i having is that my LCD display out is the register address not the value that i set from the register.The below attach is my program.Anyone can check for me where i did it wrong in my program???
LCD Display Show:
02:01:00
04/05/06
Parameter:
PIC OSC = 4MHz
I2C Speed = 100KHz
SDA & SCL pull-up resistor = 4.7Kohm
Code:
#include<p18f4520.h>
#include<i2c.h>
#include<stdlib.h>
#include<stdio.h>
#include<delays.h>
#include<system4520.h>
#pragma config OSC = HS
#pragma config PWRT = OFF
#pragma config PBADEN = OFF
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config DEBUG = OFF
void i2c_init(void);
void rtc_init(void);
void write_rtc(unsigned char add, unsigned char data);
unsigned char read_rtc(unsigned char add);
unsigned char read_rtcn(unsigned char add);
unsigned char Read_I2C(void);
void main(void)
{
int hr,mn,sc,dt,mon,yr,a,b;
unsigned char sec,min,hour,day,date,month,year;
lcd_init();
i2c_init();
rtc_init();
write_rtc(0x00, 0x80);
write_rtc(0x01, 0x00);
write_rtc(0x02, 0x19);
write_rtc(0x03, 0x04);
write_rtc(0x04, 0x03);
write_rtc(0x05, 0x02);
write_rtc(0x06, 0x12);
write_rtc(0x00, 0x00);
do {
sec = read_rtc(0x00);
min = read_rtc(0x01);
hour = read_rtc(0x02);
day = read_rtc(0x03);
date = read_rtc(0x04);
month = read_rtc(0x05);
year = read_rtc(0x06);
clear_screen();
cursor_set(0,0);
hr = ((hour & 0x03) >> 4)*10 + (hour & 0x0F);
a = ((hr/10)+48);
WriteC(a);
b = ((hr%10)+48);
WriteC(b);
cursor_set(0,2);
WriteC(0x3A);
cursor_set(0,3);
mn = (min& 0x0f) + ((min>> 4) * 10);
a = ((mn/10)+48);
WriteC(a);
b = ((mn%10)+48);
WriteC(b);
cursor_set(0,5);
WriteC(0x3A);
cursor_set(0,6);
sc = (sec & 0x0f) + (((sec >> 4) & 0x07) * 10);
a = ((sc/10)+48);
WriteC(a);
b = ((sc%10)+48);
WriteC(b);
cursor_set(1,0);
dt = (date & 0x0f) + (( date>> 4) * 10);
a = ((dt/10)+48);
WriteC(a);
b = ((dt%10)+48);
WriteC(b);
cursor_set(1,2);
WriteC(0x2f);
cursor_set(1,3);
mon = (month & 0x0f) + ((month >> 4) * 10);
a = ((mon/10)+48);
WriteC(a);
b = ((mon%10)+48);
WriteC(b);
cursor_set(1,5);
WriteC(0x2f);
cursor_set(1,6);
yr = (year & 0x0f) + ((year >> 4) * 10);
a = ((yr/10)+48);
WriteC(a);
b = ((yr%10)+48);
WriteC(b);
Delay10KTCYx(20);
} while(1);
}
void i2c_init(void)
{
PORTCbits.RC3 = 1;
PORTCbits.RC4 = 1;
SSPADD = 0x09;
SSPCON1 = 0x00;
SSPCON2 = 0x00;
SSPCON1 = 0x28;
}
void rtc_init(void)
{
StartI2C();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(0x00);
RestartI2C();
WriteI2C(0xD0);
WriteI2C(0x07);
WriteI2C(0x80);
StopI2C();
IdleI2C();
}
void write_rtc(unsigned char add, unsigned char data)
{
StartI2C();
WriteI2C(0xD0);
WriteI2C(add);
WriteI2C(data);
StopI2C();
}
unsigned char read_rtc(unsigned char add)
{
unsigned char result;
StartI2C();
WriteI2C(0xD0);
WriteI2C(add);
RestartI2C();
WriteI2C(0xD1);
IdleI2C();
result=ReadI2C();
AckI2C();
StopI2C();
return result;
}
unsigned char read_rtcn(unsigned char add)
{
unsigned char result;
StartI2C();
WriteI2C(0xD0);
WriteI2C(add);
RestartI2C();
WriteI2C(0xD1);
result=ReadI2C();
NotAckI2C();
StopI2C();
return result;
}
unsigned char Read_I2C(void)
{
SSPCON2bits.RCEN = 1;
while(!SSPSTATbits.BF);
return(SSPBUF);
}
Thank You