Chia sẻ Quà giáng sinh đây: Điều khiển quạt dùng mọi loại điều khiển

xuanhuu123

Học sinh trung cấp
à đúng rồi,, cần phải thêm vài chi tiết nữa, em đọc lại code để thêm vào cho đúng là chạy
đây là toàn bộ code e đã sửa. ah xem giúp e đc k0 ạ?
#include <AT89X52.H>
#include <INTRINS.H>
// cau hinh cac chan o day
#define sda P1_2
#define scl P1_1
#define led P0_0
#define so1 P0_1
#define so2 P0_2
#define so3 P0_3
#define quay1 P0_4
#define hoi_nuoc P0_6
#define key_1 P3_3
#define key_2 P1_3
#define key_3 P1_4
#define key_4 P1_5
#define key_5 P1_6
#define key_6 P3_1
#define key_7 P3_0
#define out_led P2
sbit led1=P3^6;
sbit led2=P3^7;
unsigned char M[10] ={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x83,0xF8,0x80,0x98};
unsigned char dislay[2] ={0xC0,0x98};
unsigned char dislay_quay_so[10] ={0xfe,0xFd,0xfb,0xf7,0xef,0xdf};
unsigned char dislay_quay_quat[10] ={0xfe,0xFd,0xbf,0xdf};

unsigned char select_led=0,toc_do_quay=0xfe,dem_quay=0,timer_quay=0,timer_so_quay,hienthi_quatquay=0xaa;
//=======================================================================
// khong can cau hinh cho nay
#define remote P3_2
// cac thong so cai dat cho viec giai ma remote, khong can cai dat cho hau het cac truong hop
#define max_start_0 0xDF;// 20 ms chieu rong toi da bit start
#define max_start_1 0xB1;// max_start = 0xffff - 20000 = 0xb1df
#define min_start 0x7D0;// 2 ms chieu rong toi thieu bit start
#define max_data_0 0x47;// 3 ms chieu rong toi da bit data
#define max_data_1 0xf4;// max_data = 0xff - 3000 = 0xf447
//=======================================================================
// bien toan cuc
unsigned char step=0,number_code = 0,so_quat=0,sec_5,sec_60,min_1,timer;// bien luu qua trinh giai ma
unsigned long data_,du_lieu[5]; // du lieu giai ma
//=======================================================================
void delay(unsigned long time)//tre time ms
{
while(time--)
{
_nop_();
_nop_();
_nop_();
}
}
void nhay_led(void)//tre time ms
{
led = !led;
delay(0xfff);
led = !led;
}
void cau_hinh(void) //
{
TMOD= 0x11; //;Che do 1 timer 0 (16 bit Gate=0 C/T=0 M1=0 M0=1)
//;Che do 1 timer 1 (16 bit Gate=0 C/T=0 M1=0 M0=1)
EX0 = 1; /* 1=Enable External interrupt 0 */
IT0 = 1; // ngat suon xuong
ET0 = 1; /* 1=Enable Timer 0 interrupt */
//EX1 = 1; /* 1=Enable External interrupt 1 */
ET1 = 1; /* 1=Enable Timer 1 interrupt */
//ET2 = 1; /* 1=Enable Timer 2 interrupt */
EA = 1; /* 0=Disable all interrupts */
TR1 = 1;
TH1 = 0xf8; //2ms
TL1 = 0x2f;
//IT1 = 1;
remote=1;
key_1=1;
key_2=1;
key_3=1;
key_4=1;
key_5=1;
key_6=1;
key_7=1;


sda=1;
scl=1;

so1=1;
so2=1;
so3=1;
quay1=1;
quay2=1;
led=1;

}

