用按键设置定时器重装值及分频值,脉冲数。经过开发板测试。图片无法上传
//步进电机
//用定时器中断(S曲线7段法)。LCD12864串口模式显示,用按键设置定时器(TIM5_Int_Init(999,999);)的两个传入参数来
//改变中断次数与分频系数达到单位时间内的脉冲值。
//脉冲可设置
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "motor.h"
#include "s_curve.h"
#include "key.h"
#include "12864.h"
//////////////////////////////////////////////
u8 key_num=0;
u8 Table0[3]={0x00};
u8 Table1[3]={0x00};
u8 Table2[3]={0x00};
u16 arr,psc,pulse;
////////////////////////////////////////////////
void display()
{
lcd_pos(1,3); //自动重装值
write_char(':');
Table0[0]=(arr%1000/100+0x30);
Table0[1]=(arr%100/10+0x30);
Table0[2]=(arr%10+0x30);
write_char(Table0[0]);
write_char(Table0[1]);
write_char(Table0[2]);
lcd_pos(2,4); //时钟预分频数
write_char(':');
Table1[0]=(psc%1000/100+0x30);
Table1[1]=(psc%100/10+0x30);
Table1[2]=(psc%10+0x30);
write_char(Table1[0]);
write_char(Table1[1]);
write_char(Table1[2]);
lcd_pos(3,3); //脉冲数
write_char(':');
Table2[0]=(pulse%1000/100+0x30);
Table2[1]=(pulse%100/10+0x30);
Table2[2]=(pulse%10+0x30);
write_char(Table2[0]);
write_char(Table2[1]);
write_char(Table2[2]);
}
////////////////////////////////////////////////////////////////
int main(void)
{
u8 key;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
lcd_gpio_Init();
Motor_Init();
TIM5_Int_Init(999,999);//
KEY_Init();
lcd_init();//12864初始化
delay_ms(20);
lcd_clear();//清屏
delay_ms(10);
lcd_wstr(1,0,"重装值 ");
lcd_wstr(2,0,"分频系数 ");
lcd_wstr(3,0,"脉冲数 ");
display();
delay_ms(2000);
s_curve_start_Motor(1,40);//1代表顺时针,400旋转脉冲
delay_ms(3000);
s_curve_start_Motor(0,40);//0代表逆时针,400旋转脉冲
delay_ms(3000);
while(1)
{
key=KEY_Scan(0);
if(key==KEY0_PRES)
{
delay_ms(50);
key_num++;
switch(key_num)
{
case 1:
send_cmd(0x0c); //关光标关闪烁
lcd_pos(1,3); //
write_char(':');
Table0[0]=(arr%1000/100+0x30);
Table0[1]=(arr%100/10+0x30);
Table0[2]=(arr%10+0x30);
write_char(Table0[0]);
write_char(Table0[1]);
write_char(Table0[2]);
send_cmd(0x80|0x04);
send_cmd(0x0f); //开光标并闪烁
break;
case 2:
send_cmd(0x0c); //关光标关闪烁
lcd_pos(2,4); //
write_char(':');
Table1[0]=(psc%1000/100+0x30);
Table1[1]=(psc%100/10+0x30);
Table1[2]=(psc%10+0x30);
write_char(Table1[0]);
write_char(Table1[1]);
write_char(Table1[2]);
send_cmd(0x90|0x05);
send_cmd(0x0f); //开光标并闪烁
break;
case 3:
send_cmd(0x0c); //关光标关闪烁
lcd_pos(3,3); //
write_char(':');
Table2[0]=(pulse%1000/100+0x30);
Table2[1]=(pulse%100/10+0x30);
Table2[2]=(pulse%10+0x30);
write_char(Table2[0]);
write_char(Table2[1]);
write_char(Table2[2]);
send_cmd(0x88|0x04);
send_cmd(0x0f); //开光标并闪烁
break;
case 4:
send_cmd(0x0c); //关光标关闪烁
key_num=0;
TIM5_Int_Init(arr,psc);//
break;
}
}
if(key_num!=0)
{
if(key==KEY1_PRES)
{
delay_ms(50);
switch(key_num)
{
case 1:arr+=10;if(arr==1000)arr=0;
send_cmd(0x0c); //关光标关闪烁
lcd_pos(1,3); //
write_char(':');
Table0[0]=(arr%1000/100+0x30);
Table0[1]=(arr%100/10+0x30);
Table0[2]=(arr%10+0x30);
write_char(Table0[0]);
write_char(Table0[1]);
write_char(Table0[2]);
send_cmd(0x80|0x04);
send_cmd(0x0f); //开光标并闪烁
break;
case 2:psc+=10;if(pulse==1000)pulse=0;
send_cmd(0x0c); //关光标关闪烁
lcd_pos(2,4); //
write_char(':');
Table1[0]=(psc%1000/100+0x30);
Table1[1]=(psc%100/10+0x30);
Table1[2]=(psc%10+0x30);
write_char(Table1[0]);
write_char(Table1[1]);
write_char(Table1[2]);
send_cmd(0x90|0x05);
send_cmd(0x0f); //开光标并闪烁
break;
case 3:pulse+=10;if(pulse==1000)pulse=0;
send_cmd(0x0c); //关光标关闪烁
lcd_pos(3,3); //
write_char(':');
Table2[0]=(pulse%1000/100+0x30);
Table2[1]=(pulse%100/10+0x30);
Table2[2]=(pulse%10+0x30);
write_char(Table2[0]);
write_char(Table2[1]);
write_char(Table2[2]);
send_cmd(0x88|0x04);
send_cmd(0x0f); //开光标并闪烁
break;
}
}
}
if(key_num==0)
{
// if(key==KEY0_PRES)
// {
// s_curve_start_Motor(1,400);//顺时针旋转1圈
// delay_ms(500);
// }
if(key==KEY1_PRES)
{
s_curve_start_Motor(0,pulse);//顺时针旋转1圈
delay_ms(500);
}
}
}
}
全部程序51hei下载地址:
stm32_步进电机.7z
(193.99 KB, 下载次数: 57)
|