Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 #define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row #define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row #define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row #define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row #define _LCD_CLEAR 0x01 //Clear display #define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a //shifted display to its original position. //Display data RAM is unaffected. #define _LCD_CURSOR_OFF 0x0C //Turn off cursor #define _LCD_UNDERLINE_ON 0x0E //Underline cursor on #define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on #define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing //display data RAM #define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing //display data RAM #define _LCD_TURN_ON 0x0C //Turn Lcd display on #define _LCD_TURN_OFF 0x08 //Turn Lcd display off #define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing //display data RAM #define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing //display data RAM #define EN_DELAY 500 #define LCD_STROBE {LCD_EN = 1; Delay_us(EN_DELAY); LCD_EN = 0; Delay_us(EN_DELAY);}; sbit LCD_RS at LATC0_bit; sbit LCD_EN at LATC1_bit; sbit LCD_D4 at LATC2_bit; sbit LCD_D5 at LATC3_bit; sbit LCD_D6 at LATC4_bit; sbit LCD_D7 at LATC5_bit; sbit LCD_RS_Direction at TRISC0_bit; sbit LCD_EN_Direction at TRISC1_bit; sbit LCD_D4_Direction at TRISC2_bit; sbit LCD_D5_Direction at TRISC3_bit; sbit LCD_D6_Direction at TRISC4_bit; sbit LCD_D7_Direction at TRISC5_bit; void LCD_Cmd(char out_char) { LCD_RS = 0; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_STROBE if(out_char == 0x01)Delay_ms(2); } void LCD_Chr(char row, char column, char out_char) { switch(row){ case 1: LCD_Cmd(0x00 + (column - 1)); break; case 2: LCD_Cmd(0x40 + (column - 1)); break; case 3: LCD_Cmd(0x54 + (column - 1)); break; case 4: LCD_Cmd(0x68 + (column - 1)); break; } LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Chr_Cp(char out_char) { LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Init() { Delay_ms(200); LCD_RS_Direction = 0; LCD_EN_Direction = 0; LCD_D4_Direction = 0; LCD_D5_Direction = 0; LCD_D6_Direction = 0; LCD_D7_Direction = 0; LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE Delay_ms(30); LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE Delay_ms(10); LCD_Cmd(0x28); LCD_Cmd(0x06); } void LCD_Out(char row, char col, char *text) { while(*text) LCD_Chr(row, col++, *text++); } void LCD_Out_Cp(char *text) { while(*text) LCD_Chr_Cp(*text++); } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; TRISA = 0xC0; TRISB = 0x00; TRISC = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,"Testing..."); LCD_Out(2,1,"NHD-0216KZW-AB5"); while(1) { } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 #define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row #define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row #define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row #define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row #define _LCD_CLEAR 0x01 //Clear display #define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a //shifted display to its original position. //Display data RAM is unaffected. #define _LCD_CURSOR_OFF 0x0C //Turn off cursor #define _LCD_UNDERLINE_ON 0x0E //Underline cursor on #define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on #define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing //display data RAM #define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing //display data RAM #define _LCD_TURN_ON 0x0C //Turn Lcd display on #define _LCD_TURN_OFF 0x08 //Turn Lcd display off #define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing //display data RAM #define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing //display data RAM #define EN_DELAY 500 #define LCD_STROBE {LCD_EN = 1; Delay_us(EN_DELAY); LCD_EN = 0; Delay_us(EN_DELAY);}; sbit LCD_RS at LATC0_bit; sbit LCD_EN at LATC1_bit; sbit LCD_D4 at LATC2_bit; sbit LCD_D5 at LATC3_bit; sbit LCD_D6 at LATC4_bit; sbit LCD_D7 at LATC5_bit; sbit LCD_RS_Direction at TRISC0_bit; sbit LCD_EN_Direction at TRISC1_bit; sbit LCD_D4_Direction at TRISC2_bit; sbit LCD_D5_Direction at TRISC3_bit; sbit LCD_D6_Direction at TRISC4_bit; sbit LCD_D7_Direction at TRISC5_bit; void LCD_Cmd(char out_char) { LCD_RS = 0; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_STROBE if(out_char == 0x01)Delay_ms(2); } void LCD_Chr(char row, char column, char out_char) { switch(row){ case 1: LCD_Cmd(0x00 + (column - 1)); break; case 2: LCD_Cmd(0x40 + (column - 1)); break; case 3: LCD_Cmd(0x54 + (column - 1)); break; case 4: LCD_Cmd(0x68 + (column - 1)); break; } LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Chr_Cp(char out_char) { LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Init() { Delay_ms(200); LCD_RS_Direction = 0; LCD_EN_Direction = 0; LCD_D4_Direction = 0; LCD_D5_Direction = 0; LCD_D6_Direction = 0; LCD_D7_Direction = 0; LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE Delay_ms(30); LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE Delay_ms(10); LCD_Cmd(0x28); LCD_Cmd(0x06); } void LCD_Out(char row, char col, char *text) { while(*text) LCD_Chr(row, col++, *text++); } void LCD_Out_Cp(char *text) { while(*text) LCD_Chr_Cp(*text++); } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; TRISA = 0xC0; TRISB = 0x00; TRISC = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,"Testing..."); LCD_Out(2,1,"NHD-0216KZW-AB5"); while(1) { } }
#define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row
#define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row
#define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row
#define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row
#define _LCD_CLEAR 0x01 //Clear display
#define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a
//shifted display to its original position.
//Display data RAM is unaffected.
#define _LCD_CURSOR_OFF 0x0C //Turn off cursor
#define _LCD_UNDERLINE_ON 0x0E //Underline cursor on
#define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on
#define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing
//display data RAM
#define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing
//display data RAM
#define _LCD_TURN_ON 0x0C //Turn Lcd display on
#define _LCD_TURN_OFF 0x08 //Turn Lcd display off
#define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing
//display data RAM
#define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing
//display data RAM
#define EN_DELAY 500
#define LCD_STROBE {LCD_EN = 1; Delay_us(EN_DELAY); LCD_EN = 0; Delay_us(EN_DELAY);};
sbit LCD_RS at LATC0_bit;
sbit LCD_EN at LATC1_bit;
sbit LCD_D4 at LATC2_bit;
sbit LCD_D5 at LATC3_bit;
sbit LCD_D6 at LATC4_bit;
sbit LCD_D7 at LATC5_bit;
sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D4_Direction at TRISC2_bit;
sbit LCD_D5_Direction at TRISC3_bit;
sbit LCD_D6_Direction at TRISC4_bit;
sbit LCD_D7_Direction at TRISC5_bit;
void LCD_Cmd(char out_char) {
LCD_RS = 0;
LCD_D4 = (out_char & 0x10)?1:0;
LCD_D5 = (out_char & 0x20)?1:0;
LCD_D6 = (out_char & 0x40)?1:0;
LCD_D7 = (out_char & 0x80)?1:0;
LCD_STROBE
LCD_D4 = (out_char & 0x01)?1:0;
LCD_D5 = (out_char & 0x02)?1:0;
LCD_D6 = (out_char & 0x04)?1:0;
LCD_D7 = (out_char & 0x08)?1:0;
LCD_STROBE
if(out_char == 0x01)Delay_ms(2);
}
void LCD_Chr(char row, char column, char out_char) {
switch(row){
case 1:
LCD_Cmd(0x80 + (column - 1));
break;
case 2:
LCD_Cmd(0xC0 + (column - 1));
break;
case 3:
LCD_Cmd(0x94 + (column - 1));
break;
case 4:
LCD_Cmd(0xD4 + (column - 1));
break;
}
LCD_RS = 1;
LCD_D4 = (out_char & 0x10)?1:0;
LCD_D5 = (out_char & 0x20)?1:0;
LCD_D6 = (out_char & 0x40)?1:0;
LCD_D7 = (out_char & 0x80)?1:0;
LCD_STROBE
LCD_D4 = (out_char & 0x01)?1:0;
LCD_D5 = (out_char & 0x02)?1:0;
LCD_D6 = (out_char & 0x04)?1:0;
LCD_D7 = (out_char & 0x08)?1:0;
LCD_EN = 1;
LCD_STROBE
}
void LCD_Chr_Cp(char out_char) {
LCD_RS = 1;
LCD_D4 = (out_char & 0x10)?1:0;
LCD_D5 = (out_char & 0x20)?1:0;
LCD_D6 = (out_char & 0x40)?1:0;
LCD_D7 = (out_char & 0x80)?1:0;
LCD_STROBE
LCD_D4 = (out_char & 0x01)?1:0;
LCD_D5 = (out_char & 0x02)?1:0;
LCD_D6 = (out_char & 0x04)?1:0;
LCD_D7 = (out_char & 0x08)?1:0;
LCD_EN = 1;
LCD_STROBE
}
void LCD_Init() {
Delay_ms(200);
LCD_RS_Direction = 0;
LCD_EN_Direction = 0;
LCD_D4_Direction = 0;
LCD_D5_Direction = 0;
LCD_D6_Direction = 0;
LCD_D7_Direction = 0;
LCD_RS = 0;
LCD_EN = 0;
LCD_D4 = 0;
LCD_D5 = 0;
LCD_D6 = 0;
LCD_D7 = 0;
Delay_ms(30);
LCD_D4 = 1;
LCD_D5 = 0;
LCD_D6 = 0;
LCD_D7 = 0;
LCD_STROBE
Delay_ms(30);
LCD_D4 = 1;
LCD_D5 = 0;
LCD_D6 = 0;
LCD_D7 = 0;
Delay_ms(30);
LCD_STROBE
LCD_D4 = 1;
LCD_D5 = 0;
LCD_D6 = 0;
LCD_D7 = 0;
Delay_ms(30);
LCD_STROBE
Delay_ms(30);
/*
LCD_D4 = 0;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
Delay_ms(10);
LCD_STROBE
*/
LCD_Cmd(0x08);
LCD_Cmd(0x01);
LCD_Cmd(0x06);
LCD_Cmd(0x02);
LCD_Cmd(0x0C);
}
void LCD_Out(char row, char col, char *text) {
while(*text)
LCD_Chr(row, col++, *text++);
}
void LCD_Out_Cp(char *text) {
while(*text)
LCD_Chr_Cp(*text++);
}
void main() {
CM1CON0 = 0x00;
CM2CON0 = 0x00;
SLRCON = 0x00;
ANSELA = 0x00;
ANSELB = 0x00;
ANSELC = 0x00;
TRISA = 0xC0;
TRISB = 0x00;
TRISC = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
LCD_Init();
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
LCD_Out(1,1,"Testing...");
LCD_Out(2,1,"NHD-0216KZW-AB5");
while(1) {
}
}
I even tried to add delays after all lcd_ commands but still it doesn't work properly
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 #define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row #define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row #define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row #define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row #define _LCD_CLEAR 0x01 //Clear display #define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a //shifted display to its original position. //Display data RAM is unaffected. #define _LCD_CURSOR_OFF 0x0C //Turn off cursor #define _LCD_UNDERLINE_ON 0x0E //Underline cursor on #define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on #define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing //display data RAM #define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing //display data RAM #define _LCD_TURN_ON 0x0C //Turn Lcd display on #define _LCD_TURN_OFF 0x08 //Turn Lcd display off #define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing //display data RAM #define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing //display data RAM #define EN_DELAY 500 #define LCD_STROBE {LCD_EN = 1; Delay_us(EN_DELAY); LCD_EN = 0; Delay_us(EN_DELAY);}; sbit LCD_RS at LATC0_bit; sbit LCD_EN at LATC1_bit; sbit LCD_D4 at LATC2_bit; sbit LCD_D5 at LATC3_bit; sbit LCD_D6 at LATC4_bit; sbit LCD_D7 at LATC5_bit; sbit LCD_RS_Direction at TRISC0_bit; sbit LCD_EN_Direction at TRISC1_bit; sbit LCD_D4_Direction at TRISC2_bit; sbit LCD_D5_Direction at TRISC3_bit; sbit LCD_D6_Direction at TRISC4_bit; sbit LCD_D7_Direction at TRISC5_bit; void LCD_Cmd(char out_char) { LCD_RS = 0; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_STROBE if(out_char == 0x01)Delay_ms(2); } void LCD_Chr(char row, char column, char out_char) { switch(row){ case 1: LCD_Cmd(0x2C + (column - 1)); break; case 2: LCD_Cmd(0x40 + (column - 1)); break; case 3: LCD_Cmd(0x54 + (column - 1)); break; case 4: LCD_Cmd(0x68 + (column - 1)); break; } LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Chr_Cp(char out_char) { LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Init() { Delay_ms(200); LCD_RS_Direction = 0; LCD_EN_Direction = 0; LCD_D4_Direction = 0; LCD_D5_Direction = 0; LCD_D6_Direction = 0; LCD_D7_Direction = 0; LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(10); LCD_Cmd(0x28); LCD_Cmd(0x06); } void LCD_Out(char row, char col, char *text) { while(*text) LCD_Chr(row, col++, *text++); } void LCD_Out_Cp(char *text) { while(*text) LCD_Chr_Cp(*text++); } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; TRISA = 0xC0; TRISB = 0x00; TRISC = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,"Testing..."); LCD_Out(2,1,"NHD-0216KZW-AB5"); while(1) { } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 #define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row #define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row #define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row #define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row #define _LCD_CLEAR 0x01 //Clear display #define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a //shifted display to its original position. //Display data RAM is unaffected. #define _LCD_CURSOR_OFF 0x0C //Turn off cursor #define _LCD_UNDERLINE_ON 0x0E //Underline cursor on #define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on #define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing //display data RAM #define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing //display data RAM #define _LCD_TURN_ON 0x0C //Turn Lcd display on #define _LCD_TURN_OFF 0x08 //Turn Lcd display off #define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing //display data RAM //#define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing //display data RAM #define _LCD_SHIFT_RIGHT 0x1C //Shift display right without changing //display data RAM #define EN_DELAY 500 #define LCD_STROBE {LCD_EN = 1; Delay_us(EN_DELAY); LCD_EN = 0; Delay_us(EN_DELAY);}; sbit LCD_RS at LATC0_bit; sbit LCD_EN at LATC1_bit; sbit LCD_D4 at LATC2_bit; sbit LCD_D5 at LATC3_bit; sbit LCD_D6 at LATC4_bit; sbit LCD_D7 at LATC5_bit; sbit LCD_RS_Direction at TRISC0_bit; sbit LCD_EN_Direction at TRISC1_bit; sbit LCD_D4_Direction at TRISC2_bit; sbit LCD_D5_Direction at TRISC3_bit; sbit LCD_D6_Direction at TRISC4_bit; sbit LCD_D7_Direction at TRISC5_bit; void LCD_Cmd(char out_char) { LCD_RS = 0; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_STROBE if(out_char == 0x01)Delay_ms(2); } void LCD_Chr(char row, char column, char out_char) { switch(row){ case 1: LCD_Cmd(0x00 + (column - 1)); break; case 2: LCD_Cmd(0x40 + (column - 1)); break; case 3: LCD_Cmd(0x80 + (column - 1)); break; case 4: LCD_Cmd(0xc0 + (column - 1)); break; } LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Chr_Cp(char out_char) { LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Init() { Delay_ms(200); LCD_RS_Direction = 0; LCD_EN_Direction = 0; LCD_D4_Direction = 0; LCD_D5_Direction = 0; LCD_D6_Direction = 0; LCD_D7_Direction = 0; LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(10); LCD_Cmd(0x28); LCD_Cmd(0x06); } void LCD_Out(char row, char col, char *text) { while(*text) LCD_Chr(row, col++, *text++); } void LCD_Out_Cp(char *text) { while(*text) LCD_Chr_Cp(*text++); } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; TRISA = 0xC0; TRISB = 0x00; TRISC = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,"Testing..."); LCD_Out(2,1,"NHD-0216KZW-AB5"); while(1) { } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 #define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row #define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row #define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row #define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row #define _LCD_CLEAR 0x01 //Clear display #define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a //shifted display to its original position. //Display data RAM is unaffected. #define _LCD_CURSOR_OFF 0x0C //Turn off cursor #define _LCD_UNDERLINE_ON 0x0E //Underline cursor on #define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on #define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing //display data RAM #define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing //display data RAM #define _LCD_TURN_ON 0x0C //Turn Lcd display on #define _LCD_TURN_OFF 0x08 //Turn Lcd display off #define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing //display data RAM //#define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing //display data RAM #define _LCD_SHIFT_RIGHT 0x1C //Shift display right without changing //display data RAM #define EN_DELAY 500 #define LCD_STROBE {LCD_EN = 1; Delay_us(EN_DELAY); LCD_EN = 0; Delay_us(EN_DELAY);}; sbit LCD_RS at LATC0_bit; sbit LCD_EN at LATC1_bit; sbit LCD_D4 at LATC2_bit; sbit LCD_D5 at LATC3_bit; sbit LCD_D6 at LATC4_bit; sbit LCD_D7 at LATC5_bit; sbit LCD_RS_Direction at TRISC0_bit; sbit LCD_EN_Direction at TRISC1_bit; sbit LCD_D4_Direction at TRISC2_bit; sbit LCD_D5_Direction at TRISC3_bit; sbit LCD_D6_Direction at TRISC4_bit; sbit LCD_D7_Direction at TRISC5_bit; void LCD_Cmd(char out_char) { LCD_RS = 0; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_STROBE if(out_char == 0x01)Delay_ms(2); } void LCD_Chr(char row, char column, char out_char) { switch(row){ case 1: LCD_Cmd(0x00 + (column - 1)); break; case 2: LCD_Cmd(0x40 + (column - 1)); break; case 3: LCD_Cmd(0x14 + (column - 1)); break; case 4: LCD_Cmd(0x54 + (column - 1)); break; } LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Chr_Cp(char out_char) { LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Init() { Delay_ms(200); LCD_RS_Direction = 0; LCD_EN_Direction = 0; LCD_D4_Direction = 0; LCD_D5_Direction = 0; LCD_D6_Direction = 0; LCD_D7_Direction = 0; LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(10); LCD_Cmd(0x28); LCD_Cmd(0x06); } void LCD_Out(char row, char col, char *text) { while(*text) LCD_Chr(row, col++, *text++); } void LCD_Out_Cp(char *text) { while(*text) LCD_Chr_Cp(*text++); } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; TRISA = 0xC0; TRISB = 0x00; TRISC = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,"Testing..."); LCD_Out(2,1,"NHD-0216KZW-AB5"); while(1) { } }
#include <reg52.H>
//---------------------------------------------------------
sbit E = P3^4;
sbit D_I = P3^0;
sbit R_W = P3^7;
void Nybble();
void Delayms(int n){
int i;
int j;
for (i=0;i<n;i++)
for (j=0;j<1000;j++)
{;}
}
void command(char i){
P1 = i;
D_I =0;
R_W =0;
Nybble();
i = i<<4;
P1 = i;
Nybble();
}
void write(char i){
P1 = i;
D_I =1;
R_W =0;
Nybble();
i = i<<4;
P1 = i;
Nybble();
}
void Nybble()
{
E = 1;
Delayms(1);
E = 0;
}
void init(){
P1 = 0;
P3 = 0;
Delayms(15);
// P1 = 0x30;
Delayms(50);
Nybble();
Delayms(50);
Nybble();
Delayms(5);
Nybble();
Delayms(5);
P1= 0x20;
Nybble();
Delayms(5);
command(0x28);
Delayms(5);
command(0x10);
Delayms(5);
command(0x0C);
Delayms(5);
command(0x06);
Delayms(5);
}
void Output()
{
code char text1[] = {"Newhaven Display Int"};
code char text2[] = {"4 Lines 20 Character"};
code char text3[] = {" Yellow OLED "};
code char text4[] = {"4-bit initialization"};
int i;
command(0x02);
for (i=0;i<20;i++){
write(text1[i]);
}
command(0xc0);
for (i=0;i<20;i++){
write(text2[i]);
}
command(0x94);
for (i=0;i<20;i++){
write(text3[i]);
}
command(0xd4);
for (i=0;i<20;i++){
write(text4[i]);
}
}
void main(void) {
P1=0;
P3=0;
while(1){
Delayms(10);
init();
command(0x01);
command(0x02);
command(0x80);
Output();
Delayms(10);
}
}
/*--------------------------------------------------------------------------
REG52.H
Header file for generic 80C52 and 80C32 microcontroller.
Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/
#ifndef __REG52_H__
#define __REG52_H__
/* BYTE Registers */
sfr P0 = 0x80;
sfr P1 = 0x90;
sfr P2 = 0xA0;
sfr P3 = 0xB0;
sfr PSW = 0xD0;
sfr ACC = 0xE0;
sfr B = 0xF0;
sfr SP = 0x81;
sfr DPL = 0x82;
sfr DPH = 0x83;
sfr PCON = 0x87;
sfr TCON = 0x88;
sfr TMOD = 0x89;
sfr TL0 = 0x8A;
sfr TL1 = 0x8B;
sfr TH0 = 0x8C;
sfr TH1 = 0x8D;
sfr IE = 0xA8;
sfr IP = 0xB8;
sfr SCON = 0x98;
sfr SBUF = 0x99;
/* 8052 Extensions */
sfr T2CON = 0xC8;
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2 = 0xCC;
sfr TH2 = 0xCD;
/* BIT Registers */
/* PSW */
sbit CY = PSW^7;
sbit AC = PSW^6;
sbit F0 = PSW^5;
sbit RS1 = PSW^4;
sbit RS0 = PSW^3;
sbit OV = PSW^2;
sbit P = PSW^0; //8052 only
/* TCON */
sbit TF1 = TCON^7;
sbit TR1 = TCON^6;
sbit TF0 = TCON^5;
sbit TR0 = TCON^4;
sbit IE1 = TCON^3;
sbit IT1 = TCON^2;
sbit IE0 = TCON^1;
sbit IT0 = TCON^0;
/* IE */
sbit EA = IE^7;
sbit ET2 = IE^5; //8052 only
sbit ES = IE^4;
sbit ET1 = IE^3;
sbit EX1 = IE^2;
sbit ET0 = IE^1;
sbit EX0 = IE^0;
/* IP */
sbit PT2 = IP^5;
sbit PS = IP^4;
sbit PT1 = IP^3;
sbit PX1 = IP^2;
sbit PT0 = IP^1;
sbit PX0 = IP^0;
/* P3 */
sbit RD = P3^7;
sbit WR = P3^6;
sbit T1 = P3^5;
sbit T0 = P3^4;
sbit INT1 = P3^3;
sbit INT0 = P3^2;
sbit TXD = P3^1;
sbit RXD = P3^0;
/* SCON */
sbit SM0 = SCON^7;
sbit SM1 = SCON^6;
sbit SM2 = SCON^5;
sbit REN = SCON^4;
sbit TB8 = SCON^3;
sbit RB8 = SCON^2;
sbit TI = SCON^1;
sbit RI = SCON^0;
/* P1 */
sbit T2EX = P1^1; // 8052 only
sbit T2 = P1^0; // 8052 only
/* T2CON */
sbit TF2 = T2CON^7;
sbit EXF2 = T2CON^6;
sbit RCLK = T2CON^5;
sbit TCLK = T2CON^4;
sbit EXEN2 = T2CON^3;
sbit TR2 = T2CON^2;
sbit C_T2 = T2CON^1;
sbit CP_RL2 = T2CON^0;
#endif
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 #define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row #define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row #define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row #define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row #define _LCD_CLEAR 0x01 //Clear display #define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a //shifted display to its original position. //Display data RAM is unaffected. #define _LCD_CURSOR_OFF 0x0C //Turn off cursor #define _LCD_UNDERLINE_ON 0x0E //Underline cursor on #define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on #define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing //display data RAM #define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing //display data RAM #define _LCD_TURN_ON 0x0C //Turn Lcd display on #define _LCD_TURN_OFF 0x08 //Turn Lcd display off #define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing //display data RAM #define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing //display data RAM #define EN_DELAY 1200 #define LCD_STROBE {LCD_EN = 1; Delay_us(EN_DELAY); LCD_EN = 0; Delay_us(EN_DELAY);}; sbit LCD_RS at LATC0_bit; sbit LCD_EN at LATC1_bit; sbit LCD_D4 at LATC2_bit; sbit LCD_D5 at LATC3_bit; sbit LCD_D6 at LATC4_bit; sbit LCD_D7 at LATC5_bit; sbit LCD_RS_Direction at TRISC0_bit; sbit LCD_EN_Direction at TRISC1_bit; sbit LCD_D4_Direction at TRISC2_bit; sbit LCD_D5_Direction at TRISC3_bit; sbit LCD_D6_Direction at TRISC4_bit; sbit LCD_D7_Direction at TRISC5_bit; void LCD_Cmd(char out_char) { LCD_RS = 0; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_STROBE if(out_char == 0x01)Delay_ms(2); } void LCD_Chr(char row, char column, char out_char) { switch(row){ case 1: LCD_Cmd(0x80 + (column - 1)); break; case 2: LCD_Cmd(0xC0 + (column - 1)); break; case 3: LCD_Cmd(0x94 + (column - 1)); break; case 4: LCD_Cmd(0xD4 + (column - 1)); break; } LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Chr_Cp(char out_char) { LCD_RS = 1; LCD_D4 = (out_char & 0x10)?1:0; LCD_D5 = (out_char & 0x20)?1:0; LCD_D6 = (out_char & 0x40)?1:0; LCD_D7 = (out_char & 0x80)?1:0; LCD_STROBE LCD_D4 = (out_char & 0x01)?1:0; LCD_D5 = (out_char & 0x02)?1:0; LCD_D6 = (out_char & 0x04)?1:0; LCD_D7 = (out_char & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Init() { Delay_ms(200); LCD_RS_Direction = 0; LCD_EN_Direction = 0; LCD_D4_Direction = 0; LCD_D5_Direction = 0; LCD_D6_Direction = 0; LCD_D7_Direction = 0; LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(30); LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; Delay_ms(3); LCD_STROBE Delay_ms(10); LCD_Cmd(0x28); LCD_Cmd(0x10); LCD_Cmd(0x0C); LCD_Cmd(0x06); } void LCD_Out(char row, char col, char *text) { while(*text) LCD_Chr(row, col++, *text++); } void LCD_Out_Cp(char *text) { while(*text) LCD_Chr_Cp(*text++); } void main() { CM1CON0 = 0x00; CM2CON0 = 0x00; SLRCON = 0x00; ANSELA = 0x00; ANSELB = 0x00; ANSELC = 0x00; TRISA = 0xC0; TRISB = 0x00; TRISC = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,"Testing..."); LCD_Out(2,1,"NHD-0216KZW-AB5"); while(1) { } }
#define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row
#define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row
#define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row
#define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row
#define _LCD_CLEAR 0x01 //Clear display
#define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a
//shifted display to its original position.
//Display data RAM is unaffected.
#define _LCD_CURSOR_OFF 0x0C //Turn off cursor
#define _LCD_UNDERLINE_ON 0x0E //Underline cursor on
#define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on
#define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing
//display data RAM
#define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing
//display data RAM
#define _LCD_TURN_ON 0x0C //Turn Lcd display on
#define _LCD_TURN_OFF 0x08 //Turn Lcd display off
#define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing
//display data RAM
#define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing
//display data RAM
#define EN_DELAY 1200
#define LCD_STROBE {LCD_EN = 1; Delay_us(EN_DELAY); LCD_EN = 0; Delay_us(EN_DELAY);};
sbit LCD_RS at LATC0_bit;
sbit LCD_EN at LATC1_bit;
sbit LCD_D4 at LATC2_bit;
sbit LCD_D5 at LATC3_bit;
sbit LCD_D6 at LATC4_bit;
sbit LCD_D7 at LATC5_bit;
sbit LCD_RS_Direction at TRISC0_bit;
sbit LCD_EN_Direction at TRISC1_bit;
sbit LCD_D4_Direction at TRISC2_bit;
sbit LCD_D5_Direction at TRISC3_bit;
sbit LCD_D6_Direction at TRISC4_bit;
sbit LCD_D7_Direction at TRISC5_bit;
char i = 0;
void LCD_Cmd(char out_char) {
LCD_RS = 0;
LCD_D4 = (out_char & 0x10)?1:0;
LCD_D5 = (out_char & 0x20)?1:0;
LCD_D6 = (out_char & 0x40)?1:0;
LCD_D7 = (out_char & 0x80)?1:0;
LCD_STROBE
LCD_D4 = (out_char & 0x01)?1:0;
LCD_D5 = (out_char & 0x02)?1:0;
LCD_D6 = (out_char & 0x04)?1:0;
LCD_D7 = (out_char & 0x08)?1:0;
LCD_STROBE
if(out_char == 0x01)Delay_ms(2);
}
void LCD_Chr(char row, char column, char out_char) {
switch(row){
case 1:
LCD_Cmd(0x80 + (column - 1));
break;
case 2:
LCD_Cmd(0xC0 + (column - 1));
break;
case 3:
LCD_Cmd(0x94 + (column - 1));
break;
case 4:
LCD_Cmd(0xD4 + (column - 1));
break;
}
LCD_RS = 1;
LCD_D4 = (out_char & 0x10)?1:0;
LCD_D5 = (out_char & 0x20)?1:0;
LCD_D6 = (out_char & 0x40)?1:0;
LCD_D7 = (out_char & 0x80)?1:0;
LCD_STROBE
LCD_D4 = (out_char & 0x01)?1:0;
LCD_D5 = (out_char & 0x02)?1:0;
LCD_D6 = (out_char & 0x04)?1:0;
LCD_D7 = (out_char & 0x08)?1:0;
LCD_EN = 1;
LCD_STROBE
}
void LCD_Chr_Cp(char out_char) {
LCD_RS = 1;
LCD_D4 = (out_char & 0x10)?1:0;
LCD_D5 = (out_char & 0x20)?1:0;
LCD_D6 = (out_char & 0x40)?1:0;
LCD_D7 = (out_char & 0x80)?1:0;
LCD_STROBE
LCD_D4 = (out_char & 0x01)?1:0;
LCD_D5 = (out_char & 0x02)?1:0;
LCD_D6 = (out_char & 0x04)?1:0;
LCD_D7 = (out_char & 0x08)?1:0;
LCD_EN = 1;
LCD_STROBE
}
void LCD_Init() {
Delay_ms(200);
LCD_RS_Direction = 0;
LCD_EN_Direction = 0;
LCD_D4_Direction = 0;
LCD_D5_Direction = 0;
LCD_D6_Direction = 0;
LCD_D7_Direction = 0;
LCD_RS = 0;
LCD_EN = 0;
LCD_D4 = 0;
LCD_D5 = 0;
LCD_D6 = 0;
LCD_D7 = 0;
Delay_ms(30);
LCD_D4 = 1;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
Delay_ms(3);
LCD_STROBE
Delay_ms(30);
LCD_D4 = 1;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
Delay_ms(3);
LCD_STROBE
Delay_ms(30);
LCD_D4 = 1;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
Delay_ms(3);
LCD_STROBE
Delay_ms(30);
LCD_D4 = 0;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
Delay_ms(3);
LCD_STROBE
Delay_ms(10);
LCD_Cmd(0x28);
LCD_Cmd(0x10);
LCD_Cmd(0x0C);
LCD_Cmd(0x06);
}
void LCD_Out(char row, char col, char *text) {
while(*text)
LCD_Chr(row, col++, *text++);
}
void LCD_Out_Cp(char *text) {
while(*text)
LCD_Chr_Cp(*text++);
}
void main() {
CM1CON0 = 0x00;
CM2CON0 = 0x00;
SLRCON = 0x00;
ANSELA = 0x00;
ANSELB = 0x00;
ANSELC = 0x00;
TRISA = 0xC0;
TRISB = 0x00;
TRISC = 0x00;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
LCD_Init();
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
LCD_Out(1,1,"Testing...");
LCD_Out(2,1,"NHD-0216KZW-AB5");
while(1) {
for(i = 0; i < 16; i++) {
LCD_Cmd(_LCD_SCROLL_LEFT);
Delay_ms(500);
}
LCD_Out(1,1,"Testing...");
LCD_Out(2,1,"NHD-0216KZW-AB5");
for(i = 0; i < 16; i++) {
LCD_Cmd(_LCD_SCROLL_RIGHT);
Delay_ms(500);
}
Delay_ms(1000);
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?