一、步进电机简介
步进电机工作时,有四拍和八拍两种模式,八拍是在四拍的基础上,同时接通两个相邻引脚,是步进电机转动一个中间角度从而形成八拍。在该步进电机中,转动一个定子角度需要八个脉冲,转动一圈需要64个脉冲,由于减速比是1:64,所以,实际上外部齿轮转动一圈需要4096个脉冲。但是,这里的1:64只是一个近似值,所以当转动多圈时,就会产生明显的偏差。
代码一:让电机转起来
#include<reg52.h>
unsigned char code BeatCode[8] = {
0x0E, 0x0C, 0x0D, 0x09, 0x0B, 0x03, 0x07, 0x06};//根据八拍的那个表格写出
void delay();
void main()
{
unsigned char tmp;
unsigned char index = 0;
while(1)
{
tmp = P1;
tmp = tmp & 0xF0; //低四位清零
tmp = tmp | BeatCode[index];
P1 = tmp;
index++;
index = index & 0x07; //当index大于等于8时,清零
delay();
}
}
void delay() //该函数的作用是控制启动频率,频率过高,步进电机将无法启动。
{
unsigned int i = 200;
while(i--);
}
代码二:转动一定的角度
/*转动一定的角度*/
#include<reg52.h>
void TrunMotor(unsigned long angle);
void main()
{
TrunMotor(360*25);
while(1);
}
void delay() //该函数的作用是控制启动频率,频率过高,步进电机将无法启动。
{
unsigned int i = 200;
while(i--);
}
void TrunMotor(unsigned long angle)
{
unsigned char tmp;
unsigned char index = 0;
unsigned long beats = 0;
unsigned char code BeatCode[8] = {
0x0E, 0x0C, 0x0D, 0x09, 0x0B, 0x03, 0x07, 0x06};//根据八拍的那个表格写出
beats = (angle*4076)/360;//计算脉冲个数,不是4096而是4076是为了消除误差
while(beats)
{
tmp = P1;
tmp = tmp & 0xF0; //低四位清零
tmp = tmp | BeatCode[index];
P1 = tmp;
index++;
index = index & 0x07; //当index大于等于8时,清零
delay();
}
}
代码三:中断控制频率
/* use interrupt to control motor*/
#include<reg52.h>
unsigned long beats = 0;//the nunber of pulses
void StartMotor(unsigned long ungle);
void main()
{
EA = 1; //enable interrupts
TMOD = 0x01;//Mode selection
TH0 = 0xF8;
TL0 = 0xCD;
ET0 = 1;//Enable clock interrupt
TR0 = 1;// Clock interrupr start
StartMotor(360*2 + 180);
while(1);
}
void StartMotor(unsigned long angle)
{
EA = 0;
beats = (angle*4096)/360;
EA = 1;
}
void InterruptTimer0() interrupt 1
{
unsigned char tmp;
static unsigned char index = 0;
unsigned char code BeatCode[8] = {
0x0E, 0x0C, 0x0D, 0x09, 0x0B, 0x03, 0x07, 0x06};
TH0 = 0xF8;
TL0 = 0xCD;
if(beats != 0)
{
tmp = P1;
tmp = tmp & 0xF0;
tmp = tmp | BeatCode[index];
P1 = tmp;
index++;
index = index & 0x07;
beats--;
}
else
{
P1 = P1 | 0x0F;
}
}
代码四:按键控制电机转动
#include<reg52.h>
sbit KEY_IN_1 = P2^4;
sbit KEY_IN_2 = P2^5;
sbit KEY_IN_3 = P2^6;
sbit KEY_IN_4 = P2^7;
sbit KEY_OUT_1 = P2^3;
sbit KEY_OUT_2 = P2^2;
sbit KEY_OUT_3 = P2^1;
sbit KEY_OUT_4 = P2^0;
signed long beats = 0;
unsigned char KeySta[4][4] = {
{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}
};
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键、 回车键、 向右键
};
void KeyDriver();
void main()
{
EA = 1; //使能总中断
TMOD = 0x01; //设置T0为模式1
TH0 = 0xFC; //为T0赋初值0xFC67,定时1ms
TL0 = 0x67;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
while (1)
{
KeyDriver(); //调用按键驱动函数
}
}
void StartMotor(signed long angle)
{
EA = 0;
beats = (angle*4076)/360;
EA = 1;
}
void StopMotor()
{
EA = 0;
beats = 0;
EA = 1;
}
void KeyAction(unsigned char keycode)
{
static bit dirMotor = 0;
if((keycode >= 0x30) &&(keycode <= 0x39))
{
if(dirMotor == 0)
{
StartMotor(360 * (keycode - 0x30));
}
else
{
StartMotor(-360 * (keycode - 0x30));
}
}
else if(keycode == 0x26)
{
dirMotor = 0;
}
else if(keycode == 0x28)
{
dirMotor = 1;
}
else if(keycode == 0x25)
{
StartMotor(90);
}
else if(keycode == 0x27)
{
StartMotor(-90);
}
else if(keycode == 0x1B)
{
StopMotor();
}
}
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(backup[i][j] != KeySta[i][j])
{
if(backup[i][j] == 0)
{
KeyAction(KeyCodeMap[i][j]);
}
backup[i][j] = KeySta[i][j];
}
}
}
}
/* 按键扫描函数,需在定时中断中调用,推荐调用间隔1ms */
void KeyScan()
{
unsigned char i;
static unsigned char keyout = 0; //矩阵按键扫描输出索引
static unsigned char keybuf[4][4] = { //矩阵按键扫描缓冲区
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF}, {0xFF, 0xFF, 0xFF, 0xFF}
};
//将一行的4个按键值移入缓冲区
keybuf[keyout][0] = (keybuf[keyout][0] << 1) | KEY_IN_1;
keybuf[keyout][1] = (keybuf[keyout][1] << 1) | KEY_IN_2;
keybuf[keyout][2] = (keybuf[keyout][2] << 1) | KEY_IN_3;
keybuf[keyout][3] = (keybuf[keyout][3] << 1) | KEY_IN_4;
//消抖后更新按键状态
for (i=0; i<4; i++) //每行4个按键,所以循环4次
{
if ((keybuf[keyout][i] & 0x0F) == 0x00)
{ //连续4次扫描值为0,即4*4ms内都是按下状态时,可认为按键已稳定的按下
KeySta[keyout][i] = 0;
}
else if ((keybuf[keyout][i] & 0x0F) == 0x0F)
{ //连续4次扫描值为1,即4*4ms内都是弹起状态时,可认为按键已稳定的弹起
KeySta[keyout][i] = 1;
}
}
//执行下一次的扫描输出
keyout++; //输出索引递增
keyout = keyout & 0x03; //索引值加到4即归零
switch (keyout) //根据索引,释放当前输出引脚,拉低下次的输出引脚
{
case 0: KEY_OUT_4 = 1; KEY_OUT_1 = 0; break;
case 1: KEY_OUT_1 = 1; KEY_OUT_2 = 0; break;
case 2: KEY_OUT_2 = 1; KEY_OUT_3 = 0; break;
case 3: KEY_OUT_3 = 1; KEY_OUT_4 = 0; break;
default: break;
}
}
void TurnMotor()
{
unsigned char tmp;
static unsigned char index = 0;
unsigned char code BeatCode[8] = {
0x0E, 0x0C, 0x0D, 0x09, 0x0B, 0x03, 0x07, 0x06};
if(beats != 0)
{
if(beats > 0)
{
index++;
index = index & 0x07;
beats--;
}
else
{
index--;
index = index & 0x07;
beats++;
}
tmp = P1;
tmp = tmp & 0xF0;
tmp = tmp | BeatCode[index];
P1 = tmp;
}
else
{
P1 = P1 | 0x0F;
}
}
/* T0中断服务函数,用于数码管显示扫描与按键扫描 */
void InterruptTimer0() interrupt 1
{
static bit div = 0;
TH0 = 0xFC;
TL0 = 0x67;
KeyScan();
div = ~ div;
if(div == 1)
{
TurnMotor();
}
}
二、蜂鸣器
通过控制不同的频率,来使蜂鸣器发声
#include <reg52.h>
sbit BUZZ = P1^6; //蜂鸣器控制引脚
unsigned int code NoteFrequ[] = { //中音1-7和高音1-7对应频率列表
523, 587, 659, 698, 784, 880, 988, //中音1-7
1047, 1175, 1319, 1397, 1568, 1760, 1976 //高音1-7
};
unsigned int code NoteReload[] = { //中音1-7和高音1-7对应的定时器重载值
65536 - (11059200/12) / (523*2), //中音1
65536 - (11059200/12) / (587*2), //2
65536 - (11059200/12) / (659*2), //3
65536 - (11059200/12) / (698*2), //4
65536 - (11059200/12) / (784*2), //5
65536 - (11059200/12) / (880*2), //6
65536 - (11059200/12) / (988*2), //7
65536 - (11059200/12) / (1047*2), //高音1
65536 - (11059200/12) / (1175*2), //2
65536 - (11059200/12) / (1319*2), //3
65536 - (11059200/12) / (1397*2), //4
65536 - (11059200/12) / (1568*2), //5
65536 - (11059200/12) / (1760*2), //6
65536 - (11059200/12) / (1976*2), //7
};
bit enable = 1; //蜂鸣器发声使能标志
bit tmrflag = 0; //定时器中断完成标志
unsigned char T0RH = 0xFF; //T0重载值的高字节
unsigned char T0RL = 0x00; //T0重载值的低字节
void PlayTwoTiger();
void main()
{
unsigned int i;
EA = 1; //使能全局中断
TMOD = 0x01; //配置T0工作在模式1
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
while (1)
{
PlayTwoTiger(); //播放乐曲--两支老虎
for (i=0; i<40000; i++); //停止一段时间
}
}
/* 两只老虎乐曲播放函数 */
void PlayTwoTiger()
{
unsigned char beat; //当前节拍索引
unsigned char note; //当前节拍对应的音符
unsigned int time = 0; //当前节拍计时
unsigned int beatTime = 0; //当前节拍总时间
unsigned int soundTime = 0; //当前节拍需发声时间
//两只老虎音符表
unsigned char code TwoTigerNote[] = {
1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5,
5,6, 5,4, 3, 1, 5,6, 5,4, 3, 1, 1, 5, 1, 1, 5, 1,
};
//两只老虎节拍表,4表示一拍,1就是1/4拍,8就是2拍
unsigned char code TwoTigerBeat[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8,
3,1, 3,1, 4, 4, 3,1, 3,1, 4, 4, 4, 4, 8, 4, 4, 8,
};
for (beat=0; beat<sizeof(TwoTigerNote); ) //用节拍索引作为循环变量
{
while (!tmrflag); //每次定时器中断完成后,检测并处理节拍
tmrflag = 0;
if (time == 0) //当前节拍播完则启动一个新节拍
{
note = TwoTigerNote[beat] - 1;
T0RH = NoteReload[note] >> 8;
T0RL = NoteReload[note];
//计算节拍总时间,右移2位相当于除4,移位代替除法可以加快执行速度
beatTime = (TwoTigerBeat[beat] * NoteFrequ[note]) >> 2;
//计算发声时间,为总时间的0.75,移位原理同上
soundTime = beatTime - (beatTime >> 2);
enable = 1; //指示蜂鸣器开始发声
time++;
}
else //当前节拍未播完则处理当前节拍
{
if (time >= beatTime) //当前持续时间到达节拍总时间时归零,
{ //并递增节拍索引,以准备启动新节拍
time = 0;
beat++;
}
else //当前持续时间未达到总时间时,
{
time++; //累加时间计数
if (time == soundTime) //到达发声时间后,指示关闭蜂鸣器,
{ //插入0.25*总时间的静音间隔,
enable = 0; //用以区分连续的两个节拍
}
}
}
}
}
/* T0中断服务函数,用于控制蜂鸣器发声 */
void InterruptTimer0() interrupt 1
{
TH0 = T0RH; //重新加载重载值
TL0 = T0RL;
tmrflag = 1;
if (enable) //使能时反转蜂鸣器控制电平
BUZZ = ~BUZZ;
else //未使能时关闭蜂鸣器
BUZZ = 1;
}