功能如下:
1、通過按鍵調(diào)節(jié)燈的亮度(1-10檔位)
3、感應(yīng),根據(jù)附近是否有人自動進行開關(guān)燈。
#include <reg52.h>
#include "pwm.h"
char PWM = 1; //1-10檔位
void time0_init(void)
{
TMOD |= 0x02; //定時器0 8位自動重裝
TH0 = 0xc8;
TL0 = 0;
EA = 1; //總中斷
ET0 = 1; //TIME0 中斷
TR0 = 1; //開啟定時器0
IN2 = 0;
}
/* 關(guān)閉臺燈 */
void Lamp_Off()
{
TR0 = 0;
ET0 = 0;
IN1 = 0;
}
/*開啟臺燈 */
void Lamp_On()
{
TR0 = 1;
ET0 = 1;
}
void time0() interrupt 1 //200us 中斷一次 周期1ms PWM頻率1KHz
{
static unsigned int num = 0;
num++;
if(num > 9)
num = 0;
if(num < PWM)
IN1 = 1;
else IN1 = 0;
}
#include <reg52.h>
#include "uart.h"
#include "pwm.h"
extern uchar Lamp_OnOff;
extern uchar Reaction_OnOff;
unsigned char Receive;
void UART_init(void)
{
TMOD |= 0x20; //T1工作模式2 8位自動重裝
TH1 = 0xfd;
TL1 = 0xfd; //比特率9600
TR1 = 1; //啟動T1定時器
SM0 = 0;
SM1 = 1; //串口工作方式1 10位異步
REN = 1; //串口允許接收
EA = 1; //開總中斷
ES = 1; //串口中斷打開
}
void UART(void) interrupt 4
{
TR0 = 0; //關(guān)閉定時器中斷,防止其影響接收數(shù)據(jù)
ET0 = 0;
if(RI) //檢測是否接收完成
{
Receive = SBUF; //取出接收緩存器的值
RI = 0;
switch(Receive)
{
case 'A': PWM++; break;
case 'S': PWM--; break;
case 'O': Lamp_On(); Lamp_OnOff = 1; break;
case 'F': Lamp_Off(); Lamp_OnOff = 0; break;
case 'M': Reaction_OnOff = ~Reaction_OnOff; break;
default : break;
}
if(PWM > 10)
PWM = 10;
else if(PWM < 1)
PWM = 1;
SBUF = PWM + '0'; //返回當前檔數(shù)
while(!TI);
TI = 0;
}
if(Lamp_OnOff)
{
while(*p)
{
SBUF = *p++;
while(!TI);
TI = 0;
}
}
void Uart_SendByte(unsigned char num)
{
SBUF = num;
while(!TI);
TI = 0;
}
資料借鑒于此紛傳
閱讀全文