Mò mãi mới ra. Tạo được thư viện giao tiếp giữa 8051 và DS12C887 rất đơn giản, gọn nhẹ. Anh em cứ lấy dùng nhé, và cho ý kiến nhé. Chạy rất rất OK!
Em nó đây:
#define SEC 0x00
#define MIN 0x02
#define HOUR 0x04
#define DATE 0x06
#define DAY 0x07
#define MONTH 0x08
#define YEAR 0x09
#define REG_A 0x0A
#define REG_B 0x0B
#define REG_D 0x0D
#define DAY_AL 0x0E
#define MONTH_AL 0x0F
unsigned char Display_Number[]={0x81,0xD7,0xC8,0xD0,0x96,0xB0,0xA0,0xD5,0x80,0x9 0};//khai bao mang chua cac gia tri gom 10 phan tu
unsigned char SoGioHangChuc,SoGioHangDonVi,SoPhutHangChuc,SoPhut HangDonVi,SoGiayHangChuc,SoGiayHangDonVi;
unsigned char SoThuHangDonVi,SoNgayHangChuc,SoNgayHangDonVi,SoTh angHangChuc,SoThangHangDonVi,SoNamHangChuc,SoNamHa ngDonVi;
unsigned char SoNgayHangChucAL,SoNgayHangDonViAL,SoThangHangChuc AL,SoThangHangDonViAL;
// Cac chuong trinh doc du lieu tu DS12C887
unsigned char DS12C887_Read(unsigned char addr)
{
char ret;
ret=addr;
ret = *((char xdata *) ret);
return ret;
}
void Read_DS12C887()
{
sec=DS12C887_Read(SEC);
min=DS12C887_Read(MIN);
hour=DS12C887_Read(HOUR);
date=DS12C887_Read(DATE);
day=DS12C887_Read(DAY);
month=DS12C887_Read(MONTH);
year=DS12C887_Read(YEAR);
day_al=DS12C887_Read(DAY_AL);
month_al=DS12C887_Read(MONTH_AL);
}
// Cac chuong trinh ghi du lieu vao DS12C887
void DS12C887_Write(unsigned char addr,unsigned char dat)
{
*((char xdata *) addr)= dat;
}
void Init_DS12C887()
{
char temp;
temp=0x20;
DS12C887_Write(REG_A,temp);
temp=0x06;
DS12C887_Write(REG_B,temp);
}
void Write_DS12C887()
{
DS12C887_Write(SEC,sec); //ghi du lieu vao thanh ghi giay
DS12C887_Write(MIN,min); //ghi du lieu vao thanh ghi phut
DS12C887_Write(HOUR,hour); //ghi du lieu vao thanh ghi gio
DS12C887_Write(DAY,day); //ghi du lieu vao thanh ghi ngay
DS12C887_Write(DATE,date); //ghi du lieu vao thanh ghi thu
DS12C887_Write(MONTH,month); //ghi du lieu vao thanh ghi thang
DS12C887_Write(YEAR,year); //ghi du lieu vao thanh ghi nam
DS12C887_Write(DAY_AL,day_al); //ghi du lieu vao thanh ghi ngay
DS12C887_Write(MONTH_AL,month_al); //ghi du lieu vao thanh ghi thang
}
Em nó đây:
#define SEC 0x00
#define MIN 0x02
#define HOUR 0x04
#define DATE 0x06
#define DAY 0x07
#define MONTH 0x08
#define YEAR 0x09
#define REG_A 0x0A
#define REG_B 0x0B
#define REG_D 0x0D
#define DAY_AL 0x0E
#define MONTH_AL 0x0F
unsigned char Display_Number[]={0x81,0xD7,0xC8,0xD0,0x96,0xB0,0xA0,0xD5,0x80,0x9 0};//khai bao mang chua cac gia tri gom 10 phan tu
unsigned char SoGioHangChuc,SoGioHangDonVi,SoPhutHangChuc,SoPhut HangDonVi,SoGiayHangChuc,SoGiayHangDonVi;
unsigned char SoThuHangDonVi,SoNgayHangChuc,SoNgayHangDonVi,SoTh angHangChuc,SoThangHangDonVi,SoNamHangChuc,SoNamHa ngDonVi;
unsigned char SoNgayHangChucAL,SoNgayHangDonViAL,SoThangHangChuc AL,SoThangHangDonViAL;
// Cac chuong trinh doc du lieu tu DS12C887
unsigned char DS12C887_Read(unsigned char addr)
{
char ret;
ret=addr;
ret = *((char xdata *) ret);
return ret;
}
void Read_DS12C887()
{
sec=DS12C887_Read(SEC);
min=DS12C887_Read(MIN);
hour=DS12C887_Read(HOUR);
date=DS12C887_Read(DATE);
day=DS12C887_Read(DAY);
month=DS12C887_Read(MONTH);
year=DS12C887_Read(YEAR);
day_al=DS12C887_Read(DAY_AL);
month_al=DS12C887_Read(MONTH_AL);
}
// Cac chuong trinh ghi du lieu vao DS12C887
void DS12C887_Write(unsigned char addr,unsigned char dat)
{
*((char xdata *) addr)= dat;
}
void Init_DS12C887()
{
char temp;
temp=0x20;
DS12C887_Write(REG_A,temp);
temp=0x06;
DS12C887_Write(REG_B,temp);
}
void Write_DS12C887()
{
DS12C887_Write(SEC,sec); //ghi du lieu vao thanh ghi giay
DS12C887_Write(MIN,min); //ghi du lieu vao thanh ghi phut
DS12C887_Write(HOUR,hour); //ghi du lieu vao thanh ghi gio
DS12C887_Write(DAY,day); //ghi du lieu vao thanh ghi ngay
DS12C887_Write(DATE,date); //ghi du lieu vao thanh ghi thu
DS12C887_Write(MONTH,month); //ghi du lieu vao thanh ghi thang
DS12C887_Write(YEAR,year); //ghi du lieu vao thanh ghi nam
DS12C887_Write(DAY_AL,day_al); //ghi du lieu vao thanh ghi ngay
DS12C887_Write(MONTH_AL,month_al); //ghi du lieu vao thanh ghi thang
}
Comment