Hello everyone.
I am working with ds1307 RTC and pic 18f4520 mcu.
Compiler - MikroC Pro for PIC. Oscillator - 20 MHz & 32kHz(for driving RTC).
My clock is not working & also not displaying given time & date value.
It's showing some random values & sometimes week is also displaying but not everytime. Also "seconds" value doesn't increments & i have also connected battery for RTC. I have attached my code , Kindly Help me.
I am working with ds1307 RTC and pic 18f4520 mcu.
Compiler - MikroC Pro for PIC. Oscillator - 20 MHz & 32kHz(for driving RTC).
My clock is not working & also not displaying given time & date value.
It's showing some random values & sometimes week is also displaying but not everytime. Also "seconds" value doesn't increments & i have also connected battery for RTC. I have attached my code , Kindly Help me.
Rich (BB code):
// Lcd module connections
sbit LCD_RS at LATB0_bit;
sbit LCD_EN at LATB1_bit;
sbit LCD_D4 at LATB2_bit;
sbit LCD_D5 at LATB3_bit;
sbit LCD_D6 at LATB4_bit;
sbit LCD_D7 at LATB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// Lcd constants
char txt1[] = "Hellow";
char txt2[] = "RTC2 click";
char txt3[] = "DATE:";
char txt4[] = "TIME:";
// Module variables
unsigned char hour, minute, second, days, weeks, months, years,hours, minutes, seconds,day,week, month, year,*txt; // Global date/time variables
char oldstate = 0;
// RTC Definitions
#define RTC_ADDR 0xD0
/**************************************************************************************************
* DS1307 Functions
**************************************************************************************************/
/**************************************************************************************************
* Read data from RTC DS1307
* input : addres of RTC register
* output: value of of RTC register
**************************************************************************************************/
unsigned char RTC_Read(unsigned char addr){
unsigned char value;
I2C1_Start(); // Issue start signal
I2C1_Wr(RTC_ADDR); // Address DS1307, see DS1307 datasheet
I2C1_Wr(addr); // Start from address
I2C1_Start(); // Issue repeated start signal
I2C1_Wr(RTC_ADDR); // Address DS1307 for reading R/W=1
value = I2C1_Rd(0); // Read seconds byte
I2C1_Stop(); // Issue stop signal
return value;
}
/**************************************************************************************************
* Write data from DS1307
* input : addres of RTC register, value of of RTC register
**************************************************************************************************/
void RTC_Write(unsigned char addr, unsigned char value){
I2C1_Start(); // Issue start signal
I2C1_Wr(RTC_ADDR); // Address DS1307
I2C1_Wr(addr); // Start from address
I2C1_Wr(value); // Write value to RTC register
I2C1_Stop(); // Issue stop signal
}
/**************************************************************************************************
* Read time from RTC DS1307
* input : pointer to variables where RTC data will be stored
* output: variables with RTC data
**************************************************************************************************/
void Read_Time(char *seconds,char *minutes,char *hours,char *week,char *day,char *month,char *year){
I2C1_Start(); // Issue start signal
I2C1_Wr(RTC_ADDR); // Address DS1307, see DS1307 datasheet
I2C1_Wr(2); // Start from address 0
I2C1_Repeated_Start(); // Issue repeated start signal
I2C1_Wr(0xD1); // Address DS1307 for reading R/W=1
*seconds = I2C1_Rd(1); // Read seconds byte
*minutes = I2C1_Rd(1); // Read minutes byte
*hours = I2C1_Rd(1); // Read hours byte
*week =I2C1_Rd(1);
*day =I2C1_Rd(1);
*month =I2C1_Rd(1);
*year =I2C1_Rd(0);
I2C1_Stop(); // Issue stop signal
}
/**************************************************************************************************
* Write time to RTC DS1307
* input : variables with RTC data
**************************************************************************************************/
void Write_Time(unsigned char c_hours, unsigned char c_minutes, unsigned char c_seconds,
unsigned char c_day, unsigned char c_week, unsigned char c_month, unsigned char c_year){
I2C1_Start(); // issue start signal
I2C1_Wr(RTC_ADDR); // address DS1307
I2C1_Wr(2); // start from word at address (REG0)
I2C1_Wr(0x80); // write $80 to REG0. (pause counter + 0 sec)
I2C1_Wr(c_minutes); // write 0 to minutes word to (REG1)
I2C1_Wr(c_hours); // write 17 to hours word (24-hours mode)(REG2)
I2C1_Wr(c_week); // write 2 - Monday (REG3)
I2C1_Wr(c_day); // write 4 to date word (REG4)
I2C1_Wr(c_month); // write 5 (May) to month word (REG5)
I2C1_Wr(c_year); // write 01 to year word (REG6)
I2C1_Stop(); // issue stop signal
I2C1_Start(); // issue start signal
I2C1_Wr(RTC_ADDR); // address DS1307
I2C1_Wr(0); // start from word at address 0
I2C1_Wr(0 | c_seconds); // write 0 to REG0 (enable counting + 0 sec)
I2C1_Stop(); // issue stop signal
}
/**************************************************************************************************
* Show on the LCD display
* input : variables with RTC data
**************************************************************************************************/
void Transform_Time(char *seconds,char *minutes,char *hours,char *week,char *day,char *month,char *year){
*seconds = ((*seconds & 0xF0) >> 4)*10 + (*seconds & 0x0F);
*minutes = ((*minutes & 0xF0) >> 4)*10 + (*minutes & 0x0F);
*hours = ((*hours & 0xF0) >> 4)*10 + (*hours & 0x0F);
*week = (*week & 0x0F);
*day = ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
*month = ((*month & 0xF0) >> 4)*10 + (*month & 0x0F);
*year = ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}
void Show_Time()
{
switch(weeks)
{
char *txt;
case 1: txt="Sun"; break;
case 2: txt="Mon"; break;
case 3: txt="Tue"; break;
case 4: txt="Wed"; break;
case 5: txt="Thu"; break;
case 6: txt="Fri"; break;
case 7: txt="Sat"; break;
}
LCD_Out(2,14, txt);
Lcd_Chr(1, 6, (day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1, 7, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1, 9, (month / 10) + 48);
Lcd_Chr(1,10, (month % 10) + 48);
Lcd_Chr(1,14, (year / 10) + 48);
Lcd_Chr(1,15, (year % 10) + 48);
Lcd_Chr(2, 6, (hours / 10) + 48);
Lcd_Chr(2, 7, (hours % 10) + 48);
Lcd_Chr(2, 9, (minutes / 10) + 48);
Lcd_Chr(2,10, (minutes % 10) + 48);
Lcd_Chr(2,12, (seconds / 10) + 48);
Lcd_Chr(2,13, (seconds % 10) + 48);
}
void Init_main()
{
ADRESL = 0;
ADRESH = 0;
ADCON0=0;
ADCON1=0x0F;
ADCON2=0x00;
PORTC =0;
TRISC =0XFF;
CMCON =7;
PORTA = 0;
TRISA2_bit = 1;
I2C1_Init(100000); // Initialize I2C bus for communication with RTC
Lcd_Init(); // Initialize Lcd
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,txt1); // Write text in first row
Lcd_Out(2,4,txt2); // Write text in second row
Delay_ms(2000); // delay 2 sec
Lcd_Cmd(_LCD_CLEAR); // Prepare and output static text on LCD
LCD_Chr(1,8,'.');
LCD_Chr(1,11,'.');
LCD_Out(1,1,txt3);
LCD_Out(2,1,txt4);
LCD_Chr(2,8,':');
LCD_Chr(2,11,':');
LCD_Out(1,12,"20");
}
/**************************************************************************************************
* MAIN PROGRAM
**************************************************************************************************/
void main()
{
Delay_ms(200);
Init_main();
Write_Time(0x00,0x11,0x00,0x02,0x01,0x02,0x14);
while(1)
{
Transform_Time(&seconds,&minutes,&hours,&week,&day,&month,&year);
Show_Time();
Read_Time(&seconds,&minutes,&hours,&week,&day,&month,&year);
}
}