新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 基于Mega128编码器控制步进电机的平衡系统

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

作者:时间:2016-11-29来源:网络收藏
提起编码器,可能大家并不陌生,因为这东西真的很常用,现在的主流编码器一般精度都是比较高的,基本上都是基于光栅的。毕竟用硬件用电刷做到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 == 7372800void delay_nus(unsigned int time){unsigned int i;for(i=0;i
				
            	
                
				
								
上一页 1 2 下一页

评论


技术专区

关闭