×
单片机 > 单片机程序设计 > 详情

基于Mega128编码器控制步进电机的平衡系统

发布时间:2020-05-26 发布时间:
|
    提起编码器,可能大家并不陌生,因为这东西真的很常用,现在的主流编码器一般精度都是比较高的,基本上都是基于光栅的。毕竟用硬件用电刷做到512精度以上是很困难的,而且成本也是很高的,这里我就不多说什么了。
    编码器一般共有三个主通道,每个主通道有分为两个分支;一个VCC,一个GND,一条屏蔽线。前两个通道一般是比较精确的脉冲信号,且之间有四分之一左右的相位差用来判断正反转的,第三通道基本上是旋转一周才会有一个脉冲信号的那种。

 
    提到步进电机,就一定要有一个合适的电机驱动,个人是比较喜欢用L298n这款芯片的,因为它价格低,操作比较简单。
 
    对于这个系统,我是用128的外部中断的下降沿触发方式来捕捉编码器的脉冲的,硬件连接方面电机驱动和主控芯片一定要注意地线的连接。
 
    下面是程序的完整代码下载地址: http://www.51hei.com/f/bmma.rar         

 
这里是delay.h
====================================================================
//根据CPU时钟频率选择
#ifndef F_CPU
//#define F_CPU 7372800
//#define F_CPU 8000000
//#define F_CPU 11059200
//#define F_CPU 12000000
#define F_CPU 16000000
#endif



//------------------------------------------------------------------------------
//1、2、3、5和10us的精确延时,用于需要精确时间的场合,比如DS18B20
//------------------------------------------------------------------------------
#if   F_CPU == 7372800
#define DELAY_1US NOP();NOP();NOP();NOP();NOP();NOP();NOP()
#define DELAY_2US DELAY_1US;DELAY_1US
#define DELAY_3US DELAY_1US;DELAY_1US;DELAY_1US
#define DELAY_5US DELAY_2US;DELAY_3US
#define DELAY_10US DELAY_5US;DELAY_5US

#elif F_CPU == 8000000
#define DELAY_1US NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP()
#define DELAY_2US DELAY_1US;DELAY_1US
#define DELAY_3US DELAY_1US;DELAY_1US;DELAY_1US
#define DELAY_5US DELAY_2US;DELAY_3US
#define DELAY_10US DELAY_5US;DELAY_5US

#elif F_CPU == 11059200
#define DELAY_1US NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP()
#define DELAY_2US DELAY_1US;DELAY_1US
#define DELAY_3US DELAY_1US;DELAY_1US;DELAY_1US
#define DELAY_5US DELAY_2US;DELAY_3US
#define DELAY_10US DELAY_5US;DELAY_5US

#elif F_CPU == 12000000
#define DELAY_1US NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP()
#define DELAY_2US DELAY_1US;DELAY_1US
#define DELAY_3US DELAY_1US;DELAY_1US;DELAY_1US
#define DELAY_5US DELAY_2US;DELAY_3US
#define DELAY_10US DELAY_5US;DELAY_5US

#elif F_CPU == 16000000
#define DELAY_1US NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP()
#define DELAY_2US DELAY_1US;DELAY_1US
#define DELAY_3US DELAY_1US;DELAY_1US;DELAY_1US
#define DELAY_5US DELAY_2US;DELAY_3US
#define DELAY_10US DELAY_5US;DELAY_5US
#endif


//------------------------------------------------------------------------------
//函数声明
//------------------------------------------------------------------------------

void delay_nus(unsigned int);//<10us时误差较大,用于无须精确延时的场合

void delay_nms(unsigned int);


#endif//__DELAY_H


====================================================================
 这里是delay.c
====================================================================
//|文件名称|delay.c
//|--------|--------------------------------------------------------------------
//|描    述|延时文件
//|--------|--------------------------------------------------------------------
//|说    明|delay_us(unsigned int time)用于不需精确定时的场合,<10us时误差较大
//|        |若要精确定时用delay.h里的宏
//|--------|--------------------------------------------------------------------
//|调用文件|delay.h
//|--------|--------------------------------------------------------------------
//|作    者|
//|--------|--------------------------------------------------------------------
//|版    本|
//|--------|--------------------------------------------------------------------
//|时    间|
//|--------|--------------------------------------------------------------------
//|E-mail  |
//|--------|--------------------------------------------------------------------
//|开发环境|ICCAVR6.31
//==============================================================================

#include "delay.h"

#if F_CPU == 7372800
void delay_nus(unsigned int time)
{
  unsigned int i;
  for(i=0;i
关键字:Mega128  编码器  步进电机  平衡系统

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机CO2检测显示程序解析