/* PORT No. * PORTA RA0 to RA3 : LCD DB4to DB7(11~14pin) * PORTA RA4 : LCD RS(4pin) * PORTA RA5 : LCD EN(6pin) */ #include //#include //#include #include //#include "lcd.h" #define MHz 000000 #define _XTAL_FREQ 16MHz //#define _XTAL_FREQ 20MHz //#define _XTAL_FREQ 64MHz //#define lcd_rs PORTAbits.RA4 // Register select pin of LCD(4pin) 動かない //#define LCD_EN PORTAbits.RA5 // Enable pin of LCD(6pin) 動かない #define lcd_rs LATA4 // Register select pin of LCD(4pin) #define LCD_EN LATA5 // Enable pin of LCD(6pin) //#define lcd_rs RA4 // Register select pin of LCD(4pin) //#define LCD_EN RA5 // Enable pin of LCD(6pin) /*******重要 注意点 ******/ /* You must Adjust the LCD contrast voltage before adjust the STROBE pulse width */ //#define LCD_STROBE (LCD_EN=1,LCD_EN=0) // spec. min 500ns ***fix 2009/11/17 air variable // at 20MHz, pulse width of (LCD_EN=1,LCD_EN=0) is about 280ns // #define LCD_STROBEon (LCD_EN=1) // spec. min 500ns ***fix 2009/11/17 air variable #define LCD_STROBEoff (LCD_EN=0) // spec. min 500ns ***fix 2009/11/17 air variable /* at 20MHz, pulse width of(LCD_EN=1;LCD_EN=0) is 280ns */ /* write a byte to the LCD in 4 bit mode */ void Lcd_data_byteset(unsigned char c){ PORTA = (PORTA & 0b11110000) | (c & 0b00001111);//set low 4bit to RA0~RA3 // LCD_STROBE; LCD_EN = 1 ; LCD_STROBEon; LCD_EN = 0 ; LCD_STROBEoff; } void Lcd_data_set(unsigned char c){ PORTA = (PORTA & 0b11110000) | ( (c >>4) & 0b00001111 ) ;//set high 4bit to RA0~RA3 // LCD_STROBE; LCD_STROBEon; LCD_STROBEoff; PORTA = (PORTA & 0b11110000) | (c & 0b00001111 ); //set low 4bit to RA0~RA3 // LCD_STROBE; LCD_STROBEon; LCD_STROBEoff; } void Lcd_write(unsigned char c){ LCD_EN = 0; lcd_rs= 0; Lcd_data_set(c); __delay_us(40); } /* Clear and home the LCD */ void Lcd_clear(void){ LCD_EN = 0; lcd_rs = 0; Lcd_write(0x01); __delay_ms(2); } /* write one character to the LCD */ void Lcd_putch(char c){ LCD_EN = 0; lcd_rs = 1; // write characters Lcd_data_set(c); __delay_us(40); } /* write a string of chars to the LCD */ void Lcd_puts(const char * s){ while(*s) Lcd_putch(*s++); } /* Go to the specified position */ void Lcd_goto(unsigned char pos){ LCD_EN = 0; lcd_rs= 0; Lcd_write(0x80+pos); } /* initialise the LCD - put into 4 bit mode */ void Lcd_init(void){ unsigned int timer_int; lcd_rs = 0; timer_int = 3; while(timer_int--){__delay_ms(10);}; //Wait 1sec. // __delay_ms(30); // power on delay min 20 Lcd_data_byteset( 0x03 ); // attention! // __delay_ms(45); __delay_ms(5); // LCD_STROBE; LCD_STROBEon; LCD_STROBEoff; __delay_ms(1); //better 100ms // LCD_STROBE; LCD_STROBEon; LCD_STROBEoff; __delay_ms(5); Lcd_data_byteset( 0x02 ) ; // set 4 bit mode __delay_us(40); // Lcd_write(0x28); // Function Set : 2lines, 5x7 dots Lcd_write(0x08); // Display Off Lcd_write(0x01); // Clear Display // __delay_ms(2); Lcd_write(0x06); // Entry Mode Set Lcd_write(0x0F); // Display On, blink of cursor __delay_ms(3); }