完成功能:
数码管显示 ‘键盘输入角度-实际运动角度-运动状态-速度等级’
键盘控制 步进电机 左转 右转 暂停 加减速
键盘输入0~9 ENTER可使步进电机运动到指定位置
这里用38译码器作为IO口扩展芯片
74HC245作为IO扩展芯片传送门,点击跳转
#include
#include
#define LED_port XBYTE[0x7FFF]
#define uint unsigned int
#define uchar unsigned char
// 步进电机定义
#define out P2
uchar code turn[] = {0xf3, 0xf2, 0xf6, 0xf4, 0xfc, 0xf8, 0xf9, 0xf1}; //单双八拍
uchar code speed[] = {1, 5, 10, 15, 20, 30, 40, 50}; //转动间隔——转动速度
uchar i = 0, flag = 0, sp = 3, count = 0, Angle = 0, Sort = -1;
uchar F = 0, S = 0, enter = 0;
void turnRight();
void turnLeft();
void adjustState();
void stepper();
void angleEnter();
// LED定义
uchar dispcode[] =
{0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71,
0x40, 0xf3, 0xF7, 0xb8}; //为实现视觉效果‘键值-扫描值’_'-''P''A(R)''L'
uchar dat[10] = {15, 15, 15, 0, 0, 0, 0, 0};
uchar active = 0;
void ShowLED();
// 键盘定义
uchar line, row, kvalue = 0, kscan, kscanA = 0, kscanB = 0;
uchar keyRowLine();
void getKeyVaule();
void delay1ms(uchar n) //定时n ms
{
uint t = n * 1000;
TH1 = (65536 - t) / 256;
TL1 = (65536 - t) % 256;
TR1 = 1;
while (TF1 != 1)
{
/* code */
}
TF1 = 1;
}
void main()//主函数
{
out = turn[i]; //初始状态定在0度
TMOD = 0x11;
TH0 = (65536 - 1000) / 256;
TL0 = (65536 - 1000) % 256;
EA = 1;
ET0 = 1;
TR0 = 1;
while (1)
{
getKeyVaule(); //获得键值,调整标志位
adjustState(); //由标志位调整速度(每次换向的时间间隔)
if (count >= speed[sp])
{
stepper(); //步进电机左转,右转,停止。
count = 0;
}
if (enter)
{ //ENTER键按下步进电机运动到指定位置
angleEnter();
}
}
}
void timer0() interrupt 1
{
TH0 = (65536 - 10000) / 256;
TL0 = (65536 - 10000) % 256;
ShowLED();
count++;
}
// LED显示
void ShowLED()
{
P3 = P3 | 0xf7; //关显示 消隐
P3 = active | 0xf8; //低三位接38译码器,其他位置1
LED_port = dispcode[dat[active]];
active = active < 8 ? active + 1 : 0;
}
void turnRight()//右转函数
{
i = i < 8 ? i + 1 : 0;
out = turn[i];
}
void turnLeft()//左转函数
{
i = i > 0 ? i - 1 : 7;
out = turn[i];
}
// 行列反转法
uchar keyRowLine()
{
uchar before;
P1 = 0x0f;
line = P1;
if (line != 0x0f)
{
/* 判断行状态 */
delay1ms(20);
line = P1;
if (line != 0x0f)
{
/* 有按键按下 */
line = line & 0x0f;
P1 = 0xf0;
delay1ms(1); //等待变化时间
row = P1;
row = row & 0xf0;
kscan = line | row;
before = P1;
while (P1 == before)
{
/* code */
}
Sort = Sort < 3 ? Sort + 1 : 0;
return kscan;
}
}
return 0xff;
}
// 速度控制
void adjustState()
{
if (F)
{
sp = sp > 0 ? sp - 1 : 7;
}
else if (S)
{
sp = sp < 8 ? sp + 1 : 0;
}
}
// 回车Enter控制角度
void angleEnter()
{
uint allAngle, showAngle;
allAngle = 100 * dat[0] + 10 * dat[1] + dat[2]; //输入的角度
i = ((allAngle % 360) / 45); //单双八拍 步距角45度
out = turn[i];
showAngle = 45 * i; //对应步进电机运动角度
dat[3] = showAngle / 100; //显示百位
dat[4] = (showAngle % 100) / 10; //显示百十位
dat[5] = showAngle % 10; //显示个位
}
// 步进电机开启暂停
void stepper()
{
if (flag == 1)
{
turnRight();
}
else if (flag == 2)
{
turnLeft();
}
else if (flag == 0)
{
i = i;
}
}
// 获得键编码,调整标志位
void getKeyVaule()
{
uchar Kscan;
Kscan = keyRowLine();
switch (Kscan)
{
case 0xee:
flag = 1;
break;
case 0xde:
flag = 2;
break;
case 0xbe:
flag = 0;
break;
case 0x7e:
enter = 1;
break;
case 0xed:
F = 1;
break;
case 0xdd:
S = 1;
break;
case 0xbd:
Angle = 0;
break;
case 0x7d:
Angle = 1;
break;
case 0xeb:
Angle = 2;
break;
case 0xdb:
Angle = 3;
break;
case 0xbb:
Angle = 4;
break;
case 0x7b:
Angle = 5;
break;
case 0xe7:
Angle = 6;
break;
case 0xd7:
Angle = 7;
break;
case 0xb7:
Angle = 8;
break;
case 0x77:
Angle = 9;
break;
default:
F = 0;
S = 0;
break;
}
dat[Sort] = Angle; //前三位显示输入的角度
dat[6] = (17 + flag); //显示步进电机运动状态
dat[7] = sp; //显示运动速度等级
}