Attiny45_I2C_DS1307_Read_Only
code :
#define DS1307_ID_Read 0xD1 //01000001 ; 001=slave address ; Read=1
#define DS1307_ID_Write 0xD0 //01000001 ; 000=slave address ; Write = 0
#define PCF8574_ID_Write 0x40
#define SDA 1//A4//0//18 // Serial data
#define SCL 2//A5//1//19 // Serial clock
char outdata;
#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;
byte minute;
byte hour;
byte wkDay;
byte date;
byte month;
byte year;
byte ctrl;
void i2c_start(void)
{
pinMode(SCL, OUTPUT);
pinMode(SDA, OUTPUT);
digitalWrite(SDA, HIGH);
digitalWrite(SCL, HIGH);
digitalWrite(SDA, LOW);
delayMicroseconds(1);
digitalWrite(SCL, LOW);
}
void i2c_acknowledge(void)
{
digitalWrite(SDA, LOW);
digitalWrite(SCL, HIGH);
delayMicroseconds(1);
digitalWrite(SCL, LOW);
}
void i2c_not_acknowledge(void)
{
digitalWrite(SDA, HIGH);
digitalWrite(SCL, HIGH);
delayMicroseconds(1);
digitalWrite(SCL, LOW);
}
void i2c_stop(void)
{
digitalWrite(SCL, HIGH);
delayMicroseconds(1);
digitalWrite(SDA, HIGH);
}
void i2c_write(unsigned char output_data)
{
int iBit=0;
for(int iloop = 0 ; iloop < 8 ; iloop++)
{
iBit = ((output_data & 0x80) >> 7 ) ;
digitalWrite(SDA,iBit); // bit output
digitalWrite(SCL, HIGH);
delayMicroseconds(1);
digitalWrite(SCL, LOW);
output_data = output_data << 1 ;
}
}
byte i2c_read (void)
{
byte buff_byte_input=0x00;
for(byte icount=0 ; icount<8 ; icount++)
{
pinMode(SDA, INPUT);
digitalWrite(SCL, HIGH);
buff_byte_input = buff_byte_input << 1 ;
buff_byte_input |= digitalRead(SDA);
digitalWrite(SCL, LOW);
}
return (buff_byte_input);
}
void PCF8574_Write(unsigned char data)
{
i2c_start(); // |-[S]-| ;Start Condition
i2c_write(PCF8574_ID_Write); // |---Slave address (PCF8574)--[A2=0,A1=0,A0=0]---| + |-R/W-|
i2c_acknowledge(); // |-[A]-| ;Active LOW
i2c_write(data); // |-------------------data to port-----------------------|
i2c_acknowledge(); // |-[A]-| ;Active LOW
i2c_stop(); // |-[P]-| ;Stop Condition
}
//---------------------LCD library ----------------------------
void Write_LCD_Byte(char command_data ,char value)
{
int iMSB=0;
int iLSB=0;
iMSB = (value & 0xF0)>>4;
iLSB = (value & 0x0F);
PCF8574_Write(command_data|iMSB);//0xFF
delayMicroseconds(1);
PCF8574_Write(0x00|iMSB);
PCF8574_Write(command_data|iLSB);
delayMicroseconds(1);
PCF8574_Write(0x00|iLSB);
}
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)
Write_LCD_Byte(ATcommand,0x01);//0x01 clear screen
}
//--------------------------end LCD library---------------------
void DS1307_Write(byte ibuff_write)
{
i2c_start();
i2c_write(DS1307_ID_Write);
i2c_acknowledge();
i2c_write(ibuff_write);
i2c_acknowledge();
i2c_stop();
}
byte DS1307_Read(void)
{
byte ibuff;
i2c_start();
i2c_write(DS1307_ID_Read);
i2c_acknowledge();
ibuff=i2c_read();
//i2c_acknowledge();
//i2c_stop();
return(ibuff);
}
void GetClock(void)
{
DS1307_Write(0x00);
second = DS1307_Read();
i2c_acknowledge();
minute = DS1307_Read();
i2c_acknowledge();
hour = DS1307_Read();
i2c_acknowledge();
wkDay = DS1307_Read();
i2c_acknowledge();
date = DS1307_Read();
i2c_acknowledge();
month = DS1307_Read();
i2c_acknowledge();
year = DS1307_Read();
i2c_acknowledge();
ctrl = DS1307_Read();
i2c_not_acknowledge();
i2c_stop();
}
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(){
pinMode(SCL, OUTPUT);
pinMode(SDA, OUTPUT);
digitalWrite(SDA, HIGH);
digitalWrite(SCL, HIGH);
initial_LCD();
}
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,'/');
buff10=date&0x0f;
Write_LCD_Byte(ATdata,((date>>4)&0x0f)+0x30);
Write_LCD_Byte(ATdata,buff10+0x30);
Write_LCD_Byte(ATdata,'/');
buff10=month&0x0f;
PrintMonthName(((month>>4)*10)+buff10);
Write_LCD_Byte(ATdata,'/');
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,' ');
Write_LCD_Byte(ATdata,' ');
buff10=ctrl&0x0f;
Write_LCD_Byte(ATdata,((ctrl>>4)&0x0f)+0x30);
Write_LCD_Byte(ATdata,buff10+0x30);
}