|
- //*******************函数声明,变量定义*******************
- #include <reg51.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- //端口定义
- sfr p0=0x80;
- sfr p1=0x90;
- sfr p2=0xa0;
- sfr p3=0xb0;
- //键
- sbit add_speed=P1^0;
- sbit sub_speed=P1^1;
- //PWM控速
- uint time_PWM=0; //PWM时间计数
- uint high_PWM=0; //PWM高电平时间
- uint t_PWM=16; //PWM周期
- //*******************延 时 程 序*******************
- void delayms(unsigned char ms)
- {
- unsigned char i ;
- while(ms--)
- {
- for(i = 0 ; i < 120 ; i++) ;
- }
- }
- //*******************键盘扫描程序*******************
- void key_change()
- {
- if(add_speed==0)
- {
- delayms(10);
- if(add_speed==0)
- {
- if(high_PWM < 16)
- {
- high_PWM++;
- } } }
- if(sub_speed==0)
- {
- delayms(10);
- if(sub_speed==0)
- {
- if(high_PWM > 0)
- {
- high_PWM--;
- } } } }
- //*******************中断程序*******************
- //PWM波生成,T0中断
- void PWM_T0(void) interrupt 1
- {
- TH0=0Xd8;
- TL0=0Xf0;
- time_PWM++;
- if(time_PWM < high_PWM)
- {
- p2=0xff;
- }
- if(time_PWM==high_PWM)
- {
- p2=0x00;
- }
- else if(time_PWM==t_PWM)
- { p2=0xff;
- time_PWM=0;
- } }
- //*******************主程序*******************
- void main()
- {p0=0xff;
- p1=0Xff;
- p2=0xff;
- p3=0xff;
- TH0=0xd8; //定时10us
- TL0=0xf0;
- TMOD=0x01;
- EA=1;
- ET0=1;
- TR0=1;
- while(1)
- { key_change();
- }}
复制代码
|
|