单片机源程序如下:
- #include<reg51.h>
- #define DATAOUT P0
- #define TH0_TL0 (65536-1000)//设定中断的间隔时长
- sbit Key_add=P1 ^ 0; //电机减速
- sbit Key_dec=P1 ^ 2; //电机加速
- sbit Key_turn=P1 ^ 1; //电机换向
- sbit PWM1=P2^6;//PWM 通道1,反转脉冲
- sbit PWM2=P2^7;//PWM 通道2,正转脉冲
- void LCD_write_char(unsigned char x,unsigned char y,char ch );
- sbit rs=P2^0;
- sbit rw=P2^1;
- sbit e=P2^2;
- void LCD_Writecmd(unsigned char cmd);
- void LCD_Writedata(unsigned char dat);
- void LCD_write_str(unsigned char x,unsigned char y,char str[]);
- void LCD_clear(void);
- void LCD_Init(void);
- void delay_us(unsigned char y);
- void delay_ms(unsigned char z);
- void Delay(unsigned char x);
- void Motor_speed_high(void);
- void Motor_speed_low(void);
- void Motor_turn(void);
- void Timer0_init(void);
- unsigned char count0 = 50;//低电平的占空比
- unsigned char Time_delay;
- unsigned char count1 = 0;//高电平的占空比
- bit Flag = 1;//电机正反转标志位,1正转,0反转
- void Delay(unsigned char x)
- {
- Time_delay = x;
- while(Time_delay != 0);//等待中断,可减少PWM输出时间间隔
- }
- void Motor_speed_high(void)//
- {
- if(Key_add==0)
- {
-
- Delay(10);
- if(Key_add==0)
- {
-
- count0 += 10;
-
-
- if(count0 >= 100)
- {
-
- count0 = 100;
- }
- }
-
- while(!Key_add);//等待键松开
- }
- }
- void Motor_speed_low(void)
- {
-
- if(Key_dec==0)
- {
-
- Delay(10);
- if(Key_dec==0)
- {
-
- count0 -= 10;
-
- if(count0 <= 0)
- {
-
- count0 = 0;
- }
- }
-
- while(!Key_dec );
- }
- }
- void Motor_turn(void)
- {
- if(Key_turn == 0)
- {
- Delay(10);
- if(Key_turn == 0)
- {
-
- Flag = ~Flag;
- }
- while(!Key_turn);
- }
- }
- void Timer0_init(void)
- {
- TMOD=0x01; //定时器0工作于方式1
- TH0=TH0_TL0/256;
- TL0=TH0_TL0%256;
- TR0=1;
- ET0=1;
- EA=1;
- }
- void Timer0_int(void) interrupt 1 using 1
- {
- TR0 = 0;//设置定时器初值期间,关闭定时器
- TL0 = TH0_TL0 % 256;
-
- TH0 = TH0_TL0 / 256; //定时器装初值
- TR0 = 1;
- if(Time_delay != 0)//延时函数用
- {
-
- Time_delay--;
- }
-
- if(Flag == 1)//电机正转
- {
- PWM1 = 0;
-
- if(++count1 < count0)
- {
-
- PWM2 = 1;
- }
- else
- PWM2 = 0;
- if(count1 >= 100)
- {
- count1=0;
- }
- }
-
- else //电机反转
- {
-
- PWM2 = 0;
-
- if(++count1 < count0)
- {
- PWM1 = 1;
- }
- else
- PWM1 = 0;
-
- if(count1 >= 100)
- {
-
- count1=0;
- }
- }
- }
- void delay_us(unsigned char j)/* 简易延时函数 */
- {
- unsigned char k,i;
- for(k=j;j>0;j--)
- for(i=100;i>0;i--);
- }
- void delay_ms(unsigned char j) /* 简易延时函数 */
- {
- unsigned char k,i;
- for(k=j;j>0;j--)
- for(i=256;i>0;i--);
- }
- void LCD_Writecmd(unsigned char cmd)//写指令到LCD1602
- {
- rs=0;
- delay_us(1);
- rw=0;
- delay_us(1);
-
- e=1;
- DATAOUT=cmd;
- delay_us(300);
- e=0;
- }
- void LCD_Writedata(unsigned char dat)//写数据到LCD1602
- {
- rs=1;
- delay_us(1);
- rw=0;
- delay_us(1);
- e=1;
- DATAOUT=dat;
- delay_us(300);
- e=0;
-
- }
- void LCD_write_char(unsigned char x,unsigned char y,char ch )//在指定位置显示一个字符
- {
- if(y==0)
- LCD_Writecmd(0x80+x);
- else
- LCD_Writecmd(0xc0+x);
- LCD_Writedata(ch);
- }
- void LCD_write_str(unsigned char x,unsigned char y,char str[])//使LCD1602显示一个字符串
- {
- unsigned char 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);
- }
- void LCD_Init(void)//LCD初始化
- {
- delay_ms(100);
- LCD_Writecmd(0x38);
- delay_ms(10);
- LCD_Writecmd(0x38);
- delay_ms(10);
- LCD_Writecmd(0x38);
- delay_ms(10);
- LCD_Writecmd(0x38);
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
直流电机.zip
(13.13 KB, 下载次数: 217)
|