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.

RTC Project on pic microcontrollers.

Status
Not open for further replies.

tapu

Full Member level 4
Joined
Sep 15, 2014
Messages
234
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Location
india
Activity points
3,041
Dear all,

I had make alarm clock on 8051 by using keil.But now i want to use pic 16f877a for project.I am new in mplab(v8.30/hi tech c compiler).i studied & made a project in mplab ide. and write a code as follow.it has built successfull.but lcd(16x2) does not show time.only shows 05:05:05.Please reply.

Code:
#include <htc.h>



void all_disp();
void time_set();

#define First_Line 0x80
#define Second_Line 0xc0
#define Curser_On 0x0f
#define Curser_Off 0x0c
#define Clear_Display 0x01

#define Data_Port PORTD



void Lcd8_Init();
void Lcd8_Command(unsigned char);
void Lcd8_Write(unsigned char,unsigned char);
void Lcd8_Display(unsigned char,const unsigned char*,unsigned int);
void Lcd8_decimal2(unsigned char,unsigned char);
void Lcd8_RA3imal3(unsigned char,unsigned char);
void Lcd8_RA3imal4(unsigned char,unsigned int);
void Delay(unsigned int);
void del();


#define DS1307_ID 0xD0
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DATE 0x04
#define MONTH 0x05
#define YEAR 0x06



DS1307_get(unsigned char);
void DS1307_settime(unsigned char, unsigned char, unsigned char);
void DS1307_setdate(unsigned char, unsigned char, unsigned char);
Send2lcd(unsigned char);
void Rtc_Write(unsigned char,unsigned char);
Rtc_Read(unsigned char);
void Rtc_rd_wr_sub();
void Rtc_Init();
void Rtc_Start();
void Rtc_Tx();
void Rtc_Rx();
void Rtc_Stop();
void Rtc_Ack();
unsigned int CY;
unsigned int Rtc_add_wr,Rtc_add_rd;
unsigned char d_rtc,datain_rtc,in_rtc,temp_rtc,dat_rtc,flag_rtc;

unsigned char i,j,a[10],cur=0;
unsigned char sec,hour,min,date,month,year,sec1,hour1,min1,sec2, hour2,min2,hh,mm,ss,dd,mn,yy;
unsigned char count[6]={0x86,0x89,0x8c,0xc6,0xc9,0xcc};

unsigned char set_hex(unsigned char tt)
{
if(tt>59)
tt+=36;
else if(tt>49)
tt+=30;
else if(tt>39)
tt+=24;
else if(tt>29)
tt+=18;
else if(tt>19)
tt+=12;
else if(tt>9)
tt+=6;
return(tt);
} 

void all_disp()
{
sec = DS1307_get(SEC);
min = DS1307_get(MIN);
hour = DS1307_get(HOUR);
date = DS1307_get(DATE);
month = DS1307_get(MONTH);
year = DS1307_get(YEAR);

sec=Send2lcd(sec);
min=Send2lcd(min);
hour=Send2lcd(hour);
date=Send2lcd(date);
month=Send2lcd(month);
year=Send2lcd(year);

Lcd8_decimal2(0x86,date);
Lcd8_Write(0x88,'-');
Lcd8_decimal2(0x89,month);
Lcd8_Write(0x8b,'-');
Lcd8_decimal2(0x8c,year);

Lcd8_decimal2(0xc6,hour);
Lcd8_Write(0xc8,'.');
Lcd8_decimal2(0xc9,min);
Lcd8_Write(0xcb,'.');
Lcd8_decimal2(0xcc,sec);
}
void main()
{
TRISA=0x00;
TRISC=0x00;
TRISD=0x00;

Lcd8_Init();
Lcd8_Display(0x80,"RTC TESTING: ",16);
Lcd8_Display(0xc0,"ZAKARIYA:    ",16);

Delay(65000);
Delay(65000);

Lcd8_Command(0x01);
Lcd8_Display(First_Line,"Date:",5);
Lcd8_Display(Second_Line,"Time:",5);
Rtc_Init();

//DS1307_setdate(0x04,0x11,0x11);
//DS1307_settime(0x16,0x00,0x00);

while(1)
{
all_disp();

}
}





void Lcd8_Init()
{
Lcd8_Command(0x38); //to select function RA0
Lcd8_Command(0x06); //RA4ry mode RA0
Lcd8_Command(0x0c); //display on
Lcd8_Command(0x01); //clear display
}

void Lcd8_Command(unsigned char com)
{
Data_Port=com;
RC7=1;
RA5=RC0=0;
Delay(125);
RC7=0;
Delay(125);
}

void Lcd8_Write(unsigned char com,unsigned char lr)
{
Lcd8_Command(com);
Data_Port=lr; // Data 
RC7=RA5=1;
RC0=0;
Delay(125);
RC7=0;
Delay(125);
}

void Lcd8_Display(unsigned char com,const unsigned char *word,unsigned int n)
{
unsigned char Lcd_i;
for(Lcd_i=0;Lcd_i<n;Lcd_i++)
{ 
Lcd8_Write(com+Lcd_i,word[Lcd_i]);
}
}

void Lcd8_decimal2(unsigned char com,unsigned char val)
{
unsigned int Lcd_hr,Lcd_t,Lcd_o;

Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;

Lcd8_Write(com,Lcd_t+0x30);
Lcd8_Write(com+1,Lcd_o+0x30);
}


