使用kiel c程序和89c51芯片或89c52芯片帮我写一份单片机红外控制1个灯亮灭的程序,要求按一下按键亮,按一下另一按键灭。
时间: 2024-02-12 16:04:36 浏览: 18
好的,以下是基于Kiel C的89c51/89c52芯片单片机红外控制灯亮灭的程序:
```c
#include <regx51.h>
#define IR_PIN P3_2
#define LED_PIN P2_0
#define KEY1_PIN P1_0
#define KEY2_PIN P1_1
unsigned char ir_code = 0;
void delay(unsigned int count) {
unsigned int i;
for(i = 0; i < count; i++);
}
void ir_init() {
TCON &= 0x0F; //设置T0为模式1
TMOD &= 0xF0; //设置T1为模式1
TMOD |= 0x01;
TH0 = 0x00;
TL0 = 0x00; //设置计数初值
TH1 = 0x00;
TL1 = 0x00;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
EA = 1;
}
void ir_interrupt() interrupt 3 {
static unsigned char bit_cnt = 0;
static unsigned int ir_data = 0;
if(!IR_PIN) {
TH0 = 0;
TL0 = 0;
bit_cnt = 0;
ir_data = 0;
} else {
ir_data <<= 1;
ir_data |= 1;
bit_cnt++;
if(bit_cnt == 16) {
ir_code = ir_data & 0xFF;
bit_cnt = 0;
ir_data = 0;
}
}
}
void main() {
ir_init();
while(1) {
if(KEY1_PIN == 0) {
LED_PIN = 1;
while(KEY1_PIN == 0);
}
if(KEY2_PIN == 0) {
LED_PIN = 0;
while(KEY2_PIN == 0);
}
if(ir_code == 0x45) {
LED_PIN = !LED_PIN;
ir_code = 0;
}
delay(1000);
}
}
```
这份程序在之前的基础上,增加了对两个按键的检测。当按下KEY1时,LED将亮起来,当按下KEY2时,LED将熄灭,而当接收到红外信号0x45时,LED的状态将翻转。