📄 lcd.c
字号:
/* LCD interface example
* Uses routines from delay.c
* 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):
*
* PORTB bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
* PORTA bit 4 is connected to the LCD RS input (register select)
* PORTB bit 3 is connected to the LCD EN bit (enable)
*
* To use these routines, set up the port I/O (TRISA, TRISB) then
* call lcd_init(), then other routines as required.
*
*/
#ifndef XTAL FREQ
#define XTAL_FREQ 4MHZ /* Crystal frequency in MHz */
#endif
#include <pic.h>
#include "lcd.h"
#include "delay.h"
#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
static bit LCD_RS @ PORTBIT(PORTA, 4); // Register select
static bit LCD_EN @ PORTBIT(PORTB, 3); // Enable
static bit spkr @ PORTBIT(PORTA, 0);
void
lcd_strobe(void)
{
LCD_EN = 1 ;
DelayUs(1);
LCD_EN = 0 ;
DelayUs(1);
}
void
lcd_write(unsigned char c)
{
PORTB = (PORTB & 0x0F) | (c & 0xF0); /*es. A=65 =>0100 0001 bin >>4 avremo 0000 0100 or PORTB&11110000 */
lcd_strobe();
PORTB = (PORTB & 0x0F) | (c << 4); /* 0100 0001 & 00001111 = 0000 0001 */
lcd_strobe();
DelayUs(50);
}
/*
* 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 & 0x0F) | (c & 0xF0);
lcd_strobe();
PORTB = (PORTB & 0x0F) | (c << 4);
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 'comando'
DelayMs(50); // power on delay
PORTB = 0x30; // attention!
lcd_strobe();
DelayMs(5);
lcd_strobe();
DelayUs(100);
lcd_strobe();
DelayMs(1);
PORTB = 0x20; // set 4 bit mode
lcd_strobe();
DelayMs(1);
lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
lcd_write(0x06); // entry mode
lcd_write(0x0C); // display on
lcd_clear(); // clear
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -