#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=10; //设置的转速,单位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 != ' |