/**********************************************************************************/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// CAC CHUONG TRINH CON GIAO TIEP VOI 24c04 //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void start(void) //dk start: 1 su cdoi tthai tu cao xuong thap tren duong sda trong khi
{ //duong scl dang o muc cao
scl=1;
sda=1;
_nop_();
_nop_();
sda=0;
scl=0;
}
void stop(void) //dk stop: 1 su cdoi trang thai tu muc thap len cao tren duong sda trong khi duong
{ // slc dang o muc cao
sda=0;
scl=1;
_nop_();
_nop_();
sda=1;
}
bit write(unsigned char x) // gui du lieu va nhan ACK
{
unsigned char i;
for(i=0;i<8;i++)
{
sda=(x&0x80)? 1:0; //dua bit du lieu ra chan SDA
scl=1;
_nop_();
_nop_();
scl=0;
x<<=1; //bit co trong so lon hon dc truyen truoc
}
sda=1;
scl=1; //nhan bit ACK tu SLAVER bao hieu ket thuc mot byte du lieu
_nop_();
_nop_();
i=sda;
scl=0;
return i;
}
unsigned char read(bit ACK) // nhan du lieu va gui ACK
{
unsigned char Data,i;
for(i=0;i<8;i++) // nhan vao 8 bit
{
scl=1;
Data<<=1;
Data=Data|sda;
scl=0;
}
sda=ACK; // gui bit ACK=> sda=0
scl=1; //master nhan/gui bit du lieu(sda) khi scl o muc cao
_nop_();// lay mau du lieu
scl=0;
_nop_();//du lieu(sda) thay doi khi scl muc thap/
sda=1;
return Data;//tra gia tri cho ham
}
unsigned long read_24(unsigned char diachi)
{ // doc 4 byte lien tiep tu 24c04

unsigned long Data;
diachi=diachi*4;
start();
write(0xa0);
write(diachi);
start();
write(0xa1);
Data = read(0);
Data = (Data<< 8) + read(0);
Data = (Data<< 8) + read(0);
Data = (Data<< 8) + read(1);
stop();//Stop I2C
return Data;
}
void write_24(unsigned char diachi,unsigned long x)
{ // ghi 4 byte lien tiep x vao 24c04
unsigned char d;
diachi=diachi - 4;
start();
write(0xa0);
write(diachi);
d = x>>24;
write(d);
d = x>>16;
write(d);
d = x>>8;
write(d);
d = x;
write(d);
stop();//Stop I2C
delay(100);
}
void save_ram(void) // luu code tu 24c04 vao ram
{
unsigned char i;
for(i=0;i<5;i++)
du_lieu = read_24(i);
}
void save(void) // luu code tu ram vao 24c04
{
write_24(number_code,data_);
du_lieu[number_code/4 - 1] = data_;
number_code = number_code + 4;
if( number_code >= 24)
{
number_code = 0;
led = 1; // stop luu code.
}
}
/*
void save2(void) // luu code vao ram
{
unsigned char a;
a = number_code /4;
du_lieu[a-1] = data_;
number_code = number_code+4;
if( number_code >= 24)
{
number_code = 0;
led = 1; // stop luu code.
}
}*/
/**********************************************************************************/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// CAC CHUONG TRINH CON GIAI MA REMOTE //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void ngat_0 (void) interrupt 0 //ngat ngoai 0 ( remote )
{
unsigned short do_rong_xung, min;
{
TR0 = 0;// stop timer 0
if(step==0)// bit start
{
step = 1;
data_=0;
TL0 = max_start_0;
TH0 = max_start_1;
TR0 = 1;// khoi dong timer 0
}
else if(step==1)// bit data
{
do_rong_xung = TH0-max_start_1;
do_rong_xung = (do_rong_xung<<8) + TL0-max_start_0;
min = min_start;
if(do_rong_xung > min)
{
step = 2;
TL0 = max_data_0;
TH0 = max_data_1;
TR0 = 1;// khoi dong timer 0
}
else step = 0;
}
else if(step==2)// bit data
{
do_rong_xung = TH0 - max_data_1 ;
do_rong_xung = (do_rong_xung<<8) + TL0- max_data_0;
TL0 = max_data_0;
TH0 = max_data_1;
TR0 = 1;// khoi dong timer 0
data_=data_<<1; // bit 0
if (do_rong_xung > 0x5DC) data_= data_+1;// neu do rong lon hong 1500us thi la bit 1
}}}
void timer_0(void) interrupt 1 //ngat timer 0 de giai ma
{
TR0 =0; //stop timer 0
if (step==1) step = 0; // giai ma that bai, bit start qua dai
else if (step==2) // Bit stop
{
EX0 = 0; // cam ngat ngoai 0 de xu ly ket qua giai ma
step = 3;}}
/**********************************************************************************/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// CAC CHUONG TRINH CON DIEU KHIEN QUAT //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void so_1(void)
{
so_quat=1;
so1=0;
so2=1;
so3=1; timer_so_quay=60;
}
void so_2(void)
{
so_quat=2;
so1=1;
so2=0;
so3=1; timer_so_quay=40;
}
void so_3(void)
{
so_quat=3;
so1=1;
so2=1;
so3=0; timer_so_quay=20;
}
void so_0(void)
{
so_quat=0;
so1=1;
so2=1;
so3=1;
}
void speed(void)
{
led = 0; // led on
if(so_quat==0) so_1();
else if(so_quat==1) so_2();
else if(so_quat==2) so_3();
else
{
led = 1; // led off
so_0();
quay1=1;hienthi_quatquay=0xaa;
}
}
void quay_quat1(void)
{
if(so_quat>0){ quay1 = !quay1;
hienthi_quatquay=~ hienthi_quatquay;}

}

void hoi_nuoc1(void)
{
if(so_quat>0){ hoi_nuoc = !hoi_nuoc;
hienthi_quatquay=~ hienthi_quatquay;}

}
void tat_quat(void)
{
so_0();
quay1 = 1;
quay2 = 1 ;
led = 1;
hoi_nuoc =1;

}

