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
00046 #define RS_HIGH() _LATB9 = 1
00047 #define RS_LOW() _LATB9 = 0
00048 #define CONFIG_RS() CONFIG_RB9_AS_DIG_OUTPUT()
00049
00050 #define RW_HIGH() _LATB13 = 1
00051 #define RW_LOW() _LATB13 = 0
00052 #define CONFIG_RW() CONFIG_RB13_AS_DIG_OUTPUT()
00053
00054 #define E_HIGH() _LATB14 = 1
00055 #define E_LOW() _LATB14 = 0
00056 #define CONFIG_E() CONFIG_RB14_AS_DIG_OUTPUT()
00057
00058 #define LCD4O _LATB5
00059 #define LCD5O _LATB6
00060 #define LCD6O _LATB7
00061 #define LCD7O _LATB8
00062 #define LCD7I _RB8
00063
00064 #define CONFIG_LCD4_AS_INPUT() CONFIG_RB5_AS_DIG_INPUT()
00065 #define CONFIG_LCD5_AS_INPUT() CONFIG_RB6_AS_DIG_INPUT()
00066 #define CONFIG_LCD6_AS_INPUT() CONFIG_RB7_AS_DIG_INPUT()
00067 #define CONFIG_LCD7_AS_INPUT() CONFIG_RB8_AS_DIG_INPUT()
00068
00069 #define CONFIG_LCD4_AS_OUTPUT() CONFIG_RB5_AS_DIG_OUTPUT()
00070 #define CONFIG_LCD5_AS_OUTPUT() CONFIG_RB6_AS_DIG_OUTPUT()
00071 #define CONFIG_LCD6_AS_OUTPUT() CONFIG_RB7_AS_DIG_OUTPUT()
00072 #define CONFIG_LCD7_AS_OUTPUT() CONFIG_RB8_AS_DIG_OUTPUT()
00073
00074 #define GET_BUSY_FLAG() LCD7I
00075
00081
00082 void configBusAsOutLCD(void) {
00083 RW_LOW();
00084 CONFIG_LCD4_AS_OUTPUT();
00085 CONFIG_LCD5_AS_OUTPUT();
00086 CONFIG_LCD6_AS_OUTPUT();
00087 CONFIG_LCD7_AS_OUTPUT();
00088 }
00089
00090
00091 void configBusAsInLCD(void) {
00092 CONFIG_LCD4_AS_INPUT();
00093 CONFIG_LCD5_AS_INPUT();
00094 CONFIG_LCD6_AS_INPUT();
00095 CONFIG_LCD7_AS_INPUT();
00096 RW_HIGH();
00097 }
00098
00099
00100 void outputToBusLCD(uint8 u8_c) {
00101 LCD4O = u8_c & 0x01;
00102 LCD5O = (u8_c >> 1)& 0x01;
00103 LCD6O = (u8_c >> 2)& 0x01;
00104 LCD7O = (u8_c >> 3)& 0x01;
00105 }
00106
00107
00108 void configControlLCD(void) {
00109 CONFIG_RS();
00110 CONFIG_RW();
00111 CONFIG_E();
00112 RW_LOW();
00113 E_LOW();
00114 RS_LOW();
00115 }
00116
00117
00118
00119 void pulseE(void) {
00120 DELAY_US(1);
00121 E_HIGH();
00122 DELAY_US(1);
00123 E_LOW();
00124 DELAY_US(1);
00125 }
00126
00127
00128
00129
00130
00131
00132 void writeLCD(uint8 u8_Cmd, uint8 u8_DataFlag,
00133 uint8 u8_CheckBusy, uint8 u8_Send8Bits) {
00134
00135 uint8 u8_BusyFlag;
00136 uint8 u8_wdtState;
00137 if (u8_CheckBusy) {
00138 RS_LOW();
00139
00140 configBusAsInLCD();
00141 u8_wdtState = _SWDTEN;
00142 CLRWDT();
00143 _SWDTEN = 1;
00144 do {
00145 E_HIGH();
00146 DELAY_US(1);
00147 u8_BusyFlag = GET_BUSY_FLAG();
00148 E_LOW();
00149 DELAY_US(1);
00150 pulseE();
00151 } while (u8_BusyFlag);
00152 _SWDTEN = u8_wdtState;
00153 } else {
00154 DELAY_MS(10);
00155 }
00156 configBusAsOutLCD();
00157 if (u8_DataFlag) RS_HIGH();
00158 else RS_LOW();
00159 outputToBusLCD(u8_Cmd >> 4);
00160 pulseE();
00161 if (u8_Send8Bits) {
00162 outputToBusLCD(u8_Cmd);
00163 pulseE();
00164 }
00165 }
00166
00167
00168 void initLCD() {
00169 DELAY_MS(50);
00170 writeLCD(0x20,0,0,0);
00171 writeLCD(0x28,0,0,1);
00172 writeLCD(0x28,0,0,1);
00173 writeLCD(0x06,0,0,1);
00174 writeLCD(0x0C,0,0,1);
00175 writeLCD(0x01,0,0,1);
00176 DELAY_MS(3);
00177 }
00178
00179
00180 void outStringLCD(char *psz_s) {
00181 while (*psz_s) {
00182 writeLCD(*psz_s, 1, 1,1);
00183 psz_s++;
00184 }
00185 }
00186
00187
00188 int main (void) {
00189 configBasic(HELLO_MSG);
00190
00191 configControlLCD();
00192 initLCD();
00193
00194 outStringLCD("******Hello, my name is Bob********");
00195 writeLCD(0xC0,0,1,1);
00196 outStringLCD("-----these lines are moving!-------");
00197 while (1) {
00198 writeLCD(0x18,0,1,1);
00199 DELAY_MS(200);
00200 doHeartbeat();
00201 }
00202 }