基于Mega128編碼器控制步進(jìn)電機的平衡系統
編碼器一般共有三個(gè)主通道,每個(gè)主通道有分為兩個(gè)分支;一個(gè)VCC,一個(gè)GND,一條屏蔽線(xiàn)。前兩個(gè)通道一般是比較精確的脈沖信號,且之間有四分之一左右的相位差用來(lái)判斷正反轉的,第三通道基本上是旋轉一周才會(huì )有一個(gè)脈沖信號的那種。
提到步進(jìn)電機,就一定要有一個(gè)合適的電機驅動(dòng),個(gè)人是比較喜歡用L298n這款芯片的,因為它價(jià)格低,操作比較簡(jiǎn)單。
對于這個(gè)系統,我是用128的外部中斷的下降沿觸發(fā)方式來(lái)捕捉編碼器的脈沖的,硬件連接方面電機驅動(dòng)和主控芯片一定要注意地線(xiàn)的連接。
下面是程序的完整代碼下載地址:http://www.51hei.com/f/bmma.rar
這里是delay.h====================================================================//根據CPU時(shí)鐘頻率選擇#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的精確延時(shí),用于需要精確時(shí)間的場(chǎng)合,比如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時(shí)誤差較大,用于無(wú)須精確延時(shí)的場(chǎng)合void delay_nms(unsigned int);#endif//__DELAY_H====================================================================這里是delay.c====================================================================//|文件名稱(chēng)|delay.c//|--------|--------------------------------------------------------------------//|描 述|延時(shí)文件//|--------|--------------------------------------------------------------------//|說(shuō) 明|delay_us(unsigned int time)用于不需精確定時(shí)的場(chǎng)合,<10us時(shí)誤差較大//| |若要精確定時(shí)用delay.h里的宏//|--------|--------------------------------------------------------------------//|調用文件|delay.h//|--------|--------------------------------------------------------------------//|作 者|//|--------|--------------------------------------------------------------------//|版 本|//|--------|--------------------------------------------------------------------//|時(shí) 間|//|--------|--------------------------------------------------------------------//|E-mail |//|--------|--------------------------------------------------------------------//|開(kāi)發(fā)環(huán)境|ICCAVR6.31//==============================================================================#include "delay.h"#if F_CPU == 7372800void delay_nus(unsigned int time){unsigned int i;for(i=0;i
評論