2019-07-20 08:52发布
正点原子 发表于 2016-6-1 20:45 帮顶
dop101 发表于 2016-6-22 10:23 楼主我也在写跟你一样的,没思路。现在只是实现了恒速。能不能分享你的程序。谢谢
最多设置5个标签!
2.用定时器的主从模式,可以非常准确的知道脉冲数,但是这样会频繁响应中断。
谢谢原子哥
给你看一篇文章吧http://wenku.baidu.com/view/39fe ... 71.html?from=search
我是参照这个来写的
[mw_shl_code=applescript,true]//=========================================================================//
//M542 DRIVER CODE:定义驱动器的端口
//
//创建日期:2015/12/30
//=========================================================================//
//初始化PE1/PE3为输出口.并使能这2个口的时钟
//PE1对应pulse,PE3对应DIR(方向)
#include "driver.h"
#include "delay.h"
#include "usart.h"
#include "adc.h"
void Driver_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);//使能GPIOE时钟
//GPIOE5初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//分别用这个口 控制方向
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIO
}
int cnt;
int value2[]={994,994,994,994,992,992,992,992,992,990,990,990,990,988,988,988,986,986,986,984, //S型加减速曲线,时PWM的输出频率
984,982,982,982,980,980,978,978,976,974,974,972,970,970,968,966,964,964,962,960, //从1KHz变到10Khz,即使得步进电机
958,956,954,950,948,946,944,940,938,934,932,928,926,922,918,914,910,906,902,898, //加速运动,加速运动即让它反过来
892,887,882,877,872,866,860,855,848,842,835,828,821,814,807,799,791,783,775,766, //速度约从1.3cm/s加速到12cm/s左右
758,750,740,730,720,712,702,692,682,670,660,650,638,628,616,606,594,584,572,562,
550,538,528,516,506,494,484,472,462,450,440,430,418,408,398,388,380,370,360,350,
342,334,324,316,308,300,292,286,278,272,264,258,252,244,240,234,228,222,218,212,
208,202,198,194,190,186,182,178,174,172,168,166,162,160,156,154,152,150,146,144,
142,140,138,136,136,134,132,130,130,128,126,126,124,122,122,120,120,118,118,118,
116,116,114,114,114,112,112,112,110,110,110,110,108,108,108,108,108,106,106,106,106};
int value1[]={ 994,994,994,992,992,992,992,992,990,990,990,990,988,988,988,986,986,986,984,984, //从1KHz加速到1000/66=15KHz左右
984,982,982,980,980,978,978,976,974,974,972,972,970,968,966,964,964,962,960,958, //速度约从1.3cm/s加速到19.2cm/s左右
956,954,950,948,946,944,940,938,936,932,928,926,922,918,914,910,906,902,898,892,
888,882,878,872,866,860,854,848,842,836,828,822,814,806,798,790,782,774,766,756,
748,738,728,718,708,698,688,678,666,656,646,634,622,612,600,588,576,566,554,542,
530,518,506,494,484,472,460,448,438,426,414,404,394,382,372,362,352,342,332,322,
312,304,294,286,278,270,262,254,246,238,232,224,218,212,206,200,194,188,182,178,
172,168,162,158,154,150,146,142,138,134,132,128,124,122,120,116,114,112,110,106,
104,102,100,98 ,96 ,96 ,94 ,92 ,90 ,88 ,88 ,86 ,86 ,84 ,82 ,82 ,80 ,80 ,78 ,78 ,
76 ,76 ,76 ,74 ,74 ,74 ,72 ,72 ,72 ,70 ,70 ,70 ,70 ,68 ,68 ,68 ,68 ,68 ,66 ,66 ,66};
int value[]={596,596,596,596,596,596,596,594,594,594,594,594,594,594,592,592,592,592,592,590, //从1.6KHz加速到1000/64=15.6KHz左右
590,590,590,588,588,588,586,586,586,584,584,584,582,582,580,580,578,578,576,576, //速度约从2.1cm/s加速到19.8cm/s左右
574,574,572,570,570,568,566,564,562,560,560,558,556,552,550,548,546,544,542,538,
536,532,530,526,524,520,516,514,510,506,502,498,494,488,484,480,476,470,466,460,
454,450,444,438,432,426,420,414,408,402,396,390,384,376,370,364,356,350,344,336,
330,324,316,310,304,296,290,284,276,270,264,258,252,246,240,234,228,222,216,210,
206,200,194,190,184,180,176,172,166,162,158,154,150,146,144,140,136,134,130,128,
124,122,118,116,114,112,110,108,104,102,100,100,98 ,96 ,94 ,92 ,90 ,90 ,88 ,86 ,
86 ,84 ,84 ,82 ,82 ,80 ,80 ,78 ,78 ,76 ,76 ,76 ,74 ,74 ,74 ,72 ,72 ,72 ,70 ,70 ,
70 ,70 ,68 ,68 ,68 ,68 ,68 ,66 ,66 ,66 ,66 ,66 ,66 ,66 ,64 ,64 ,64 ,64 ,64 ,64 ,64};
/********************************************************
函数名:HeadADC
功 能:让电机一边前进一边进采样
输 入:无
输 出:无
********************************************************/
void HeadADC(void) //
{
GPIO_SetBits(GPIOE,GPIO_Pin_5); // PE5与驱动器DIR-对应,使之高电位,沿一个方向运动
delay_us(20);
TIM_Cmd(TIM3,ENABLE);
TIM_Cmd(TIM4,ENABLE);
Speed_up();
}
/********************************************************
函数名:BackADC
功 能:让步进边后退一边采样
输 入:无
输 出:无
********************************************************/
void BackADC(void) //Num为后退的步数,Dis为每步的大小,以mm为单位
{
GPIO_ResetBits(GPIOE,GPIO_Pin_5); // PE5与驱动器DIR-对应,使之高电位,沿一个方向运动
delay_us(20);
TIM_Cmd(TIM3,ENABLE);
TIM_Cmd(TIM4,ENABLE);
Speed_up();
}
/********************************************************
函数名:Head
功 能:开启定时器4的PWM输出,让步进电机只有前进功能
输 入:无
输 出:无 *
********************************************************/
void Head(void) //
{
GPIO_SetBits(GPIOE,GPIO_Pin_5); // PE5与驱动器DIR-对应,使之高电位,沿一个方向运动
delay_us(20);
TIM_Cmd(TIM4,ENABLE); //开启TIM4,输出PWM波
Speed_up();
}
/********************************************************
函数名:Head
功 能:开启定时器4的PWM输出,让步进电机只有前进功能
输 入:无
输 出:无
********************************************************/
void Back(void) //
{
GPIO_ResetBits(GPIOE,GPIO_Pin_5); // PE5与驱动器DIR-对应,使之高电位,沿一个方向运动
delay_us(20);
TIM_Cmd(TIM4,ENABLE); //开启TIM4,输出PWM波
Speed_up();
}
/********************************************************
函数名:Speed_up
功 能:步进电机加速函数
输 入:无
输 出:无
********************************************************/
void Speed_up(void)
{
for (cnt = 0; cnt<201; cnt++)
{
delay_us(2*value[0]); //每2个PWM脉冲产生后,预装载值和占空比
TIM_SetAutoreload(TIM4,value[cnt]);
TIM_SetCompare1(TIM4,value[cnt]/2);
}
}
/********************************************************
函数名:Speed_down
功 能:步进电机减速函数
输 入:无
输 出:无
********************************************************/
void Speed_down(void)
{
for (cnt =200; cnt>=0; cnt--)
{
delay_us(2*value[0]); //每2个PWM脉冲产生后,预装载值和占空比
TIM_SetAutoreload(TIM4,value[cnt]);
TIM_SetCompare1(TIM4,value[cnt]/2);
}
}
[/mw_shl_code]
一周热门 更多>