《直流电机调速控制系统的C语言程序(共9页).doc》由会员分享,可在线阅读,更多相关《直流电机调速控制系统的C语言程序(共9页).doc(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
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; / 反转 sb
2、it IN2=P34; /红色的线 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
3、j; for(i=0;in;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;
4、LCD_EN=1; while(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(uch
5、ar x,uchar y,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+
6、; if(*Data!=0) 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;专心-专注-专业