Dientuchiase chia sẻ code và mạch đồng hồ Giờ - Phút - Nhiệt độ 2 IN 1, nhiều bạn hỏi để làm..Nên Post lên các bạn xem nhé..
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút hiện ra ở góc phải phía trên, nhấn vào để đến trang download
ĐIỆN TỬ CHIA SẺ
Phạm Văn Ngọc Anh- 01644326695
Code mã nguồn C:
#include <REGX52.H>
#include <MATH.H>
//Dinh nghia cac chan phim dieu chinh
#define Mode_Menu P3_2 //phim Mode de chon doi tuong dieu chinh
#define Mode_Up P3_1 //phim Up de tang doi tuong
#define Mode_Down P3_3 //phim Down de giam doi tuong
//dinh nghia cac chan dieu khien Anode Led
#define Led_ampm_Sang P1_0=0; //Led chi thi am_pm, sang la pm.
#define Led_ampm_Tat P1_0=1;
#define GioHangChuc_Sang P1_1=0;
#define GioHangDonVi_Sang P1_2=0;
#define PhutHangChuc_Sang P1_4=0;
#define PhutHangDonVi_Sang P1_3=0;
#define Led_C_Sang P1_5=0; //Led phan cach gio va phut
#define Led_C_Tat P1_5=1;
#define OffLed P1=0xFF;
#define DQ P0_0
#define SCL P0_6
#define SDA P0_5
//dinh nghia cac chan dieu khien 7 doan
#define CLK P2_0
#define STR P2_1
#define SDI P2_2
//dinh nghia dia chi cac thanh ghi trong DS1307
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
//cac ham dung cho DS1307
void I2C_Start(); //khoi dong cho DS1307
void I2C_stop();
unsigned char I2C_write(unsigned char dat); //ham ghi 1 byte du kieu vao DS1307
unsigned char I2C_read(); //ham doc 1 byte du kieu vao DS1307
unsigned char DS1307_read(unsigned char addr); //ham doc du lieu tu 1 thanh ghi bat ky
void DS1307_write(unsigned char addr,unsigned char dat); //ham ghi du lieu tu 1 thanh ghi bat ky
void Init_DS1307();
void Write_DS1307(); //ham cai dat thoi gian vao DS1307
void Read_DS1307(); //ham doc thoi gian vao DS1307
void Convert_Number(); //ham chuyen doi tach so de hien thi tren led 7 thanh
void GHI_7DOAN(unsigned char addr);
void Display_Gio_Phut_Giay(); //ham hien thi gio phut giay tren led 7 thanh
void Display_Gio();
void Display_Phut();
void Display_Temp();
void ScanKey(); //ham quet phim nhan
//khai bao bien //0 1 2 3 4 5 6 7 8 9 temp C
unsigned char Display_Number[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x1C,0x46};//khai bao mang chua cac gia tri gom 10 phan tu
unsigned char SoGioHangChuc,SoGioHangDonVi,SoPhutHangChuc,SoPhutHangDonVi,SoGiayHangChuc,SoGiayHangDonVi;
unsigned int dem_timer,Millisec,Millisec1; //bien dem ms
unsigned char sec,hour,min,min_temp,hour_temp; //cac bien dem gio phut giay....
char sec2,count;
unsigned int Nhiet_Do,NhietDoHangChuc,NhietDoHangDonVi;
unsigned char kt_sec; //bien kiem tra xem RTC co hoat dong khong
unsigned char kt_sec2;
unsigned char ampm,Mode,Chop_Tat,Chop_Tat1; //bien vao cac che do chinh thoi gian
static bit presence;
//cac ham khai bao
void delay_us(unsigned int Time)
{
unsigned int i,j;
for(i=0;i<Time;i++)
for(j=0;j<2;j++);
}
void delay_ms(unsigned int ms) //ham delay
{
unsigned int i,j;
for(i=0;i<ms;i++)
for(j=0;j<121;j++);
}
bit DS18B20_Init()
{
DQ = 1 ;
Display_Temp();
//delay_us(8);
DQ = 0 ;
Display_Temp();
//delay_us(90) ;
DQ = 1 ;
Display_Temp();
//delay_us(8) ;
presence = DQ ;
Display_Temp();
Display_Temp();
//delay_us(100) ;
DQ = 1 ;
return(presence) ;
}
//-------------------------------------------------
// Ham doc 1 byte tu DS18b20 ra ngoai
//-------------------------------------------------
unsigned char ds18b20_readbyte(void)
{
unsigned char i = 0 ;
unsigned char Data = 0 ;
for (i = 8 ; i > 0 ; i--)
{
DQ = 0 ;
Data >>= 1 ;
DQ = 1 ;
if(DQ)
Data |= 0x80 ;
Display_Temp();
//delay_us(4) ;
}
return (Data) ;
}
//-------------------------------------------------
// Ham viet 1 byte vao DS18b20
//-------------------------------------------------
void ds18b20_writebyte(unsigned char Data)
{
unsigned char i = 0 ;
for (i = 8 ; i > 0 ; i--)
{
DQ = 0 ;
DQ = Data&0x01 ;
//Display_Temp();
delay_us(5) ;
DQ = 1 ;
Data>>=1 ;
}
}
//-------------------------------------------------
// Ham chuyen doi va tra ve gia tri nhiet do do duoc
//-------------------------------------------------
float ds18b20_readtemp(void)
{
float temp;
unsigned char a,b;
DS18B20_Init(); // Khoi tao DS18b20
ds18b20_writebyte(0xCC) ; // Cho phep VDK truy cap thang den cac lenh bo nho cua DS18b20
ds18b20_writebyte(0x44) ; // Khoi dong qua trinh do va chuyen doi nhiet do ra so nhi phan
Display_Temp();
DS18B20_Init() ;
ds18b20_writebyte(0xCC) ;
ds18b20_writebyte(0xBE) ; // Cho phep doc du lieu tu bo nho DS18b20 ra ngoai
Display_Temp();
a = ds18b20_readbyte();
temp=((float)(a&0x0f))/16; // Lay phan thuc cua gia tri nhiet do
b = ds18b20_readbyte();
a =((a&0xf0)>>4)|((b&0x0f)<<4) ; // Lay phan nguyen cua gia tri nhiet do
temp=temp+a;
return temp; // Tra ve gia tri nhiet do
}
void timer0_ISR(void) interrupt 1 // ham ngat timer 0 :1ms
{
TF0 = 0;//xoa co tran
TH0 =0x00; // nap du lieu vao thanh ghi timer
TL0 =0x00;
TR0=1;
Millisec++;
if(Millisec==160)Millisec=0;
if (Millisec<100)
Chop_Tat=0;
else Chop_Tat=1;
}
void timer1_ISR(void) interrupt 3 // ham ngat timer 1 :1ms
{
dem_timer++;
if(dem_timer>=100)
{dem_timer=0;
Nhiet_Do=ds18b20_readtemp();}
TR1 = 0;
TH1 = 0xc3; // 50ms
TL1 = 0x50;
TR1 = 1;
}
//chuong trinh chinh
void main(void)
{
//khoi tao cho bo dem timer
TCON = 0x11; // che do 16 bit tu nap lai
TH0=0x00;
TL0=0x00; //nap gia tri dem cho thanh ghi timer 0
TH1 = 0xc3; // 50ms
TL1 = 0x50;
ET0 = 1; // Enable Timer 0 Interrupts
TR0 = 1; // Start Timer 0 Running
EA=1; //cho phep ngat
count=0;
sec=DS1307_read(0);
if(sec&0x80==1) //kiem tra bit CH
{DS1307_write(0,sec&0x7f); }
while (1)
{
if(Mode==3)
{//Nhiet_Do=ds18b20_readtemp();
ET1 = 1; // Enable Timer 1 Interrupts
TR1 = 1; // Start Timer 1 Running
NhietDoHangChuc=Nhiet_Do/10;
NhietDoHangDonVi=Nhiet_Do%10;
EA=0;
Display_Temp();
EA=1;}
else
{ET1 = 0;
TR1 = 0;
Read_DS1307();}
ScanKey();
}
}
//******************************************************************************
// CAC CHUONG TRINH CON CHO I2C
//******************************************************************************
void I2C_start() //ham khoi dong I2C
{
SCL = 1;
SCL = 0;
SDA = 1;
SCL = 1;
SDA = 0;
SCL = 0;
}
void I2C_stop()
{
SCL = 1; //keo chan SCL len 1
SCL = 0;
SDA = 0;
SCL = 1;
SDA = 1;
}
unsigned char I2C_write(unsigned char dat)//ham ghi 1 byte du lieu vao DS1307
{
unsigned char i;
for (i=0;i<8;i++)
{
SDA = (dat & 0x80); ///? 1:0;
SCL=1;SCL=0;
dat<<=1;
}
SCL = 1;
SCL = 0;
return dat;
}
unsigned char I2C_read(void)
{
bit rd_bit;
unsigned char i, dat;
dat = 0x00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
SCL = 1;
rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCL = 0; /* Clear SCL */
}
return dat;
}
// CAC CHUONG TRINH CON CHO DS1307
//******************************************************************************
unsigned char DS1307_read(unsigned char addr)
{
unsigned int temp,ret;
I2C_start(); /* Start i2c bus */
I2C_write(0xD0); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_start(); /* Start i2c bus */
I2C_write(0XD1); /* Connect to DS1307 for Read */
ret = I2C_read(); /* Receive data */
I2C_stop();
//**********************************************************
temp = ret; /*BCD to HEX*/
ret = (((ret/16)*10)+ (temp & 0x0f)); /*for Led 7seg*/
//**********************************************************
return ret;
}
void DS1307_write(unsigned char addr,unsigned char dat)
{
unsigned int temp;
//**********************************************
temp = dat ; /*HEX to BCD*/
dat = (((dat/10)*16)|(temp %10)); /*for Led 7seg*/
//**********************************************
I2C_start(); /* Start i2c bus */
I2C_write(0XD0); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_write(dat); /* Connect to DS1307 for Read */
I2C_stop();
}
void Init_DS1307()
{
sec = 8; //doc giay tu DS1307
min = 5; //doc phut tu DS1307
//hour = 7; //doc gio tu DS1307
}
void Read_DS1307()
{
sec = DS1307_read(SEC); //doc giay tu DS1307
min = DS1307_read(MIN); //doc phut tu DS1307
hour = DS1307_read(HOUR); //doc gio tu DS1307
}
void Write_DS1307()
{
DS1307_write(SEC,sec); //ghi du lieu vao thanh ghi giay
DS1307_write(MIN,min); //ghi du lieu vao thanh ghi phut
DS1307_write(HOUR,hour); //ghi du lieu vao thanh ghi gio
}
//ham quet led hien thi gio phut giay
void Display_Gio_Phut_Giay()
{
switch (Mode)
{
case 0:
{Display_Gio();
Display_Phut();
break;}
case 1:
{if (Chop_Tat==0)Display_Gio();
Display_Phut();
break;}
case 2:
{Display_Gio();
if (Chop_Tat==0)Display_Phut();
break;}
}
OffLed;
if (ampm==1)Led_ampm_Sang
else Led_ampm_Tat;
delay_ms(1);
OffLed;
if (Chop_Tat==0)Led_C_Sang;
delay_ms(1);
}
void Display_Gio()
{
unsigned int Data;
if (SoGioHangChuc!=0)
{OffLed;
Data=Display_Number[SoGioHangChuc];
GHI_7DOAN(Data);
GioHangChuc_Sang;
delay_ms(1);}
OffLed;
Data=Display_Number[SoGioHangDonVi];
GHI_7DOAN(Data);
GioHangDonVi_Sang;
delay_ms(1);
}
void Display_Phut()
{
unsigned int Data;
OffLed;
Data=Display_Number[SoPhutHangChuc];
GHI_7DOAN(Data);
PhutHangChuc_Sang;
delay_ms(1);
OffLed;
Data=Display_Number[SoPhutHangDonVi];
GHI_7DOAN(Data);
PhutHangDonVi_Sang;
delay_ms(1);
}
void Display_Temp()
{
unsigned int Data;
if (NhietDoHangChuc!=0)
{OffLed;
Data=Display_Number[NhietDoHangChuc];
GHI_7DOAN(Data);
GioHangChuc_Sang;
delay_ms(1);}
OffLed;
Data=Display_Number[NhietDoHangDonVi];
GHI_7DOAN(Data);
GioHangDonVi_Sang;
delay_ms(1);
OffLed;
Data=Display_Number[10];
GHI_7DOAN(Data);
PhutHangChuc_Sang;
delay_ms(1);
OffLed;
Data=Display_Number[11];
GHI_7DOAN(Data);
PhutHangDonVi_Sang;
delay_ms(1);
}
void Convert_Number()
{
if (hour>12)
{hour_temp=hour-12;
ampm=1;}
if(hour==0)
{hour_temp=12;
ampm=1;}
if ((hour>=1)&(hour<=12))
{hour_temp=hour;
ampm=0;}
SoGiayHangChuc=sec/10;
SoGiayHangDonVi=sec%10;
SoPhutHangChuc=min/10;
SoPhutHangDonVi=min%10;
SoGioHangChuc=hour_temp/10;
SoGioHangDonVi=hour_temp%10;
}
void GHI_7DOAN(unsigned char addr)
{
unsigned char temp,i;
temp=addr;
for(i=0;i<8;i++)
{
SDI=temp&0x01;
CLK=0;
CLK=1;
temp>>=1;
} // truyen duoc 1 byte
STR=0;
STR=1;
}
void ScanKey() //ham quet phim nhan
{
if(Mode_Menu==0)
{Convert_Number();
Display_Gio_Phut_Giay();
Mode++;
if(Mode==4)Mode=0;//Mode =0 la che do binh thuong, 1 la dieu chinh gio, 2 la dieu chinh phut, 3 la hien thi nhiet do
while(Mode_Menu==0)
{Convert_Number();
Display_Gio_Phut_Giay();}
}
else if(Mode_Up==0)
{
switch (Mode)
{
case 0:
{Convert_Number();
Display_Gio_Phut_Giay();
break;}
case 1:
{Convert_Number();
Display_Gio_Phut_Giay();
hour++;;
if(hour>23)hour=0;
while(Mode_Up==0)
{Convert_Number();
Display_Gio_Phut_Giay();}
DS1307_write(2,hour);
break;}
case 2:
{Convert_Number();
Display_Gio_Phut_Giay();
min++;;
if(min>59)min=0;
while(Mode_Up==0)
{Convert_Number();
Display_Gio_Phut_Giay();}
DS1307_write(1,min);
break;}
}
}
else if(Mode_Down==0)
{
switch (Mode)
{
case 0:
{Convert_Number();
Display_Gio_Phut_Giay();
break;}
case 1:
{Convert_Number();
Display_Gio_Phut_Giay();
hour--;;
if(hour<0)hour=23;
while(Mode_Down==0)
{Convert_Number();
Display_Gio_Phut_Giay();}
DS1307_write(2,hour);
break;}
case 2:
{Convert_Number();
Display_Gio_Phut_Giay();
min--;;
if(min<0)min=59;
while(Mode_Down==0)
{Convert_Number();
Display_Gio_Phut_Giay();}
DS1307_write(1,min);
break;}
}
}
if (Mode!=3)
{Convert_Number();
Display_Gio_Phut_Giay();}
}
ĐIỆN TỬ CHIA SẺ
Phạm Văn Ngọc Anh- 01644326695
Đăng nhận xét