|
proteus
單片機(jī)源程序如下:
- #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)動(dòng)的圈數(shù)
- {
- uchar i,j;
- for(i=0;i<5*n;i++)//一個(gè)八拍72度,旋轉(zhuǎn)一圈要5個(gè)八拍
- {
- for (j=0;j<8;j++)
- {
- if(K3==0)break;
- P1=FFW[j];
- Delayms(100);//兩個(gè)脈沖之間的長(zhǎng)短,用來(lái)改變電機(jī)的轉(zhuǎn)速
- }
- }
- }
- void setp_motor2_ffw(uchar n)//設(shè)置轉(zhuǎn)動(dòng)的圈數(shù)
- {
- uchar i,j;
- for(i=0;i<5*n;i++)//一個(gè)八拍72度,旋轉(zhuǎn)一圈要5個(gè)八拍
- {
- for (j=0;j<8;j++)
- {
- if(K3==0)break;
- P1=FFW[j];
- Delayms(100);//兩個(gè)脈沖之間的長(zhǎng)短,用來(lái)改變電機(jī)的轉(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ù)制代碼
|
|