/*LCD interfacing using 4-bit mode*/#include <pic.h>#include "delay.c"//configuration fuse__CONFIG(XT & WDTDIS & PWRTDIS & UNPROTECT);static bit LCD_RS @ ((unsigned)&PORTA*8+0); // Register selectstatic bit LCD_EN @ ((unsigned)&PORTA*8+1); // Enable#define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0))void lcd_write(unsigned char c){ PORTB = (PORTB & 0xF0) | (c >> 4); LCD_STROBE; PORTB = (PORTB & 0xF0) | (c & 0x0F); LCD_STROBE; DelayUs(40);}/* * Clear and home the LCD */void lcd_clear(void){ LCD_RS = 0; lcd_write(0x1); DelayMs(2);}/* write a string of chars to the LCD */void lcd_puts(const char * s){ LCD_RS = 1; // write characters while(*s) lcd_write(*s++);}/* write one character to the LCD */void lcd_putch(char c){ LCD_RS = 1; // write characters PORTB = (PORTB & 0xF0) | (c >> 4); LCD_STROBE; PORTB = (PORTB & 0xF0) | (c & 0x0F); LCD_STROBE; DelayUs(40);}/* * Go to the specified position */void lcd_goto(unsigned char pos){ LCD_RS = 0; lcd_write(0x80+pos);} /* initialise the LCD - put into 4 bit mode */void lcd_init(void){ LCD_RS = 0; // write control bytes DelayMs(15); // power on delay PORTB = 0x03; // attention! LCD_STROBE; DelayMs(5); LCD_STROBE; DelayUs(100); LCD_STROBE; DelayMs(5); PORTB = 0x02; // set 4 bit mode LCD_STROBE; DelayUs(40); lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font (0010 1000) lcd_write(0x08); // display off (0000 1000) lcd_write(0x0F); // display on, blink curson on lcd_write(0x06); // entry mode}void main(){ TRISA &= ~0x03; //RA2 & RA3 are output PORTA &= ~0x03; TRISB &= ~0x0F; //RB<3:0> are output PORTB &= ~0x0F; //RB<3:0> are low lcd_init(); //initialize LCD, set to 4-bit mode lcd_clear(); //clear LCD lcd_puts("abcde"); //display text in first line lcd_goto(0x40); //display text in second line lcd_puts("pogi si paranz"); while(1);}
#include <16f84a.h>#use delay (clock = 4000000)#fuses XT, NOWDT, NOPUT, NOPROTECT#define LCD_RS PIN_A0#define LCD_EN PIN_A1#define LCD_STROBE (output_high(LCD_EN), output_low(LCD_EN))void lcd_write(unsigned char c){ output_b((c >> 4) & 0x0F); LCD_STROBE; output_b(c & 0x0F); LCD_STROBE; delay_us(40); }void lcd_clear(void){ output_low(LCD_RS); lcd_write(0x01); delay_ms(2);}void lcd_puts( char * string){ output_high(LCD_RS); while(*string) lcd_write(*string++);}void lcd_putch(char c){ output_high(LCD_RS); output_b((c >> 4) & 0x0F); LCD_STROBE; output_b(c & 0x0F); LCD_STROBE; delay_us(40); }void lcd_goto(unsigned char pos){ output_low(LCD_RS); lcd_write(0x80+pos);}void lcd_init(void){ output_low(LCD_RS); // write control bytes delay_ms(15); // power on delay output_b(0x03); // attention! LCD_STROBE; delay_ms(5); LCD_STROBE; delay_us(100); LCD_STROBE; delay_ms(5); output_b(0x02); // set 4 bit mode LCD_STROBE; delay_us(40); lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font lcd_write(0x08); // display off lcd_write(0x0F); // display on, blink curson on lcd_write(0x06); // entry mode}void main(){ unsigned char a[] = "pogi si paranz"; set_tris_a(0x00); set_tris_b(0x00); lcd_init(); //initialize LCD, set to 4-bit mode lcd_clear(); //clear LCD lcd_putch('a'); //display chars in first line lcd_putch('b'); lcd_putch('c'); lcd_putch('d'); lcd_putch('e'); lcd_goto(0x40); //display text in second line lcd_puts(a); //display text in a[] array}
nice one daddy paranz. my pogi pa
nice sir paranz... naexplain mo kaagad kung papano ang gagawin at mga kailangan sa pagiinterface ng LCD. ganda ng example mo.pwede pa kopya...?gagawin ko na tong include file para di na ako gumawa.syempre, papalitan ko na lang dun sa main.
sure you can use it naman..yung sa hi-tech C code, meron na yan sample program sa sample folder ng hitech C installation folder.. tapos pinort ko lang sa CCS C when a forum member asked for a similar sample program on LCD
Wow! Ang bilis ng mga solusyon ah! Iintindihin ko na lang to pero malamang ayos na ayos na to.Thanks mga master! Lalo na kay master "POGI"! (DragonBol?) Hehe.
@paranz,parang nakita ko na yan "somewhere" na pogi sa LCD.... di ko lang maremember kung saan
Followup: Nakaka-display na po ako ng text ("Hello World!", "Pogi rin ako", etc. ) kaya nga lang, yung application ko needs to show through the lcd the data which is computed every certain period e.g. every 2secs.Dun sa mga code na nakita ko, isa-isa sinesend yung characters: while(*s) lcd_write(*s++);at yung input ay predefined: lcd_puts("pogi si paranz");Kaya, paano ko po babaguhin yung code para magprint ng real time data (int or float) data every 2secs? Yung parang printf lang: printf("Reading: %d",data);Thank you!
/*LCD interfacing using 4-bit mode*/#include <pic.h>#include "delay.c"//configuration fuse__CONFIG(XT & WDTDIS & PWRTDIS & UNPROTECT);#define LCD_RS RA0#define LCD_EN RA1#define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0))void lcd_write(unsigned char c){ PORTB = (PORTB & 0xF0) | (c >> 4); LCD_STROBE; PORTB = (PORTB & 0xF0) | (c & 0x0F); LCD_STROBE; DelayUs(40);}/* * Clear and home the LCD */void lcd_clear(void){ LCD_RS = 0; lcd_write(0x1); DelayMs(2);}/* write a string of chars to the LCD */void lcd_puts(const char * s){ LCD_RS = 1; // write characters while(*s) lcd_write(*s++);}/* write one character to the LCD */void lcd_putch(char c){ LCD_RS = 1; // write characters PORTB = (PORTB & 0xF0) | (c >> 4); LCD_STROBE; PORTB = (PORTB & 0xF0) | (c & 0x0F); LCD_STROBE; DelayUs(40);}/* * Go to the specified position */void lcd_goto(unsigned char pos){ LCD_RS = 0; lcd_write(0x80+pos);} /* initialise the LCD - put into 4 bit mode */void lcd_init(void){ LCD_RS = 0; // write control bytes DelayMs(15); // power on delay PORTB = 0x03; // attention! LCD_STROBE; DelayMs(5); LCD_STROBE; DelayUs(100); LCD_STROBE; DelayMs(5); PORTB = 0x02; // set 4 bit mode LCD_STROBE; DelayUs(40); lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font (0010 1000) lcd_write(0x08); // display off (0000 1000) lcd_write(0x0F); // display on, blink curson on lcd_write(0x06); // entry mode}void delay(void) //software delay{ int i,j; for(i=0;i<0xFF;i++) for(j=0;j<0xFF;j++) ;}/* Convert integer 'i', radix 10 to a string */void itoa(unsigned char *buf, int i) { unsigned int rem; unsigned char *s,length=0; s = buf; //<points to address of buf if (i == 0) //if i is 0, buf is 0 *s++ = '0'; else { if (i < 0) { *buf++ = '-'; s = buf; i = -i; } while (i) { ++length; rem = i % 10; *s++ = rem + '0'; i /= 10; } for(rem=0; ((unsigned char)rem)<length/2; rem++) { *(buf+length) = *(buf+((unsigned char)rem)); *(buf+((unsigned char)rem)) = *(buf+(length-((unsigned char)rem)-1)); *(buf+(length-((unsigned char)rem)-1)) = *(buf+length); } } *s=0;}void main(){ unsigned char value = 0x00; //counter value unsigned char string_value[5]; //hold 5 strings TRISA &= ~0x03; //RA2 & RA3 are output PORTA &= ~0x03; TRISB &= ~0x0F; //RB<3:0> are output PORTB &= ~0x0F; //RB<3:0> are low lcd_init(); //initialize LCD, set to 4-bit mode lcd_clear(); //clear LCD lcd_puts("abcde"); //display text in first line lcd_goto(0x40); //display text in second line lcd_puts("pogi si paranz"); delay(); //delay lcd_clear(); //remove previous display while(1) { itoa(string_value, value); //convert value to string, //then assign to string_value lcd_goto(0x00); //goto first line lcd_puts(string_value); //display value to LCD value++; //increment value delay(); //delay }}
;*************************************************************/07/30/08*****; File: LCD_20x4_test.asm @ 4Mhz. ; Project name: LCD Test 4x20 ;*************************************************************************** LIST P=16F84 INCLUDE "P16F84.INC" __CONFIG H'3FF1' RADIX HEX ERRORLEVEL -302,-202;*************************************************************************** ; Pin configuration for 20x4 LCD ;*************************************************************************** ; RB0 = D4 ; RB1 = D5; RB2 = D6 ; RB3 = D7 ; RB4 = E; RB5 = RS ;*************************************************************************** LCD_TEMP1 EQU 0X20 LCD_TEMP2 EQU 0X22 LCD_TEMP3 EQU 0X23 COUNT1 EQU 0X24COUNT2 EQU 0X25 #DEFINE EN PORTB,4 #DEFINE RS PORTB,5 ;*************************************************************************** ORG 0X00 BSF STATUS,RP0 MOVLW 0X00 MOVWF TRISB BCF STATUS,RP0 CLRF PORTB CALL LCD_INIT GOTO MAIN ;*************************************************************************** LCD_TABLE: ADDWF PCL,F RETLW 0X32 ; Set 4-bit mode RETLW 0X2C ; Set 2 lines LCD RETLW 0X08 ; Display OFF RETLW 0X06 ; Set so cursor addr is incremented RETLW 0X0C ; Display ON, cursor OFF, blink OFF RETLW 0X01 ; Clear display RETLW 0X02 ; Home cursor, clear RAM RETLW 0X00 ; Return zero as end marker;*************************************************************************** ORG 0X100SET_STRING: ADDWF PCL,F ; 01234567890123456789MSG_L1: DT " I love ",0 MSG_L2: DT " electronicslab ",0MSG_L3: DT " by: ",0 MSG_L4: DT " bulate1983 ",0 ;*************************************************************************** MAIN: CALL LCD_LINE_1 MOVLW MSG_L1 CALL STRING_OUT CALL LCD_LINE_2 MOVLW MSG_L2 CALL STRING_OUT CALL LCD_LINE_3 MOVLW MSG_L3 CALL STRING_OUT CALL LCD_LINE_4 MOVLW MSG_L4 CALL STRING_OUT GOTO MAIN ;***************************************************************************STRING_OUT: MOVWF LCD_TEMP1 ; Save the string DECF LCD_TEMP1,F ; Subract 1 to string.LOOP01: MOVLW 0X01 MOVWF PCLATH MOVF LCD_TEMP1,W CALL SET_STRING ; Add the Count text to PCL XORLW 0X00 BTFSC STATUS,Z ; String = 0? GOTO CLR_PCL CALL LCD_CHAR ; display to LCD INCF LCD_TEMP1,F ; Point to next character GOTO LOOP01 ; Keep checking CLR_PCL: CLRF PCLATH RETURN ;***************************************************************************; LCD Subroutine 20x4 Display ;*************************************************************************** LCD_INIT: CALL DELAY_5 ; Wait to Initialize the LCD CLRF LCD_TEMP2SET_LCD: MOVF LCD_TEMP2,W CALL LCD_TABLE XORLW 0X00 BTFSC STATUS,Z RETURN CALL LCD_CMD INCF LCD_TEMP2,F CALL DELAY_5 GOTO SET_LCD ;*************************************************************************** LCD_LINE_1: MOVLW 0X80 ; Go to start of line 1 CALL LCD_CMD ; Yes call Command Display RETURN LCD_LINE_2: MOVLW 0XC0 ; Go to start of line 2 CALL LCD_CMD ; Yes call Command Display RETURN LCD_LINE_3: MOVLW 0X94 ; Go to start of line 3 CALL LCD_CMD ; Yes call Command Display RETURNLCD_LINE_4: MOVLW 0XD4 ; Go to start of line 4 CALL LCD_CMD ; Yes call Command Display RETURN ;*************************************************************************** PULSE_EN: BSF EN NOP BCF EN RETURN ;***************************************************************************LCD_CMD: MOVWF LCD_TEMP3 SWAPF LCD_TEMP3,W ; Send the upper nibble ANDLW B'00001111' ; Clear the upper 4-bit of W MOVWF PORTB BCF RS CALL PULSE_EN MOVF LCD_TEMP3,W ; Send the lower nibble ANDLW B'00001111' ; Clear the upper 4-bit of W MOVWF PORTB BCF RS CALL PULSE_EN CALL DELAY_1 RETURN ;*************************************************************************** LCD_CHAR: MOVWF LCD_TEMP3 SWAPF LCD_TEMP3,W ; Send the upper nibble ANDLW B'00001111' ; Clear the upper 4-bit of W MOVWF PORTB BSF RS CALL PULSE_EN MOVF LCD_TEMP3,W ; Send the lower nibble ANDLW B'00001111' ; Clear the upper 4-bit of W MOVWF PORTB BSF RS CALL PULSE_EN CALL DELAY_1 RETURN ;*************************************************************************** ; Delay Subroutine @ 4Mhz. ;***************************************************************************DELAY_1: MOVLW .200 MOVWF COUNT1 LOOP1: NOP NOP DECFSZ COUNT1,F GOTO LOOP1 RETURN DELAY_5: MOVLW .5 MOVWF COUNT2LOOP2: CALL DELAY_1 DECFSZ COUNT2,F GOTO LOOP2 RETURN ;*************************************************************************** END