jayanth.devarayanadurga
Banned
- Joined
- Dec 4, 2012
- Messages
- 4,280
- Helped
- 822
- Reputation
- 1,654
- Reaction score
- 791
- Trophy points
- 1,393
- Location
- Bangalore, India
- Activity points
- 0
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 #include <htc.h> #define _XTAL_FREQ 4000000 // 4 MHz clock __CONFIG(0X3F39); #define LCD_EN RC6 #define LCD_RS RC7 #define LCD_DATA PORTD #define LCD_STROBE LCD_EN = 1; __delay_ms(1); LCD_EN=0; void lcddata(unsigned char value); void lcdcmd(unsigned char value); void lcd_init(); void display (); unsigned char time [6] = {"Time:"}, val; void lcd_init() { LCD_EN=0; LCD_RS = 0; __delay_us(10); LCD_DATA=0X0c; LCD_EN = 1; __delay_ms(2); LCD_RS = 0; __delay_us(10); LCD_DATA=0X0c; LCD_STROBE; __delay_ms(2); LCD_RS = 0; __delay_us(10); LCD_DATA=0X0c; LCD_STROBE; __delay_ms(2); LCD_RS = 0; __delay_us(10); LCD_DATA=0X04; LCD_STROBE; __delay_ms(2); lcdcmd(0X28); __delay_ms(5); lcdcmd(0X0); __delay_ms(5); lcdcmd(0X01); __delay_ms(5); lcdcmd(0X06); __delay_ms(5); } void display (){ lcdcmd(0x80); for (char i=0;i<5;i++){ lcddata(time[i]); } } void lcddata(unsigned char val) { //8bit reversing val = (val & 0x0F) << 4 | (val & 0xF0) >> 4; val = (val & 0x33) << 2 | (val & 0xCC) >> 2; val = (val & 0x55) << 1 | (val & 0xAA) >> 1; LCD_RS = 1; LCD_DATA=(val&0x0f); LCD_STROBE; LCD_DATA =((val>>4)&0x0f); LCD_STROBE; __delay_ms(10); } void lcdcmd(unsigned char val) { //8bit reversing val = (val & 0x0F) << 4 | (val & 0xF0) >> 4; val = (val & 0x33) << 2 | (val & 0xCC) >> 2; val = (val & 0x55) << 1 | (val & 0xAA) >> 1; LCD_RS = 0; LCD_DATA=(val&0x0f); LCD_STROBE; LCD_DATA =((val>>4)&0x0f); LCD_STROBE; __delay_ms(10); } void main(){ TRISC = 0x00; TRISD = 0x00; PORTC = 0x00; PORTD = 0x00; lcd_init(); display(); while(1){ ; } }
#define LCD_D4 PORTBbits.RB0
#define LCD_D5 PORTCbits.RC7
#define LCD_D6 PORTDbits.RD1
#define LCD_D7 PORTAbits.RA0
// then to set/reset individual bits use
LCD_D4 = (value & 0x01 == 0)?0:1;
LCD_D5 = (value & 0x02 == 0)?0:1;
LCD_D6 = (value & 0x04 == 0)?0:1;
LCD_D7 = (value & 0x08 == 0)?0: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 LCD_PORT &= 0x0F;// Make Data pins zero LCD_PORT |= (Command&0xF0); // Write Upper nibble of data LCD_EN = 1; // Give a pulse on E pin __delay_us(EN_Delay); // so that LCD can latch the LCD_EN = 0; __delay_us(EN_Delay); LCD_PORT &= 0x0F;// Make Data pins zero LCD_PORT |= ((Command<<4)&0xF0); // Write Lower nibble of data LCD_EN = 1; // Give a pulse on E pin __delay_us(EN_Delay); // so that LCD can latch the LCD_EN = 0; __delay_us(EN_Delay);
#define ENBL EN=1; __delay_ms(1);EN=0;
#define RS RC0
#define EN RC1
#define LCD_D4 RE0
#define LCD_D5 RB3
#define LCD_D6 RC6
#define LCD_D7 RD7
void lcd_send(char type,unsigned char val) //type 0=command 1=data lcd value
{
RS=type;
LCD_D4 = (val & 0x10)?1:0;
LCD_D5 = (val & 0x20)?1:0;
LCD_D6 = (val & 0x40)?1:0;
LCD_D7 = (val & 0x80)?1:0;
ENBL;
LCD_D4 = (val & 0x01)?1:0;
LCD_D5 = (val & 0x02)?1:0;
LCD_D6 = (val & 0x04)?1:0;
LCD_D7 = (val & 0x08)?1:0;
ENBL;
__delay_us(100);
}
Code C - [expand] 1 2 3 4 LCD_D4 = (val & 0x10)?1:0; LCD_D5 = (val & 0x20)?1:0; LCD_D6 = (val & 0x40)?1:0; LCD_D7 = (val & 0x80)?1:0;
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 LCD_D4 = (val & 0x10)?1:0; LCD_D5 = (val & 0x20)?1:0; LCD_D6 = (val & 0x40)?1:0; LCD_D7 = (val & 0x80)?1:0; ENBL; LCD_D4 = (val & 0x01)?1:0; LCD_D5 = (val & 0x02)?1:0; LCD_D6 = (val & 0x04)?1:0; LCD_D7 = (val & 0x08)?1:0;
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 void LCD_Init() { ///////////////// Reset process from datasheet ////////////// _delay_ms(30); ////LCD_PORT &= 0x0F; // Make Data pins zero LCD_D4 = (val & 0x10)?1:0; LCD_D5 = (val & 0x20)?1:0; LCD_D6 = (val & 0x40)?1:0; LCD_D7 = (val & 0x80)?1:0; ////LCD_PORT |= 0x30; // Write 0x3 value on data bus LCD_EN = 1; // Give a pulse on E pin _delay_ms(EN_Delay); // so that LCD can latch the LCD_EN = 0; // data from data bus _delay_ms(EN_Delay); _delay_ms(6); LCD_PORT &= 0x0F; // Make Data pins zero LCD_D4 = (val & 0x10)?1:0; LCD_D5 = (val & 0x20)?1:0; LCD_D6 = (val & 0x40)?1:0; LCD_D7 = (val & 0x80)?1:0; ////LCD_PORT |= 0x30; // Write 0x3 value on data bus LCD_EN = 1; // Give a pulse on E pin _delay_ms(EN_Delay); // so that LCD can latch the LCD_EN = 0; // data from data bus _delay_ms(EN_Delay); _delay_ms(3); LCD_PORT &= 0x0F; // Make Data pins zero LCD_D4 = (val & 0x10)?1:0; LCD_D5 = (val & 0x20)?1:0; LCD_D6 = (val & 0x40)?1:0; LCD_D7 = (val & 0x80)?1:0; ////LCD_PORT |= 0x30; // Write 0x3 value on data bus LCD_EN = 1; // Give a pulse on E pin _delay_ms(EN_Delay); // so that LCD can latch the LCD_EN = 0; // data from data bus _delay_ms(EN_Delay); _delay_ms(2); LCD_PORT &= 0x0F; // Make Data pins zero LCD_D4 = (val & 0x10)?1:0; LCD_D5 = (val & 0x20)?1:0; LCD_D6 = (val & 0x40)?1:0; LCD_D7 = (val & 0x80)?1:0; ////LCD_PORT |= 0x20; // Write 0x2 value on data bus LCD_EN = 1; // Give a pulse on E pin _delay_ms(EN_Delay); // so that LCD can latch the LCD_EN = 0; // data from data bus _delay_ms(EN_Delay); _delay_ms(2); /////////////// Reset Process End //////////////// LCD_Cmd(0x28); //function set LCD_Cmd(0x0C); //display on,cursor off,blink off LCD_Cmd(0x06); //entry mode, set increment }
void lcd_init()
{
EN=0;
RS=0;
__delay_us(10);
LCD_D4 = 1;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
// LCD=0X30;
ENBL;
__delay_ms(2);
RS=0;
__delay_us(10);
LCD_D4 = 1;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
// LCD=0X30;
ENBL;
__delay_ms(2);
RS=0;
__delay_us(10);
// LCD=0X0c;
LCD_D4 = 1;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
// LCD=0X30;
ENBL;
__delay_ms(2);
RS=0;
__delay_us(10);
LCD_D4 = 0;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
// LCD=0X20;
ENBL;
__delay_ms(2);
lcd_send(0,0X28);
__delay_ms(5);
lcd_send(0,0X0C);
__delay_ms(5);
lcd_send(0,0X01);
__delay_ms(5);
lcd_send(0,0X06);
__delay_ms(5);
}
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 /* * main.c * * Created: 5/7/2013 9:09:11 AM * Author: Jayanth Devarayanadurga */ #include <htc.h> //Define CPU Frequency //This must be defined, if __delay_ms() or //__delay_us() functions are used in the code #define _XTAL_FREQ 20000000 // Configuration word for PIC16F877 //__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON // & LVP_OFF & CPD_OFF & DEBUG_OFF); #define LCD_RS PORTBbits.RB0 #define LCD_EN PORTBbits.RB7 #define LCD_D4 PORTCbits.RC0 #define LCD_D5 PORTCbits.RC4 #define LCD_D6 PORTDbits.RD3 #define LCD_D7 PORTDbits.RD6 #define LCD_RS_Direction TRISBbits.TRISB0 #define LCD_EN_Direction TRISBbits.TRISB7 #define LCD_D4_Direction TRISCbits.TRISC0 #define LCD_D5_Direction TRISCbits.TRISC4 #define LCD_D6_Direction TRISDbits.TRISD3 #define LCD_D7_Direction TRISDbits.TRISD6 #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 // Constants #define EN_Delay 5 // Give a pulse on E pin // so that LCD can latch the // data from data bus #define LCD_STROBE LCD_EN = 1; __delay_ms(EN_Delay); LCD_EN = 0; __delay_ms(EN_Delay); //Function Prototypes void LCD_Cmd(unsigned char Command); void LCD_Ch(unsigned int row, unsigned int col, char LCDChar); void LCD_Init(); void LCD_Out(unsigned int row, unsigned int col, const char *str); void LCD_Cmd(unsigned char Command) { LCD_RS = 0; // It is a command LCD_D4 = (Command & 0x10)?1:0; LCD_D5 = (Command & 0x20)?1:0; LCD_D6 = (Command & 0x40)?1:0; LCD_D7 = (Command & 0x80)?1:0; LCD_EN = 1; __delay_ms(EN_Delay); LCD_EN = 0; __delay_ms(EN_Delay); LCD_D4 = (Command & 0x01)?1:0; LCD_D5 = (Command & 0x02)?1:0; LCD_D6 = (Command & 0x04)?1:0; LCD_D7 = (Command & 0x08)?1:0; LCD_EN = 1; __delay_ms(EN_Delay); LCD_EN = 0; __delay_ms(EN_Delay); if(Command == 0x01) __delay_ms(2); // Delay for cursor to return at zero position } void LCD_Ch(unsigned int row, unsigned int col, char LCDChar) { switch(row){ case 1: LCD_Cmd(0x80 + col-1); break; case 2: LCD_Cmd(0xC0 + col-1); break; case 3: LCD_Cmd(0x94 + col-1); break; case 4: LCD_Cmd(0xD4 + col-1); break; } LCD_RS = 1; // It is data LCD_D4 = (LCDChar & 0x10)?1:0; LCD_D5 = (LCDChar & 0x20)?1:0; LCD_D6 = (LCDChar & 0x40)?1:0; LCD_D7 = (LCDChar & 0x80)?1:0; LCD_EN = 1; __delay_ms(EN_Delay); LCD_EN = 0; __delay_ms(EN_Delay); LCD_D4 = (LCDChar & 0x01)?1:0; LCD_D5 = (LCDChar & 0x02)?1:0; LCD_D6 = (LCDChar & 0x04)?1:0; LCD_D7 = (LCDChar & 0x08)?1:0; LCD_EN = 1; __delay_ms(EN_Delay); LCD_EN = 0; __delay_ms(EN_Delay); } void LCD_Init() { LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; 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; ///////////////// Reset process from datasheet ////////////// __delay_ms(30); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_EN = 1; __delay_ms(EN_Delay); LCD_EN = 0; __delay_ms(EN_Delay); __delay_ms(6); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_EN = 1; __delay_ms(EN_Delay); LCD_EN = 0; __delay_ms(EN_Delay); __delay_ms(3); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_EN = 1; __delay_ms(EN_Delay); LCD_EN = 0; __delay_ms(EN_Delay); __delay_ms(2); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x2 value on data bus LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_EN = 1; __delay_ms(EN_Delay); LCD_EN = 0; __delay_ms(EN_Delay); __delay_ms(2); /////////////// Reset Process End //////////////// LCD_Cmd(0x28); //function set LCD_Cmd(0x0C); //display on,cursor off,blink off LCD_Cmd(0x06); //entry mode, set increment } void LCD_Out(unsigned int row, unsigned int col, const char *str) { while(*str) LCD_Ch(row,col++,*str++); // print first character on LCD row = 1; col = 1; } int main(void){ TRISA = 0x00; TRISB = 0x00; TRISC = 0x00; TRISD = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; LCD_Init(); LCD_Cmd(_LCD_CLEAR); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Out(1,1,"PIC18F458"); LCD_Out(2,1,"LCD 4-bit"); LCD_Out(3,1,"20X4"); LCD_Out(4,1,"Working?"); while(1); return (0); }
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 /* * main.c * * Created: 5/7/2013 9:09:11 AM * Author: Jayanth Devarayanadurga */ #include <htc.h> //Define CPU Frequency //This must be defined, if __delay_ms() or //__delay_us() functions are used in the code #define _XTAL_FREQ 4000000 // Configuration word for PIC16F877 //__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON // & LVP_OFF & CPD_OFF & DEBUG_OFF); #define LATA0_bit PORTAbits.RA0 #define LATA1_bit PORTAbits.RA1 #define LATA2_bit PORTAbits.RA2 #define LATA3_bit PORTAbits.RA3 #define LATA4_bit PORTAbits.RA4 #define LATA5_bit PORTAbits.RA5 #define LATA6_bit PORTAbits.RA6 #define LATA7_bit PORTAbits.RA7 #define LATB0_bit PORTBbits.RB0 #define LATB1_bit PORTBbits.RB1 #define LATB2_bit PORTBbits.RB2 #define LATB3_bit PORTBbits.RB3 #define LATB4_bit PORTBbits.RB4 #define LATB5_bit PORTBbits.RB5 #define LATB6_bit PORTBbits.RB6 #define LATB7_bit PORTBbits.RB7 #define LATC0_bit PORTCbits.RC0 #define LATC1_bit PORTCbits.RC1 #define LATC2_bit PORTCbits.RC2 #define LATC3_bit PORTCbits.RC3 #define LATC4_bit PORTCbits.RC4 #define LATC5_bit PORTCbits.RC5 #define LATC6_bit PORTCbits.RC6 #define LATC7_bit PORTCbits.RC7 #define LATD0_bit PORTDbits.RD0 #define LATD1_bit PORTDbits.RD1 #define LATD2_bit PORTDbits.RD2 #define LATD3_bit PORTDbits.RD3 #define LATD4_bit PORTDbits.RD4 #define LATD5_bit PORTDbits.RD5 #define LATD6_bit PORTDbits.RD6 #define LATD7_bit PORTDbits.RD7 #define TRISA0_bit TRISAbits.TRISA0 #define TRISA1_bit TRISAbits.TRISA1 #define TRISA2_bit TRISAbits.TRISA2 #define TRISA3_bit TRISAbits.TRISA3 #define TRISA4_bit TRISAbits.TRISA4 #define TRISA5_bit TRISAbits.TRISA5 #define TRISA6_bit TRISAbits.TRISA6 #define TRISA7_bit TRISAbits.TRISA7 #define TRISB0_bit TRISBbits.TRISB0 #define TRISB1_bit TRISBbits.TRISB1 #define TRISB2_bit TRISBbits.TRISB2 #define TRISB3_bit TRISBbits.TRISB3 #define TRISB4_bit TRISBbits.TRISB4 #define TRISB5_bit TRISBbits.TRISB5 #define TRISB6_bit TRISBbits.TRISB6 #define TRISB7_bit TRISBbits.TRISB7 #define TRISC0_bit TRISCbits.TRISC0 #define TRISC1_bit TRISCbits.TRISC1 #define TRISC2_bit TRISCbits.TRISC2 #define TRISC3_bit TRISCbits.TRISC3 #define TRISC4_bit TRISCbits.TRISC4 #define TRISC5_bit TRISCbits.TRISC5 #define TRISC6_bit TRISCbits.TRISC6 #define TRISC7_bit TRISCbits.TRISC7 #define TRISD0_bit TRISDbits.TRISD0 #define TRISD1_bit TRISDbits.TRISD1 #define TRISD2_bit TRISDbits.TRISD2 #define TRISD3_bit TRISDbits.TRISD3 #define TRISD4_bit TRISDbits.TRISD4 #define TRISD5_bit TRISDbits.TRISD5 #define TRISD6_bit TRISDbits.TRISD6 #define TRISD7_bit TRISDbits.TRISD7 #define LCD_RS LATB0_bit #define LCD_EN LATB7_bit #define LCD_D4 LATC0_bit #define LCD_D5 LATC4_bit #define LCD_D6 LATD3_bit #define LCD_D7 LATD6_bit #define LCD_RS_Direction TRISB0_bit #define LCD_EN_Direction TRISB7_bit #define LCD_D4_Direction TRISC0_bit #define LCD_D5_Direction TRISC4_bit #define LCD_D6_Direction TRISD3_bit #define LCD_D7_Direction TRISD6_bit #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 // Constants #define EN_Delay 500 // Give a pulse on E pin // so that LCD can latch the // data from data bus #define LCD_STROBE {LCD_EN = 1; __delay_us(EN_Delay); LCD_EN = 0; __delay_us(EN_Delay);}; //Function Prototypes void LCD_Cmd(unsigned char Command); void LCD_Ch(unsigned int row, unsigned int col, char LCDChar); void LCD_Init(); void LCD_Out(unsigned int row, unsigned int col, const char *str); void LCD_Cmd(unsigned char Command) { LCD_RS = 0; // It is a command LCD_D4 = (Command & 0x10)?1:0; LCD_D5 = (Command & 0x20)?1:0; LCD_D6 = (Command & 0x40)?1:0; LCD_D7 = (Command & 0x80)?1:0; LCD_STROBE LCD_D4 = (Command & 0x01)?1:0; LCD_D5 = (Command & 0x02)?1:0; LCD_D6 = (Command & 0x04)?1:0; LCD_D7 = (Command & 0x08)?1:0; LCD_STROBE if(Command == 0x01) __delay_ms(2); // Delay for cursor to return at zero position } void LCD_Ch(unsigned int row, unsigned int col, char LCDChar) { switch(row){ case 1: LCD_Cmd(0x80 + col-1); break; case 2: LCD_Cmd(0xC0 + col-1); break; case 3: LCD_Cmd(0x94 + col-1); break; case 4: LCD_Cmd(0xD4 + col-1); break; } LCD_RS = 1; // It is data LCD_D4 = (LCDChar & 0x10)?1:0; LCD_D5 = (LCDChar & 0x20)?1:0; LCD_D6 = (LCDChar & 0x40)?1:0; LCD_D7 = (LCDChar & 0x80)?1:0; LCD_STROBE LCD_D4 = (LCDChar & 0x01)?1:0; LCD_D5 = (LCDChar & 0x02)?1:0; LCD_D6 = (LCDChar & 0x04)?1:0; LCD_D7 = (LCDChar & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Init() { LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_RS_Direction = 0; LCD_EN_Direction = 0; LCD_D4_Direction = 0; LCD_D5_Direction = 0; LCD_D6_Direction = 0; LCD_D7_Direction = 0; ///////////////// Reset process from datasheet ////////////// __delay_ms(30); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(6); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(3); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(2); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x2 value on data bus LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(2); /////////////// Reset Process End //////////////// LCD_Cmd(0x28); //function set LCD_Cmd(0x0C); //display on,cursor off,blink off LCD_Cmd(0x06); //entry mode, set increment } void LCD_Out(unsigned int row, unsigned int col, const char *str) { while(*str) LCD_Ch(row,col++,*str++); // print first character on LCD row = 1; col = 1; } int main(void){ TRISA = 0x00; TRISB = 0x00; TRISC = 0x00; TRISD = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; LCD_Init(); LCD_Cmd(_LCD_CLEAR); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Out(1,1,"PIC18F458"); LCD_Out(2,1,"LCD 4-bit"); LCD_Out(3,1,"20X4"); LCD_Out(4,1,"Working?"); while(1); return (0); }
Code C - [expand] 1 2 3 #pragma config WDTE = OFF #pragma config FOSC = FOSC_XT #pragma config PWRTE = ON
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 /* * main.c * * Created: 5/7/2013 9:09:11 AM * Author: Jayanth Devarayanadurga */ #include <stdio.h> #include <stdlib.h> #include <xc.h> //Define CPU Frequency //This must be defined, if __delay_ms() or //__delay_us() functions are used in the code #define _XTAL_FREQ 4000000 #pragma CONFIG1 WDTE = OFF #pragma CONFIG1 FOSC = FOSC_XT //#pragma config PWRTE = ON #define LATA0_bit LATAbits.LATA0 #define LATA1_bit LATAbits.LATA1 #define LATA2_bit LATAbits.LATA2 #define LATA3_bit LATAbits.LATA3 #define LATA4_bit LATAbits.LATA4 #define LATA5_bit LATAbits.LATA5 #define LATA6_bit LATAbits.LATA6 #define LATA7_bit LATAbits.LATA7 #define LATB0_bit LATBbits.LATB0 #define LATB1_bit LATBbits.LATB1 #define LATB2_bit LATBbits.LATB2 #define LATB3_bit LATBbits.LATB3 #define LATB4_bit LATBbits.LATB4 #define LATB5_bit LATBbits.LATB5 #define LATB6_bit LATBbits.LATB6 #define LATB7_bit LATBbits.LATB7 #define LATC0_bit LATCbits.LATC0 #define LATC1_bit LATCbits.LATC1 #define LATC2_bit LATCbits.LATC2 #define LATC3_bit LATCbits.LATC3 #define LATC4_bit LATCbits.LATC4 #define LATC5_bit LATCbits.LATC5 #define LATC6_bit LATCbits.LATC6 #define LATC7_bit LATCbits.LATC7 #define LATD0_bit LATDbits.LATD0 #define LATD1_bit LATDbits.LATD1 #define LATD2_bit LATDbits.LATD2 #define LATD3_bit LATDbits.LATD3 #define LATD4_bit LATDbits.LATD4 #define LATD5_bit LATDbits.LATD5 #define LATD6_bit LATDbits.LATD6 #define LATD7_bit LATDbits.LATD7 #define TRISA0_bit TRISAbits.TRISA0 #define TRISA1_bit TRISAbits.TRISA1 #define TRISA2_bit TRISAbits.TRISA2 #define TRISA3_bit TRISAbits.TRISA3 #define TRISA4_bit TRISAbits.TRISA4 #define TRISA5_bit TRISAbits.TRISA5 #define TRISA6_bit TRISAbits.TRISA6 #define TRISB0_bit TRISBbits.TRISB0 #define TRISB1_bit TRISBbits.TRISB1 #define TRISB2_bit TRISBbits.TRISB2 #define TRISB3_bit TRISBbits.TRISB3 #define TRISB4_bit TRISBbits.TRISB4 #define TRISB5_bit TRISBbits.TRISB5 #define TRISB6_bit TRISBbits.TRISB6 #define TRISB7_bit TRISBbits.TRISB7 #define TRISC0_bit TRISCbits.TRISC0 #define TRISC1_bit TRISCbits.TRISC1 #define TRISC2_bit TRISCbits.TRISC2 #define TRISC3_bit TRISCbits.TRISC3 #define TRISC4_bit TRISCbits.TRISC4 #define TRISC5_bit TRISCbits.TRISC5 #define TRISC6_bit TRISCbits.TRISC6 #define TRISC7_bit TRISCbits.TRISC7 #define TRISD0_bit TRISDbits.TRISD0 #define TRISD1_bit TRISDbits.TRISD1 #define TRISD2_bit TRISDbits.TRISD2 #define TRISD3_bit TRISDbits.TRISD3 #define TRISD4_bit TRISDbits.TRISD4 #define TRISD5_bit TRISDbits.TRISD5 #define TRISD6_bit TRISDbits.TRISD6 #define TRISD7_bit TRISDbits.TRISD7 #define LCD_RS LATB0_bit #define LCD_EN LATB7_bit #define LCD_D4 LATC0_bit #define LCD_D5 LATC4_bit #define LCD_D6 LATD3_bit #define LCD_D7 LATD6_bit #define LCD_RS_Direction TRISB0_bit #define LCD_EN_Direction TRISB7_bit #define LCD_D4_Direction TRISC0_bit #define LCD_D5_Direction TRISC4_bit #define LCD_D6_Direction TRISD3_bit #define LCD_D7_Direction TRISD6_bit #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 // Constants #define EN_Delay 500 // Give a pulse on E pin // so that LCD can latch the // data from data bus #define LCD_STROBE {LCD_EN = 1; __delay_us(EN_Delay); LCD_EN = 0; __delay_us(EN_Delay);}; //Function Prototypes void LCD_Cmd(unsigned char Command); void LCD_Ch(unsigned int row, unsigned int col, char LCDChar); void LCD_Init(); void LCD_Out(unsigned int row, unsigned int col, const char *str); void LCD_Cmd(unsigned char Command) { LCD_RS = 0; // It is a command LCD_D4 = (Command & 0x10)?1:0; LCD_D5 = (Command & 0x20)?1:0; LCD_D6 = (Command & 0x40)?1:0; LCD_D7 = (Command & 0x80)?1:0; LCD_STROBE LCD_D4 = (Command & 0x01)?1:0; LCD_D5 = (Command & 0x02)?1:0; LCD_D6 = (Command & 0x04)?1:0; LCD_D7 = (Command & 0x08)?1:0; LCD_STROBE if(Command == 0x01) __delay_ms(2); // Delay for cursor to return at zero position } void LCD_Ch(unsigned int row, unsigned int col, char LCDChar) { switch(row){ case 1: LCD_Cmd(0x80 + col-1); break; case 2: LCD_Cmd(0xC0 + col-1); break; case 3: LCD_Cmd(0x94 + col-1); break; case 4: LCD_Cmd(0xD4 + col-1); break; } LCD_RS = 1; // It is data LCD_D4 = (LCDChar & 0x10)?1:0; LCD_D5 = (LCDChar & 0x20)?1:0; LCD_D6 = (LCDChar & 0x40)?1:0; LCD_D7 = (LCDChar & 0x80)?1:0; LCD_STROBE LCD_D4 = (LCDChar & 0x01)?1:0; LCD_D5 = (LCDChar & 0x02)?1:0; LCD_D6 = (LCDChar & 0x04)?1:0; LCD_D7 = (LCDChar & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Init() { LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_RS_Direction = 0; LCD_EN_Direction = 0; LCD_D4_Direction = 0; LCD_D5_Direction = 0; LCD_D6_Direction = 0; LCD_D7_Direction = 0; ///////////////// Reset process from datasheet ////////////// __delay_ms(30); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(6); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(3); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(2); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x2 value on data bus LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(2); /////////////// Reset Process End //////////////// LCD_Cmd(0x28); //function set LCD_Cmd(0x0C); //display on,cursor off,blink off LCD_Cmd(0x06); //entry mode, set increment } void LCD_Out(unsigned int row, unsigned int col, const char *str) { while(*str) LCD_Ch(row,col++,*str++); // print first character on LCD row = 1; col = 1; } int main(void){ TRISA = 0x00; TRISB = 0x00; TRISC = 0x00; TRISD = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; PORTD = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LCD_Init(); LCD_Cmd(_LCD_CLEAR); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Out(1,1,"PIC18F458"); LCD_Out(2,1,"LCD 4-bit"); LCD_Out(3,1,"20X4"); LCD_Out(4,1,"Working?"); while(1); return (0); }
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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 /* * main.c * * Created: 5/7/2013 9:09:11 AM * Author: Jayanth Devarayanadurga */ #include <htc.h> //Define CPU Frequency //This must be defined, if __delay_ms() or //__delay_us() functions are used in the code #define _XTAL_FREQ 4000000 // Configuration word for PIC16F877 //__CONFIG( FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON // & LVP_OFF & CPD_OFF & DEBUG_OFF); #define LATA0_bit LATAbits.LATA0 #define LATA1_bit LATAbits.LATA1 #define LATA2_bit LATAbits.LATA2 #define LATA3_bit LATAbits.LATA3 #define LATA4_bit LATAbits.LATA4 #define LATA5_bit LATAbits.LATA5 #define LATA6_bit LATAbits.LATA6 #define LATA7_bit LATAbits.LATA7 #define LATB0_bit LATBbits.LATB0 #define LATB1_bit LATBbits.LATB1 #define LATB2_bit LATBbits.LATB2 #define LATB3_bit LATBbits.LATB3 #define LATB4_bit LATBbits.LATB4 #define LATB5_bit LATBbits.LATB5 #define LATB6_bit LATBbits.LATB6 #define LATB7_bit LATBbits.LATB7 #define LATC0_bit LATCbits.LATC0 #define LATC1_bit LATCbits.LATC1 #define LATC2_bit LATCbits.LATC2 #define LATC3_bit LATCbits.LATC3 #define LATC4_bit LATCbits.LATC4 #define LATC5_bit LATCbits.LATC5 #define LATC6_bit LATCbits.LATC6 #define LATC7_bit LATCbits.LATC7 #define LATD0_bit LATDbits.LATD0 #define LATD1_bit LATDbits.LATD1 #define LATD2_bit LATDbits.LATD2 #define LATD3_bit LATDbits.LATD3 #define LATD4_bit LATDbits.LATD4 #define LATD5_bit LATDbits.LATD5 #define LATD6_bit LATDbits.LATD6 #define LATD7_bit LATDbits.LATD7 #define TRISA0_bit TRISAbits.TRISA0 #define TRISA1_bit TRISAbits.TRISA1 #define TRISA2_bit TRISAbits.TRISA2 #define TRISA3_bit TRISAbits.TRISA3 #define TRISA4_bit TRISAbits.TRISA4 #define TRISA5_bit TRISAbits.TRISA5 #define TRISA6_bit TRISAbits.TRISA6 #define TRISB0_bit TRISBbits.TRISB0 #define TRISB1_bit TRISBbits.TRISB1 #define TRISB2_bit TRISBbits.TRISB2 #define TRISB3_bit TRISBbits.TRISB3 #define TRISB4_bit TRISBbits.TRISB4 #define TRISB5_bit TRISBbits.TRISB5 #define TRISB6_bit TRISBbits.TRISB6 #define TRISB7_bit TRISBbits.TRISB7 #define TRISC0_bit TRISCbits.TRISC0 #define TRISC1_bit TRISCbits.TRISC1 #define TRISC2_bit TRISCbits.TRISC2 #define TRISC3_bit TRISCbits.TRISC3 #define TRISC4_bit TRISCbits.TRISC4 #define TRISC5_bit TRISCbits.TRISC5 #define TRISC6_bit TRISCbits.TRISC6 #define TRISC7_bit TRISCbits.TRISC7 #define TRISD0_bit TRISDbits.TRISD0 #define TRISD1_bit TRISDbits.TRISD1 #define TRISD2_bit TRISDbits.TRISD2 #define TRISD3_bit TRISDbits.TRISD3 #define TRISD4_bit TRISDbits.TRISD4 #define TRISD5_bit TRISDbits.TRISD5 #define TRISD6_bit TRISDbits.TRISD6 #define TRISD7_bit TRISDbits.TRISD7 #define LCD_RS LATB0_bit #define LCD_EN LATB7_bit #define LCD_D4 LATC0_bit #define LCD_D5 LATC4_bit #define LCD_D6 LATD3_bit #define LCD_D7 LATD6_bit #define LCD_RS_Direction TRISB0_bit #define LCD_EN_Direction TRISB7_bit #define LCD_D4_Direction TRISC0_bit #define LCD_D5_Direction TRISC4_bit #define LCD_D6_Direction TRISD3_bit #define LCD_D7_Direction TRISD6_bit #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 // Constants #define EN_Delay 500 // Give a pulse on E pin // so that LCD can latch the // data from data bus #define LCD_STROBE {LCD_EN = 1; __delay_us(EN_Delay); LCD_EN = 0; __delay_us(EN_Delay);}; //Function Prototypes void LCD_Cmd(unsigned char Command); void LCD_Ch(unsigned int row, unsigned int col, char LCDChar); void LCD_Init(); void LCD_Out(unsigned int row, unsigned int col, const char *str); void LCD_Cmd(unsigned char Command) { LCD_RS = 0; // It is a command LCD_D4 = (Command & 0x10)?1:0; LCD_D5 = (Command & 0x20)?1:0; LCD_D6 = (Command & 0x40)?1:0; LCD_D7 = (Command & 0x80)?1:0; LCD_STROBE LCD_D4 = (Command & 0x01)?1:0; LCD_D5 = (Command & 0x02)?1:0; LCD_D6 = (Command & 0x04)?1:0; LCD_D7 = (Command & 0x08)?1:0; LCD_STROBE if(Command == 0x01) __delay_ms(2); // Delay for cursor to return at zero position } void LCD_Ch(unsigned int row, unsigned int col, char LCDChar) { switch(row){ case 1: LCD_Cmd(0x80 + col-1); break; case 2: LCD_Cmd(0xC0 + col-1); break; case 3: LCD_Cmd(0x94 + col-1); break; case 4: LCD_Cmd(0xD4 + col-1); break; } LCD_RS = 1; // It is data LCD_D4 = (LCDChar & 0x10)?1:0; LCD_D5 = (LCDChar & 0x20)?1:0; LCD_D6 = (LCDChar & 0x40)?1:0; LCD_D7 = (LCDChar & 0x80)?1:0; LCD_STROBE LCD_D4 = (LCDChar & 0x01)?1:0; LCD_D5 = (LCDChar & 0x02)?1:0; LCD_D6 = (LCDChar & 0x04)?1:0; LCD_D7 = (LCDChar & 0x08)?1:0; LCD_EN = 1; LCD_STROBE } void LCD_Init() { LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; LCD_RS_Direction = 0; LCD_EN_Direction = 0; LCD_D4_Direction = 0; LCD_D5_Direction = 0; LCD_D6_Direction = 0; LCD_D7_Direction = 0; ///////////////// Reset process from datasheet ////////////// __delay_ms(30); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(6); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(3); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(2); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x2 value on data bus LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE __delay_ms(2); /////////////// Reset Process End //////////////// LCD_Cmd(0x28); //function set LCD_Cmd(0x0C); //display on,cursor off,blink off LCD_Cmd(0x06); //entry mode, set increment } void LCD_Out(unsigned int row, unsigned int col, const char *str) { while(*str) LCD_Ch(row,col++,*str++); // print first character on LCD row = 1; col = 1; } int main(void){ TRISA = 0x00; TRISB = 0x00; TRISC = 0x00; TRISD = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; LATD = 0x00; LCD_Init(); LCD_Cmd(_LCD_CLEAR); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Out(1,1,"PIC18F458"); LCD_Out(2,1,"LCD 4-bit"); LCD_Out(3,1,"20X4"); LCD_Out(4,1,"Working?"); while(1); return (0); }
Code C - [expand] 1 2 #pragma CONFIG1 WDTE_OFF #pragma CONFIG1 FOSC_XT
#include <pic.h> //test
#define _XTAL_FREQ 20000000
#define LCD_RS RC0
#define LCD_EN RC1
#define LCD_D4 RB7
#define LCD_D5 RC6
#define LCD_D6 RD0
#define LCD_D7 RD7
#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
// Constants
#define EN_Delay 500
// Give a pulse on E pin
// so that LCD can latch the
// data from data bus
#define LCD_STROBE LCD_EN = 1; __delay_us(EN_Delay); LCD_EN = 0; __delay_us(EN_Delay);
//Function Prototypes
void LCD_Cmd(unsigned char Command);
void LCD_Ch(unsigned int row, unsigned int col, char LCDChar);
void LCD_Init();
void LCD_Out(unsigned int row, unsigned int col, const char *str);
void LCD_Cmd(unsigned char Command)
{
LCD_RS = 0; // It is a command
LCD_D4 = (Command & 0x10)?1:0;
LCD_D5 = (Command & 0x20)?1:0;
LCD_D6 = (Command & 0x40)?1:0;
LCD_D7 = (Command & 0x80)?1:0;
LCD_STROBE;
LCD_D4 = (Command & 0x01)?1:0;
LCD_D5 = (Command & 0x02)?1:0;
LCD_D6 = (Command & 0x04)?1:0;
LCD_D7 = (Command & 0x08)?1:0;
LCD_STROBE;
if(Command == 0x01) __delay_ms(2); // Delay for cursor to return at zero position
}
void LCD_Ch(unsigned int row, unsigned int col, char LCDChar)
{
switch(row)
{
case 1:
LCD_Cmd(0x80 + col-1);
break;
case 2:
LCD_Cmd(0xC0 + col-1);
break;
case 3:
LCD_Cmd(0x94 + col-1);
break;
case 4:
LCD_Cmd(0xD4 + col-1);
break;
}
LCD_RS = 1; // It is data
LCD_D4 = (LCDChar & 0x10)?1:0;
LCD_D5 = (LCDChar & 0x20)?1:0;
LCD_D6 = (LCDChar & 0x40)?1:0;
LCD_D7 = (LCDChar & 0x80)?1:0;
LCD_STROBE;
LCD_D4 = (LCDChar & 0x01)?1:0;
LCD_D5 = (LCDChar & 0x02)?1:0;
LCD_D6 = (LCDChar & 0x04)?1:0;
LCD_D7 = (LCDChar & 0x08)?1:0;
LCD_EN = 1;
LCD_STROBE;
}
void LCD_Init()
{
LCD_EN=0;
LCD_RS=0;
__delay_us(100);
LCD_D4 = 1;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
// LCD=0X30;
LCD_STROBE;
__delay_ms(2);
LCD_RS=0;
LCD_D4 = 1;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
// LCD=0X30;
LCD_STROBE;
__delay_ms(2);
LCD_RS=0;
// LCD=0X30;
LCD_D4 = 1;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
LCD_STROBE;
__delay_ms(2);
LCD_RS=0;
LCD_D4 = 0;
LCD_D5 = 1;
LCD_D6 = 0;
LCD_D7 = 0;
// LCD=0X20;
LCD_STROBE;
__delay_ms(2);
LCD_Cmd(0X28);
__delay_ms(5);
LCD_Cmd(0X0C);
__delay_ms(5);
LCD_Cmd(0X01);
__delay_ms(5);
LCD_Cmd(0X06);
__delay_ms(5);
}
void LCD_Out(unsigned int row, unsigned int col, const char *str)
{
while(*str)
LCD_Ch(row,col++,*str++); // print first character on LCD
row = 1;
col = 1;
}
int main(void)
{
unsigned char data;
TRISA = 0x00; TRISB = 0x00; TRISC = 0x00; TRISD = 0x00;
LCD_Init();
LCD_Cmd(_LCD_CLEAR);
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Out(1,1,"PIC18F458");
LCD_Out(2,1,"LCD 4-bit");
LCD_Out(3,1,"20X4");
LCD_Out(4,1,"Working?");
while(1);
return (0);
}
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 /* * main.c * * Created: 5/7/2013 9:09:11 AM * Author: Jayanth Devarayanadurga */ #define LCD_RS P2_0_bit #define LCD_EN P2_1_bit #define LCD_D4 P2_2_bit #define LCD_D5 P2_7_bit #define LCD_D6 P3_0_bit #define LCD_D7 P3_3_bit #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 // Constants #define EN_Delay 500 // Give a pulse on E pin // so that LCD can latch the // data from data bus #define LCD_STROBE {LCD_EN = 1; Delay_us(EN_Delay); LCD_EN = 0; Delay_us(EN_Delay);}; //Function Prototypes void LCD_Cmd(unsigned char Command); void LCD_Ch(unsigned int row, unsigned int col, char LCDChar); void LCD_Init(); void LCD_Out(unsigned int row, unsigned int col, const char *str); void LCD_Cmd(unsigned char Command) { LCD_RS = 0; // It is a command LCD_D4 = (Command & 0x10)?1:0; LCD_D5 = (Command & 0x20)?1:0; LCD_D6 = (Command & 0x40)?1:0; LCD_D7 = (Command & 0x80)?1:0; LCD_STROBE; LCD_D4 = (Command & 0x01)?1:0; LCD_D5 = (Command & 0x02)?1:0; LCD_D6 = (Command & 0x04)?1:0; LCD_D7 = (Command & 0x08)?1:0; LCD_STROBE; if(Command == 0x01) Delay_ms(2); // Delay for cursor to return at zero position } void LCD_Ch(unsigned int row, unsigned int col, char LCDChar) { switch(row){ case 1: LCD_Cmd(0x80 + col-1); break; case 2: LCD_Cmd(0xC0 + col-1); break; case 3: LCD_Cmd(0x94 + col-1); break; case 4: LCD_Cmd(0xD4 + col-1); break; } LCD_RS = 1; // It is data LCD_D4 = (LCDChar & 0x10)?1:0; LCD_D5 = (LCDChar & 0x20)?1:0; LCD_D6 = (LCDChar & 0x40)?1:0; LCD_D7 = (LCDChar & 0x80)?1:0; LCD_STROBE; LCD_D4 = (LCDChar & 0x01)?1:0; LCD_D5 = (LCDChar & 0x02)?1:0; LCD_D6 = (LCDChar & 0x04)?1:0; LCD_D7 = (LCDChar & 0x08)?1:0; LCD_EN = 1; LCD_STROBE; } void LCD_Init() { LCD_RS = 0; LCD_EN = 0; LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; ///////////////// Reset process from datasheet ////////////// Delay_ms(30); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE; Delay_ms(6); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE; Delay_ms(3); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x3 value on data bus LCD_D4 = 1; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE; Delay_ms(2); //Make Data pins zero LCD_D4 = 0; LCD_D5 = 0; LCD_D6 = 0; LCD_D7 = 0; //Write 0x2 value on data bus LCD_D4 = 0; LCD_D5 = 1; LCD_D6 = 0; LCD_D7 = 0; LCD_STROBE; Delay_ms(2); /////////////// Reset Process End //////////////// LCD_Cmd(0x28); //function set LCD_Cmd(0x0C); //display on,cursor off,blink off LCD_Cmd(0x06); //entry mode, set increment } void LCD_Out(unsigned int row, unsigned int col, const char *str) { while(*str) LCD_Ch(row,col++,*str++); // print first character on LCD row = 1; col = 1; } void main(){ P0 = 0x00; P1 = 0x00; P2 = 0x00; P3 = 0x00; LCD_Init(); LCD_Cmd(_LCD_CLEAR); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Out(1,1,"AT89C52"); LCD_Out(2,1,"LCD 4-bit"); LCD_Out(3,1,"20X4"); LCD_Out(4,1,"Working?"); while(1); }
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?