51单片机用定时器中断实现两相四线步进电机时序控制
2018-02-28 14:48
417 查看
#include<reg52.h> #define iniMotor() (MOTOR=0x00) sfr MOTOR=0x90;//define the sfr MOTOR is the address of P1 port static void iniTimer0(); //ini timer 0 static void motorDrive(unsigned char const pul,unsigned char const dir); //stepper motor drive function int main(){ /*ini MOTOR(set P1 port to low)*/ iniMotor(); /*ini timer 0*/ iniTimer0(); /*infinite loop,wait to interrupt of T0*/ while(1); return 0; } void iniTimer0(){ EA=1; ET0=1; //open the T0 interrupt TMOD=0x01; //use mode 1:M1M0=01 TR0=1; //run timer 0 } void motorDrive(unsigned char const pul,unsigned char const dir){ static unsigned char count; /*counterclockwise rotation sequential of motor:A- --> B- --> A+ --> B+ clockwise rotation sequential of motor:B+ --> A+ --> B- --> A- */ static unsigned char table[2][4]={{0x01,0x04,0x02,0x08},{0x08,0x02,0x04,0x01}}; /*judge the pulse is high lever or low*/ if(pul) /*drive the motor,dir is high lever or low:0 is counter clock;1 is clock*/ MOTOR=table[dir][(count++) % 4]; } /*T0 interrupt function*/ void emitPulse() interrupt 1{ //unsigned short pulse_cycle,wait_time; static unsigned char pul,dir=1;//pulse,direction for motorDrive():dir=0,counterclock;dir=1,clock //pulse_cycle=2000;//pulse_cycle is 2000us(2 ms) //wait_time=pulse_cycle/2;//duty cycle of high speed pulse is 50%,so wait time is 50% of pulse_cycle TH1=(65536-1000) >> 8; //set interrupt time of T0 TL1=(65536-1000); pul=~pul & 0x01; //if pul=1,pul=0;pul=0,pul=1 motorDrive(pul,dir);//emit pulse }
以上程序是有问题的,只是提供一种参考,因为T0中断服务程序执行时间过长,导致定时器中断时效性不是很好(即中断服务程序还未执行完,定时器又溢出了)。
由于51单片机性能有限,再怎么优化,定时器中断都不是实现步进电机控制的好方法,采用延时子程序对步进电机进行时序控制是相对来讲比较好的方法,时效性比定时器中断好得多。
相关文章推荐
- 51单片机定时器中断控制灯1S亮的程序
- 【数码管/定时器/中断】一个定时器控制数码管显示
- 51单片机STC89C52 定时器(中断方式)
- Cubieboard上使用U-boot通过定时器中断控制LED
- 51单片机 步进电机的驱动和旋转角度控制
- STM32 普通定时器(TIM3)中断 实现流水灯实验
- 51单片机使用74LS165实现并入串出(中断和轮询方法)
- 使用STM32的systick定时器中断实现RTC工作过程出错
- 用定时器0控制切换流水灯顺序,用外部中断控制两种数码管显示方式
- 用51单片机控制RTL8019AS实现以太网通讯
- 用龙芯1c库在裸机编程环境中实现硬件定时器中断
- 51单片机定时器0中断时执行外部中断0
- 51单片机控制zigbee模块实现的多机通讯系统
- [置顶] ARM开发(10)基于STM32的通用定时器中断控制蜂鸣器响
- 基于51单片机的按键控制超声波测距及显示(带提示音)的实现
- dsp控制DM9000实现802.3数据收发第三篇,调试过程程序第二版;接受部分可用,但是容易掉帧,采用的读取dm9000中断寄存器的方式获取数据
- 单片机控制步进电机T型加速实现原理
- 用龙芯1c库在RT-Thread下实现硬件定时器中断
- 【定时器/中断/PWM】利用一个定时器实现一路PWM波的输出---点亮LED
- 51单片机以定时中断的方法实现DS18B20时序