Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] DS1307 code is not working

Status
Not open for further replies.

ADGAN

Full Member level 5
Full Member level 5
Joined
Oct 9, 2013
Messages
295
Helped
4
Reputation
8
Reaction score
4
Trophy points
18
Visit site
Activity points
1,837
Hi! I'm trying to port a code written for DS1307 in MikroC to MPLAB. It compiles but when I simulate in Proteus nothing shows on the I2C debugger. What have I done wrong?

Code:
#include <stdlib.h>
#include <i2c.h>

char tnum[4],txt;


void start1307s(){
OpenI2C1(SLAVE_7_STSP_INT,SLEW_OFF);
//START SIGNAL FOR DS1307***********
StartI2C1() ;// issue start signal
WriteI2C1(0xD0); // address PCF8530
WriteI2C1(0); // start from word at address 0
WriteI2C1(0); // write 0 to config word (enable counting)
StopI2C1 (); // issue stop signal
//**********************************
}

void Zero_Fill(char *value) { // fill text representation
if (value[1] == 0) { // with leading zero
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}


//--------------------- Reads time and date information from RTC (DS1307)
void Read_Time(char *sec, char *min, char *hr, char *week_day, char *day, char *mn, char *year) {
StartI2C1();
WriteI2C1(0xD0);
WriteI2C1(0);
RestartI2C1 ();
WriteI2C1(0xD1);
*sec =ReadI2C1();
*min =ReadI2C1();
*hr =ReadI2C1();
*week_day =ReadI2C1();
*day =ReadI2C1();
*mn =ReadI2C1();
*year =ReadI2C1();
StopI2C1 ();
}

//-------------------- Formats date and time
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);
}



//-------------------- Output values to LCD
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {

switch(week_day){
case 1: strcpypgm2ram(txt,"Sun"); break;
case 2: strcpypgm2ram(txt,"Mon"); break;
case 3: strcpypgm2ram(txt,"Tue"); break;
case 4: strcpypgm2ram(txt,"Wed"); break;
case 5: strcpypgm2ram(txt,"Thu"); break;
case 6: strcpypgm2ram(txt,"Fri"); break;
case 7: strcpypgm2ram(txt,"Sat"); break;
}
LCD_TextOut(3,4,txt);

btoa(day,tnum);
Zero_Fill(txt);
LCD_TextOut(3,7,txt);

btoa(mn,tnum);
Zero_Fill(txt);
LCD_TextOut(3,11,txt);

btoa(year,tnum);
Zero_Fill(txt);
LCD_TextOut(3,17,txt);

}

My main function: (This is a part of it)
Code:
unsigned char sec, min1, hr, week_day, day, mn, year;

void __init(void){
LCD_Init();
Init_Delay();
} 


char txt1[] = "ENERGY CONSUMPTION";

void main(void)
{
        LCD_TextOut(0,1,txt1);
        start1307s();
        Read_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
       Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
       	Display_Time(sec, min1, hr, week_day, day, mn, year);
 

Thanx. Have couple of questions. Why OpenI2C is in master mode isn't DS1307 the slave? What is slew rate? How do I set time ?

- - - Updated - - -

How do I adjust the delays according to my crystal and I don't know what is the value of Fosc. I'm using a 4MHz external crystal and the PLL of the PIC.
 
Last edited:

Thank for the info Janyanth. How do I set time in your code? It still only shows 0's.
 

I couldn't test this in real hardware, only in Proteus. When I test it in it, the clock is not incrementing. I only changed the read & the write function. My previous code which I have posted above works well in Proteus. But the I2C debugger shows that the data is transferred on the data bus and also I notice that it is transmitting at a much higher speed than in my previous code.

Code:
#include <delays.h>
#include <i2c.h>

#define STROBE {E = 1; Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); Delay1TCY(); E = 0;};

char tnum[4],txt;

void readDS1307(char *sec, char *min1, char *hr, char *week_day, char *day, char *mn, char *year){
	StartI2C1();
	IdleI2C1();
	WriteI2C1(0xD0);
	IdleI2C1();
	WriteI2C1(0x00);
	IdleI2C1();
	RestartI2C1();
	IdleI2C1();
	WriteI2C(0xD1); 
	IdleI2C(); 
				
	*sec = ReadI2C1();
	AckI2C1();
	*min1 = ReadI2C1();
	AckI2C1();
	*hr = ReadI2C1();
	AckI2C1();
	*week_day = ReadI2C1();
	AckI2C1();
	*day = ReadI2C1();
	AckI2C1();
	*mn = ReadI2C1();
	AckI2C1();
	*year = ReadI2C1();
	NotAckI2C1();
	StopI2C1();

}

void writeDS1307(){
	StartI2C1();
	IdleI2C1();
	WriteI2C1(0xD0);
	IdleI2C1();
	WriteI2C1(0x00);
	IdleI2C1();
	WriteI2C1(0x80);
	IdleI2C1();
	WriteI2C1(0x41); //minutes
	IdleI2C1();
	WriteI2C1(0x21); // hours
	IdleI2C1();
	WriteI2C1(0x10); //day
	IdleI2C1();
	WriteI2C1(0x18); //date
	IdleI2C1();
    WriteI2C1(0x10); //month
	IdleI2C1();
	WriteI2C1(0x13); //year
	IdleI2C1();
	Delay1KTCYx(25);
	StopI2C1();
	
	StartI2C1();
	IdleI2C1();
	WriteI2C1(0xD0);
	IdleI2C1();
	WriteI2C1(0x00);
	IdleI2C1();
	WriteI2C1(0x00); //minutes
	IdleI2C1();
	Delay1KTCYx(25);
	StopI2C1();
}


void Zero_Fill(char *value) { // fill text representation
if (value[1] == 0) { // with leading zero
value[1] = value[0];
value[0] = 48;
value[2] = 0;
}
}


//-------------------- Formats date and time
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);
}



