#include<reg51.h> //库文件定义
#define uchar unsigned char //宏定义uchar
sbit LED=P1^0; //台灯控制端
sbit ONOFF=P2^0; //开关控制端
sbit ADD=P2^1; //亮度增加
sbit DEC=P2^2; //亮度降低
uchar Brightness; //256位的亮度调节
bit MARK; //台灯开关标志位
void delay1ms(unsigned int a) {
uchar i;
while(--a!=0)
{
for(i=0;i<125;i++);
}
}
void delay(unsigned int d) {
uchar i;
while(--d!=0)
{
for(i=0;i<2;i++);
}
}
void drive(void) {
unsigned int a;
a=Brightness;
LED=0;
delay(a);
a=~a;
LED=1;
delay(a);
a=~a;
}
void open(void){
if(ONOFF==0)
{
delay1ms(20);
if(ONOFF==0)
{
MARK=1;
while(ONOFF==0);
}
}
} //去抖动 如果开关 开启 将MARK置1
void cloos(void){
if(ONOFF==0)
{
delay1ms(20);
if(ONOFF==0)
{
MARK=0;
while(ONOFF==0);
}
} } //去抖动 如果开关 关闭 将MARK置0
void ADD0(void){
if(ADD==0)
{
Brightness++;
if(Brightness>254)
{
Brightness=254;
}
}
}
void DEC0(void){
if(DEC==0)
{
Brightness--;
if(Brightness<1)
{
Brightness=1;
}
}
}
void int0(void){
Brightness=130;
MARK=0;
LED=1;
ONOFF=1;
ADD=1;
DEC=1;
}
void main(void){
int0();
while(1){
if(MARK==0)
{
cloos();
}
if(MARK==1)
{
drive();
open();
ADD0();
DEC0();
}
}
}
这是一个调光电路和程序 ,可是不知道怎么回事 按下ONOFF电路没反应,谢谢大家帮忙啊 |