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
I have written a LCD 4 bit C Code. I want to implement it in PIC ASM. I want to know how to write the following C Code in PIC ASM.
- - - Updated - - -
I have written this piece of C Code in ASM for PIC. Is this right?
C Code
ASM Code
- - - Updated - - -
Here is my new code. Is this right? I have translated C Code to PIC asm. Will the code work especially LCD_Out macro?
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 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_D4 = (Command & 0x01)?1:0; LCD_D5 = (Command & 0x02)?1:0; LCD_D6 = (Command & 0x04)?1:0; LCD_D7 = (Command & 0x08)?1:0; 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; } }
- - - Updated - - -
I have written this piece of C Code in ASM for PIC. Is this right?
C Code
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 #define LCD_STROBE {LCD_EN = 1; __delay_us(EN_Delay); LCD_EN = 0; __delay_us(EN_Delay);}; 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 }
ASM Code
Code ASM - [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 cblock d1 d2 d3 endc start LCD_Cmd MACRO Cmd ;Cmd is the argument movlw 0x00; movwf LCD_RS if Cmd & 0x10 bsf LCD_D4 else bcf LCD_D4 endif if Cmd & 0x20 bsf LCD_D5 else bcf LCD_D5 endif if Cmd & 0x40 bsf LCD_D6 else bcf LCD_D6 endif if Cmd & 0x80 bsf LCD_D7 else bcf LCD_D7 endif bsf LCD_RS call Delay4MHz500us bcf LCD_RS call Delay4MHz500us if Cmd & 0x01 bsf LCD_D4 else bcf LCD_D4 endif if Cmd & 0x02 bsf LCD_D5 else bcf LCD_D5 endif if Cmd & 0x04 bsf LCD_D6 else bcf LCD_D6 endif if Cmd & 0x08 bsf LCD_D7 else bcf LCD_D7 endif bsf LCD_RS call Delay4MHz500us bcf LCD_RS call Delay4MHz500us if Command == 0x01 call Delay4MHz2ms ENDM LCD_Chr MACRO row, col, LCDChar if row == 1 LCD_Cmd 0x80 + col-1 elseif row == 2 LCD_Cmd 0xC0 + col-1 elseif row == 3 LCD_Cmd 0x94 + col-1 elseif row == 4 LCD_Cmd 0xD4 + col-1 endif bsf LCD_RS if Cmd & 0x10 bsf LCD_D4 else bcf LCD_D4 endif if Cmd & 0x20 bsf LCD_D5 else bcf LCD_D5 endif if Cmd & 0x40 bsf LCD_D6 else bcf LCD_D6 endif if Cmd & 0x80 bsf LCD_D7 else bcf LCD_D7 endif bsf LCD_RS call Delay4MHz500us bcf LCD_RS call Delay4MHz500us if Cmd & 0x01 bsf LCD_D4 else bcf LCD_D4 endif if Cmd & 0x02 bsf LCD_D5 else bcf LCD_D5 endif if Cmd & 0x04 bsf LCD_D6 else bcf LCD_D6 endif if Cmd & 0x08 bsf LCD_D7 else bcf LCD_D7 endif bsf LCD_RS call Delay4MHz500us bcf LCD_RS call Delay4MHz500us ENDM ; Delay = 0.0005 seconds ; Clock frequency = 4 MHz ; Actual delay = 0.0005 seconds = 500 cycles ; Error = 0 % Delay4MHz500us ;496 cycles movlw 0xA5 movwf d1 Delay4MHz500us_0 decfsz d1, f goto Delay4MHz500us_0 ;4 cycles (including call) return ; Delay = 0.002 seconds ; Clock frequency = 4 MHz ; Actual delay = 0.002 seconds = 2000 cycles ; Error = 0 % Delay4MHz2ms ;1993 cycles movlw 0x8E movwf d2 movlw 0x02 movwf d3 Delay4MHz2ms_0 decfsz d2, f goto label1 decfsz d3, f label1: goto Delay4MHz2ms_0 ;3 cycles goto label2 label2: nop ;4 cycles (including call) return end
- - - Updated - - -
Here is my new code. Is this right? I have translated C Code to PIC asm. Will the code work especially LCD_Out macro?
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 #define LCD_STROBE {LCD_EN = 1; __delay_us(EN_Delay); LCD_EN = 0; __delay_us(EN_Delay);}; 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; }
Code ASM - [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 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 cblock d1 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 endc start LCD_Cmd MACRO Cmd ;Cmd is the argument movlw 0x00; movwf LCD_RS if Cmd & 0x10 bsf LCD_D4 else bcf LCD_D4 endif if Cmd & 0x20 bsf LCD_D5 else bcf LCD_D5 endif if Cmd & 0x40 bsf LCD_D6 else bcf LCD_D6 endif if Cmd & 0x80 bsf LCD_D7 else bcf LCD_D7 endif call LCD_STROBE if Cmd & 0x01 bsf LCD_D4 else bcf LCD_D4 endif if Cmd & 0x02 bsf LCD_D5 else bcf LCD_D5 endif if Cmd & 0x04 bsf LCD_D6 else bcf LCD_D6 endif if Cmd & 0x08 bsf LCD_D7 else bcf LCD_D7 endif call LCD_STROBE if Command == 0x01 call Delay4MHz2ms ENDM LCD_Chr MACRO row, col, LCDChar if row == 1 LCD_Cmd 0x80 + col-1 elseif row == 2 LCD_Cmd 0xC0 + col-1 elseif row == 3 LCD_Cmd 0x94 + col-1 elseif row == 4 LCD_Cmd 0xD4 + col-1 endif bsf LCD_RS if Cmd & 0x10 bsf LCD_D4 else bcf LCD_D4 endif if Cmd & 0x20 bsf LCD_D5 else bcf LCD_D5 endif if Cmd & 0x40 bsf LCD_D6 else bcf LCD_D6 endif if Cmd & 0x80 bsf LCD_D7 else bcf LCD_D7 endif call LCD_STROBE if Cmd & 0x01 bsf LCD_D4 else bcf LCD_D4 endif if Cmd & 0x02 bsf LCD_D5 else bcf LCD_D5 endif if Cmd & 0x04 bsf LCD_D6 else bcf LCD_D6 endif if Cmd & 0x08 bsf LCD_D7 else bcf LCD_D7 endif call LCD_STROBE ENDM LCD_Init MACRO bcf LCD_RS bcf LCD_EN bcf LCD_D4 bcf LCD_D5 bcf LCD_D6 bcf LCD_D7 bcf LCD_RS_Direction bcf LCD_EN_Direction bcf LCD_D4_Direction bcf LCD_D5_Direction bcf LCD_D6_Direction bcf LCD_D7_Direction ;///////////////// Reset process from datasheet ////////////// call Delay4MHz30ms ;//Make Data pins zero bcf LCD_D4 bcf LCD_D5 bcf LCD_D6 bcf LCD_D7 ;//Write 0x3 value on data bus bsf LCD_D4 bsf LCD_D5 bcf LCD_D6 bcf LCD_D7 call LCD_STROBE call Delay4MHz6ms ;//Make Data pins zero bcf LCD_D4 bcf LCD_D5 bcf LCD_D6 bcf LCD_D7 ;//Write 0x3 value on data bus bsf LCD_D4 bsf LCD_D5 bcf LCD_D6 bcf LCD_D7 call LCD_STROBE call Delay4MHz3ms ;//Make Data pins zero bcf LCD_D4 bcf LCD_D5 bcf LCD_D6 bcf LCD_D7 ;//Write 0x3 value on data bus bsf LCD_D4 bsf LCD_D5 bcf LCD_D6 bcf LCD_D7 call LCD_STROBE call Delay4MHz2ms ;//Make Data pins zero bcf LCD_D4 bcf LCD_D5 bcf LCD_D6 bcf LCD_D7 ;//Write 0x3 value on data bus bcf LCD_D4 bsf LCD_D5 bcf LCD_D6 bcf LCD_D7 call LCD_STROBE call Delay4MHz2ms ;/////////////// Reset Process End //////////////// LCD_Cmd 0x28 ;//function set LCD_Cmd 0x0C ;//display on,cursor off,blink off LCD_Cmd 0x06 ENDM LCD_Out MACRO row, col, *LCDStr while *LCDStr LCD_Chr row, col, *LCDStr++ endw movlw 0x01 movwf row movwf col ENDM LCD_STROBE MACRO bsf LCD_RS call Delay4MHz500us bcf LCD_RS call Delay4MHz500us ENDM ; Delay = 0.0005 seconds ; Clock frequency = 4 MHz ; Actual delay = 0.0005 seconds = 500 cycles ; Error = 0 % Delay4MHz500us ;496 cycles movlw 0xA5 movwf d1 Delay4MHz500us_0 decfsz d1, f goto Delay4MHz500us_0 ;4 cycles (including call) return ; Delay = 0.002 seconds ; Clock frequency = 4 MHz ; Actual delay = 0.002 seconds = 2000 cycles ; Error = 0 % Delay4MHz2ms ;1993 cycles movlw 0x8E movwf d2 movlw 0x02 movwf d3 Delay4MHz2ms_0 decfsz d2, f goto label1 decfsz d3, f label1: goto Delay4MHz2ms_0 ;3 cycles goto label2 label2: nop ;4 cycles (including call) return ; Delay = 0.03 seconds ; Clock frequency = 4 MHz ; Actual delay = 0.03 seconds = 30000 cycles ; Error = 0 % Delay4MHz30ms ;29993 cycles movlw 0x6E movwf d4 movlw 0x18 movwf d5 Delay4MHz30ms_0 decfsz d4, f goto label4 decfsz d5, f label4: goto Delay4MHz30ms_0 ;3 cycles goto label5 label5: nop ;4 cycles (including call) return ; Delay = 0.002 seconds ; Clock frequency = 4 MHz ; Actual delay = 0.002 seconds = 2000 cycles ; Error = 0 % Delay4MHz2ms ;1993 cycles movlw 0x8E movwf d6 movlw 0x02 movwf d7 Delay4MHz2ms_0 decfsz d6, f goto label6 decfsz d7, f label6: goto Delay4MHz2ms_0 ;3 cycles goto label7 label7: nop ;4 cycles (including call) return ; Delay = 0.003 seconds ; Clock frequency = 4 MHz ; Actual delay = 0.003 seconds = 3000 cycles ; Error = 0 % Delay4MHz3ms ;2993 cycles movlw 0x56 movwf d8 movlw 0x03 movwf d9 Delay4MHz3ms_0 decfsz d8, f goto label8 decfsz d9, f label8: goto Delay4MHz3ms_0 ;3 cycles goto label9 label9: nop ;4 cycles (including call) return ; Delay = 0.006 seconds ; Clock frequency = 4 MHz ; Actual delay = 0.006 seconds = 6000 cycles ; Error = 0 % Delay4MHz6ms ;5993 cycles movlw 0xAE movwf d10 movlw 0x05 movwf d11 Delay4MHz6ms_0 decfsz d10, f goto label10 decfsz d11, f label10: goto Delay4MHz6ms_0 ;3 cycles goto label11 label11: nop ;4 cycles (including call) return end
Last edited: