00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "pic24_all.h"
00031
00032
00043 #define RS_HIGH() _LATB9 = 1
00044 #define RS_LOW() _LATB9 = 0
00045 #define CONFIG_RS() CONFIG_RB9_AS_DIG_OUTPUT()
00046
00047 #define RW_HIGH() _LATB13 = 1
00048 #define RW_LOW() _LATB13 = 0
00049 #define CONFIG_RW() CONFIG_RB13_AS_DIG_OUTPUT()
00050
00051 #define E_HIGH() _LATB14 = 1
00052 #define E_LOW() _LATB14 = 0
00053 #define CONFIG_E() CONFIG_RB14_AS_DIG_OUTPUT()
00054
00055 #define LCD4O _LATB5
00056 #define LCD5O _LATB6
00057 #define LCD6O _LATB7
00058 #define LCD7O _LATB8
00059 #define LCD7I _RB8
00060
00061 #define CONFIG_LCD4_AS_INPUT() CONFIG_RB5_AS_DIG_INPUT()
00062 #define CONFIG_LCD5_AS_INPUT() CONFIG_RB6_AS_DIG_INPUT()
00063 #define CONFIG_LCD6_AS_INPUT() CONFIG_RB7_AS_DIG_INPUT()
00064 #define CONFIG_LCD7_AS_INPUT() CONFIG_RB8_AS_DIG_INPUT()
00065
00066 #define CONFIG_LCD4_AS_OUTPUT() CONFIG_RB5_AS_DIG_OUTPUT()
00067 #define CONFIG_LCD5_AS_OUTPUT() CONFIG_RB6_AS_DIG_OUTPUT()
00068 #define CONFIG_LCD6_AS_OUTPUT() CONFIG_RB7_AS_DIG_OUTPUT()
00069 #define CONFIG_LCD7_AS_OUTPUT() CONFIG_RB8_AS_DIG_OUTPUT()
00070
00071 #define GET_BUSY_FLAG() LCD7I
00072
00073
00074
00080
00081 void configBusAsOutLCD(void) {
00082 RW_LOW();
00083 CONFIG_LCD4_AS_OUTPUT();
00084 CONFIG_LCD5_AS_OUTPUT();
00085 CONFIG_LCD6_AS_OUTPUT();
00086 CONFIG_LCD7_AS_OUTPUT();
00087 }
00088
00089
00090 void configBusAsInLCD(void) {
00091 CONFIG_LCD4_AS_INPUT();
00092 CONFIG_LCD5_AS_INPUT();
00093 CONFIG_LCD6_AS_INPUT();
00094 CONFIG_LCD7_AS_INPUT();
00095 RW_HIGH();
00096 }
00097
00098
00099 void outputToBusLCD(uint8 u8_c) {
00100 LCD4O = u8_c & 0x01;
00101 LCD5O = (u8_c >> 1)& 0x01;
00102 LCD6O = (u8_c >> 2)& 0x01;
00103 LCD7O = (u8_c >> 3)& 0x01;
00104 }
00105
00106
00107 void configControlLCD(void) {
00108 CONFIG_RS();
00109 CONFIG_RW();
00110 CONFIG_E();
00111 RW_LOW();
00112 E_LOW();
00113 RS_LOW();
00114 }
00115
00116
00117
00118 void pulseE(void) {
00119 DELAY_US(1);
00120 E_HIGH();
00121 DELAY_US(1);
00122 E_LOW();
00123 DELAY_US(1);
00124 }
00125
00126
00127
00128
00129
00130
00131 void writeLCD(uint8 u8_Cmd, uint8 u8_DataFlag,
00132 uint8 u8_CheckBusy, uint8 u8_Send8Bits) {
00133
00134 uint8 u8_BusyFlag;
00135 uint8 u8_wdtState;
00136 if (u8_CheckBusy) {
00137 RS_LOW();
00138
00139 configBusAsInLCD();
00140 u8_wdtState = _SWDTEN;
00141 CLRWDT();
00142 _SWDTEN = 1;
00143 do {
00144 E_HIGH();
00145 DELAY_US(1);
00146 u8_BusyFlag = GET_BUSY_FLAG();
00147 E_LOW();
00148 DELAY_US(1);
00149 pulseE();
00150 } while (u8_BusyFlag);
00151 _SWDTEN = u8_wdtState;
00152 } else {
00153 DELAY_MS(10);
00154 }
00155 configBusAsOutLCD();
00156 if (u8_DataFlag) RS_HIGH();
00157 else RS_LOW();
00158 outputToBusLCD(u8_Cmd >> 4);
00159 pulseE();
00160 if (u8_Send8Bits) {
00161 outputToBusLCD(u8_Cmd);
00162 pulseE();
00163 }
00164 }
00165
00166
00167 #define GOTO_LINE1() writeLCD(0x80,0,1,1)
00168 #define GOTO_LINE2() writeLCD(0xC0,0,1,1)
00169 #define GOTO_LINE3() writeLCD(0x94,0,1,1)
00170 #define GOTO_LINE4() writeLCD(0xD4,0,1,1)
00171
00172
00173 void initLCD() {
00174 DELAY_MS(50);
00175 writeLCD(0x20,0,0,0);
00176 writeLCD(0x28,0,0,1);
00177 writeLCD(0x28,0,0,1);
00178 writeLCD(0x06,0,0,1);
00179 writeLCD(0x0C,0,0,1);
00180 writeLCD(0x01,0,0,1);
00181 DELAY_MS(3);
00182 }
00183
00184
00185 void outStringLCD(char *psz_s) {
00186 while (*psz_s) {
00187 writeLCD(*psz_s, 1, 1,1);
00188 psz_s++;
00189 }
00190 }
00191
00192
00193 int main (void) {
00194 configBasic(HELLO_MSG);
00195
00196 configControlLCD();
00197 initLCD();
00198
00199 GOTO_LINE1();
00200 outStringLCD("Line 1");
00201 GOTO_LINE2();
00202 outStringLCD("Line 2");
00203 GOTO_LINE3();
00204 outStringLCD("Line 3");
00205 GOTO_LINE4();
00206 outStringLCD("Line 4");
00207 while (1) {
00208 doHeartbeat();
00209 }
00210 }