Dilpreet Singh
Junior Member level 1
- Joined
- Jan 21, 2015
- Messages
- 19
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 155
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 #include <avr/io.h> #include <avr/delay.h> #include <util/delay.h> #define RS 0 #define RW 1 #define EN 2 #define lcd_port PORTD #define F_CPU 11592000ul #define sbit(reg,bit) reg |= (1<<bit) #define cbit(reg,bit) reg &= ~(1<<bit) void init_ports(); void lcd_set_4bit(); void lcd_init(); void lcd_wr_command(unsigned char); void lcd_wr_char(char); void lcd_home(); void lcd_cursor(char, char); void lcd_print(char, char, unsigned int, int); void lcd_string(char*); unsigned int temp; unsigned int unit; unsigned int tens; unsigned int hundred; unsigned int thousand; unsigned int million; /*****Function to Reset LCD*****/ void lcd_set_4bit() { _delay_ms(1); cbit(lcd_port,RS); //RS=0 --- Command Input cbit(lcd_port,RW); //RW=0 --- Writing to LCD _delay_ms(10); lcd_port = 0x30; //Sending 3 _delay_ms(100); sbit(lcd_port,EN); //Set Enable Pin _delay_ms(20); //Delay cbit(lcd_port,EN); //Clear Enable Pin _delay_ms(10); cbit(lcd_port,RS); //RS=0 --- Command Input cbit(lcd_port,RW); //RW=0 --- Writing to LCD _delay_ms(100); lcd_port = 0x30; //Sending 3 _delay_ms(100); sbit(lcd_port,EN); //Set Enable Pin _delay_ms(20); //Delay cbit(lcd_port,EN); //Clear Enable Pin _delay_ms(10); cbit(lcd_port,RS); //RS=0 --- Command Input cbit(lcd_port,RW); //RW=0 --- Writing to LCD lcd_port = 0x30; //Sending 3 _delay_ms(100); sbit(lcd_port,EN); //Set Enable Pin _delay_ms(20); //Delay cbit(lcd_port,EN); //Clear Enable Pin _delay_ms(10); cbit(lcd_port,RS); //RS=0 --- Command Input cbit(lcd_port,RW); //RW=0 --- Writing to LCD lcd_port = 0x20; //Sending 2 to initialise LCD 4-bit mode _delay_ms(100); sbit(lcd_port,EN); //Set Enable Pin _delay_ms(20); //Delay cbit(lcd_port,EN); //Clear Enable Pin } /*****Function to Initialize LCD*****/ void lcd_init() { _delay_ms(10); lcd_wr_command(0x28); //LCD 4-bit mode and 2 lines. _delay_ms(100); lcd_wr_command(0x01); _delay_ms(100); lcd_wr_command(0x06); _delay_ms(100); lcd_wr_command(0x0E); _delay_ms(100); lcd_wr_command(0x80); _delay_ms(100); } /*****Function to Write Command on LCD*****/ void lcd_wr_command(unsigned char cmd) { unsigned char temp; temp = cmd; temp = temp & 0xF0; lcd_port &= 0x0F; _delay_ms(100); lcd_port |= temp; _delay_ms(100); cbit(lcd_port,RS); cbit(lcd_port,RW); sbit(lcd_port,EN); _delay_ms(100); _delay_ms(5); cbit(lcd_port,EN); cmd = cmd & 0x0F; cmd = cmd<<4; lcd_port &= 0x0F; lcd_port |= cmd; _delay_ms(100); cbit(lcd_port,RS); cbit(lcd_port,RW); sbit(lcd_port,EN); _delay_ms(5); cbit(lcd_port,EN); } /*****Function to Write Data on LCD*****/ void lcd_wr_char(char letter) { char temp; temp = letter; temp = (temp & 0xF0); lcd_port &= 0x0F; lcd_port |= temp; _delay_ms(100); sbit(lcd_port,RS); cbit(lcd_port,RW); sbit(lcd_port,EN); _delay_ms(5); cbit(lcd_port,EN); letter = letter & 0x0F; letter = letter<<4; lcd_port &= 0x0F; lcd_port |= letter; _delay_ms(100); sbit(lcd_port,RS); cbit(lcd_port,RW); sbit(lcd_port,EN); _delay_ms(20); cbit(lcd_port,EN); } void lcd_home() { lcd_wr_command(0x80); } /*****Function to Print String on LCD*****/ void lcd_string(char *str) { while(*str != '\0') { lcd_wr_char(*str); str++; _delay_ms(100); } } /*** Position the LCD cursor at "row", "column". ***/ void lcd_cursor (char row, char column) { switch (row) { case 1: lcd_wr_command (0x80 + column - 1); break; case 2: lcd_wr_command (0xc0 + column - 1); break; case 3: lcd_wr_command (0x94 + column - 1); break; case 4: lcd_wr_command (0xd4 + column - 1); break; default: break; } } /***** Function To Print Any input value upto the desired digit on LCD *****/ void lcd_print (char row, char coloumn, unsigned int value, int digits) { unsigned char flag=0; if(row==0||coloumn==0) { lcd_home(); _delay_ms(100); } else { lcd_cursor(row,coloumn); } if(digits==5 || flag==1) { million=value/10000+48; lcd_wr_char(million); flag=1; } if(digits==4 || flag==1) { temp = value/1000; thousand = temp%10 + 48; lcd_wr_char(thousand); flag=1; } if(digits==3 || flag==1) { temp = value/100; hundred = temp%10 + 48; lcd_wr_char(hundred); flag=1; } if(digits==2 || flag==1) { temp = value/10; tens = temp%10 + 48; lcd_wr_char(tens); flag=1; } if(digits==1 || flag==1) { unit = value%10 + 48; lcd_wr_char(unit); } if(digits>5) { lcd_wr_char('E'); } } int main() { DDRD=0b11111111; lcd_init(); lcd_set_4bit(); int a=43; while(1){ _delay_ms(200); lcd_string("123456789 "); } }
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 /* * LCD_4_bit.c * * Created: 2/17/2015 1:04:20 AM * Author: Jayanth Devarayanadurga */ #define F_CPU 400000 #include <avr/io.h> #include <util/delay.h> #include <string.h> #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 RS 0 #define EN 2 #define LCD_PORT PORTD #define sbit(reg,bit) reg |= (1<<bit) #define cbit(reg,bit) reg &= ~(1<<bit) void LCD_Init(); void LCD_Cmd(char); void LCD_Chr(char, char, char); void LCD_Chr_Cp(char); void LCD_Out(char, char, char*); void LCD_Out_Cp(char*); void lcd_print(char, char, unsigned int, int); void LCD_Init() { _delay_ms(100); cbit(LCD_PORT,RS); //RS=0 --- Command Input LCD_PORT = 0x30; //Sending 3 sbit(LCD_PORT,EN); //Set Enable Pin _delay_us(50); //Delay cbit(LCD_PORT,EN); //Clear Enable Pin _delay_ms(30); LCD_PORT = 0x30; //Sending 3 sbit(LCD_PORT,EN); //Set Enable Pin _delay_us(50); //Delay cbit(LCD_PORT,EN); //Clear Enable Pin _delay_ms(30); //Clear Enable Pin LCD_PORT = 0x30; //Sending 3 sbit(LCD_PORT,EN); //Set Enable Pin _delay_us(50); //Delay cbit(LCD_PORT,EN); //Clear Enable Pin _delay_ms(30); //Clear Enable Pin LCD_PORT = 0x20; //Sending 3 sbit(LCD_PORT,EN); //Set Enable Pin _delay_us(50); //Delay cbit(LCD_PORT,EN); //Clear Enable Pin _delay_ms(30); //Clear Enable Pin LCD_Cmd(0x28); LCD_Cmd(0x0C); } void LCD_Cmd(char out_char) { cbit(LCD_PORT,RS); LCD_PORT = out_char & 0xF0; sbit(LCD_PORT,EN); _delay_us(50); cbit(LCD_PORT,EN); LCD_PORT = (out_char & 0x0F) << 4; sbit(LCD_PORT,EN); _delay_us(50); cbit(LCD_PORT,EN); } 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; }; sbit(LCD_PORT,RS); LCD_PORT = out_char & 0xF0; sbit(LCD_PORT,EN); _delay_us(50); cbit(LCD_PORT,EN); LCD_PORT = (out_char & 0x0F) << 4; sbit(LCD_PORT,EN); _delay_us(50); cbit(LCD_PORT,EN); } void LCD_Chr_Cp(char out_char) { sbit(LCD_PORT,RS); LCD_PORT = out_char & 0xF0; sbit(LCD_PORT,EN); _delay_us(50); cbit(LCD_PORT,EN); LCD_PORT = (out_char & 0x0F) << 4; sbit(LCD_PORT,EN); _delay_us(50); cbit(LCD_PORT,EN); } void LCD_Out(char row, char column, char *str) { while(*str) LCD_Chr(row, column, *str++); } void LCD_Out_Cp(char *str) { while(*str) LCD_Chr_Cp(*str++); } int myInt = 255; char str[17]; int main() { DDRD = 0b11111111; PORTD = 0x00; LCD_Init(); LCD_Cmd(_LCD_CURSOR_OFF); LCD_Cmd(_LCD_CLEAR); LCD_Out(1,1,"ATMega32"); LCD_Out(2,1,"LCD 4 bit"); LCD_Out(3,1,"16x2, 20x2, 20x4"); LCD_Out(4,1,"Testing..."); _delay_ms(5000); LCD_Cmd(_LCD_CLEAR); itoa(myInt, str, 10); LCD_Out(1,1,str); while(1){ } }
so sir what is problem ?
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?