LCD12864 ,LCD屏显开发—幽冥大陆(一百46)-东方仙盟

发布时间:2026/6/25 23:50:16
LCD12864 ,LCD屏显开发—幽冥大陆(一百46)-东方仙盟 控制代码// LiquidCrystal_I2C.cpp #include LiquidCrystal_I2C.h #include Wire.h // When the display powers up, it is configured as follows: // // 1. Display clear // 2. Function set: // DL 0; 4-bit interface data // N 0; 1-line display // F 0; 5x8 dot character font // 3. Display on/off control: // D 0; Display off // C 0; Cursor off // B 0; Blinking off // 4. Entry mode set: // I/D 1; Increment by 1 // S 0; No display shift // // Note, however, that resetting the Arduino doesnt reset the LCD, so we // cant assume that its in that state when a sketch starts (and the // LiquidCrystal constructor is called). LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows) { _Addr lcd_Addr; _cols lcd_cols; _rows lcd_rows; _backlightval LCD_NOBACKLIGHT; } void LiquidCrystal_I2C::init(){ init_priv(); } void LiquidCrystal_I2C::init_priv() { Wire.begin(); _displayfunction LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; begin(_cols, _rows); } void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) { if (lines 1) { _displayfunction | LCD_2LINE; } _numlines lines; _currline 0; // for some 1 line displays you can select a 10 pixel high font if ((dotsize ! 0) (lines 1)) { _displayfunction | LCD_5x10DOTS; } // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! // according to datasheet, we need at least 40ms after power rises above 2.7V // before sending commands. Arduino can turn on way befer 4.5V so well wait 50 delay(50); // Now we pull both RS and R/W low to begin commands expanderWrite(_backlightval); // reset expanderand turn backlight off (Bit 8 of expander) delay(1000); //put the LCD into 4 bit mode // this is according to the hitachi HD44780 datasheet // figure 24 pg 46 // we start in 8bit mode, try to set 4 bit mode write4bits(0x03 4); delayMicroseconds(4500); // wait min 4.1ms // second try write4bits(0x03 4); delayMicroseconds(4500); // wait min 4.1ms // third go! write4bits(0x03 4); delayMicroseconds(150); // finally, set to 4-bit interface write4bits(0x02 4); // set # lines, font size, etc. command(LCD_FUNCTIONSET | _displayfunction); // turn the display on with no cursor or blinking default _displaycontrol LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; display(); // clear it off clear(); // Initialize to default text direction (for roman languages) _displaymode LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; // set the entry mode command(LCD_ENTRYMODESET | _displaymode); home(); } /********** high level commands, for the user! */ void LiquidCrystal_I2C::clear(){ command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero delayMicroseconds(2000); // this command takes a long time! } void LiquidCrystal_I2C::home(){ command(LCD_RETURNHOME); // set cursor position to zero delayMicroseconds(2000); // this command takes a long time! } void LiquidCrystal_I2C::setCursor(uint8_t col, uint8_t row){ int row_offsets[] { 0x00, 0x40, 0x14, 0x54 }; if ( row _numlines ) { row _numlines-1; // we count rows starting w/0 } command(LCD_SETDDRAMADDR | (col row_offsets[row])); } // Turn the display on/off (quickly) void LiquidCrystal_I2C::noDisplay() { _displaycontrol ~LCD_DISPLAYON; command(LCD_DISPLAYCONTROL | _displaycontrol); } void LiquidCrystal_I2C::display() { _displaycontrol | LCD_DISPLAYON; command(LCD_DISPLAYCONTROL | _displaycontrol); } // Turns the underline cursor on/off void LiquidCrystal_I2C::noCursor() { _displaycontrol ~LCD_CURSORON; command(LCD_DISPLAYCONTROL | _displaycontrol); } void LiquidCrystal_I2C::cursor() { _displaycontrol | LCD_CURSORON; command(LCD_DISPLAYCONTROL | _displaycontrol); } // Turn on and off the blinking cursor void LiquidCrystal_I2C::noBlink() { _displaycontrol ~LCD_BLINKON; command(LCD_DISPLAYCONTROL | _displaycontrol); } void LiquidCrystal_I2C::blink() { _displaycontrol | LCD_BLINKON; command(LCD_DISPLAYCONTROL | _displaycontrol); } // These commands scroll the display without changing the RAM void LiquidCrystal_I2C::scrollDisplayLeft(void) { command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); } void LiquidCrystal_I2C::scrollDisplayRight(void) { command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); } // This is for text that flows Left to Right void LiquidCrystal_I2C::leftToRight(void) { _displaymode | LCD_ENTRYLEFT; command(LCD_ENTRYMODESET | _displaymode); } // This is for text that flows Right to Left void LiquidCrystal_I2C::rightToLeft(void) { _displaymode ~LCD_ENTRYLEFT; command(LCD_ENTRYMODESET | _displaymode); } // This will right justify text from the cursor void LiquidCrystal_I2C::autoscroll(void) { _displaymode | LCD_ENTRYSHIFTINCREMENT; command(LCD_ENTRYMODESET | _displaymode); } // This will left justify text from the cursor void LiquidCrystal_I2C::noAutoscroll(void) { _displaymode ~LCD_ENTRYSHIFTINCREMENT; command(LCD_ENTRYMODESET | _displaymode); } // Allows us to fill the first 8 CGRAM locations // with custom characters void LiquidCrystal_I2C::createChar(uint8_t location, uint8_t charmap[]) { location 0x7; // we only have 8 locations 0-7 command(LCD_SETCGRAMADDR | (location 3)); for (int i0; i8; i) { write(charmap[i]); } } //turn the (optional) backlight off/on void LiquidCrystal_I2C::noBacklight(void) { _backlightvalLCD_NOBACKLIGHT; expanderWrite(0); } void LiquidCrystal_I2C::backlight(void) { _backlightvalLCD_BACKLIGHT; expanderWrite(0); } /*********** mid level commands, for sending data/cmds */ inline void LiquidCrystal_I2C::command(uint8_t value) { send(value, 0); } inline size_t LiquidCrystal_I2C::write(uint8_t value) { send(value, Rs); return 1; } /************ low level data pushing commands **********/ // write either command or data, with automatic 4/8-bit selection void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) { uint8_t highnibvalue0xf0; uint8_t lownib(value4)0xf0; write4bits((highnib)|mode); write4bits((lownib)|mode); } void LiquidCrystal_I2C::write4bits(uint8_t value) { expanderWrite(value); pulseEnable(value); } void LiquidCrystal_I2C::expanderWrite(uint8_t _data){ Wire.beginTransmission(_Addr); Wire.write((int)(_data) | _backlightval); Wire.endTransmission(); } void LiquidCrystal_I2C::pulseEnable(uint8_t _data){ expanderWrite(_data | En); // En high delayMicroseconds(1); // enable pulse must be 450ns expanderWrite(_data ~En); // En low delayMicroseconds(50); // commands need 37us to settle } // Compatibility API for old sketches void LiquidCrystal_I2C::load_custom_character(uint8_t char_num, uint8_t *rows){ createChar(char_num, rows); } void LiquidCrystal_I2C::setBacklight(uint8_t new_val){ if(new_val){ backlight(); // turn backlight on }else{ noBacklight(); // turn backlight off } } void LiquidCrystal_I2C::printstr(const char c[]){ //This function is not identical to the function used for real I2C displays //its here so the user sketch doesnt have to be rewritten print(c); }文件头// LiquidCrystal_I2C.h #ifndef LiquidCrystal_I2C_h #define LiquidCrystal_I2C_h #include Arduino.h #include Wire.h // commands #define LCD_CLEARDISPLAY 0x01 #define LCD_RETURNHOME 0x02 #define LCD_ENTRYMODESET 0x04 #define LCD_DISPLAYCONTROL 0x08 #define LCD_CURSORSHIFT 0x10 #define LCD_FUNCTIONSET 0x20 #define LCD_SETCGRAMADDR 0x40 #define LCD_SETDDRAMADDR 0x80 // flags for display entry mode #define LCD_ENTRYRIGHT 0x00 #define LCD_ENTRYLEFT 0x02 #define LCD_ENTRYSHIFTINCREMENT 0x01 #define LCD_ENTRYSHIFTDECREMENT 0x00 // flags for display on/off control #define LCD_DISPLAYON 0x04 #define LCD_DISPLAYOFF 0x00 #define LCD_CURSORON 0x02 #define LCD_CURSOROFF 0x00 #define LCD_BLINKON 0x01 #define LCD_BLINKOFF 0x00 // flags for display/cursor shift #define LCD_DISPLAYMOVE 0x08 #define LCD_CURSORMOVE 0x00 #define LCD_MOVERIGHT 0x04 #define LCD_MOVELEFT 0x00 // flags for function set #define LCD_8BITMODE 0x10 #define LCD_4BITMODE 0x00 #define LCD_2LINE 0x08 #define LCD_1LINE 0x00 #define LCD_5x10DOTS 0x04 #define LCD_5x8DOTS 0x00 // I2C PCF8574 pin mapping #define En B10000000 // Enable bit #define Rw B01000000 // Read/Write bit #define Rs B00100000 // Register select bit #define LCD_BACKLIGHT 0x08 #define LCD_NOBACKLIGHT 0x00 class LiquidCrystal_I2C : public Print { public: LiquidCrystal_I2C(uint8_t lcd_addr, uint8_t lcd_cols, uint8_t lcd_rows); void init(); void begin(uint8_t cols, uint8_t lines, uint8_t dotsize LCD_5x8DOTS); // 基础显示控制 void clear(); void home(); void setCursor(uint8_t col, uint8_t row); // 显示开关 void noDisplay(); void display(); // 光标控制 void noCursor(); void cursor(); void noBlink(); void blink(); // 屏幕滚动 void scrollDisplayLeft(); void scrollDisplayRight(); // 文字方向 void leftToRight(); void rightToLeft(); void autoscroll(); void noAutoscroll(); // 自定义字符 void createChar(uint8_t location, uint8_t charmap[]); // 背光控制 void backlight(); void noBacklight(); void setBacklight(uint8_t new_val); // 兼容旧版接口 void load_custom_character(uint8_t char_num, uint8_t *rows); void printstr(const char c[]); // Print基类重写支持print()输出 virtual size_t write(uint8_t); private: void init_priv(); void send(uint8_t value, uint8_t mode); void write4bits(uint8_t value); void expanderWrite(uint8_t _data); void pulseEnable(uint8_t _data); inline void command(uint8_t value); uint8_t _Addr; uint8_t _cols; uint8_t _rows; uint8_t _backlightval; uint8_t _displayfunction; uint8_t _displaycontrol; uint8_t _displaymode; uint8_t _numlines; uint8_t _currline; }; #endif适配 DFRobot I2C 接口 LCD1602/2004的 Arduino C 驱动库全部代码为 C 编写维护状态仓库已归档停止维护项目整体迁移至 GitLab 新仓库tandembyte/LCD_I2C最后更新2016-03-07修复 Arduino IDE 1.6.7 版本 write () 函数 bug共发布 4 个版本10 位贡献者参与开发平台适配通过library.json增加 ESP8266 平台支持兼容 Arduino、PlatformIO 开发环境。二、仓库文件清单与用途核心驱动源码LiquidCrystal_I2C.h类声明头文件提供 LCD 初始化、显示、光标、背光等对外 APILiquidCrystal_I2C.cpp底层 I2C 通信、LCD 指令时序实现兼容 WEH001602 型号屏幕IDE 配置文件keywords.txtArduino 编辑器语法高亮配置library.propertiesArduino 库管理器识别元数据迭代更新版本号library.jsonPlatformIO 平台配置文件文档README.md说明库用途、归档迁移提示、版本历史人人皆为创造者共创方能共成长每个人都是使用者也是创造者是数字世界的消费者更是价值的生产者与分享者。在智能时代的浪潮里单打独斗的发展模式早已落幕唯有开放连接、创意共创、利益共享才能让个体价值汇聚成生态合力让技术与创意双向奔赴实现平台与伙伴的快速成长、共赢致远。原创永久分成共赴星辰大海原创创意共创、永久收益分成是东方仙盟始终坚守的核心理念。我们坚信每一份原创智慧都值得被尊重与回馈以永久分成锚定共创初心让创意者长期享有价值红利携手万千伙伴向着科技星辰大海笃定前行拥抱硅基 生命与数字智能交融的未来共筑跨越时代的数字文明共同体。东方仙盟拥抱知识开源共筑数字新生态在全球化与数字化浪潮中东方仙盟始终秉持开放协作、知识共享的理念积极拥抱开源技术与开放标准。我们相信唯有打破技术壁垒、汇聚全球智慧才能真正推动行业的可持续发展。开源赋能中小商户通过将前端异常检测、跨系统数据互联等核心能力开源化东方仙盟为全球中小商户提供了低成本、高可靠的技术解决方案让更多商家能够平等享受数字转型的红利。共建行业标准我们积极参与国际技术社区与全球开发者、合作伙伴共同制定开放协议 与技术规范推动跨境零售、文旅、餐饮等多业态的系统互联互通构建更加公平、高效的数字生态。知识普惠共促发展通过开源社区 、技术文档与培训体系东方仙盟致力于将前沿技术转化为可落地的行业实践赋能全球合作伙伴共同培育创新人才推动数字经济 的普惠式增长阿雪技术观在科技发展浪潮中我们不妨积极投身技术共享。不满足于做受益者更要主动担当贡献者 。无论是分享代码、撰写技术博客还是参与开源项目 维护改进每一个微小举动都可能蕴含推动技术进步的巨大能量。东方仙盟是汇聚力量的天地我们携手在此探索硅基 生命为科技进步添砖加瓦。Hey folks, in this wild tech - driven world, why not dive headfirst into the whole tech - sharing scene? Dont just be the one reaping all the benefits; step up and be a contributor too. Whether youre tossing out your code snippets , hammering out some tech blogs, or getting your hands dirty with maintaining and sprucing up open - source projects, every little thing you do might just end up being a massive force that pushes tech forward. And guess what? The Eastern FairyAlliance is this awesome place where we all come together. Were gonna team up and explore the whole silicon - based life thing, and in the process, well be fueling the growth of technology