資源描述:
《STC單片機(無PWM模塊)的步進(jìn)電機驅(qū)動程序C語言》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、基于STC單片機(無PWM模塊)的步進(jìn)電機驅(qū)動程序C語言2009-04-0814:03///從不明白到調(diào)試通以后,才發(fā)現(xiàn)是超級簡單。#include"STC12C2052AD.H"#include#include"12C56(54)_mode.h"#include"type.h"unsignedintint_flag;//unsignedlonginti=0;voiddelay(unsignedlonginti);voidInt0Int(void);voidmotor_ccw();vo
2、idmotor_cw();voidmain(void){//---初始化IO----//P1.4-P1.7為開漏,P1.0-P1.3為準(zhǔn)雙向口(傳統(tǒng)8051I/O口模式)P1M0=0xf0;??P1M1=0xf0;//定義中斷觸發(fā)方式為INT0,INT1低電平觸發(fā)TCON=INT0_LOW;??//初始化中斷???IE=EnINT0_C
3、EnINT1_C;//允許INT0,INT1中斷IPH=NOIP_C;???????//此處不設(shè)優(yōu)先級IP=NOIP_C;//初始化變量//EA=true;??//啟動中斷
4、,開始正常工作EA=false;??//關(guān)中斷int_flag=0;P1=0x00;while(1){??????if(INT0==0)??{???motor_ccw();???P17=1;?????????}??elseif(INT1==0)???{????motor_cw();????P16=1;???}/*switch(int_flag)??{??casezoom_in:???motor_ccw();???break;??casezoom_out:??????motor_cw();???break;?
5、?default:???break;??}*///int_flag=0;}}voiddelay(unsignedlonginti){//unsignedlonginti;???for(;i>0;i--);//{//_nop_();??//執(zhí)行一次需要一個機器指令,一個機器指令所需時間為:T=12/fosc=12/11059200=1.085us??//}}/********************************//*Int0(P3.2)中斷服務(wù)??*//*Int0(P3.2)中斷服務(wù)?????*/
6、/********************************/voidInt0Int(void)interrupt0using1{int_flag=1;//motor_cw();P14=0;???}voidInt1Int(void)interrupt2using1{int_flag=2;//motor_ccw();P15=0;}??/***********************************//*????????馬達(dá)控制????????????*//*CCW為逆時針反轉(zhuǎn),CW為順時針正轉(zhuǎn)*
7、//***********************************/voidmotor_ccw()//逆時針{P1=0x09;delay(2000);//延時約2MSP1=0x05;delay(2000);P1=0x06;delay(2000);P1=0x0A;delay(2000);???}voidmotor_cw()//順時針{???P1=0x0A;delay(2000);P1=0X06;delay(2000);P1=0X05;delay(2000);P1=0X09;delay(2000);??
8、??}基于STC單片機(無PWM模塊)的步進(jìn)電機驅(qū)動C語言程序///從不明白到調(diào)試通以后,才發(fā)現(xiàn)是超級簡單。#include"STC12C2052AD.H"#include#include"12C56(54)_mode.h"#include"type.h"unsignedintint_flag;//unsignedlonginti=0;voiddelay(unsignedlonginti);voidInt0Int(void);voidmotor_ccw();voidmotor_cw()
9、;voidmain(void){//---初始化IO----//P1.4-P1.7為開漏,P1.0-P1.3為準(zhǔn)雙向口(傳統(tǒng)8051I/O口模式)P1M0=0xf0;?P1M1=0xf0;//定義中斷觸發(fā)方式為INT0,INT1低電平觸發(fā)TCON=