預想功能:按下按鍵,電機反轉(zhuǎn)5S后,延時一會,再以相同速度正轉(zhuǎn)5S,停止
現(xiàn)在的現(xiàn)象:下載程序后電機亂轉(zhuǎn)的,求求大佬了
單片機源程序如下:
- #include"reg51.h"
- #define uint16 unsigned int
- #define uint8 unsigned char
- #define GPIO_MOTOR P1
- sbit k1=P3^1;
- unsigned char code FFS[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09};//·′×a
- unsigned char code FFN[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08};//Õy×a
- typedef unsigned int u16;
- unsigned char Direction,Speed;
- void Delay(unsigned int t);
- void Motor();
- void Motor()
- {
- unsigned char i;
- for(i=0;i<8;i++)
- {
- if(Direction==1)
- GPIO_MOTOR=FFS[i]&0x1f;
- if(Direction==0)
- GPIO_MOTOR=FFN[i]&0x1f;
- Delay(Speed);
-
- }
- }
- void Delay(unsigned int t)//t=5000Ñó3ù4s
- {
- unsigned int k;
- while(t--)
- {
- for(k=0;k<80;k++)
- {}
- }
- }
- void main(void)
- {
-
- TMOD=0x10;
- TH1=0x3C;
- TL1=0xB0;
- EA=1;
- ET1=1;
- TR1=0;
-
-
- while(1)
- {
- if(k1==1)
- {
- Delay(1);
- if(k1==1)
- {
- TR1=1;
- Direction=1;
- Speed=10;
-
- }
- }
- else
- {
-
- Delay(5000);
- TR1=0;
-
- }
-
- }
- }
- void timer() interrupt 3
- {
- static u16 i;
- TH1=0x3C;
- TL1=0xB0;
- i++;
-
- if(i==100)
- {
- Delay(5000);
- Delay(5000);
- Delay(5000);
- Direction=0;
- Speed=10;
- i=0;
-
- }
- Motor();
- }
復制代碼
|