//-------------------- Output values to LCD
void Display_Time(char sec, char min, char hr, char week_day, char day, char mn, char year) {

switch(week_day){
case 1: LCD_ConstTextOut(2,4,"Sun"); break;
case 2: LCD_ConstTextOut(2,4,"Mon"); break;
case 3: LCD_ConstTextOut(2,4,"Tue"); break;
case 4: LCD_ConstTextOut(2,4,"Wed"); break;
case 5: LCD_ConstTextOut(2,4,"Thu"); break;
case 6: LCD_ConstTextOut(2,4,"Fri"); break;
case 7: LCD_ConstTextOut(2,4,"Sat"); break;
}


btoa(sec,tnum);
Zero_Fill(txt);
LCD_TextOut(2,7,txt);

btoa(day,tnum);
Zero_Fill(txt);
LCD_TextOut(2,9,txt);

btoa(hr,tnum);
Zero_Fill(txt);
LCD_TextOut(2,9,txt);

btoa(mn,tnum);
Zero_Fill(txt);
LCD_TextOut(2,11,txt);

btoa(year,tnum);
Zero_Fill(txt);
LCD_TextOut(2,17,txt);    

}

Main

Code:
void main(void)
{
OpenI2C1(MASTER,SLEW_OFF);
   	Delay10KTCYx(25);
    writeDS1307();	
while(1){
    
    readDS1307(&sec,&min1,&hr,&week_day,&day,&mn,&year);
    Transform_Time(&sec,&min1,&hr,&week_day,&day,&mn,&year);
    Display_Time(sec, min1, hr, week_day, day, mn, year);
    lastdate_check(day, mn, year, last_date);
    lastmonth_check(day, mn, year);
    lastyear_check(day, mn, year);

}
}
 

Zip and post your complete MPLAB project files.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
btoa(sec,tnum);
Zero_Fill(txt);
LCD_TextOut(1,11,txt);
 
btoa(min,tnum);
Zero_Fill(txt);
LCD_TextOut(1,8,txt);
 
btoa(hr,tnum);
Zero_Fill(txt);
LCD_TextOut(1,5,txt);
 
btoa(day,tnum);
Zero_Fill(txt);
LCD_TextOut(2,11,txt);
 
btoa(mn,tnum);
Zero_Fill(txt);
LCD_TextOut(2,8,txt);
 
btoa(year,tnum);
Zero_Fill(txt);
LCD_TextOut(2,5,txt);

 

New file attached. Post code of btoa() function.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
while(1){
    
    readDS1307();
    Transform_Time(sec, min1, hr, week_day, day, mn, year);
    Display_Time(sec, min1, hr, week_day, day, mn, year);
    lastdate_check(day, mn, year, last_date);
    lastmonth_check(day, mn, year);
    lastyear_check(day, mn, year);
 
}

 

Attachments

  • RTCC_Init.rar
    1.1 KB · Views: 67
  • Like
Reactions: ADGAN

    ADGAN

    Points: 2
    Helpful Answer Positive Rating
btoa function is a built in function in MPLAB which converts a 8-bit signed byte to a string. The attached file didn't compile, I'll try again.
 

Hi! Jayanth the code you attached won't compile. I tried to modify it but didn't succeed.
 

It compiles now. I undid the modifications. But in the simulation I can see that the clock stops when the RTCC functions are called even though the data is been transferred on the data bus.
View attachment PIC18F45K22.rar
 

The file paths were wrong. I fixed the RTCC_Init.c It compiles but doesn't link. There is some error while linking. It says "Processor type is not same in all files". Give the link from where you downloaded the original project.

How you said that it compiles fine? It was not at all compiling.

98856d1385058829-error.png
 

Attachments

  • PIC18F45K22 rev1.rar
    307.2 KB · Views: 69
  • error.png
    error.png
    168.8 KB · Views: 127

Hello,

Find the attached files that I wrote for almost same time of work. Amend accordingly and change file extensions.

Enjoy!
 

Attachments

  • i2c.h.txt
    1.6 KB · Views: 85
  • i2c.c.txt
    1.2 KB · Views: 95
  • ds1307.h.txt
    529 bytes · Views: 95
  • ds1307.c.txt
    3.3 KB · Views: 85

@Jayanth I don't know how but there is no such problem when I compile it in my PC. Now the original one is quite different from my one. So I don't think it will be much useful.

@babar_ali Thanx I'll try it.
 

@Jayanth the problem could be in 'OpenI2C1(MASTER,SLEW_OFF)' because when I remove it the clock won't stop. Pls help.
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top