MrSingh
Newbie level 5
- Joined
- Mar 21, 2014
- Messages
- 10
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 69
Hi i am interfacing PIC18f4550 with DS1307 RTC .Firstly it was showing Correct values on Proteus Simulation however after few minutes it showed a random value then switched back to correct values now after some time it again showed random value without switching back to correct one.
when the display was showing random value the data in clock panel was 155-13-51 sumthing like this
here is my code
my circuit diagram is
also the values of RTC
when the display was showing random value the data in clock panel was 155-13-51 sumthing like this
here is my code
Code:
#define DS1307 0xD0
//end definition
//Begin Pin assignment
//i2c pin assignment
sbit Soft_I2C_Scl at RB3_bit;
sbit Soft_I2C_Sda at RB4_bit;
sbit Soft_I2C_Scl_Direction at TRISB3_bit;
sbit Soft_I2C_Sda_Direction at TRISB4_bit;
// LCD pin connection
sbit LCD_RS at RE0_bit;
sbit LCD_EN at RE2_bit;
sbit LCD_D0 at RD0_bit;
sbit LCD_D1 at RD1_bit;
sbit LCD_D2 at RD2_bit;
sbit LCD_D3 at RD3_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;
// LCD Pin direction
sbit LCD_RS_Direction at TRISE0_bit;
sbit LCD_EN_Direction at TRISE2_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D3_Direction at TRISD3_bit;
sbit LCD_D2_Direction at TRISD2_bit;
sbit LCD_D1_Direction at TRISD1_bit;
sbit LCD_D0_Direction at TRISD0_bit;
//End Pin assignment
//Start variable declaration
char colon[] = ":";
char i;
unsigned char sec, min1, hr, week_day, day, mn, year;
char *txt, tnum[4];
unsigned short myDate,myHour,myMin,myYear,myMon,myDay;
//End variable declaration
//Start PRogram
//LCD library
void ibusy() //lcd busy function check
{
TRISD=0xFF;
LATE2_bit=1;
LATE0_bit=0;
LATE1_bit=1;
while(LCD_D7)
{
LATE2_bit=0;
//Delay_ms(50);
LATE2_bit=1;
}
TRISD=0x00;
}
void lcmd(char xy) //lcd command o/p
{ ibusy();
LATD=xy;
LATE0_bit=0;
LATE1_bit=0;
LATE2_bit=1;
Delay_ms(10);
LATE2_bit=0;
Delay_ms(10);
// Delay_ms(100);
}
void ldata(char xy) //lcd data o/p
{ ibusy();
LATD=xy;
LATE0_bit=1;
LATE1_bit=0;
LATE2_bit=1;
Delay_ms(10); // orig20ms
LATE2_bit=0;
Delay_ms(10); //orig 40ms
// Delay_ms(100);
}
void lcdinit() // not used initialization sequence
{ Delay_ms(10000);
lcmd(0x38); //
Delay_ms(1000);
lcmd (0x0C); //
Delay_ms(1000);
lcmd(0x01); //
Delay_ms(1000);
lcmd(0x06); //
Delay_ms(1000);
lcmd(0x80); //
Delay_ms(1000);
}
void icmd(char xy) // used 4 linitailization commands
{ //ibusy();
LATD=xy;
LATE0_bit=0;
LATE1_bit=0;
LATE2_bit=1;
Delay_ms(20);
LATE2_bit=0;
Delay_ms(40);
// Delay_ms(100);
}
void init() //lcd initialization code
{ //
Delay_ms(10000); // comment for testing
icmd(0x0F); //
Delay_ms(1000);
icmd(0x38); // is necessary 4 2line operation
Delay_ms(1000);
/* icmd(0x38); //
Delay_ms(1000);
lcmd (0x08); //
Delay_ms(1000);
lcmd(0x01); //
Delay_ms(1000);
lcmd(0x06); //
Delay_ms(1000);
lcmd(0x80); //
Delay_ms(1000);*/
}
void lcursor(char row, char column) // set cursor position
{
switch (row) {
case 1: lcmd(0x80 + column - 1); break;
case 2: lcmd(0xc0 + column - 1); break;
default: break;
}
}
void lout(char row, char column, char *text) // o/p a string of data
{ lcursor(row,column) ;
while(*text) // Write data to LCD up to null
{ // Wait while LCD is busy
ldata(*text); // Write character to LCD
text++; // Increment buffer
}
return;
}
void lchr(char row, char column, char text) //o/p a letter
{ lcursor(row,column) ;
ldata(text); // Write character to LCD
return;
}
//All functions related to RTC interfacing with MCU using sw-i2c
void Write_Time() {
Delay_ms(2000);
Soft_I2C_Start(); // issue start signal
Soft_I2C_Write(DS1307); // address DS1307
Soft_I2C_Write(0); // start from word at address (REG0)
Soft_I2C_Write(0x80); // write $80 to REG0. (pause counter + 0 sec)
Soft_I2C_Write(myMin); // write 0 to minutes word to (REG1)
Soft_I2C_Write(myHour); // write 17 to hours word (24-hours mode)(REG2)
Soft_I2C_Write(myDay); // write 2 - Monday (REG3)
Soft_I2C_Write(myDate); // write 4 to date word (REG4)
Soft_I2C_Write(myMon); // write 5 (May) to month word (REG5)
Soft_I2C_Write(myYear); // write 01 to year word (REG6)
Soft_I2C_Stop(); // issue stop signal
Delay_ms(2000);
Soft_I2C_Start(); // issue start signal
Soft_I2C_Write(DS1307); // address DS1307
Soft_I2C_Write(0); // start from word at address 0
Soft_I2C_Write(0); // write 0 to REG0 (enable counting + 0 sec)
Soft_I2C_Stop(); // issue stop signal
}
void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
Soft_I2C_Start();
Soft_I2C_Write(DS1307);
Soft_I2C_Write(0);
Soft_I2C_Start();
Soft_I2C_Write(0xD1);
*sec =Soft_I2C_Read(1);
*min =Soft_I2C_Read(1);
*hr =Soft_I2C_Read(1);
*week_day =Soft_I2C_Read(1);
*day =Soft_I2C_Read(1);
*mn =Soft_I2C_Read(1);
*year =Soft_I2C_Read(0);
Soft_I2C_Stop();
}
void Transform_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
*sec = ((*sec & 0x70) >> 4)*10 + (*sec & 0x0F);
*min = ((*min & 0xF0) >> 4)*10 + (*min & 0x0F);
*hr = ((*hr & 0x30) >> 4)*10 + (*hr & 0x0F);
*week_day =(*week_day & 0x07);
*day = ((*day & 0xF0) >> 4)*10 + (*day & 0x0F);
*mn = ((*mn & 0x10) >> 4)*10 + (*mn & 0x0F);
*year = ((*year & 0xF0)>>4)*10+(*year & 0x0F);
}
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {
switch(week_day){
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;
}
/*init();
lcmd(0x01); // Clear display
lcmd(0x0C); // Cursor off*/
lchr(1, 1, (hr / 10) + 48);
lchr(1, 2, (hr % 10) + 48);
lchr(1,3,':') ;
lchr(1, 4, (min / 10) + 48);
lchr(1,5, (min % 10) + 48);
lchr(1,6,':') ;
lchr(1,7, (sec / 10) + 48);
lchr(1,8, (sec % 10) + 48);
//
//Lcd_Out(1,1,txt);
lchr(1, 10, (day / 10) + 48);
// Print tens digit of day variable
lchr(1, 11, (day % 10) + 48);
// Print oness digit of day variable
lchr(1,12,'/') ;
// Lcd_Chr(1,13, (mn / 10) + 48);
lchr(1,13, (mn % 10) + 48);
lchr(1,14,'/') ;
lchr(1,15, (year / 10) + 48);
// Print year vaiable
lchr(1,16, (year % 10) + 48);
}
void Set_Time()
{
myDate=0x27;
myMon=0x04;
myYear=0x14;
myHour=0x12;
myMin=0x55;
myDay= 0x01;
Write_Time();
}
void WhatIsTime()
{
Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // read time from RTC(DS1307)
Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year); // format date and time
Display_Time(sec, min1, hr, week_day, day, mn, year);
}
void main() //Main program flow
{
ADCON1 = 0x0F; // page 268, disable analaog
/// CMCON = 0x07; //originallly uncommented
INTCON2 = 0x80; // disable pull up in port b
// clears internal latches
LATB = 0x18; // enable internal pull ups
LATA = 0x00;
LATC = 0x00;
LATD = 0x00;
LATE = 0x00;
// Make all outputs
TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
TRISE = 0x00;
PORTC=0x33;
TRISC=0x33;
init(); // Initialize LCD
lcmd(0x01); // Clear LCD display
lcmd(0x0C); // Turn cursor off
Soft_I2C_Init(); // initialize I2C
lout(1,1,"Software I2C");lout(2,1,"Initialized") ;
Delay_ms(1000);
lcmd(0x01); // Clear LCD display
lcmd(0x0C); // Turn cursor off
lout(1,1,"Clock using");lout(2,1,"RTC") ;
Delay_ms(1000);
lcmd(0x01); // Clear LCD display
lcmd(0x0C); // Turn cursor off
while(1)
{
WhatIsTime(); // Answers the question asked by the MCU
}
}
also the values of RTC