直流电机转速测量与控制程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
直流电机转速测量与控制程序
#include "reg51.h"
#include "absacc.h"
#define LEDLen 6
#define Tick 10 // 10 x 100us = 1ms
unsigned int C100us; // 100us记数单元
xdata unsigned char CS0832 _at_ 0xb000;
xdata unsigned char OUTBIT _at_ 0x8002; // 位控制口xdata unsigned char OUTSEG _at_ 0x8004; // 段控制口xdata unsigned char IN _at_ 0x8001; // 键盘读入口
unsigned char LEDBuf[LEDLen]; // 显示缓冲
code unsigned char LEDMAP[] = { // 八段管显示码
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};
code unsigned char KeyTable[] = { // 键码定义
0x16, 0x15, 0x14, 0xff,
0x13, 0x12, 0x11, 0x10,
0x0d, 0x0c, 0x0b, 0x0a,
0x0e, 0x03, 0x06, 0x09,
0x0f, 0x02, 0x05, 0x08,
0x00, 0x01, 0x04, 0x07
};
long int n;
unsigned char flag;
unsigned char DA;
unsigned char Key_temp;
unsigned char k_TEMP[2];
void time1_int(void);
void delay(unsigned char CNT)
{
unsigned char i;
while (CNT-- !=0)
for (i=50; i !=0; i--);
}
unsigned char TestKey()
{
OUTBIT = 0; // 输出线置为0
return (~IN & 0x0f); // 读入键状态(高四位不用) }
unsigned char GetKey()
{
unsigned char Pos;
unsigned char i;
unsigned char k;
i = 6;
Pos = 0x20; // 找出键所在列
do {
OUTBIT = ~ Pos;
Pos >>= 1;
k = ~IN & 0x0f;
} while ((--i != 0) && (k == 0));
// 键值= 列x 4 + 行
if (k != 0) {
i *= 4;
if (k & 2)
i += 1;
else if (k & 4)
i += 2;
else if (k & 8)
i += 3;
OUTBIT = 0;
do delay(10); while (TestKey()); // 等键释放
return(KeyTable[i]); // 取出键码
} else return(0xff);
}
void DisplayLED()
{
unsigned char i;
unsigned char Pos;
unsigned char LED;
Pos = 0x20; // 从左边开始显示for (i = 0; i < LEDLen; i++) {
OUTBIT = 0; // 关所有八段管LED = LEDBuf[i];
OUTSEG = LED;
OUTBIT = Pos; // 显示一位八段管delay(5);
Pos >>= 1; // 显示下一位
}
OUTBIT = 0; // 关所有八段管}
void time1_int(void) interrupt 3
{
C100us--;
if (C100us == 0)
{
C100us = Tick;
TR0 = 0;
TR1 = 0;
n=TH0*256+TL0;
TH0 = 0;
TL0 = 0;
TH1 = 255 - 50;
TL1 = 255 - 50;
flag = 1;
TR0 = 1;
TR1 = 1;
}
}
void main()
{
unsigned char i = 0;
//TMOD = 0X05; // T0计数方式C100us = Tick;
n = 0;
TMOD = 0X15;
TH0 = 0;
TL0 = 0;
TH1 = 255 - 50; // 定时100US
TL1 = 255 - 50;
ET1 = 1; // 开中断
EA = 1;
while(1){
if (TestKey())
{
Key_temp = GetKey() & 0x0f ;
LEDBuf[i] = LEDMAP[Key_temp];
k_TEMP[i] = Key_temp;
i++;
if(i==2)
{
i = 0;
break;
}
}
}
Key_temp = k_TEMP[0]*10+k_TEMP[1];
DA = 0x2f;
CS0832 = DA;
TR0 = 1;
TR1 = 1;
while(1){
while(flag)
{