用单片机控制电机正反转,并用proteus进行了仿真
单片机源程序如下:
- #include <reg51.h>
- #include <math.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define ON 0
- #define OFF 1
- sbit PWM=P3^5;
- sbit MP=P3^4;
- bit FLAG=0;
- uchar code dispbit[6]={0x20,0x10,0x08,0x04,0x02,0x01};
-
- uchar code seg[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
- //0,1,2,3,4,5,6,7,8,9
- uchar disbuf[6]={0,0,0,0,10,10};
- uint temp[6];
- uint discount=0;
- uint timecount=0;//定时的次数
- uint T0count=0;
- uint hus=0;
- uint k=60;
- uint Vc;
- uint Vs;
- uint tm=3000;
- uint x;
- void t0_serv() interrupt 1
- { T0count++;
- }
- void t1_serv() interrupt 3
- {
- PWM=1;
- TH1=(65536-tm)/256;
- TL1=(65536-tm)%256;
- hus++;
- if(hus==tm)
- { PWM=0;
- TH1=(25536+tm)/256;
- TL1=(25536+tm)%256;
-
- }
- timecount++;
- if(timecount==250)
- {
- //FLAG=1;
- TR0=0;
- hus=0;
- timecount=0;
- Vc=T0count*65536+TH0*256+TL0;
- Vs=144;
- if(abs(Vc-Vs)>5)
- { tm=abs(k*(Vc-Vs));
- T0count=0;
- TR0=1;
- TR1=1;
- ET0=1;
- ET1=1;
- EA=1;
- }
- FLAG=1;
- TR0=0;
- timecount=0;
- }
- P0=dispbit[discount];
- P1=seg[disbuf[discount]];
- discount++;
- if(discount==6)
- { discount=0;
- }
- }
- void main()
- { uint i;
- TMOD=0x15;
- TH0=0;
- TL0=0;
- TH1=(65536-tm)/256;
- TL1=(65536-tm)%256;
- TR0=1;
- TR1=1;
- ET0=1;
- ET1=1;
- EA=1;
- while(1)
- { if(FLAG==1)
- { FLAG=0;
- x=T0count*65536+TH0*256+TL0;
- for(i=0;i<6;i++)
- {temp[i]=0;
- }
- i=0;
- while(x/10)
- {
- temp[i]=x%10;
- x=x/10;
- i++;
- }
- temp[i]=x;
- for(i=0;i<6;i++)
- {
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
仿真文件及原代码.zip
(73.85 KB, 下载次数: 24)
|