ATtiny45 PCF8574 LCD DS1307 Show Time Only by TinyWireM.h



Tiny45WireMasterI2C.rar
header file: TinyWireM.zip
Code:

/* ATtiny45 as an I2C Master
 * I2C master reading DS1307 RTC. (display with LCD(contol by PCF8574))
 * SETUP:
 * ATtiny Pin 1 = (RESET) N/U                      ATtiny Pin 8 = VCC (2.7-5.5V)
 * ATtiny Pin 2 = NC                               ATtiny Pin 7 = SCK (DS1307 and PCF8574)
 * ATtiny Pin 3 = NC                               ATtiny Pin 6 = NC
 * ATtiny Pin 4 = GND                              ATtiny Pin 5 = SDA (DS1307 and PCF8574)
 */

#include <TinyWireM.h>                  // I2C Master lib for ATTinys which use USI
#define PCF8574_ID 0x20//0x40
#define DS1307_ID 0x68   //1101000[R/W] ; 001=slave address  ; Read=1
#define ATcommand 0x20                  //LCD command   EN=1 ; RS=0       0010  0000
#define ATdata 0x30                     //LCD data      EN=1 ; RS=1       0011  0000
//DS1307 Clock Buffer
byte second=0x00;                                
byte minute=0x24;
byte hour=0x19;
byte wkDay=0x01;
byte date=0x22;
byte month=0x01;
byte year=0x12;
byte ctrl=0x00;;


void PCF8574_Write(unsigned char data)
{
  TinyWireM.beginTransmission(PCF8574_ID);
  TinyWireM.send(data);                 // Access Command Register
  TinyWireM.endTransmission();          // Send to the slave
  //delayMicroseconds(16); //  minimun~16-20
}
void Write_LCD_Byte(char command_data ,int value) //bool lcd_command ,
{
  int iMSB=0;
  int iLSB=0;
  iMSB = (value & 0xF0)>>4;
  iLSB = (value & 0x0F);
  PCF8574_Write(command_data|iMSB);//0xFF
  PCF8574_Write(0x00|iMSB);
  PCF8574_Write(command_data|iLSB);
  PCF8574_Write(0x00|iLSB);
  delay(1);
}

