code:
#include <OneWire.h>
#include <TinyWireM.h> // I2C Master lib for ATTinys which use USI
#define PCF8574_ID 0x20 //slave address (PCF8574):[0][1][0][0][A2][A1][A0][R/W]
#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
OneWire ds(3); // on pin 10
byte second=0x00;
byte minute=0x30;
byte hour=0x23;
byte wkDay=0x02;
byte date=0x23;
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
}
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)
{
PCF8574_Write(0x00);//ให้ขา E = 0 ให้ขา RS= 0 กำหนดเป็นคำสั่ง ให้ขา R/W = 0 เขียนข้อมูล
PCF8574_Write(0x23); //1 0x20 + 0x03
PCF8574_Write(0x03);
PCF8574_Write(0x23); //2
PCF8574_Write(0x03);
PCF8574_Write(0x23); //3
PCF8574_Write(0x03);
PCF8574_Write(0x22);//Function Set (BIT5=1) [001DNF00 ],Sets buss size 4bit (D=0)ข้อมูลเฉพาะ D4-D7 จะถูกส่งออกไปที่บัส
PCF8574_Write(0x02);
Write_LCD_Byte(ATcommand,0x28);// 00101000 : [001DNF00 ],Sets buss size 4bit (D=0),number of lines=2 (N=1),font size =5x7(F=0)
Write_LCD_Byte(ATcommand,0x0C);//00001100 : [00001DUB] ,Display On (D=1),Cursor Off (U=0) ,blinking block cursor Off (B=0)
Write_LCD_Byte(ATcommand,0x06);//00000110 : [000001MS] , Cursor direction right (M=1);no scroll (S=0)
Write_LCD_Byte(ATcommand,0x01);
}
//--------------------------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 setup()
{
TinyWireM.begin(); // initialize I2C lib
initial_LCD(); //initial LCD
//SetClock();
}
void loop(void) {
float celsius, fahrenheit;
byte data[9]; //9 Byte Array Savd Temperature Code
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(ATdata,0xff);
Write_LCD_Byte(ATcommand,0x80|0x40);// line 2 เริ่มตำแหน่ง 00
buff10=date&0x0f;
Write_LCD_Byte(ATdata,((date>>4)&0x0f)+0x30);
Write_LCD_Byte(ATdata,buff10+0x30);
Write_LCD_Byte(ATdata,0xa5);// |
buff10=month&0x0f;
Write_LCD_Byte(ATdata,((month>>4)&0x0f)+0x30);
Write_LCD_Byte(ATdata,buff10+0x30);
Write_LCD_Byte(ATdata,0xa5);// |
buff10=year&0x0f;
Write_LCD_Byte(ATdata,((year>>4)&0x0f)+0x30);
Write_LCD_Byte(ATdata,buff10+0x30);
Write_LCD_Byte(ATdata,0xff);// |
delay(1000); //Wait 1 Second Repeat
if(ds.reset()) //If Device ACK on Reset
{
ds.write(0xCC); //Skip ROM Command
ds.write(0x44); //Start Conversion Temp
ds.reset(); //Re-Start New Command
ds.write(0xCC); //Skip ROM Command
ds.write(0xBE); //Read Scratchpad RAM(Temperature Data)
for ( int i = 0; i<9; i++) //9 Byte Read Scratchpad RAM
{
data[i] = ds.read();
}
Write_LCD_Byte(ATcommand,0x80|0x09);// line 1 เริ่มตำแหน่ง 00
celsius =( ((float)data[0]/2) - ( 0.25 + ( ((float)data[7]-(float)data[6]) / (float)data[7] ) ) );
fahrenheit = celsius * 1.8 + 32.0;
Write_LCD_Byte(ATdata,((int)celsius/10)|0x30);
Write_LCD_Byte(ATdata,((int)celsius%10)|0x30);
Write_LCD_Byte(ATdata,'.');
Write_LCD_Byte(ATdata,((int)(celsius*10)%10)|0x30);
Write_LCD_Byte(ATdata,((int)(celsius*100)%10)|0x30);
Write_LCD_Byte(ATdata,0xDF);
Write_LCD_Byte(ATdata,'C');
Write_LCD_Byte(ATcommand,0x80|0x49);// line 1 เริ่มตำแหน่ง 00
Write_LCD_Byte(ATdata,((int)fahrenheit/10)|0x30);
Write_LCD_Byte(ATdata,((int)fahrenheit%10)|0x30);
Write_LCD_Byte(ATdata,'.');
Write_LCD_Byte(ATdata,((int)(fahrenheit*10)%10)|0x30);
Write_LCD_Byte(ATdata,((int)(fahrenheit*100)%10)|0x30);
Write_LCD_Byte(ATdata,0xDF);
Write_LCD_Byte(ATdata,'F');
}
}