void LcdInit() { }
void LcdCheckAC() //功能:根据光标位置调整AC {
switch ( LcdCursor ) { case 16:
LcdSetAC(16); break;
LcdWriteCmd(0x30); //设置基本指令集 LcdDelay(3);
LcdWriteCmd(0x30); //设置基本指令集(需要再执行一次) LcdDelay(1);
LcdWriteCmd(0x0C); //开启显示 LcdDelay(3); LcdClear();
//清屏
//功能:LCD初始化
LcdDelay(250);
LcdWriteCmd(0x06); //设置进入点 LcdDelay(10);
case 32:
LcdSetAC(8); break;
case 48:
LcdSetAC(24);
}
break;
case 64:
LcdCursor = 0; LcdSetAC(0); break;
default: }
break;
void LcdPutChar(unsigned char c) //功能:显示ASCII码 { }
void LcdPutHZ(unsigned char ch, unsigned char cl) //功能:显示汉字 {
if ( LcdCursor & 0x01 )
{//显示汉字时,必须偶地址对准,即光标位置不能是奇数 }
LcdWriteDat(ch); LcdWriteDat(cl); LcdCursor += 2; LcdCheckAC();
LcdPutChar(' '); //额外输出一个空格 LcdWriteDat(c); LcdCursor++; LcdCheckAC();
}
void LcdPuts(unsigned char *s) //功能:显示字符串 { }
unsigned char ch, cl; for (;;) { }
ch = *s++;
if ( ch == '\\0' ) break; if ( ch < 0x80 ) { } else { }
cl = *s++; LcdPutHZ(ch,cl); LcdPutChar(ch);
8.1.2 主函数程序
/* */
#include
unsigned char SD[4]={'1','2','3','4'};//速度设定 unsigned char FK[4]={'2','2','3','4'};//速度反馈
unsigned int D=0; //方向控制中间变量 unsigned int Pwm=0; //速度产生中间变量
unsigned int Pwm_Value=0; //速度计算中间量 unsigned int Value=0; //速度初值
sbit KEY1=P2^0; //按键 sbit KEY2=P2^1; sbit KEY3=P2^2;
sbit MotorA=P1^6; //两个电机控制端 sbit MotorB=P1^7;
bit SWTR; bit SWTF;
unsigned int SWTV;
#define Pwm_MAX 255
//速度级数
//延时程序变量
/********************************************************/ 函数:Delay() 延时1ms 65.53s
t>0时,延时(t*0.001)s t=0时。延时65.53s
/********************************************************/
void Delay(unsigned int T) //延时程度 { SWTV=T; SWTR=1; while(!SWTF); SWTR=0; SWTF=0; }
void KEY() {
if(Pwm_Value>255) Pwm_Value=255; if(Pwm_Value<0) Pwm_Value=0;
if(KEY3==0) {
if(D==0) D=1; if(KEY1==0) { }
if(KEY2==0) { }
Pwm_Value=Pwm_Value-1; Pwm_Value=Pwm_Value+1;
//按键处理函数
else D=0; }