Chào mọi người,em gửi mọi người đoạn code của em, mong mọi người chỉ em cách thêm vào cách đếm lên, xuống( dùng 4 led thu phát hồng ngoại, xe vào thì đếm lên, xe ra thì đếm xuống ) có nút nhấn đặt trước số đếm ( phần này thì em đang bí ). Phần hiển thị nhiệt độ thì đã tốt, em đang thêm vào điều khiển relay báo chuông và máy bơm nước. Mong mọi người giúp đỡ.
#include <AT89x52.H>
#define DATA_LCD P2
#define E P3_7
#define RW P3_6
#define RS P1_6
#define DATA_ADC P0
#define KENH_A P3_0
#define KENH_B P3_1
#define KENH_C P3_2
#define ALE P3_3
#define START P1_4
#define EOC P1_5
#define MALENH 0
#define MAHT 1
#define MODE P1_0
#define UP P1_1
#define DN P1_2
#define ROLE P1_3
#define X P1_7
// Liet ke ham
void delay();
void ghi_LCD();
void ghi_chuoi();
void KTAO_LCD();
void DOC_ADC ();
void TS_GM ();
void TS_GM_LCD();
void hienthi_LCD();
void KT_UP();
void KT_DN();
void KT_MODE();
//Khai bao
unsigned char KQADC,DEM, KQD, TT, TT1;
unsigned char MADV, MACH, MATR, MADV_ADC, MACH_ADC, MATR_ADC;
unsigned char BMODE;
unsigned char T_TREN;
unsigned char T_DUOI;
// Viet chuong trinh con
void delay (unsigned int x)
{
unsigned int y;
for(y=0;y<x;y++){}
}
//Bit chon LCD
void ghi_LCD(bit chonRS, unsigned char DL_LCD)
{
DATA_LCD=DL_LCD;
RS=chonRS;
RW=0;
E=1;
E=0;
delay(100);
}
//Ghi chuoi
void ghi_chuoi(char*chuoi)
{
while(*chuoi)
{ghi_LCD(MAHT,*chuoi ++);}
}
//Khoi tao LCD
void KTAO_LCD()
{
ghi_LCD(MALENH,0x38);
ghi_LCD(MALENH,0x38);
ghi_LCD(MALENH,0x38);
ghi_LCD(MALENH,0x0C);
ghi_LCD(MALENH,0x06);
ghi_LCD(MALENH,0x01);
}
// Doc ADC
void DOC_ADC()
{
KENH_A=1;
KENH_B=1;
KENH_C=1;
ALE=1 ; START=1;
ALE=0; START=0;
delay(500);
KQADC=DATA_ADC;
}
//Tach so, giai ma ADC
void TS_GM_ADC()
{
MADV_ADC=(KQADC%10)+0x30;
MACH_ADC= ((KQADC/10)%10)+0x30;
MATR_ADC= (KQADC/100)+0x30;
if (MATR_ADC==0x30)
{MATR_ADC=' ';
if(MACH_ADC==0x30) MACH_ADC=' ';}
}
//Tach so giai ma dem xe
void TS_GM()
{
MADV=(DEM%10)+0x30;
MACH= ((DEM/10)%10)+0x30;
MATR= (DEM/100)+0x30;
if (MATR==0x30)
{MATR=' ';
if(MACH==0x30) MACH=' ';}
}
//Hien thi LCD
void hienthi_LCD()
{ ghi_LCD(MALENH, 0x80);
ghi_chuoi("SO XE: ");
ghi_LCD(MALENH,0x8A);
ghi_LCD(MAHT,MATR);
ghi_LCD(MAHT,MACH);
ghi_LCD(MAHT,MADV);
ghi_LCD(MALENH,0xC0);
ghi_chuoi("NHIET DO: ");
ghi_LCD(MALENH,0xCA);
ghi_LCD(MAHT,MATR_ADC);
ghi_LCD(MAHT,MACH_ADC);
ghi_LCD(MAHT,MADV_ADC);
ghi_chuoi("oC");
}
void main()
{ TMOD=0x05; TR0=1;
TMOD =0x55; TR1=1;
// TMOD=0X50; TR1=1; TH1=0; TL1=0;
KTAO_LCD();
while(1)
{ DOC_ADC();
TS_GM_ADC();
hienthi_LCD();
delay(500);
//DEM=(TH0<<8)+TL0;
TS_GM();
hienthi_LCD();
if(TL0!=0)
{if(TT==0)
{DEM=DEM+TL0;
TL0=0;}
TS_GM();
if (TL1!=0) // phan nay em lam tao lao, khong chay duoc. @@!
{if(TT1==0)
{DEM=DEM-TL0;
TL1=0;} }
}
}
}
#include <AT89x52.H>
#define DATA_LCD P2
#define E P3_7
#define RW P3_6
#define RS P1_6
#define DATA_ADC P0
#define KENH_A P3_0
#define KENH_B P3_1
#define KENH_C P3_2
#define ALE P3_3
#define START P1_4
#define EOC P1_5
#define MALENH 0
#define MAHT 1
#define MODE P1_0
#define UP P1_1
#define DN P1_2
#define ROLE P1_3
#define X P1_7
// Liet ke ham
void delay();
void ghi_LCD();
void ghi_chuoi();
void KTAO_LCD();
void DOC_ADC ();
void TS_GM ();
void TS_GM_LCD();
void hienthi_LCD();
void KT_UP();
void KT_DN();
void KT_MODE();
//Khai bao
unsigned char KQADC,DEM, KQD, TT, TT1;
unsigned char MADV, MACH, MATR, MADV_ADC, MACH_ADC, MATR_ADC;
unsigned char BMODE;
unsigned char T_TREN;
unsigned char T_DUOI;
// Viet chuong trinh con
void delay (unsigned int x)
{
unsigned int y;
for(y=0;y<x;y++){}
}
//Bit chon LCD
void ghi_LCD(bit chonRS, unsigned char DL_LCD)
{
DATA_LCD=DL_LCD;
RS=chonRS;
RW=0;
E=1;
E=0;
delay(100);
}
//Ghi chuoi
void ghi_chuoi(char*chuoi)
{
while(*chuoi)
{ghi_LCD(MAHT,*chuoi ++);}
}
//Khoi tao LCD
void KTAO_LCD()
{
ghi_LCD(MALENH,0x38);
ghi_LCD(MALENH,0x38);
ghi_LCD(MALENH,0x38);
ghi_LCD(MALENH,0x0C);
ghi_LCD(MALENH,0x06);
ghi_LCD(MALENH,0x01);
}
// Doc ADC
void DOC_ADC()
{
KENH_A=1;
KENH_B=1;
KENH_C=1;
ALE=1 ; START=1;
ALE=0; START=0;
delay(500);
KQADC=DATA_ADC;
}
//Tach so, giai ma ADC
void TS_GM_ADC()
{
MADV_ADC=(KQADC%10)+0x30;
MACH_ADC= ((KQADC/10)%10)+0x30;
MATR_ADC= (KQADC/100)+0x30;
if (MATR_ADC==0x30)
{MATR_ADC=' ';
if(MACH_ADC==0x30) MACH_ADC=' ';}
}
//Tach so giai ma dem xe
void TS_GM()
{
MADV=(DEM%10)+0x30;
MACH= ((DEM/10)%10)+0x30;
MATR= (DEM/100)+0x30;
if (MATR==0x30)
{MATR=' ';
if(MACH==0x30) MACH=' ';}
}
//Hien thi LCD
void hienthi_LCD()
{ ghi_LCD(MALENH, 0x80);
ghi_chuoi("SO XE: ");
ghi_LCD(MALENH,0x8A);
ghi_LCD(MAHT,MATR);
ghi_LCD(MAHT,MACH);
ghi_LCD(MAHT,MADV);
ghi_LCD(MALENH,0xC0);
ghi_chuoi("NHIET DO: ");
ghi_LCD(MALENH,0xCA);
ghi_LCD(MAHT,MATR_ADC);
ghi_LCD(MAHT,MACH_ADC);
ghi_LCD(MAHT,MADV_ADC);
ghi_chuoi("oC");
}
void main()
{ TMOD=0x05; TR0=1;
TMOD =0x55; TR1=1;
// TMOD=0X50; TR1=1; TH1=0; TL1=0;
KTAO_LCD();
while(1)
{ DOC_ADC();
TS_GM_ADC();
hienthi_LCD();
delay(500);
//DEM=(TH0<<8)+TL0;
TS_GM();
hienthi_LCD();
if(TL0!=0)
{if(TT==0)
{DEM=DEM+TL0;
TL0=0;}
TS_GM();
if (TL1!=0) // phan nay em lam tao lao, khong chay duoc. @@!
{if(TT1==0)
{DEM=DEM-TL0;
TL1=0;} }
}
}
}
Comment