📄 f_8_25_lcd_lines_4bit.c
字号:
#include "config.h"#include <stdio.h> #include <ctype.h>#include <stdlib.h> #include "delay.h"// macros to isolate interface dependencies#define EHIGH RA2=1#define ELOW RA2=0#define E_OUTPUT {ADCON1=0x06;TRISA2 = 0;}#define RSHIGH RA5=1#define RSLOW RA5=0#define RS_OUTPUT TRISA5 = 0#define RWHIGH RA3=1#define RWLOW RA3=0#define RW_OUTPUT TRISA3 = 0#define BUSY_FLAG RB3#define DATA_DIR_RD {TRISB3=1;TRISB2=1;\ TRISB1=1;TRISB0=1;}#define DATA_DIR_WR {TRISB3=0;TRISB2=0;\ TRISB1=0;TRISB0=0;}#define OUTPUT_DATA(x) {PORTB = x;}
//prototypes
void epulse (void);void lcd_write( unsigned char cmd, unsigned char data_flag, unsigned char chk_busy, unsigned char dflag);
void lcd_init(void);
void putch (char c);
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 our single char output function // which will be character output 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(3); // wait for busy flag to be ready}// send 8 bit char to LCDvoid putch (char c) { lcd_write(c,1,1,1);}#if defined(__18CXX)void _user_putc (auto char c) { //char function called by _H_USER stream putch (c);}#endifvoid main(void){ // configure, see control pins as outputs // initialize as low E_OUTPUT; RS_OUTPUT; RW_OUTPUT; ELOW; RSLOW; RWLOW; lcd_init (); printf("******Hello, my name is Bob********"); lcd_write(0xC0,0,1,1); // cursor to 2nd line printf("-----these lines are moving!-------"); while(1) { // shift left lcd_write(0x18,0,1,1); DelayMs(100); DelayMs(100); DelayMs(100); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -