- #include "STC15Fxxxx.h"
- #include "LCD1602.h"
- #include "delay.h"
- #include "Motor.h"
- #include "GPIO.h"
- sbit ENA=P2^5;
- sbit ENB=P2^4;
- sbit key=P2^7;
- sbit SetSpeedkey=P2^6;
- u16 T1InterruptCount=0,MotorPulsCount=0,MotorSpeed=0;
- u16 SetSpeed=0;
- u8 a=0,b=0,c=0;
- void Int0Init( )
- {
- IT0=1;
- EX0=1;
- EA=1;
- }
- void Timer1Init( )
- {
- AUXR &= 0xBF;
- TMOD &= 0x0F;
- TL1 = 0x00;
- TH1 = 0x4C;
- TF1 = 0;
- TR1 = 1;
- TR0 = 1;
- ET1=1;
- EA=1;
- }
- void Timer3Init(void) //1??@11.0592MHz
- {
- T4T3M |= 0x02;
- T3L = 0xCD; //每过1ms 中断一次
- T3H = 0xD4;
- T4T3M |= 0x08;
- IE2 =IE2|0x20;
- }
- void SpeedSet()
- {
- if(0==SetSpeedkey)
- {
- DelayMS(10);
- if(0==SetSpeedkey)
- {
- SetSpeed=SetSpeed+10;
- if(SetSpeed>100)
- SetSpeed=50;
- while(!SetSpeedkey);//等待释放
- }
- }
- }
- void main( )
- {
- bit KeyFlag=0;
- GPIOInit( );
- LCD_Init( );
- Int0Init( );
- Timer1Init( );
- Timer3Init( );
- MotorStop( );
-
-
- while(1)
- {
- if( 0==key )
- {
- DelayMS( 10 );
- if( 0==key )
- {
- KeyFlag=!KeyFlag;
- while( !key );
- if( KeyFlag )
- MotorGoHead( );
- else
- MotorStop( );
- }
- }
- SpeedSet();
- if(MotorSpeed<SetSpeed)
- {
- a++;
- c++;
- }
-
- LCD_Write_String( 6,0,"SetSpeed" );
- LCD_Write_MultChar( 0,1, c );
- LCD_Write_MultChar( 0,0,a );
- LCD_Write_String( 6,1," MotorSpeed" );
- }
- }
- void Int0_ISR( void ) interrupt 0
- {
- MotorPulsCount++;
- IE0=0;
- }
- void Timer1_ISR( void ) interrupt 3
- {
- T1InterruptCount++;
- if(T1InterruptCount>=20)//1????
- {
- TR1=0;
- MotorSpeed=MotorPulsCount*3;//MotorPulsCount*60/20/4
- MotorPulsCount=0;
- TR1=1;
- T1InterruptCount=0;
- }
- }
- void Timer3_ISR( void ) interrupt 19
- {
- b++;
- // if(b<a)
- //
- // MotorGoHead( );//1MS*a运动时间 1MS*b 停止时间 在100MS时间内 PWM
- // else
- // MotorStop( );
- if(b<a)
- ENA=1;
- else
- ENA=0;
- if(b<c)
- ENB=1;
- else
- ENB=0;
- if(b>100)
- {
- b=0;
- }
- }
复制代码
|