#include "reg52.h" //此文件中定义了单片机的一些特殊功能寄存器
typedef unsigned int u16; //对数据类型进行声明定义 typedef unsigned char u8;
sbit k1=P1^0; sbit k2=P1^1; sbit led=P0^0; sbit k3=P1^2; u16 count,value,timer1;
void delayms(u16 ms) { int i,j; for(i=ms;i>0;i--) for(j=110;j>0;j--); } void Timer1Init() { TMOD|=0X10;//选择为定时器1模式,工作方式1,仅用TR1打开启动。
TH1 = 0xFF; TL1 = 0xff; //1us
ET1=1;//打开定时器1中断允许 EA=1;//打开总中断 TR1=1;//打开定时器 }
void tiaoguang() { if(count>100) { count=0; if(k1==0) //DIR控制增加 { value++; } if(k2==0) { value--; }
}
if(value>=990) { value=990; } if(value<=10) { value=10; }
if(timer1>1000) //PWM周期为1000*1us { timer1=0; }
if(timer1 <value) { led=1; } else { led=0; } }
void main() { u8 a; Timer1Init(); //定时器1初始化 while(1) { if(k3==0) { delayms(10); if(k3==1) a++; if(a>1) a=0; } switch(a) { case(0): tiaoguang() ;break;//恢复亮度 case(1): led=0 ;break;//熄灭LED
} } }
void Time1(void) interrupt 3 //3 为定时器1的中断号 1 定时器0的中断号 0 外部中断1 2 外部中断2 4 串口中断 { TH1 = 0xFF; TL1 = 0xff; //1us timer1++; count++;
} 程序运行的时候不能关灯,用实验板进行实验的,调光正常,但就是不能关灯,不明白哪里错了,像大神们请教下
|