PIC18F4550 WITH REAL TIME CLOCK (RTC) DS1307 AND UART-SIMULATION -PROTEUS

Photo of author

By Jackson Taylor



DOWNLOAD FILES
NOTES

  • COMPILER – MIKRO C PRO 6.6.1
  • SIMULATOR -PROTEUS 8.1

PROGRAM
unsigned char seconds,minute,hour,weekday, days, month, years;
void settime (void);
void Read_Time(void) ;
void main()
{
UART1_Init(9600); // Initialize UART module at 9600 bps
Delay_ms(100);    // Wait for UART module to stabilize
I2C1_Init(100000);        // perform initialization
settime ();
  while (1) {
    Read_Time();      // read time from RTC(DS1307)
 Delay_ms(1000);
  }
}
void Read_Time(void)
{
  I2C1_Start();
  I2C1_Wr(0xD0);
  I2C1_Wr(0);
  I2C1_Repeated_Start();
  I2C1_Wr(0xD1);
  seconds =I2C1_Rd(1);       //seconds   //convert bcd to ascii for lcd
  minute =I2C1_Rd(1);     //minute       //convert bcd to ascii for lcd
  hour=I2C1_Rd(1);        //hour        //convert bcd to ascii for lcd
  weekday =I2C1_Rd(1);  //weekday ex-monday=2   //convert bcd to ascii for lcd
  days =I2C1_Rd(1);       //day         //convert bcd to ascii for lcd
  month =I2C1_Rd(1);        //month       //convert bcd to ascii for lcd
  years =I2C1_Rd(0);      //year         //convert bcd to ascii for lcd
  I2C1_Stop();
//DATA SENDING SECTION TO UART
//DATE
UART1_Write(((days&0xf0)>>4)+0x30);//convert bcd to ascii for lcd
UART1_Write((days&0x0f)+0x30);  //convert bcd to ascii for lcd
UART1_Write(‘:’);
UART1_Write(((month&0xf0)>>4)+0x30); //convert bcd to ascii for lcd
UART1_Write((month&0x0f)+0x30);   //convert bcd to ascii for lcd
UART1_Write(‘:’);
UART1_Write(((years&0xf0)>>4)+0x30);  //convert bcd to ascii for lcdii
UART1_Write((years&0x0f)+0x30);      //convert bcd to ascii for lcd
UART1_Write(0XD);   //NEW LINE
//TIME
UART1_Write(((hour&0xf0)>>4)+0x30);
UART1_Write((hour&0x0f)+0x30);
UART1_Write(‘:’);
UART1_Write(((minute&0xf0)>>4)+0x30);
UART1_Write((minute&0x0f)+0x30);
UART1_Write(‘:’);
UART1_Write(((seconds&0xf0)>>4)+0x30);
UART1_Write((seconds&0x0f)+0x30);
UART1_Write(0XD);   //NEW LINE
}
void settime (void)
{
I2C1_Init(100000);     // initialize full master mode
   I2C1_Start();          // issue start signal
   I2C1_Wr(0xD0);         // address DS1307
   I2C1_Wr(0);            // start from word at address (REG0)
   I2C1_Wr(0x80);         // write $80 to REG0. (pause counter + 0 sec)
   I2C1_Wr(0x25);            // write 0 to minutes word to (REG1)
   I2C1_Wr(0x16);         // write 17 to hours word (24-hours mode)(REG2)
   I2C1_Wr(0x02);         // write 2 – Monday (REG3)
   I2C1_Wr(0x24);         // write 4 to date word (REG4)
   I2C1_Wr(0x04);         // write 5 (May) to month word (REG5)
   I2C1_Wr(0x15);         // write 01 to year word (REG6)
   I2C1_Stop();           // issue stop signal
   I2C1_Start();          // issue start signal
   I2C1_Wr(0xD0);         // address DS1307
   I2C1_Wr(0);            // start from word at address 0
   I2C1_Wr(0);            // write 0 to REG0 (enable counting + 0 sec)
   I2C1_Stop();           // issue stop signal
     delay_ms(500);
}

See also
RFID INTERFACING WITH 8051 (HARDWARE AND SIMULATION)