|
51单片机做控制芯片,控制电机转速
GT38无线串口作为通信控制
自制控制板,控制电机正反转
整合作为遥控船的动力控制系统
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg52.h>
- #include <stdio.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define lint long int
- sfr WDT_CONTR=0xe1;
- lint PWM_COUNT1;
- lint PWM_COUNT2;
- lint HUXI_COUNT1;
- lint HUXI_COUNT2;
- lint PWM_VLAUE1;
- lint PWM_VLAUE2;
- lint COUNTB;
- bit direc_flag1;
- bit direc_flag2;
- sbit open=P1^0;
- sbit stop=P1^1;
- sbit speed=P1^2;
- sbit slow=P1^3;
- sbit d1=P1^0;
- sbit d2=P1^1;
- uint flag,A;
- float sudu,pysd;
- /********************************************************************/
- void timer0()
- {
- TMOD=0x02;
- TH0=0x38;
- TL0=0X38;
- TR0=1;
- ET0=1;
- WDT_CONTR=0x35;
- }
- void timer1()
- {
- TMOD=0x20;
- TH1=0xfd;
- TL1=0Xfd;
- TR1=1;
- SM0=0;
- SM1=1;
- REN=1;
- }
- void delay(uchar z)
- {
- uchar x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void Delay2500ms()
- {
- unsigned char i, j, k;
-
-
- i = 114;
- j = 255;
- k = 107;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- /*********************************************************************/
- void init()
- {
- EA=0;
- ES=1;
- HUXI_COUNT1 = 0;
- HUXI_COUNT2 = 0;
- PWM_COUNT1 = 0;
- PWM_COUNT2 = 0;
-
- PWM_VLAUE1 = 1000;
- PWM_VLAUE2 = 1000;
- direc_flag1 = 0;
- direc_flag2 = 0;
- WDT_CONTR=0x35;
- }
- /**********************************************************************/
- /*
- key2=P3^4;
- key3=P3^5;
- key4=P3^6;
- key5=P3^7;
- */
- void control()
- {
- if(open==0)
- {
- delay(5);
- if(open==0)
- {
- EA=1;
- }
- while(!open);
- delay(1);
- while(!open);
- }
-
- if(stop==0)
- {
- delay(5);
- if(stop==0)
- { WDT_CONTR=0x35;
- EA=0;
- d1=0;
- d2=0;
- }
- while(!stop);
- delay(1);
- while(!stop);
- }
-
- if(speed==0)
- {
- delay(5);
- if(speed==0)
- {if(PWM_VLAUE1>0)
- PWM_VLAUE1=PWM_VLAUE1-50;
- else
- PWM_VLAUE1=0;
-
- if(PWM_VLAUE2>0)
- PWM_VLAUE2=PWM_VLAUE2-100;
- else
- PWM_VLAUE2=0;
- }
- while(!speed);
- delay(1);
- while(!speed);
- }
-
- if(slow==0)
- {
- delay(5);
- if(slow==0)
- {if(PWM_VLAUE1<1000)
- PWM_VLAUE1=PWM_VLAUE1+50;
- else
- PWM_VLAUE1=1000;
-
- if(PWM_VLAUE2<1000)
- PWM_VLAUE2=PWM_VLAUE2+100;
- else
- PWM_VLAUE2=1000;
- }
- while(!slow);
- delay(1);
- while(!slow);
- }
- }
- /***************sudu,pysu*****************/
- void counter()
- { int r=2;
- float sudu,f1,f2,*c,*m,pysd;
- f1=1-PWM_VLAUE1/1000;
- f2=1-PWM_VLAUE2/1000;
- sudu=f1*60/r;
- pysd=f2*60/r;
- c=&sudu;
- m=&pysd;
- }
- /***************???????*******************/
- void gt38()
- { float *c,*m;
- if(flag==1)
- { ES=0;
- flag=0;
- SBUF=*c;
- while(!TI);
- TI=0;
- ES=1;
- SBUF='a';
- while(!TI);
- TI=0;
- ES=1;
- SBUF='n';
- while(!TI);
- TI=0;
- ES=1;
- while(!TI);
- TI=0;
- ES=1;
- SBUF='d';
- while(!TI);
- TI=0;
- ES=1;
- SBUF=*m;
- while(!TI);
- TI=0;
- ES=1;
- WDT_CONTR=0x35;
- }
- }
- /*********************************************/
- /***************************************************/
- void main()
- {
- init();
- timer0();
- timer1();
- while(1)
- {
- control();
- counter();
- gt38();
- WDT_CONTR=0x35;
- }
- }
- /***********************************************************************/
- void time0()interrupt 1
- {
- PWM_COUNT1++;
- HUXI_COUNT1++;
- if(PWM_COUNT1 == PWM_VLAUE1)
- d1=1;
- if(PWM_COUNT1 == 1000)
- {
- d1=0;
- PWM_COUNT1 = 0;
- }
- if((HUXI_COUNT1==5000) && (direc_flag1 == 0))
- {
- HUXI_COUNT1=0;
- PWM_VLAUE1=PWM_VLAUE1-100;
- while(PWM_VLAUE1==500) //**********************************************//
- {direc_flag1=1;}
- }
-
- PWM_COUNT2++;
- HUXI_COUNT2++;
- if(PWM_COUNT2 == PWM_VLAUE2)
- d1=1;
- if(PWM_COUNT2 == 1000)
- {
- d1=0;
- PWM_COUNT2 = 0;
- }
- if((HUXI_COUNT2==5000) && (direc_flag2 == 0))
- {
- HUXI_COUNT2=0;
- PWM_VLAUE2=PWM_VLAUE2-200;
- while(PWM_VLAUE1==400) //********************************************//
- {direc_flag2=1;}
- }
- }
- /*********************************************/
- void es() interrupt 4
- {
- RI=0;
- scanf("%s=SBUF","Hi Tom date");
- flag=1;
- }
复制代码
所有资料51hei提供下载:
代码.rar
(503.33 KB, 下载次数: 20)
|
评分
-
查看全部评分
|