1、/MCU:AT89S51/晶振:12M#include regX52.H#include math.h#include intrins.h#define LCDIO P0 /定义P0口与LCD1602旳数据口相接#define ON 1#define OFF 0 sbit LCD_RS=P26; sbit LCD_RW=P25; sbit LCD_EN=P24; sbit LCD_BUSY=LCDIO7; sbit SW4=P20; / 速度设置加 sbit SW3=P21; / 速度设置减 sbit SW2=P22; /停 sbit SW1=P23; / 反转 sbit IN2=P34; /
2、红色旳线 sbit PWM=P35; /黄色旳线 sbit IN1=P36; /紫色旳线 int display; int flag; int time; int a; int s=25; unsigned long int p; unsigned long int xiao; unsigned long int h; unsigned long int v; unsigned char dispbuff4; /寄存各位数据/*1ms延时子程序*/void delay_nms(unsigned int n) unsigned int i; unsigned char j; for(i=0;in
3、;i+) for(j=0;j120;j+) ; /空操作/*1ms延时子程序*/*0.1ms延时子程序*/void control_speed(unsigned int n) unsigned char j; for(j=0;jn;j+) ; /空操作/*0.1ms延时子程序*/#define uchar unsigned char#define uint unsigned int/LCD驱动void LCD_check_busy(void) /检测LCD状态,看它是不是还在忙呢do LCD_EN=0; LCD_RS=0; LCD_RW=1; LCDIO=0xff; LCD_EN=1; whi
4、le(LCD_BUSY=1); LCD_EN=0;/*LCD1602写命令*/void LCD_send_command(uchar command,busy1)if(busy1) LCD_check_busy();LCD_RS=0;LCD_RW=0;LCD_EN=1;LCDIO=command;LCD_EN=0;/*LCD1602写数据*/void LCD_send_data(uchar dat)LCD_check_busy();LCD_RS=1;LCD_RW=0;LCD_EN=1;LCDIO=dat;LCD_EN=0;void LCD_write_char(uchar x,uchar y,
5、uchar dat) unsigned char address; if (x=0) address=0x80+y; else address=0xc0+y; LCD_send_command(address,0); LCD_send_data(dat);/*LCD1602显示字符串*/void LCD_disp_string(uchar x,uchar y,uchar *Data)if(x=0) if(y16) LCD_send_command(0x80+y,0); for(;y16&*Data!=0;y+) LCD_send_data(*(Data); Data+; if(*Data!=0
6、) y=0; x=1; if(x=1) LCD_send_command(0xc0+y,0); for(;y100) xiao=0;vs+;if(vs100)vs=100; if(SW3=0)/设定速度减 xiao+;if(xiao100) xiao=0;vs-;if(vswork); break; case 0x02: /反转 LCD_disp_string(1,0x07,(vs+Y*2) /速度过大,迅速调整 s=s-3; if(s=vs=(v-Y*2) /速度过大, 慢速调整。 s=s-1; /delay_nms(1);if(s(v+Y*2) /速度过大,迅速调整 s=s+3; if(s95)s=95; if(vs-Y)=v=(vs-Y*2) /速度过大, 慢速调整。 s=s+1; /delay_nms(1);if(s95)s=95; else s=s;/*速度自动调整(每一秒调整一次)*/ /*频率进入端口*/void inter1() interrupt 2p+; void timer0() interrupt 1/定期中断入口TH0=0x3c;TL0=0xb0; / 50ms /定期器赋值time+;if(time=20) /一秒time=0;v=p/2;p=0;xiao=0;display=1;