压缩包中含有程序、仿真图、HEX文件。
51单片机pwm控制直流电机的仿真原理图如下(proteus仿真工程可到本帖附件中下载):
仿真
单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define lcd P0
- #define KEY P3
- void LCD_Writecmd(uchar cmd);
- void LCD_Writedata(uchar dat);
- void LCD_write_char(uchar x,uchar y,char ch );
- void LCD_write_str(uchar x,uchar y,char str[]);
- void LCD_Init(void);
- void LCD_clear();
- void delay_us(uchar n);
- void delay_ms(uchar m);
- sbit RW=P2^5;
- sbit RS=P2^6;
- sbit EN=P2^7;
- sbit pwm1=P1^0;
- sbit pwm2=P1^1;
- sbit pwm=P1^3;
- void timer_init();
- uchar a=50,b=0,c=0,flag=0,flag1=0;
- void main()
- {
- LCD_Init();
- timer_init();
- LCD_write_str(0,0,"motor:");
- while(1)
- {
- b=a/10+0x30;
- c=a%10+0x30;
- if(KEY==0xfe)
- {
- LCD_clear();
- LCD_write_str(0,0,"motor:");
- LCD_write_str(0,1,"ZKB:");
- LCD_write_str(11,0,"ON");
- LCD_write_char(8,1,b);
- LCD_write_char(9,1,c);
- TR0=1;
- }
- if(KEY==0xfd)
- {
- LCD_clear();
- LCD_write_str(0,0,"motor:");
- LCD_write_str(0,1,"ZKB:");
- LCD_write_str(11,0,"OFF");
- LCD_write_char(9,1,0);
- TR0=0;
- pwm1=0;
- pwm2=0;
- }
- if(KEY==0xfb)
- {
- delay_ms(200);
- if(KEY!=0xfb)
- {
- flag1=0;
- a+=10;
- }
- }
- if(KEY==0xf7)
- {
- delay_ms(200);
- if(KEY!=0xf7)
- {
- flag1=1;
- a-=10;
- }
- }
- if(KEY==0xef)//正转
- {
- pwm=0;
- delay_ms(200);
- if(KEY!=0xef)
- {
- LCD_write_str(11,0," T");
- pwm=pwm1;
- TR0=1;
- pwm1=0;
- pwm2=1;
-
-
- }
- }
- if(KEY==0xdf)//反转
- {
- pwm=0;
- delay_ms(200);
- if(KEY!=0xef)
- {
- LCD_write_str(11,0," F");
- TR0=1;
- pwm=pwm2;
- pwm1=1;
- pwm2=0;
- }
- }
- if(flag1==1)
- {
-
- LCD_write_str(0,0,"motor:");
- LCD_write_str(0,1,"ZKB:");
- LCD_write_char(8,1,b);
- LCD_write_char(9,1,c);
- if(a<=1)a=99;
- }
- if(flag1==0)
- {
- LCD_write_str(0,0,"motor:");
- LCD_write_str(0,1,"ZKB:");
- LCD_write_char(8,1,b);
- LCD_write_char(9,1,c);
- if(a>=99)a=0;
- }
- }
- }
- /***********************************/
- void timer_init()
- {
- TMOD=0x01;
- TH0=(65535-a*100)/256;
- TL0=(65535-a*100)%256;
- EA=1;
- ET0=1;
- pwm1=1;
- }
- void timer0() interrupt 1
- {
- if(flag)
- {
- TH0=(65535-a*100)/256;
- TL0=(65535-a*100)%256;
- pwm1=1;
- flag=0;
- }
- else
- {
- TH0=((65535-(100-a)*100))/256;
- TL0=((65535-(100-a)*100))%256;
- pwm1=0;
- flag=1;
- }
- }
- /******************************/
- void delay_us(uchar n)
- {
- for(n=200;n>0;n--);
- }
- void delay_ms(unsigned char ms)
- {
- unsigned char i ;
- while(ms--)
- {
- for(i = 0 ; i < 120 ; i++) ;
- }
- }
- void LCD_Writecmd(uchar cmd)//写指令到LCD1602
- {
- RS=0;
- delay_us(1);
- RW=0;
- delay_us(1);
-
- EN=1;
- lcd=cmd;
- delay_us(300);
- EN=0;
- }
- void LCD_Writedata(uchar dat)//写数据到LCD1602
- {
- RS=1;
- delay_us(1);
- RW=0;
- delay_us(1);
- EN=1;
- lcd=dat;
- delay_us(300);
- EN=0;
-
- }
- void LCD_write_char(uchar x,uchar y,char ch )//在指定位置显示一个字符
- {
- if(y==0)
- LCD_Writecmd(0x80+x);
- else
- LCD_Writecmd(0xc0+x);
- LCD_Writedata(ch);
- }
- void LCD_write_str(uchar x,uchar y,char str[])//使LCD1602显示一个字符串
- {
- uchar i=0;
- if(y==0)
- LCD_Writecmd(0x80+x);
- else
- LCD_Writecmd(0xc0+x);
- while(str[i]!='\0')
- {
- LCD_Writedata(str[i]);
- ++i;
- }
-
- }
- void LCD_clear(void)//清屏函数
- {
- LCD_Writecmd(0x01);
- delay_ms(5);
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
51.zip
(24.11 KB, 下载次数: 143)
|