自动开锁器前期设计,遇见问题!
时间:10-02
整理:3721RD
点击:
这是一个前期模拟程序和简单的硬件系统实验,附上程序和图片,图1是舵机控制的PWM脉冲占空比,图2是在PROTUES上仿真的左转极限2ms脉宽总脉宽为20ms的波形图,仿真没有问题,硬件连线没有问题,开始我以为是单片机的电源负载不够,之后我单独换了一个电源提供给电机,还是没有预期的效果,想不明白,故请高手看看。
程序:
程序:
- #include<reg51.h>
- #define Stop 0
- #define Left 1
- #define Right 2
- sbit ControlPort =P2^0;
- sbit KeyLeft =P3^2;
- sbit KeyRight =P3^3;
- sbit KeyStop =P3^4;
- unsigned char TimeOutCounter=0,LeftOrRight=0;
- void InitialTimer(void)
- {
- EA=1;
- ET1=1;
- TMOD=0x10;
- TH1=(65535-500)/256; //定时0.5ms
- TL1=(65535-500)%256;
- }
- void ControlLeftOrRight(void)
- {
- if(KeyStop==0)
- {
- TR1=1;
- LeftOrRight=Stop;
- }
-
- if(KeyLeft==0)
- {
- TR1=1;
- LeftOrRight=Left;
- }
-
- if(KeyRight==0)
- {
- TR1=1;
- LeftOrRight=Right;
- }
- }
- void main()
- {
- P2=0;
- InitialTimer();
- while(1)
- {
- ControlLeftOrRight();
- }
- }
- void Timer1(void) interrupt 3
- {
- TH1=(65535-500)/256;
- TL1=(65535-500)%256;
- TimeOutCounter++;
- switch(LeftOrRight)
- {
- case 0:
- if(TimeOutCounter<=3) //1.5ms的停止脉宽
- {
- ControlPort=1;
- }
- else
- {
- ControlPort=0;
- }
- break;
- case 1:
- if(TimeOutCounter<=4) //2ms的左转极限脉宽
- {
- ControlPort=1;
- }
- else
- {
- ControlPort=0;
- }
- break;
- case 2:
- if(TimeOutCounter<=2) //1ms的右转极限脉宽
- {
- ControlPort=1;
- }
- else
- {
- ControlPort=0;
- }
- break;
- default:break;
- }
- if(TimeOutCounter==40)TimeOutCounter=0; //0.5*40=20ms总脉宽
- }