void Lcd8_RA3imal3(unsigned char com,unsigned char val)
{
unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;

Lcd_h=val/100;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;

Lcd8_Write(com,Lcd_h+0x30);
Lcd8_Write(com+1,Lcd_t+0x30);
Lcd8_Write(com+2,Lcd_o+0x30);
}

void Lcd8_RA3imal4(unsigned char com,unsigned int val) 
{
unsigned int Lcd_th,Lcd_thr,Lcd_h,Lcd_hr,Lcd_t,Lcd_o;

val = val%10000;
Lcd_th=val/1000;
Lcd_thr=val%1000;
Lcd_h=Lcd_thr/100;
Lcd_hr=Lcd_thr%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;

Lcd8_Write(com,Lcd_th+0x30);
Lcd8_Write(com+1,Lcd_h+0x30);

Lcd8_Write(com+2,Lcd_t+0x30);
Lcd8_Write(com+3,Lcd_o+0x30);
}

void Delay(unsigned int del)
{
while(del--);
}

void del()
{
Delay(65000);
Delay(65000);
}


/*********************************LCD HEADER FILE END*************************************/



/*********************************RTC HEADER FILE START*********************************/


DS1307_get(unsigned char addr)
{

Rtc_Start(); 
Rtc_Read(addr);
Rtc_Stop();

return;
}

void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
Rtc_Start(); 

Rtc_Write(0x00,ss); /* Write sec on RAM address 00H */
Rtc_Write(0x01,mm); /* Write min on RAM address 01H */
Rtc_Write(0x02,hh); /* Write hour on RAM address 02H */

Rtc_Stop(); /* Stop i2c bus */
}


void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
Rtc_Start();

Rtc_Write(0x04,dd); /* Write date on RAM address 04H */
Rtc_Write(0x05,mm); /* Write month on RAM address 05H */
Rtc_Write(0x06,yy); /* Write year on RAM address 06H */

Rtc_Stop(); /* Stop i2c bus */
}


Send2lcd(unsigned char value)
{
unsigned char buf1,buf2,buf = 0;

buf1 = value & 0xF0; /* Filter for high byte */
buf1 = (buf1>>4); /* Convert to ascii code */
buf2 = value & 0x0F; /* Filter for low byte */
buf=(buf1*10)+buf2;

return buf;
}


void Rtc_Init()//lower order 256 bytes of the chip
{
Rtc_add_wr=0xd0;
Rtc_add_rd=0xd1;
}


void Rtc_Write(unsigned char zig,unsigned char zag)// program to write to EEPROM
{
dat_rtc=zig;
temp_rtc=zag;
Rtc_rd_wr_sub();
above:
d_rtc=temp_rtc;
Rtc_Tx();
if (CY==1)goto above;
CY=0;
Rtc_Stop();
}


Rtc_Read(unsigned char zig)// program to read from EEPROM
{
dat_rtc=zig;
Rtc_rd_wr_sub();
Rtc_Start();
be:
d_rtc=Rtc_add_rd; // 0xd1 =Rtc_add_rd
Rtc_Tx();
if(CY==1)goto be;
Rtc_Rx();
Rtc_Ack();
CY=0;
Rtc_Stop();
return(datain_rtc);
}


void Rtc_Start()// must for any operation on EEPROM
{
RC4=1;
RC3=1;
RC4=0;
RC3=0;
}


void Rtc_Stop()// this is similar to the START operation whereas this should be performed after the completion of any operation
{
RC4=0;
RC3=1;
RC4=1;
}


void Rtc_Tx()// program to send the device address, read/write address,data to be written
{
signed char i_rtc;
for(i_rtc=7;i_rtc>=0;i_rtc--)// should necessarily be initialised as signed char.
{
CY=(d_rtc>>i_rtc)&0x01;
RC4=CY;
RC3=1;// clock is essRA4ial inorder to write or read
RC3=0;// clk should be alternated
}
RC4=1;
RC3=1;
CY=RC4;
RC3=0;
}


void Rtc_Rx()// program read the data from the EEPROM
{
unsigned char l_rtc;
RC4=1;
for (l_rtc=0;l_rtc<=7;l_rtc++)
{
RC3=1;
in_rtc=in_rtc<<1;
in_rtc|=RC4;
RC3=0;
}
datain_rtc=in_rtc;
in_rtc=0;
}


void Rtc_Ack()// this is to intimate the EEPROM that the read operation is over
{
RC4=1;
RC3=1;
RC3=0;
}


void Rtc_rd_wr_sub()// this routine will be used by both the read & write operations to send the device address & the address at which the corresponding action is to be taken
{
Rtc_Start();
here1:
d_rtc=Rtc_add_wr;// 0xd0 device address is passed
Rtc_Tx();
if(CY==1)goto here1;
again1:
d_rtc=dat_rtc;// the address from which data is to be read/written is to be passed
Rtc_Tx();
if(CY==1)goto again1;
}
Is theire any step or setting missed As i used pic first time.
 

Looks like you have copied 8051 I2C routines without understanding their operation or considering the hardware differences between 8051 and PIC GPIO.

I2C pins (at least SDA) have to be driven in an open drain manner, doing this with a PIC involves writing to the TRIS register. It's surely possible to implement bit-banging I2C routines, but it would be much easier to use the PIC hardware I2C interface as everybody does.

- - - Updated - - -

You have some retry loops in the code that repeat the last sent byte if no ACK is detected. It can't work, just delete it.
 

Connect the RTC to I2C lines of the PIC and define the pins in the code as it is tertiary functions. Then follow the RTC datasheet to set & read the timings.

Make sure you are connecting the pull-up resistors to the I2C lines.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top