00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "includes.h"
00012
00013 #define LcmIdleState 0
00014 #define LcmUpdateState 1
00015
00016 #define LcmHigh4bitSubstate 0
00017 #define LcmLow4bitSubstate 1
00018
00019 #define LcmData0 RJ4
00020 #define LcmData1 RJ5
00021 #define LcmData2 RJ6
00022 #define LcmData3 RJ7
00023
00024 #define LcmBackLightEn RB4
00025 #define LcmRs RB1
00026 #define LcmRw RB2
00027 #define LcmEn RB3
00028
00029
00030 INT8U LcmClean[]={0x01,0x00};
00031 INT8U LcmInitialCommand0[]={0x33,0x32,0x08,0x01,0x06,0x0c,0x00};
00032 INT8U Lcm1stLineStart[]={0x80,0x00};
00033 INT8U Lcm2ndLineStart[]={0xc0,0x00};
00034
00035 INT8U LcmBuffer[17];
00036 LCM_CONTROL LcmCtrl;
00037
00038
00039
00040
00041
00042 void LcmInit(void){
00043 TRISJ=0;
00044 TRISB=0;
00045
00046 LcmData0=1;
00047 LcmData1=1;
00048 LcmData2=1;
00049 LcmData3=1;
00050 LcmBackLightEn=1;
00051 LcmRs=1;
00052 LcmRw=1;
00053 LcmEn=1;
00054
00055 LcmCtrl.HasData=FALSE;
00056 LcmCtrl.Length=0;
00057 LcmCtrl.Timer=200;
00058 LcmCtrl.State=LcmIdleState;
00059 LcmCtrl.SubState=LcmHigh4bitSubstate;
00060 }
00061
00062
00063
00064
00065
00066 INT8U LcmStateGet(void){
00067 return LcmCtrl.State;
00068 }
00069
00070
00071
00072
00073
00074 ERROR LcmUpdate(INT8U *pLcmData,INT8U DataType){
00075 INT8U Length;
00076 const INT8U *pData;
00077
00078 if(LcmCtrl.State!=LcmIdleState){
00079 DebugStringPrint("\r\n Lcm0 error");
00080 return IS_ERROR;
00081 }
00082 pData=pLcmData;
00083 for(Length=0;Length<50;){
00084 if(*pData!=0){
00085 Length++;
00086 pData++;
00087 }
00088 else break;
00089 }
00090 if(Length>=50){
00091 DebugStringPrint("\r\n Lcm1 error");
00092 return IS_ERROR;
00093 }
00094 LcmCtrl.DataType=DataType;
00095 LcmCtrl.Length=Length;
00096 LcmCtrl.pData=pLcmData;
00097 LcmCtrl.SubState=LcmHigh4bitSubstate;
00098 LcmCtrl.State=LcmUpdateState;
00099 TMR1ON=0;
00100 LcmCtrl.HasData=TRUE;
00101 TMR1ON=1;
00102 while(LcmStateGet()!=0) LcmSvc();
00103 return NO_ERROR;
00104 }
00105
00106 void LcmSvc(void){
00107 if(LcmCtrl.HasData==FALSE) return;
00108 if(LcmCtrl.Timer!=0) return;
00109 LcmCtrl.Timer=10;
00110 if(LcmCtrl.DataType==LCM_COMMAND){
00111 if(LcmCtrl.SubState==LcmHigh4bitSubstate){
00112 LcmCommandWrite((*LcmCtrl.pData&0xf0)>>4);
00113 LcmCtrl.SubState=LcmLow4bitSubstate;
00114 }
00115 else{
00116 LcmCommandWrite(*LcmCtrl.pData&0x0f);
00117 LcmCtrl.Length--;
00118 LcmCtrl.pData++;
00119 LcmCtrl.SubState=LcmHigh4bitSubstate;
00120 }
00121 }
00122 else{
00123 if(LcmCtrl.SubState==LcmHigh4bitSubstate){
00124 LcmDramWrite((*LcmCtrl.pData&0xf0)>>4);
00125 LcmCtrl.SubState=LcmLow4bitSubstate;
00126 }
00127 else{
00128 LcmDramWrite(*LcmCtrl.pData&0x0f);
00129 LcmCtrl.Length--;
00130 LcmCtrl.pData++;
00131 LcmCtrl.SubState=LcmHigh4bitSubstate;
00132 }
00133 }
00134 if(LcmCtrl.Length==0) LcmCtrl.HasData=FALSE;
00135 }
00136
00137 void LcmCommandWrite(INT8U Command){
00138 LcmRw=0;
00139 LcmRw=0;
00140 LcmRw=0;
00141 LcmRs=0;
00142 LcmRs=0;
00143 LcmRs=0;
00144 if(Command&0x01) LcmData0=1;
00145 else LcmData0=0;
00146 if(Command&0x02) LcmData1=1;
00147 else LcmData1=0;
00148 if(Command&0x04) LcmData2=1;
00149 else LcmData2=0;
00150 if(Command&0x08) LcmData3=1;
00151 else LcmData3=0;
00152 LcmEn=1;
00153 LcmEn=1;
00154 LcmEn=1;
00155 LcmEn=0;
00156 LcmEn=0;
00157 LcmEn=0;
00158 }
00159
00160 void LcmDramWrite(INT8U Data){
00161 LcmRw=0;
00162 LcmRw=0;
00163 LcmRw=0;
00164 LcmRs=1;
00165 LcmRs=1;
00166 LcmRs=1;
00167 if(Data&0x01) LcmData0=1;
00168 else LcmData0=0;
00169 if(Data&0x02) LcmData1=1;
00170 else LcmData1=0;
00171 if(Data&0x04) LcmData2=1;
00172 else LcmData2=0;
00173 if(Data&0x08) LcmData3=1;
00174 else LcmData3=0;
00175 LcmEn=1;
00176 LcmEn=1;
00177 LcmEn=1;
00178 LcmEn=0;
00179 LcmEn=0;
00180 LcmEn=0;
00181 }
00182