lcd n pic16f

Download Lcd n PIC16F

If you can't read please download the document

Upload: hairilfaiz86

Post on 16-Jun-2015

495 views

Category:

Technology


1 download

DESCRIPTION

Programming for LCD HD 44780 for PIC16F887 using HI-TECH 9.82 Compiler. Many function, easy to use.

TRANSCRIPT

  • 1. /*website : http://picnelectronic.blogspot.com/ compiler : HI-TECH C compiler version 9.82 Programmer : HF */ #include //to include content before compiling #define _XTAL_FREQ 20000000 //DEFINE CRYSTAL VALUE __CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & BOREN_OFF & MCLRE_ON & LVP_OFF); /*High speed crystal oscillator Disable Watchdog Timer Enable Power Up Timer Disable Brown Out Reset MCLR function is enabled Disable Low Voltage Programming*/ /****************LCD FUNCTION START*************************/ /* * LCD interface example * This code will interface to a standard LCD controller * like the Hitachi HD44780. It uses it in 4 bit mode, with * the hardware connected as follows (the standard 14 pin * LCD connector is used): * * * To use these routines, set up the port I/O (TRISD) then * call lcd_init(), then other routines as required. * printf function (stdio.h) to write data to LCD */ #include #define LCD_RS RD0 //LCD configuration #define LCD_EN RD1 #define LCD_D4 RD2 #define LCD_D5 RD3 #define LCD_D6 RD4 #define LCD_D7 RD5 #define LCD_PULSE() ((LCD_EN = 1),(LCD_EN=0)) /* write a byte to the LCD in 4 bit mode */ void lcd_write(unsigned char c) { __delay_us(40); LCD_D4=(c>>4)&1; //LCD High nibble LCD_D5=(c>>5)&1; LCD_D6=(c>>6)&1; LCD_D7=(c>>7)&1;

2. LCD_PULSE(); LCD_D4=(c>>0)&1; //LCD low Nibble LCD_D5=(c>>1)&1; LCD_D6=(c>>2)&1; LCD_D7=(c>>3)&1; LCD_PULSE(); } void lcd_clear(void) //Clear and home the LCD { LCD_RS = 0; lcd_write(0x1); __delay_ms(2); } void lcd_string(const char * s) /* write a string of chars to the LCD */ { LCD_RS = 1; // write characters while(*s) lcd_write(*s++); } /* write one character to the LCD */ void lcd_char(char c) { LCD_RS = 1; // write characters lcd_write( c ); } void putch(char c) { LCD_RS = 1; // write characters lcd_write( c ); } /* * Go to the specified position */ void lcd_goto(unsigned char row, unsigned char pos) { LCD_RS = 0; switch(row){ case 0:{lcd_write(0x80+pos);break;} case 1:{lcd_write(0xC0+pos);break;} case 2:{lcd_write(0x94+pos);break;} case 3:{lcd_write(0xD4+pos);break;} } 3. } /* initialise the LCD - put into 4 bit mode */ void lcd_init() { LCD_RS = 0; LCD_EN = 0; __delay_ms(15); // wait 15mSec after power applied, LCD_D4=1; LCD_D5=1; LCD_D6=1; LCD_D7=0; LCD_PULSE(); __delay_ms(5); LCD_PULSE(); __delay_us(200); LCD_PULSE(); __delay_us(200); // Four bit mode LCD_D4=0; LCD_D5=1; LCD_D6=0; LCD_D7=0; LCD_PULSE(); lcd_write(0x28); // Set interface length lcd_write(0x0C); // Display On, Cursor On, Cursor Blink lcd_clear(); // Clear screen lcd_write(0x6); // Set entry Mode } /*******************LCD FUNCTION END************************/ void main(void) // the main function { TRISD = 0b00000000; //Set PORTD as output lcd_init(); //initialize LCD display lcd_goto(0,0); //row 1 pos 1 printf("Tutorial LCD"); //display on LCD lcd_goto(1,0); //row 2 pos 1 printf("Hitachi HD44780"); while(1){ } }