做了一个单片机按键控制步进电机程序,将addendt和num定义为int或long型,均不能正常工作,定义为char型,没问题。求大侠给予指导,在下先谢了!!
代码如下:
- #include<reg52.h>
- sbit KEYIN_4 = P2^7; //矩阵键盘
- sbit KEYIN_3 = P2^6;
- sbit KEYIN_2 = P2^5;
- sbit KEYIN_1 = P2^4;
- sbit KEYOUT_1 = P2^3;
- sbit KEYOUT_2 = P2^2;
- sbit KEYOUT_3 = P2^1;
- sbit KEYOUT_4 = P2^0;
- sbit ADDR0 = P1^0; //三八译码器选通
- sbit ADDR1 = P1^1;
- sbit ADDR2 = P1^2;
- sbit ADDR3 = P1^3;
- sbit ENLED = P1^4;
- unsigned char code KeyCodeMap[4][4] = { //矩阵按键编号到标准键盘键码的映射表
- { 0x31, 0x32, 0x33, 0x26 }, //数字键 1、数字键 2、数字键 3、向上键
- { 0x34, 0x35, 0x36, 0x25 }, //数字键 4、数字键 5、数字键 6、向左键
- { 0x37, 0x38, 0x39, 0x28 }, //数字键 7、数字键 8、数字键 9、向下键
- { 0x30, 0x1B, 0x0D, 0x27 } //数字键 0、ESC 键、 回车键、 向右键
- };
- unsigned char code BeatCode[8] = { //步进电机节拍对应的 IO 控制代码
- 0xE, 0xC, 0xD, 0x9, 0xB, 0x3, 0x7, 0x6};
- unsigned char code LedChar[] = {
- 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
- 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E};
- unsigned char LedBuff[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
- unsigned char KeySta[4][4] = {
- {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}};
- signed long beats = 0; //电机总节拍数
- bit dir = 0; //电机正反转标志
- void KeyDriver();
- void main()
- {
- EA = 1;
- ENLED = 0;
- ADDR3 = 1;
- LedBuff[0] = LedChar[0];
- TMOD = 0x01;
- TH0 = 0xFC;
- TL0 = 0x67;
- ET0 = 1;
- TR0 = 1;
- while(1)
- {
- KeyDriver();
- }
- }
- void ShowNumber(unsigned char num) //数码管显示函数
- {
- signed char i;
- unsigned char buf[6];
- for(i=0; i<3; i++) //分离转动圈数暂存
- {
- buf[i] = num % 10;
- num = num / 10;
- }
- for(i=3; i>=1; i--) //高位是零不显示
- {
- if(buf[i] == 0)
- LedBuff[i] = 0xFF;
- else
- break;
- }
- if(dir != 0) //反转时圈数前面加负号
- {
- if(i < 3)
- LedBuff[i+1] = 0xBF;
- }
- for( ; i>=0; i--) //显示电机转动圈数
- LedBuff[i] = LedChar[buf[i]];
- }
- void StartMotor(signed long angle) //电机启动函数
- {
- EA = 0;
- beats = angle * 4076 / 360;
- EA = 1;
- }
- void StopMotor() //电机停止函数
- {
- beats = 0;
- }
- void KeyAction(unsigned char keycode) //按键动作函数
- {
- static unsigned char addend = 0;
- if((keycode >= 0x30) && (keycode <= 0x39))
- {
- addend = (addend * 10) + (keycode - 0x30);
- if(addend > 90)
- addend = 0;
- ShowNumber(addend);
- }
- else if(keycode == 0x26)
- {
- dir = 0;
- ShowNumber(addend);
- }
- else if(keycode == 0x28)
- {
- dir = 1;
- ShowNumber(addend);
- }
- else if(keycode == 0x25)
- StartMotor(90);
- else if(keycode == 0x27)
- StartMotor(-90);
- else if(keycode == 0x0D)
- {
- if(dir == 0)
- {
- StartMotor(360 * addend);
- }
- else
- {
- StartMotor(- 360 * addend);
- }
- }
- else if(keycode == 0x1B)
- {
- StopMotor();
- addend = 0;
- ShowNumber(addend);
- }
- }
- void KeyDriver() //按键驱动函数
- {
- unsigned char i, j;
- static unsigned char backup[4][4] = {
- {1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}};
- for(i=0; i<4; i++)
- {
- for(j=0; j<4; j++)
- {
- if(KeySta[i][j] != backup[i][j])
- {
- if(backup[i][j] == 0)
- {
- KeyAction(KeyCodeMap[i][j]);
- }
- backup[i][j] = KeySta[i][j];
- }
- }
- }
- }
- void KeyScan() //按键扫描函数
- {
- unsigned char i;
- unsigned char keybuf[4][4];
- static unsigned char keyout = 0;
- keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEYIN_1;
- keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEYIN_2;
- keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEYIN_3;
- keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEYIN_4;
- for(i=0; i<4; i++)
- {
- if((keybuf[keyout][i] & 0x0F) == 0x0F)
- KeySta[keyout][i] = 1;
- else if((keybuf[keyout][i] & 0x0F) == 0x00)
- KeySta[keyout][i] = 0;
- }
- keyout++;
- keyout = keyout & 0x3;
- switch(keyout)
- {
- case 0: KEYOUT_4 = 1; KEYOUT_1 = 0; break;
- case 1: KEYOUT_1 = 1; KEYOUT_2 = 0; break;
- case 2: KEYOUT_2 = 1; KEYOUT_3 = 0; break;
- case 3: KEYOUT_3 = 1; KEYOUT_4 = 0; break;
- default: break;
- }
- }
- void LedScan() //数码管扫描函数
- {
- static unsigned char i = 0;
- P0 = 0xFF;
- P1 = (P1 & 0xF8) | i;
- P0 = LedBuff[i];
- if(i < 5)
- i++;
- else
- i = 0;
- }
- void TurnMotor() //电机运转函数
- {
- unsigned char tmp;
- static unsigned char index = 0;
- if(beats != 0)
- {
- if(beats > 0)
- {
- index++;
- index = index & 0x7;
- beats--;
- }
- else
- {
- index--;
- index = index & 0x7;
- beats++;
- }
- tmp = P3;
- tmp = (tmp & 0xF0) | BeatCode[index];
- P3 = tmp;
- }
- else
- P3 = P3 | 0x0F;
- }
- void Timer0() interrupt 1
- {
- static bit div = 0;
- TH0 = 0xFC;
- TL0 = 0x67;
- KeyScan();
- LedScan();
- div = ~div;
- if(div)
- {
- TurnMotor();
- }
- }
复制代码
|