📄 lcd_module.c
字号:
#include <stdio.h> #include <ctype.h>#include <stdlib.h> // write a string to Optrex 8x2 LCD// Interface is 4-bit // LCD: PIC// DB7-DB4 to RB7-RB4// E to RA5// R/W to RC1// RS to RC2// Vdd to Vdd// Vss to Vss// VL to Vss// on LCD pins DB3-DB0, K, A are left unconnected// macros to isolate interface dependencies#define EHIGH RA5=1#define ELOW RA5=0#define E_OUTPUT {ADCON1=0x06;TRISA5 = 0;}#define RSHIGH RB5=1#define RSLOW RB5=0#define RS_OUTPUT TRISB5 = 0#define RWHIGH RB4=1#define RWLOW RB4=0#define RW_OUTPUT TRISB4 = 0#define BUSY_FLAG RA3#define DATA_DIR_RD {TRISA3=1;TRISA2=1;\ TRISA1=1;TRISA0=1;}#define DATA_DIR_WR {TRISA3=0;TRISA2=0;\ TRISA1=0;TRISA0=0;}#define OUTPUT_DATA(x) {PORTA = x;}void epulse(void){ DelayUs(1); EHIGH; DelayUs(1); ELOW; DelayUs(1);}void lcd_write( unsigned char cmd, unsigned char data_flag, unsigned char chk_busy, unsigned char dflag){ char bflag,c; if (chk_busy) { RSLOW; //RS = 0 to check busy // check busy DATA_DIR_RD; //set data pins all inputs RWHIGH; // R/W = 1, for read do { EHIGH; DelayUs(1); // upper 4 bits bflag = BUSY_FLAG; ELOW; DelayUs(1); epulse(); } while(bflag); } else { DelayMs(10); // don't use busy, just delay } DATA_DIR_WR; if (data_flag) RSHIGH; // RS=1, data byte else RSLOW; // RS=0, command byte // device is not busy RWLOW; // R/W = 0, for write c = cmd >> 4; // send upper 4 bits OUTPUT_DATA(c); epulse(); if (dflag) { c = cmd & 0x0F; //send lower 4 bits OUTPUT_DATA(c); epulse(); }}void lcd_init(void) {#if defined(__18CXX) stdout = _H_USER; // assign STDOUT to LCD#endif DelayMs(50); //wait for device to settle lcd_write(0x20,0,0,0); // 4 bit interface lcd_write(0x28,0,0,1); // 2 line display, 5x7 font lcd_write(0x28,0,0,1); // repeat lcd_write(0x06,0,0,1); // enable display lcd_write(0x0C,0,0,1); // turn display on; cursor, blink is off lcd_write(0x01,0,0,1); // clear display, move cursor to home DelayMs(50); // wait for busy flag to be ready}#ifndef SERIAL_PUTCH// send 8 bit char to LCDvoid putch (char c) { lcd_write(c,1,1,1);}#if defined(__18CXX)void _user_putc (auto char c) { putch (c);}#endif#endif#define LCD_PUTCH 1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -