#include <reg51.h> sbit lamp_pwm = P2^3; sbit key0 = P3^0; sbit key1 = P3^1; #define uint unsigned int #define uchar unsigned char uint inc = 8000; uint flag=0; void main() { TMOD |= 0x01; IT0 = 1; EX0 = 1; ET0 = 1; EA = 1; while (1) ; } void INT0s() interrupt 0 using 0 { TR0 = 0; if (!key0) // liangdu- { inc += 50; if (inc >= 8000) { inc = 8000; } } if (!key1) //liangdu+ { inc -= 50; if (inc <= 800) { inc = 800; } } TH0 = (65536 - inc) / 256; TL0 = (65536 - inc) % 256; TR0 = 1; } void timer1() interrupt 1 using 0 { unsigned char q; lamp_pwm = 0; for (q = 0; q < 2; q++); lamp_pwm = 1; }在此代码中加入一个总开关,负责灯炮pwm的开关
时间: 2024-04-28 09:26:09 浏览: 200
可以在主函数中加入一个变量来表示总开关的状态,然后在INT1的中断函数中判断总开关状态,根据状态来控制灯炮PWM的开关。代码如下:
#include <reg51.h>
sbit lamp_pwm = P2^3;
sbit key0 = P3^0;
sbit key1 = P3^1;
sbit switch = P3^2; // 总开关
#define uint unsigned int
#define uchar unsigned char
uint inc = 8000;
uint flag = 0;
bit switch_state = 1; // 总开关状态,默认为开
void main()
{
TMOD |= 0x01;
IT0 = 1;
EX0 = 1;
ET0 = 1;
EA = 1;
while (1)
{
switch_state = switch; // 读取总开关状态
}
}
void INT0s() interrupt 0 using 0
{
TR0 = 0;
if (!key0) // 亮度+
{
inc += 50;
if (inc >= 8000)
{
inc = 8000;
}
}
if (!key1) // 亮度-
{
inc -= 50;
if (inc <= 800)
{
inc = 800;
}
}
TH0 = (65536 - inc) / 256;
TL0 = (65536 - inc) % 256;
TR0 = 1;
}
void timer1() interrupt 1 using 0
{
if (switch_state) // 总开关为开
{
unsigned char q;
lamp_pwm = 0;
for (q = 0; q < 2; q++);
lamp_pwm = 1;
}
}
在以上代码中,我们新增了一个sbit变量switch,表示总开关的状态。在主函数中,我们通过读取P3口的第二位来获取总开关状态。在timer1中断函数中,我们加入了对总开关状态的判断,只有当总开关为开时,才执行灯炮PWM的开关。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![text/plain](https://img-home.csdnimg.cn/images/20250102104920.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)