该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
nclude #include #include "i2c.h"#include "delay.h"sbit HotW_key=P1^0; //引脚定义初始化sbit JuLi1_key=P1^1;sbit hotLed=P1^2;sbit Deng_led=P1^4;sbit buzzer=P3^4;unsigned long times_20ms=0;//系统时间计数unsigned char pwmVal = 5;//pwm值初始化unsigned char firstIn=0; //pwm计数初始化unsigned char juliFlag=0;//距离控制标志unsigned int Juli_num=0;//距离检测计数unsigned char openLed=0; //打开led标志unsigned long laterNum = 100; //延时计数void Init_Timer0(void);//定时器初始化void UART_Init(void);void SendByte_U(unsigned char dat);void SendStr_U(unsigned char *s,unsigned char length);void main (void){unsigned char num=0;Init_Timer0(); //定时器0初始化UART_Init();DelayMs(50); //延时有助于稳定Deng_led=1;while(1) //主循环{if(HotW_key==1) //检测到红外信号{hotLed = 0;//检测到人体指示灯num=ReadADC(0);//读取AD检测到的光敏的值if(num>=90)//光线交暗{if((juliFlag == 0))//距离需要打开led{ openLed = 1;//打开led标志}}else{openLed = 0; //led打开标志hotLed = 1;//人体指示灯}}else//检测不到人{openLed = 0; //led打开标志}if(JuLi1_key==0) //距离检测{DelayMs(10);buzzer=0;if(Juli_num< 200 )Juli_num++;else{ juliFlag =1 ; //距离过进标志openLed = 0;//打开led标志}}else{juliFlag = 0 ; //距离过进标志buzzer=1;//关闭蜂鸣器Juli_num = 0;}}}void Init_Timer0(void){TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响 TH0=(65536-10000)/256; //重新赋值 20msTL0=(65536-10000)%256;EA=1; //总中断打开ET0=1; //定时器中断打开TR0=1; //定时器开关打开}void UART_Init(void){ SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收 TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装 TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHzTL1 = TH1; TR1 = 1; // TR1: timer 1 打开 EA = 1; //打开总中断 ES = 1; //打开串口中断}void SendByte_U(unsigned char dat){unsigned char time_out;time_out=0x00;SBUF = dat; //将数据放入SBUF中while((!TI)