一 电路
二 蜂鸣器与继电器工作原理
2.1蜂鸣器与继电器
2.2 十六进制与二进制
二进制 | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
十六进制 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
2.3非门
二 代码
工程目标:实现8路led灯闪烁3遍后熄灭,依次点亮led灯,继电器吸合,led灯依次熄灭,蜂鸣器鸣叫一会儿后关闭,循环进行。
代码1
#include "reg52.h"sbit HC138_A = P2^5;
sbit HC138_B = P2^6;
sbit HC138_C = P2^7;void Delay(unsignde int t)
{while(t--);while(t--);
}//实现延时功能的函数void jidianqiyufengmingqi()
{unsigned char i;//定义char型变量HC138_C = 1;HC138_B = 0;HC138_A = 0;//输出Y4for(i=0;i<3;i++){P0 = 0x00;//实现了全亮Delay(60000);Delay(60000);//延时P0 = 0xff;//实现了全灭Delay(60000);Delay(60000);//延时}由全灭到全亮,循环3次for(i=0;i<=8;i++){P0 = 0xff<<i;//实现了由全灭到全亮Delay(60000);Delay(60000);//延时}实现了由全灭,再逐个点亮HC138_C = 1;HC138_B = 0;HC138_A = 1;//输出Y5P0 = 0x10;Delay(60000);//继电器吸合Delay(60000);//延时P0 = 0x00;//继电器关闭for(i=0;i<=8;i++){P0 = 0x00<<i;//实现了由全亮到全灭Delay(60000);Delay(60000);//延时}实现了由全亮,再逐个灭HC138_C = 1;HC138_B = 0;HC138_A = 1;//输出Y5P0 = 0x40;//蜂鸣器鸣叫Delay(60000);Delay(60000);//延时P0 = 0x00;//蜂鸣器关闭}//实现了又全亮到全灭,再逐个亮起,继电器工作,led灯再逐个灭掉,蜂鸣器又工作的函数功能void InitSystem()
{HC138_C = 1;HC138_B = 0;HC138_A = 1;//输出Y5P0 =0x00;
}void main()
{InitSystem()while(1)
{jidianqiyufengmingqi();
}
代码2
#include "reg52.h"sbit HC138_A = P2^5;
sbit HC138_B = P2^6;
sbit HC138_C = P2^7;void Delay(unsignde int t)
{while(t--);while(t--);
}//实现延时功能的函数void InitHC138(unsigned char n)
{switch (n){ case 4;HC138_C = 1;HC138_B = 0;HC138_A = 0;//输出Y4break;case 5;HC138_C = 1;HC138_B = 0;HC138_A = 1;//输出Y5break;}
}void jidianqiyufengmingqi()
{unsigned char i;//定义char型变量InitHC138(4);for(i=0;i<3;i++){P0 = 0x00;//实现了全亮Delay(60000);Delay(60000);//延时P0 = 0xff;//实现了全灭Delay(60000);Delay(60000);//延时}由全灭到全亮,循环3次for(i=0;i<=8;i++){P0 = 0xff<<i;//实现了由全灭到全亮Delay(60000);Delay(60000);//延时}实现了由全灭,再逐个点亮InitHC138(5);P0 = 0x10;Delay(60000);//继电器吸合Delay(60000);//延时P0 = 0x00;//继电器关闭InitHC138(4);for(i=0;i<=8;i++){P0 = 0x00<<i;//实现了由全亮到全灭Delay(60000);Delay(60000);//延时}实现了由全亮,再逐个灭InitHC138(5);P0 = 0x40;//蜂鸣器鸣叫Delay(60000);Delay(60000);//延时P0 = 0x00;//蜂鸣器关闭}//实现了又全亮到全灭,再逐个亮起,继电器工作,led灯再逐个灭掉,蜂鸣器又工作的函数功能void InitSystem()
{HC138_C = 1;HC138_B = 0;HC138_A = 1;//输出Y5P0 =0x00;
}void main()
{InitSystem()while(1)
{jidianqiyufengmingqi();
}