基于Mega128編碼器控制步進電機的平衡系統(tǒng)
編碼器一般共有三個主通道,每個主通道有分為兩個分支;一個VCC,一個GND,一條屏蔽線。前兩個通道一般是比較精確的脈沖信號,且之間有四分之一左右的相位差用來判斷正反轉(zhuǎn)的,第三通道基本上是旋轉(zhuǎn)一周才會有一個脈沖信號的那種。
提到步進電機,就一定要有一個合適的電機驅(qū)動,個人是比較喜歡用L298n這款芯片的,因為它價格低,操作比較簡單。
對于這個系統(tǒng),我是用128的外部中斷的下降沿觸發(fā)方式來捕捉編碼器的脈沖的,硬件連接方面電機驅(qū)動和主控芯片一定要注意地線的連接。
下面是程序的完整代碼下載地址:http://www.51hei.com/f/bmma.rar
這里是delay.h====================================================================//根據(jù)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//------------------------------------------------------------------------------//函數(shù)聲明//------------------------------------------------------------------------------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里的宏//|--------|--------------------------------------------------------------------//|調(diào)用文件|delay.h//|--------|--------------------------------------------------------------------//|作 者|//|--------|--------------------------------------------------------------------//|版 本|//|--------|--------------------------------------------------------------------//|時 間|//|--------|--------------------------------------------------------------------//|E-mail |//|--------|--------------------------------------------------------------------//|開發(fā)環(huán)境|ICCAVR6.31//==============================================================================#include "delay.h"#if F_CPU == 7372800void delay_nus(unsigned int time){unsigned int i;for(i=0;i
評論