L298N直流步進(jìn)電機(jī)驅(qū)動直流電機(jī)程序
- #include
- #defineucharunsignedchar
- #defineuintunsignedint
- sbit INPUT1=P1^0;//控制口
- sbit INPUT2=P1^1;
- sbit INPUT3=P1^2;
- sbit INPUT4=P1^3;
- sbit ENA=P1^4;//產(chǎn)生PWM波
- sbit ENB=P1^5;
- uint MA=0,MB=0;
- uint SpeedA=20;//50%占空比
- uint SpeedB=20;
- voiddelay(uint z){
- uint x,y;
- for(x=z;x>0;x--)
- for(y=125;y>0;y--);
- }
- voidmain(void)
- {
- L298N_init();
- while(1){}
- }
- voidL298N_init(void)
- {
- delay(1000);
- delay(1000);
- INPUT1=1;INPUT2=0;
- INPUT3=1;INPUT4=0;//電機(jī)停此
- TH0=0xF4;
- TL0=0x48;
- TH1=0xF4;
- TL1=0x48;
- TMOD=0x11;
- TR0=1;
- TR1=1;
- ET0=1;
- ET1=1;
- EA=1;
- }
- voidtime0_int()interrupt 1using1
- {
- TR0=0;
- TH0=0xF4;
- TL0=0x48;
- MA++;
- if(MA
- ENA=1;
- }
- elseENA=0;
- if(MA==40){
- MA=0;
- }
- TR0=1;
- }
- voidtime1_int()interrupt 3using1
- {
- TR1=0;
- TH1=0xF4;
- TL1=0x48;
- MB=MB+1;
- if(MB
- ENB=1;
- }
- elseENB=0;
- if(MB==40){
- MB=0;
- }
- TR1=1;
- }
評論