本帖最后由 许明康2013 于 2015-4-29 12:02 编辑
四个按键,加减速和正反转,通过占空比调节速度;L298驱动,LCD1602显示设定速度和反馈速度
程序附录: 程序有问题,减速为零,电机还在转???波形也没有????
#include
#include
//IO口设置
***it a=P2^4; //L298信号口,{ a=1 ;b=0;} //启动输出 正转
***it b=P2^5; // { a=0 ;b=1;} //启动输出 反转
***it K1 =P1^0 ; //减少键
***it K2 =P1^1 ; //增加键
***it K3 =P1^2 ; //正转键
***it K4 =P1^3 ; //反转键
unsigned int speed_s=2; //设置的转速,单位r/s
unsigned int speed_m=0; //测量的转速
unsigned char zhuanxiang=1; //转向标志,0不转,1正转,2反转.默认正转
//LCD模块IO口以及子程序定义
#define LCD_DATA P0
***it LCD_RS = P2^0;
***it LCD_RW = P2^1;
***it LCD_EN = P2^2;
unsigned char dispBuff0[16]={'S','D',':',' ',' ',' ',' ',' ',' ',' ',' ','r','/','m','i','n'};//LCD第一行显示 不显示用空格
unsigned char dispBuff1[16]={'D','Q',':',' ',' ',' ',' ',' ',' ',' ',' ','r','/','m','i','n'};//LCD第二行显示
void Delay20ms() ;
void LCD_WriteDat(unsigned char lcd_dat);
void LCD_WriteCmd(unsigned char lcd_cmd);
unsigned char LCD_ReadStatus(void);
void LCD_Goto(unsigned char x,unsigned char y);
void LCD_Display(unsigned char row,unsigned char *str);
/*1、LCD模块子函数*/
void LCD_Init(void)
{
Delay20ms();
LCD_WriteCmd(0x38); //8位机接口、双行显示、5×7字符点阵;
LCD_WriteCmd(0x0c); //显示开启、光标不显示也不闪烁;
LCD_WriteCmd(0x01); //清屏;
LCD_WriteCmd(0x06); //光标右移一位、整屏不移动;
LCD_Goto(0,0);
}
void LCD_WriteDat(unsigned char lcd_dat)
{
unsigned char tmp;
tmp = LCD_ReadStatus(); //读状态;
while((tmp & 0x80)) //是否忙 ?
{
tmp = LCD_ReadStatus();
}
LCD_RS = 1;
LCD_RW = 0;
LCD_DATA = lcd_dat;
_nop_();
LCD_EN = 0;
_nop_();
_nop_();
LCD_EN = 1;
}
void LCD_WriteCmd(unsigned char lcd_cmd) //写指令数据到LCD
{
unsigned char tmp;
tmp = LCD_ReadStatus();
while((tmp & 0x80))
{
tmp = LCD_ReadStatus();
}
LCD_RS = 0;
LCD_RW = 0;
LCD_DATA = lcd_cmd;
_nop_();
LCD_EN = 0;
_nop_();
_nop_();
LCD_EN = 1;
}
unsigned char LCD_ReadStatus(void)
{
unsigned char tmp;
#if 0
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
tmp = LCD_DATA;
LCD_EN = 0;
#endif
LCD_DATA = 0xff;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 0;
_nop_();
_nop_();
LCD_EN = 1;
tmp = LCD_DATA;
return tmp;
}
void LCD_Goto(unsigned char x,unsigned char y)
{
unsigned char tmp;
if(y) //若是第二行;
{
tmp = 0xc0 + x;
LCD_WriteCmd(tmp);
}
else
{
tmp = 0x80 + x;
LCD_WriteCmd(tmp);
}
}
void LCD_Display(unsigned char row,unsigned char *str)
{
if(row)
{
LCD_Goto(0,1);
}
else
{
LCD_Goto(0,0);
}
while(*str != ' |