📄 lcd.c
字号:
/*
* LCD interface example
* Uses routines from delay.c
* This code will interface to a standard LCD controller
* like the samsung KS0066. It uses it in 4 bit mode, with
* the hardware connected as follows (the standard 16 pin
* LCD connector is used):
*
* PORTC bits 4-7 are connected to the LCD data bits 4-7 (high nibble)
* PORTA bit 1 is connected to the LCD RS input (register select)
* PORTA bit 2 is connected to the LCD EN bit (enable)
*
* To use these routines, set up the port I/O (TRISC, TRISB) then
* call lcd_init(), then other routines as required.
*
*/
#include <pic.h>
#include "lcd.h"
#include "delay.h"
#define TLCD_RS TRISA1
#define TLCD_EN TRISA2
#define TLCD_A TRISA3
#define LCD_RS RA1 // 寄存器选择
#define LCD_EN RA2 // 使能
#define LCD_A RA3 // 背光,RA4开漏输出,欲输出1,需要外接上拉电阻
#define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0))
#define LCD_CURSOR(x) lcd_write(((x)&0x7F)|0x80)
/* light lcd & dark*/
void lcd_light(void)
{
LCD_A = 0;
}
void lcd_dark(void)
{
LCD_A = 1;
}
/* write a byte to the LCD in 4 bit mode */
void lcd_write(unsigned char c)
{
PORTC = (PORTC & 0x0F) | (c &0xF0);
LCD_STROBE;
PORTC = (PORTC & 0x0F) | (c<<4);
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)//当S的值不为0时,执行,故,需要花一个RAM,将其清0作为整个字符数组的结束标记
{
lcd_write(*s++);
}
}
/* write one character to the LCD */
void lcd_putch(char c)
{
LCD_RS = 1; // write characters
PORTC = (PORTC & 0x0F) | (c &0xF0);
LCD_STROBE;
PORTC = (PORTC & 0x0F) | (c<<4);
LCD_STROBE;
DelayUs(40);
}
/* Go to the specified position */
void lcd_goto(unsigned char pos)
{
LCD_RS = 0;
LCD_CURSOR(pos);
}
/* initialise the LCD - put into 4 bit mode */
#ifndef SPLC780D
void lcd_init(void)
{
TLCD_RS = 0;
TLCD_EN = 0;
TLCD_A = 0;
LCD_RS = 0;
LCD_EN = 0;
LCD_A = 0;
LCD_RS = 0; // write control bytes
lcd_write(0x2); // return home
DelayMs(2);
lcd_write(0x28); // 4 bit mode, 2 lines, 5x7 font
lcd_write(0x0C); // display on, curson off, blink off
lcd_clear(); // clear display
lcd_write(0x06); // right, increment address
}
#else
void lcd_init(void)
{
TLCD_RS = 0;
TLCD_EN = 0;
TLCD_A = 0;
LCD_RS = 0;
LCD_EN = 0;
LCD_A = 0;
LCD_RS = 0; // write control bytes
lcd_write(0b00000010); // return home
DelayMs(10);
lcd_write(0b00111111); // 8 bit interface
DelayMs(10);
lcd_write(0b00111111); // 8 bit interface
DelayMs(10);
lcd_write(0b00101000); // 4 bit interface,2 lines,5*8
DelayMs(10);
lcd_write(0b00001100); // dispaly on
DelayMs(10);
lcd_write(0b00000001); // clear LCD
DelayMs(10);
lcd_write(0b00000110); // entry mode set
DelayMs(10);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -