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

Photo of author

By Jackson Taylor

DOWNLOAD FILES

NOTES

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

PROGRAM

                      unsigned char seconds,minute,hour,weekday, days, month, years;
                      sbit LCD_RS at RD2_bit;
                      sbit LCD_EN at RD3_bit;
                      sbit LCD_D4 at RD4_bit;
                      sbit LCD_D5 at RD5_bit;
                      sbit LCD_D6 at RD6_bit;
                      sbit LCD_D7 at RD7_bit;
                      sbit LCD_RS_Direction at TRISD2_bit;
                      sbit LCD_EN_Direction at TRISD3_bit;
                      sbit LCD_D4_Direction at TRISD4_bit;
                      sbit LCD_D5_Direction at TRISD5_bit;
                      sbit LCD_D6_Direction at TRISD6_bit;
                      sbit LCD_D7_Direction at TRISD7_bit;
                      // End LCD module connections
                      void settime (void);
                      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();
                        // LCD_Out(1,1,"Date:");
                         LCD_Chr(1,1,((days&0xf0)>>4)+0x30);//convert bcd to ascii for lcd
                         LCD_Chr_cp((days&0x0f)+0x30);  //convert bcd to ascii for lcd
                         LCD_Chr_cp(':');
                         LCD_Chr_cp(((month&0xf0)>>4)+0x30); //convert bcd to ascii for lcd
                         LCD_Chr_cp((month&0x0f)+0x30);   //convert bcd to ascii for lcd
                          LCD_Chr_cp(':');
                        LCD_Chr_cp(((years&0xf0)>>4)+0x30);  //convert bcd to ascii for lcdii
                         LCD_Chr_cp((years&0x0f)+0x30);      //convert bcd to ascii for lcd
                       // LCD_Out(2,1,"Time:");
                         LCD_Chr(2,1,((hour&0xf0)>>4)+0x30);
                         LCD_Chr_cp((hour&0x0f)+0x30);
                         LCD_Chr_cp(':');
                         LCD_Chr_cp(((minute&0xf0)>>4)+0x30);
                         LCD_Chr_cp((minute&0x0f)+0x30);
                          LCD_Chr_cp(':');
                         LCD_Chr_cp(((seconds&0xf0)>>4)+0x30);
                         LCD_Chr_cp((seconds&0x0f)+0x30);
                      }
                      void main() 
                      {
                        Lcd_Init();                // Initialize LCD
                        Lcd_Cmd(_LCD_CLEAR);       // Clear LCD display
                        Lcd_Cmd(_LCD_CURSOR_OFF);  // Turn cursor off
                        I2C1_Init(100000);
                      settime ();                                           // perform initialization
                        while (1) {
                          Read_Time();      // read time from RTC(DS1307)
                       Delay_ms(1000);
                        }
                      }
                      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
Smart Digital Energy Meter using power line communication modem