直流电机
2014-02-23 20:22
344 查看
原理图:
程序代码:
1602LCD头文件:
程序代码:
#include"reg51.h" #include"intrins.h" #include"LCD1602.H" /*****************宏定义********************/ #define uchar unsigned char #define uint unsigned int sbit P20=P2^0; sbit P21=P2^1; uchar flag=0; //高低电平标志 bit direction=0; //方向标志 static uchar constant=5; //可以改变占空比 /****************函数声明****************/ void delay(uchar i); void reverse(void); //反转子函数 /****************定时器t0***************/ void time0(void) interrupt 1 using 1 { static uchar i; i++; //频率为固定的1kHZ左右,只是占空比发生变化 if(i<=constant) flag=1; if(i<=10&&i>constant) // constant=5,可以改变占空比 flag=2; //初始化为5:5 if(i==10) i=0; TH0=0xff; TL0=0xe7; //定时25us初值重装 } /****************改变转向标志*****************/ void int1_srv (void) interrupt 2 using 2 { if(INT1==0) //外部中断1中断 { while(!INT1); //等待外部中断1按键放开 direction=!direction; //电机方向取反 } } /****************中断,调节占空比*****************/ void change(void) interrupt 0 using 0 { if(INT0==0) //外部中断0中断 { while(!INT0); //等待外部中断0按键放开 constant++; //改变占空比 /********************************/ write_lcd_string(0,0,"mc= : ! "); write_lcd_string(0,1,"speed= ! "); if(constant==10) { write_lcd_char(3,0,coder[0]); write_lcd_char(5,0,coder[1]); write_lcd_char(6,0,coder[0]); } else { _nop_(); //_nop_()相当于汇编的NOP] } if(constant!=10) { write_lcd_char(3,0,coder[constant]); write_lcd_char(5,0,coder[10-constant]); } else { _nop_(); //_nop_()相当于汇编的NOP] } /********************************/ if(constant==1)write_lcd_char(6,1,coder[1]); if(constant==2)write_lcd_char(6,1,coder[2]); if(constant==3)write_lcd_char(6,1,coder[3]); if(constant==4)write_lcd_char(6,1,coder[4]); if(constant==5)write_lcd_char(6,1,coder[5]); if(constant==6)write_lcd_char(6,1,coder[6]); if(constant==7)write_lcd_char(6,1,coder[7]); if(constant==8)write_lcd_char(6,1,coder[8]); if(constant==9)write_lcd_char(6,1,coder[9]); if(constant==10)write_lcd_char(6,1,coder[0]); /********************************/ if(constant==10) constant=0; } } /****************延时子程序****************/ void delay(uchar i) { while(i--) _nop_(); //_nop_()相当于汇编的NOP } /*****************主函数main***************/ void main() { EA=1; TMOD=0x01; //TO做定时,工作方式1 ET0=1; TR0=1; EX0=1; //开外部中断0 IT0=1; //下降沿触发 EX1=1; //开外部中断1 IT1=1; //下降沿触发 TH0=0xff; TL0=0xe7; //定时25us /*************液晶初始化***************/ lcd_init(); write_lcd_string(0,0,"mc=5:5 ! "); write_lcd_string(0,1,"speed=5! "); delay(10); /***************死循环*****************/ while(1) { reverse(); //调用反转子函数 } }
1602LCD头文件:
/*************************** //lcd1602.h //data 2009 ***************************/ unsigned char code coder[]="0123456789abcdef"; #define LCD_PORT P0 #define DISPLAYON 0x0c #define DISPLAYOFF 0x08 #define INPUTMODE 0x06 #define DISPLAYCLR 0x01 #define RETURNHOME 0x02 sbit LCD_RS=P2^4; sbit LCD_RW=P2^5; sbit LCD_EN=P2^6; void write_lcd_command(unsigned char); void write_lcd_data(unsigned char); void write_lcd_char(unsigned char x,bit y,unsigned char DATA); //void write_lcd_string(unsigned char x,unsigned char y,unsigned char *p); void lcd_init(); bit read_busy(); void gotoxy(unsigned char x,bit y); /***************************************************************/ void lcd_init() { write_lcd_command (DISPLAYCLR);//清屏 while(read_busy());//读忙标志 write_lcd_command (0x38);//8位数据发送模式 while(read_busy());//读忙标志 write_lcd_command (DISPLAYON);//0x0C while(read_busy());//读忙标志 write_lcd_command (INPUTMODE);//input mode while(read_busy());//读忙标志 write_lcd_command (0x80);//设置LCD开始显示地址 while(read_busy());//读忙标志 write_lcd_command (DISPLAYCLR);//清屏 } void write_lcd_command(unsigned char command) { LCD_PORT = command; LCD_RS = 0; LCD_RW = 0; LCD_EN = 1; LCD_EN = 0; } void write_lcd_data(unsigned char dat) { LCD_PORT = dat; LCD_RS = 1; LCD_RW = 0; LCD_EN = 1; LCD_EN = 0; } void write_lcd_char(unsigned char x,bit y,unsigned char DAT) { gotoxy( x, y); while(read_busy());//读忙标志 write_lcd_data(DAT); } void write_lcd_string(unsigned char x,unsigned char y,unsigned char *p) { gotoxy( x, y); while (*p) { while(read_busy());//读忙标志 write_lcd_data(*p); p++; } } bit read_busy() { unsigned char readdata; LCD_PORT = 0xff; LCD_RS = 0; LCD_RW = 1; LCD_EN = 1; readdata = LCD_PORT; LCD_EN = 0; if (readdata >> 7) return (1); else return (0); } void gotoxy(unsigned char x,bit y) { while(read_busy());//读忙标志 if(y) write_lcd_command(0xc0 + x);//2 line else write_lcd_command(0x80 + x);//1 line } 希望对大家有用,谢谢!
相关文章推荐
- 【连载】 FPGA Verilog HDL 系列实例--------直流电机PWM控制
- Arduino 各种模块篇 motor shield 电机扩展板(舵机、直流电机、步进电机party)
- 【Arduino学习笔记】使用L298N电机驱动模块驱动直流电机
- IK为交流电机,DK为直流电机,RK为可逆式正反转电机
- 直流电机和交流电机的区别
- 三相异步电机,直流电机工作原理
- 直流电机选型过程
- [51单片机] 以PWM控制直流电机为例建一个简单的51工程框架
- MCU—直流电机
- 直流电机PWM调速系统中控制电压非线性研究
- 直流电机 步进电机 伺服电机
- STM32F103定时器输出PWM波控制直流电机
- Arduino 入门程序示例之直流电机(2015-06-15)
- arduino-解决舵机与直流电机冲突问题
- 直流电机PID控制例程程序注释
- 直流电机 步进电机 伺服电机
- STM32F103定时器输出PWM波控制直流电机
- 直流电机故障问题
- 初了解—直流电机
- Arduino通过L298N红板控制板控制直流电机