void initial_LCD(void)
{
  //delay(50);//ให้รออย่างน้อย 15ms
  PCF8574_Write(0x00);//ให้ขา E = 0 ให้ขา RS= 0 กำหนดเป็นคำสั่ง  ให้ขา R/W = 0 เขียนข้อมูล
  //delay(50);
  //ส่งข้อมูลค่ำสั่ง Function Set 3 ครั้ง  ข้อมูล D4-D7 จะถูกส่งไปที่ bus
  PCF8574_Write(0x23);      //1    0x20 + 0x03
  //delayMicroseconds(1);
  PCF8574_Write(0x03);
  //delay(5);                //wait 4ms
  PCF8574_Write(0x23);     //2
  //delayMicroseconds(1);
  PCF8574_Write(0x03);
  //delayMicroseconds(100);  //wait 100us
  PCF8574_Write(0x23);     //3
  //delayMicroseconds(1);
  PCF8574_Write(0x03);
  //end
  //กำหนดการทำงานของ LCD ให้เป็น 4 bit 0010xxxx
  PCF8574_Write(0x22);//Function Set (BIT5=1) [001DNF00 ],Sets buss size 4bit (D=0)ข้อมูลเฉพาะ D4-D7 จะถูกส่งออกไปที่บัส
  //delayMicroseconds(1);
  PCF8574_Write(0x02);
  //end เมื่อเป็น 4 bit ต้องส่งข้อมูลคำสั่งสองครั้งๆ ละ 4 bit
  //Function Set (BIT5=1)  0x28  กำหนด ขนาดข้อมูล,จำนวนบรรทัด, ขนาดตัวอักษร
  Write_LCD_Byte(ATcommand,0x28);// 00101000 : [001DNF00 ],Sets buss size 4bit (D=0),number of lines=2 (N=1),font size =5x7(F=0)
  //Display/Cursor (BIT3=1)  0x0C กำหนดการแสดงผล, เคอร์เซอร์
  Write_LCD_Byte(ATcommand,0x0C);//00001100 : [00001DUB] ,Display On (D=1),Cursor Off (U=0) ,blinking block cursor Off (B=0)
  //Entry Mode Set (BIT2=1) 0x06 โหมดการป้อนข้อมูล
  Write_LCD_Byte(ATcommand,0x06);//00000110 : [000001MS] , Cursor direction right (M=1);no scroll (S=0)
  //0x01 clear screen
  Write_LCD_Byte(ATcommand,0x01);
  //0x0F cursor on
  //Write_LCD_Byte(ATcommand,0x0F);
}
//--------------------------end LCD library---------------------
void DS1307_Write(byte ibuff_write)
{
  TinyWireM.beginTransmission(DS1307_ID);
  TinyWireM.send(ibuff_write);                 // Access Command Register
  TinyWireM.endTransmission();          // Send to the slave
  delayMicroseconds(16); //  minimun~16-20
}
byte DS1307_Read(void)
{
  byte ibuff;
  TinyWireM.requestFrom(DS1307_ID,1); // Request 1 byte from slave
  ibuff= TinyWireM.receive();          // get the temperature
  return(ibuff);
}
void GetClock(void)
{
  DS1307_Write(0x00);
  second = DS1307_Read();
  minute = DS1307_Read();
  hour   = DS1307_Read();
  wkDay  = DS1307_Read();
  date   = DS1307_Read();
  month  = DS1307_Read();
  year   = DS1307_Read();
  ctrl   = DS1307_Read();
}
void SetClock()
{
  TinyWireM.beginTransmission(DS1307_ID);
  TinyWireM.send(0x00);                 // start Address fist
  TinyWireM.send(second);                 // Access Command Register
  TinyWireM.send(minute);                 // Access Command Register
  TinyWireM.send(hour);                 // Access Command Register
  TinyWireM.send(wkDay);                 // Access Command Register
  TinyWireM.send(date);                 // Access Command Register
  TinyWireM.send(month);                 // Access Command Register
  TinyWireM.send(year);                 // Access Command Register
  TinyWireM.send(ctrl);                 // Access Command Register
  TinyWireM.endTransmission();          // Send to the slave
  delayMicroseconds(16); //  minimun~16-20
}
void PrintDayName(byte Name)
{
  switch (Name)
  {
    case 1: Write_LCD_Byte(ATdata,'S');
            Write_LCD_Byte(ATdata,'u');
            Write_LCD_Byte(ATdata,'n');
            break;
    case 2: Write_LCD_Byte(ATdata,'M');
            Write_LCD_Byte(ATdata,'o');
            Write_LCD_Byte(ATdata,'n');
            break;
    case 3: Write_LCD_Byte(ATdata,'T');
            Write_LCD_Byte(ATdata,'u');
            Write_LCD_Byte(ATdata,'e');
            break;
    case 4: Write_LCD_Byte(ATdata,'W');
            Write_LCD_Byte(ATdata,'e');
            Write_LCD_Byte(ATdata,'d');
            break;
    case 5: Write_LCD_Byte(ATdata,'T');
            Write_LCD_Byte(ATdata,'h');
            Write_LCD_Byte(ATdata,'u');
            break;
    case 6: Write_LCD_Byte(ATdata,'F');
            Write_LCD_Byte(ATdata,'r');
            Write_LCD_Byte(ATdata,'i');
            break;
   case 7:  Write_LCD_Byte(ATdata,'S');
            Write_LCD_Byte(ATdata,'a');
            Write_LCD_Byte(ATdata,'t');
            break;
   default: Write_LCD_Byte(ATdata,'?');
            Write_LCD_Byte(ATdata,'?');
            Write_LCD_Byte(ATdata,'?');
 }
}
void PrintMonthName(byte Name)
{
  switch (Name)
  {
    case 1:  Write_LCD_Byte(ATdata,'J');
             Write_LCD_Byte(ATdata,'a');
             Write_LCD_Byte(ATdata,'n');
             break;
    case 2:  Write_LCD_Byte(ATdata,'F');
             Write_LCD_Byte(ATdata,'e');
             Write_LCD_Byte(ATdata,'b');
             break;
    case 3:  Write_LCD_Byte(ATdata,'M');
             Write_LCD_Byte(ATdata,'a');
             Write_LCD_Byte(ATdata,'r');
             break;
    case 4:  Write_LCD_Byte(ATdata,'A');
             Write_LCD_Byte(ATdata,'p');
             Write_LCD_Byte(ATdata,'r');
             break;
    case 5:  Write_LCD_Byte(ATdata,'M');
             Write_LCD_Byte(ATdata,'a');
             Write_LCD_Byte(ATdata,'y');
             break;
    case 6:  Write_LCD_Byte(ATdata,'J');
             Write_LCD_Byte(ATdata,'u');
             Write_LCD_Byte(ATdata,'n');
             break;
    case 7:  Write_LCD_Byte(ATdata,'J');
             Write_LCD_Byte(ATdata,'u');
             Write_LCD_Byte(ATdata,'y');
             break;
    case 8:  Write_LCD_Byte(ATdata,'A');
             Write_LCD_Byte(ATdata,'u');
             Write_LCD_Byte(ATdata,'g');
             break;
    case 9:  Write_LCD_Byte(ATdata,'S');
             Write_LCD_Byte(ATdata,'e');
             Write_LCD_Byte(ATdata,'p');
             break;
    case 10: Write_LCD_Byte(ATdata,'O');
             Write_LCD_Byte(ATdata,'c');
             Write_LCD_Byte(ATdata,'t');
             break;
    case 11: Write_LCD_Byte(ATdata,'N');
             Write_LCD_Byte(ATdata,'o');
             Write_LCD_Byte(ATdata,'v');
             break;
    case 12: Write_LCD_Byte(ATdata,'D');
             Write_LCD_Byte(ATdata,'e');
             Write_LCD_Byte(ATdata,'c');
             break;
    default: Write_LCD_Byte(ATdata,'?');
             Write_LCD_Byte(ATdata,'?');
             Write_LCD_Byte(ATdata,'?');
  }
}
void setup()
{
  TinyWireM.begin();                    // initialize I2C lib
  initial_LCD();
  //SetClock();
}
void loop()
{  
  byte buff10;
  GetClock();
  Write_LCD_Byte(ATcommand,0x80|0x00);// line 1 เริ่มตำแหน่ง 00
  buff10=hour&0x0f;
  Write_LCD_Byte(ATdata,((hour>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);
  Write_LCD_Byte(ATdata,':');
  buff10=minute&0x0f;
  Write_LCD_Byte(ATdata,((minute>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);
  Write_LCD_Byte(ATdata,':');
  buff10=second&0x0f;
  Write_LCD_Byte(ATdata,((second>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);
  Write_LCD_Byte(ATcommand,0x80|0x40);// line 2 เริ่มตำแหน่ง 00
  PrintDayName(wkDay);
  Write_LCD_Byte(ATdata,0xff);//|
  buff10=date&0x0f;
  Write_LCD_Byte(ATdata,((date>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);
  Write_LCD_Byte(ATdata,0xff);//|
  buff10=month&0x0f;
  PrintMonthName(((month>>4)*10)+buff10);
  Write_LCD_Byte(ATdata,0xff);// |
  Write_LCD_Byte(ATdata,'2');
  Write_LCD_Byte(ATdata,'0');
  buff10=year&0x0f;
  Write_LCD_Byte(ATdata,((year>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);
  Write_LCD_Byte(ATdata,' ');// 0x7c|
  buff10=ctrl&0x0f;
  Write_LCD_Byte(ATdata,((ctrl>>4)&0x0f)+0x30);
  Write_LCD_Byte(ATdata,buff10+0x30);
}