QQ圖片20180807203826.png (23.06 KB, 下載次數(shù): 42)
下載附件
proteus
2018-8-7 20:39 上傳
單片機源程序如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar code FFW[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
- uchar code REW[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};
- sbit K1=P3^0;
- sbit K2=P3^1;
- sbit K3=P3^2;
- sbit D40=P2^6;
- sbit D50=P2^7;
- void Delayms(uint i)
- {
- uchar j;
- while(i--)
- {
- for(j=0;j<115;j++)
- {
- ;
- }
- }
- }
- void setp_motor1_ffw(uchar n)//設(shè)置轉(zhuǎn)動的圈數(shù)
- {
- uchar i,j;
- for(i=0;i<5*n;i++)//一個八拍72度,旋轉(zhuǎn)一圈要5個八拍
- {
- for (j=0;j<8;j++)
- {
- if(K3==0)break;
- P1=FFW[j];
- Delayms(100);//兩個脈沖之間的長短,用來改變電機的轉(zhuǎn)速
- }
- }
- }
- void setp_motor2_ffw(uchar n)//設(shè)置轉(zhuǎn)動的圈數(shù)
- {
- uchar i,j;
- for(i=0;i<5*n;i++)//一個八拍72度,旋轉(zhuǎn)一圈要5個八拍
- {
- for (j=0;j<8;j++)
- {
- if(K3==0)break;
- P1=FFW[j];
- Delayms(100);//兩個脈沖之間的長短,用來改變電機的轉(zhuǎn)速
- }
- }
- }
- void setp_motor1_rew(uchar n)
- {
- uchar i,j;
- for(i=0;i<5*n;i++)
- {
- for (j=0;j<8;j++)
- {
- if(K3==0)break;
- P1=REW[j];
- Delayms(50);
- }
- }
- }
- void setp_motor2_rew(uchar n)
- {
- uchar i,j;
- for(i=0;i<5*n;i++)
- {
- for (j=0;j<8;j++)
- {
- if(K3==0)break;
- P1=REW[j];
- Delayms(50);
- }
- }
- }
- void main()
- {
- uchar n=5;
- while(1)
- {
- if(K1==0)
- {
- D40=1;
- setp_motor1_ffw(n);
- D40=0;
- D50=1;
- P0=0x00;
- setp_motor2_rew(n);
- D50=0;
- }
- else if(K2==0)
- {
- D40=1;
- setp_motor1_rew(n);
- D40=0;
- D50=1;
- P0=0x00;
- setp_motor2_ffw(n);
- D50=0;
- }
- else
- {
- P1=0x00;
- }
- }
- }
復(fù)制代碼
|