您好,欢迎来到宝玛科技网。
搜索
您的当前位置:首页直流电机的驱动程序

直流电机的驱动程序

来源:宝玛科技网


#include

#define uchar unsigned char

#define uint unsigned int

char flag=1; //按键标志,当flag=1时表示没有按下,当flag=0时表示有按键按下

sbit S1=P1^0; //电机pwm输入端

sbit S2=P1^1;

sbit S3=P1^2;

sbit S4=P1^3;

sbit EN=P0^0;

sbit INA=P0^1;

sbit INB=P0^2;

uint count=5;

void delayms(int ms)

1

{

uchar i;

while(ms--)

{

for(i=250;i>0;i--);

}

}

void init_sys(void) {

/*定时器初始化*/

TMOD=0x01;

TH0=0xd8;

TL0=0xf0;

/*系统初始化函数*/

2

TR0=1;

ET0=1;

EA=1;

}

void front() {

EN=1;

INA=1;

INB=0; }

void back() {

EN=1;

//电机正转

//方向端置1 电机正转

//电机反转

3

INA=0;

INB=1;

}

void main( )

{

init_sys();

while(1)

{

if(flag==1)

{

if(S1==0)

{

delayms(2); //延时去抖

4

if(S1==0)

{

flag=0;

front();

}

}

if(S2==0)

{

delayms(2); //延时去抖

if(S2==0)

{

flag=0;

back();

5

}

}

if(S3==0)

{

delayms(2); //延时去抖

if(S3==0)

{

flag=0;

count+=10;

if(count>9)count=9;

if(count<1)count=1;

}

}

6

if(S4==0)

{

delayms(2); //延时去抖

if(S4==0)

{

flag=0;

count--;

if(count>9)count=9;

if(count<1)count=1;

}

}

if(S1!=0 && S2!=0 && S3!=0 && S4!=0) flag=1;

}

7

}

}

void timer0(void) interrupt 1 using 2

{

static uchar click= 0; TH0=0xd8; TL0=0xf0;

++click;

if (click>=10) click=0;

if (click<=count) EN=1;

else

EN=0;

/*中断次数计数器变量*/

/*恢复定时器初始值*/

8

}

9

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baomayou.com 版权所有 赣ICP备2024042794号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务