# include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar count; //0.5ms次数标识
sbit pwm = P3^0; //pwm信号输出
sbit jia = P3^7; //角度增加按键检测I/O口
sbit jian = P3^6; //角度减小按键检测I/O口
uchar jd; //角度标志位
sbit dula = P2^6;
sbit wela = P2^7;
uchar code table[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d,
0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
void delay(uchar i)
{
uchar j, k;
for (j=i; j>0; j--)
for (k=125; k>0; k--);
}
void
time0_Init() //定时器初始化
{
TMOD = 0x01; //定时器0工作方式1
IE = 0x82;
TH0 = 0xfe;
TL0 = 0x33; //11.0592MHz晶振,0.5ms
TR0 = 1; //定时器开始
}
void Time0_Int() interrupt 1
{
TH0 = 0xfe;
TL0 = 0x33;
if (count < jd) //判断0.5ms次数是否小于角度标志
pwm = 1;
else
pwm = 0; //大于则输出低电平
++ count;
count%= 40; //次数保证为40,保持周期20ms
}
void keyscan() //键盘扫描函数
{
if (0 == jia)
{
delay(10);
if (0 == jia)
{
++ jd;
count = 0;
if (6 == jd)
jd = 5;
while (!jia);
}
}
if (0 == jian)
{
delay(10);
if (0 == jian)
{
-- jd;
count = 0;
if (0 == jd)
jd = 1;
while (!jian);
}
}
}
void display() //数码管显示函数
{
uchar bai, shi, ge;
switch (jd)
{
case 1:
bai = 0;
shi = 0;
ge = 0;
break;
case 2:
bai = 0;
shi = 4;
ge = 5;
break;
case 3:
bai = 0;
shi = 9;
ge = 0;
break;
case 4:
bai = 1;
shi = 3;
ge = 5;
break;
case 5:
bai = 1;
shi = 8;
ge = 0;
break;
}
dula = 0;
P0 = table[bai];
dula = 1;
dula = 0;
wela = 0;
P0 = 0xfe;
wela = 1;
wela = 0;
delay(5);
P0 = table[shi];
dula = 1;
dula = 0;
// wela = 0;
P0 = 0xfd;
wela = 1;
wela = 0;
delay(5);
P0 = table[ge];
dula = 1;
dula = 0;
// wela = 0;
P0 = 0xfb;
wela = 1;
wela = 0;
delay(5);
}
void main()
{
jd = 3;
count = 0;
Time0_Init();
while(1)
{
keyscan();
display();
}
}
舵机为什么不动呀?愁人!
一周热门 更多>