新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于Mega128編碼器控制步進(jìn)電機(jī)的平衡系統(tǒng)

基于Mega128編碼器控制步進(jìn)電機(jī)的平衡系統(tǒng)

作者: 時(shí)間:2016-11-29 來(lái)源:網(wǎng)絡(luò) 收藏
====================================================================這里是dianji.c====================================================================#include"dianji.h"#include"delay.h"#include/*步進(jìn)電機(jī)旋轉(zhuǎn)一周子函數(shù)*/void one_circle(void){unsigned int i;for(i=0;i<3200;i++){EN_1;delay_us(100);EN_0;delay_us(100);}}/*步進(jìn)電機(jī)根據(jù)編碼器計(jì)算的數(shù)值旋轉(zhuǎn)任意角度子函數(shù)*/void circle(unsigned int xcircle){unsigned int i,j;for(j=0;j#include#include//一些變量的定義int k=0;unsigned int x=1,d=0,c=0,cir=0;unsigned int a,aa,q,angle=0,p=0;//595(數(shù)碼管)顯示數(shù)組unsigned char led[]={0x3F, // 00x06, // 10x5B, // 20x4F, // 30x66, // 40x6D, // 50x7D, // 60x07, // 70x7F, // 80x6F, // 9};//外部中斷0~3向量號(hào)碼#pragma interrupt_handler Zhongduan_0:2#pragma interrupt_handler Zhongduan_1:3#pragma interrupt_handler Zhongduan_2:4//端口輸入輸出初始化void init_xianshi(void){DDRA=0XFF;DDRB=0XFF;//595控制口DDRC=0XFF;//正反轉(zhuǎn)提示LEDPORTC=0XFF;//LED初始化(暗)}//595串入一字節(jié)并處發(fā)送一字節(jié)子函數(shù)void send_595(unsigned char dat){unsigned char i;LCK_0;SDI_1;SCK_0;//上面的三條語(yǔ)句為了初始化端口狀態(tài)for(i=0;i<8;i++){LCK_0;//時(shí)鐘線拉低if(dat&0x80)SDI_1;else SDI_0;dat=dat<<1;delay_us(100); LCK_1; //時(shí)鐘線拉高將數(shù)據(jù)讀入595的移位寄存器 delay_us(100); }SCK_1; //發(fā)送數(shù)據(jù)到并行端口SCK_0; }void jiaoduzhuanhuan(void){init_xianshi();EIMSK=0X0F;EICRA=0XAA; while(1){ if(a!=aa) /*如果有角度變化就執(zhí)行下面的程序*/{angle++;if(!(PING&0X04))cir++; while(cir){if(!k){delay_ms(800);if(d<=3)one_circle();d++;}} if(!(PING&0X02)){while(PING&0X01){DIR_0;circle(1);p++;while(p==angle);}while(PING&0X01);if(q=-1)//正轉(zhuǎn)DIR_1;circle(1);if(q==1) //反轉(zhuǎn)DIR_1;}aa=a;if(!x){PORTC=0X00;}}/*下面為數(shù)碼管顯示編碼器當(dāng)前計(jì)數(shù)的數(shù)值*/PORTA=0X08; send_595(0x00);send_595(led[abs(k)%10]);PORTA=0X01; send_595(0x00);send_595(led[(abs(k)%100)/10]);PORTA=0X02; send_595(0x00);send_595(led[(abs(k)%1000)/100]);PORTA=0X04; send_595(0x00);send_595(led[abs(k)/1000]);SEI(); /*使能中斷*/} }void Zhongduan_0(void){CLI();x=0;////////////////// if(PIND&0X02)q=-1;if(!(PIND&0X02))q=1;////////////////k=k+q;SEI();}void Zhongduan_1(void){CLI();if(PIND&0X01)q=1;k=k+q;if(abs(k)==10000){k=0;}a=k;//EIMSK=0X0E;SEI();}void Zhongduan_2(void){CLI();k=0;SEI();}====================================================================這里是main.c====================================================================#include#include#include"delay.h"#include"caiji.h"#include"dianji.h"void init(void){DDRC=0X03;PORTC=0X00;}void main(void){init(); //初始化子函數(shù)jiaoduzhuanhuan(); //主程序的實(shí)現(xiàn)}====================================================================
上一頁(yè) 1 2 下一頁(yè)

評(píng)論


技術(shù)專區(qū)

關(guān)閉