#include <reg52.h>
sbit SEV_PWM = P1^0; //设定PWM输出的I/O端口
unsigned char count = 0;
unsigned char SEV_conut =8;
sbit K6=P1^1;//设置两个按键,代表抬起和按下。根据实际硬件资源编写。
sbit K7=P1^2;
sbit LED=P1^3; /*延时程序*/
void delay(unsigned char i)
{
unsigned char j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
/*定时器T0初始化*/
void Com_Init() {
TMOD &= 0x00;
TMOD |= 0x01; //定时器T0设置成方式1
TH0 = 0xff; //定时常数 0.1ms 晶振为11.0592MHz
TL0 = 0xa4;
ET0 = 1;
TR0 = 1;
EA=1;
} /*T0中断初始化*/
void Time0_Init() interrupt 1 {
TR0 = 0;
TH0 = 0xff; //重装时间常数 0.1ms
TL0 = 0xa4;
if(count <= SEV_conut) //5==0° 10==90°
{
SEV_PWM = 1;
}
else {
SEV_PWM = 0;
}
count++;
if (count >= 200) //T = 20ms清零
{
count = 0;
}
TR0 = 1; //开启T0
}
void main(){
Com_Init();
while(1) {
if(K6==0)
{
delay(10);
if(K6==0)
{
SEV_conut =5;//5代表0.5ms
count=0;
}
}
if(K7==0)
{
delay(10);
if(K7==0)
{
SEV_conut =8;
count=0;
}
}
//SEV_conut =5;//舵机恢复到0°的位置
//count=0;//让定时器重新计数
//约延时2s
//SEV_conut =8;//8这个值是作者调试的角度,得到现象是触控笔向下按压屏幕
//count=0;
//Delay(100000);
//延时函数写得不好,读者可改写。
}
}
|