|
#include < reg51.h >
#include <intrins.h>
unsigned char code LED7Code[] = {~0x40,~0x79,~0x24,~0x30,~0x19,~0x12,~0x02,~0x78,~0x00,~0x10,~0x88};
unsigned char TIM0H;
unsigned char TIM0L;
unsigned int NUM;
void delay(unsigned int i);
sbit K1=P2^7; //++
sbit K2=P2^6; //--
//P0 口为 PPM 舵机信号输出
//P1 口接1位数码管输出
/*********************************************************
主函数
**********************************************************/
void main()
{
TMOD=0x11;
TH1=0xb1;
TL1=0xe0;
EA=1;
ET1=1; //允许定时/计数器1 中断
TR1=1; //启动定时/计数器1 中断
TH0=0xfa; //1.5ms定时
TL0=0x24;
ET0=1;
TR0=1;
NUM=1500;
while(1)
{
if(K1==0)
{
delay(5);
if(K1==0) NUM++;
if(NUM>=2000)NUM=2000;
}
if(K2==0)
{
delay(5);
if(K2==0) NUM--;
if(NUM<=1000)NUM=1000;
}
P1=~LED7Code[((NUM-1000)/100)];
TIM0H=((65536-NUM)/256);
TIM0L=((65536-NUM)%256);
}
}
/*********************************************************
中断服务函数
**********************************************************/
void Time1(void) interrupt 3
{
TH1=0xb1; //20ms定时
TL1=0xe0;
P0=~P0;
TR0=1;
}
void Time0(void) interrupt 1
{
TH0=TIM0H;
TL0=TIM0L;
P0=~P0;
TR0=0;
}
void delay(unsigned int i)
{
char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}
|
|