void hen_gio(void)
{

sec_5 =0;
sec_60=0;
min_1=0;
timer= timer+60;
if(timer >= 240)
{
TR1 = 0;
nhay_led();
delay(0xBFF);
timer=0;
}
else TR1 = 1;
}
/**********************************************************************************/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// CAC CHUONG TRINH CON PHUC VU NGAT //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//

void timer1(void) interrupt 3 //ngat
{
TR1 = 0;
TH1 = 0xf8; //2ms
TL1 = 0x2f;
/////////////////////////////
led1=led2=1;
if( so_quat!=0){//quat dang chay
//////////////////////////////////////////////////////
if( select_led==0){
if(hienthi_quatquay==0xaa){ //hien thi không quay quat
if(dem_quay>3){ dem_quay=0 ;}
out_led = dislay_quay_quat[dem_quay];}
///////////////////////////////////////
else{out_led = dislay_quay_so[dem_quay];} //hien thi quay quat

led1=0;
////////////////////////////////////////////
timer_quay++;
if(timer_quay >timer_so_quay){ timer_quay=0;
//
// toc_do_quay<<=1; toc_do_quay= toc_do_quay|0x01;

dem_quay++;
if(hienthi_quatquay==0xaa){
if(dem_quay>3){ dem_quay=0 ;}}
else{ if(dem_quay>5){ dem_quay=0 ;}}

}
}
else if(select_led==1){out_led= M[so_quat];led2=0;}
}
///////////////////////////////////////////////////////
else{ //quat dang tat
if( select_led==0){
out_led= M[0];led1=0;}
else if(select_led==1){out_led= 0x8e;led2=0;} // 0x8e=F
}
/////////////////////////////////////////////////////////
select_led++; if( select_led>1)select_led=0;
/////////////////////////////

TR1 = 1;
}

void timer2(void) interrupt 5 //ngat
{

}
/**********************************************************************************/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// CAC CHUONG TRINH CON XU LY PHIM //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void nut_1(void) // key 1
{
unsigned char c=0;
delay(100);
if(key_1==0)
{
while(key_1 ==0) // doi den khi nha phim
{
c++;
delay(500);
if (c>=130)
{
c=0;
tat_quat();
number_code =4; // cho phep luu code
led=0; // bat led
while(key_1 ==0) ;
goto next1;
}
}
number_code =0;// khong luu code
so_1();
next1:
nhay_led();
}
}
void nut_2(void) // key 2
{
delay(100);
if(key_2==0)
{
while(key_2 ==0){}; // doi den khi nha phim
so_2();
number_code =0;// khong luu code
nhay_led();
}
}
void nut_3(void) // key 3
{
delay(100);
if(key_3==0)
{
while(key_3 ==0){}; // doi den khi nha phim
so_3();
number_code =0;// khong luu code
nhay_led();
}
}
void nut_4(void) // key 4
{
delay(100);
if(key_4==0)
{
while(key_4 ==0){}; // doi den khi nha phim
quay_quat1();
number_code =0;// khong luu code
nhay_led();
}
}
//void nut_5(void) // key 5
//{
// delay(100);
// if(key_5==0)
// {
// while(key_5 ==0){}; // doi den khi nha phim
// quay_quat2();
// number_code =0;// khong luu code
// nhay_led();
//// }
// }
//void nut_6(void) // key 6
// {
// delay(100);
// if(key_6==0)
// {
// while(key_6 ==0){}; // doi den khi nha phim
// hoi_nuoc1();
// number_code =0;// khong luu code
// nhay_led();
// }
// }
void nut_5(void) // key 7
{
delay(100);
if(key_5==0)
{
while(key_5 ==0){}; // doi den khi nha phim
tat_quat ();
number_code =0;// khong luu code
nhay_led();
}
}
/**********************************************************************************/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
// CHUONG TRINH CHINH //
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
void main()
{
cau_hinh();
save_ram();
while(1)
{
if(step==3) //ket qua giai ma
{
step = 0;
if( number_code > 0)
save();
else if( data_ == du_lieu[0]) //remote key 1
so_1();
else if( data_ == du_lieu[1]) //remote key 2
so_2();
else if( data_ == du_lieu[2]) //remote key 3
so_3();
else if( data_ == du_lieu[3]) //remote key 4
quay_quat1();
else if( data_ == du_lieu[4]) //remote key 5
tat_quat();
else if( data_ == du_lieu[5]) //remote key 6
quay_quat();
else if( data_ == du_lieu[6]) //remote key 7
quay_quat2();
else goto thoat;
nhay_led();
thoat:
EX0 = 1;
}
else if(key_1 ==0) //ket qua giai ma
nut_1();
else if(key_2 ==0) //ket qua giai ma
nut_2();
else if(key_3 ==0) //ket qua giai ma
nut_3();
else if(key_4 ==0) //ket qua giai ma
nut_4();
else if(key_5 ==0) //ket qua giai ma
nut_5();
else if(key_6 ==0) //ket qua giai ma
nut_6();
else if(key_7 ==0) //ket qua giai ma
nut_7();

}
}
 

Quảng cáo Google