在ardino中实现1602的I2C调试程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

在ardino中实现1602的I2C调试程序//YWROBOT#includeLiquidCrystal_I2C.h#includeinttypes.h#includeWProgram.h#includeWire.h//Whenthedisplaypowersup,itisconfiguredasfollows:////1.Displayclear//2.Functionset://DL=1;8-bitinterfacedata//N=0;1-linedisplay//F=0;5x8dotcharacterfont//3.Displayon/offcontrol://D=0;Displayoff//C=0;Cursoroff//B=0;Blinkingoff//4.Entrymodeset://I/D=1;Incrementby1//S=0;Noshift////Note,however,thatresettingtheArduinodoesn'tresettheLCD,sowe//can'tassumethatitsinthatstatewhenasketchstarts(andthe//LiquidCrystalconstructoriscalled).LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_tlcd_Addr,uint8_tlcd_cols,uint8_tlcd_rows){_Addr=lcd_Addr;_cols=lcd_cols;_rows=lcd_rows;_backlightval=LCD_NOBACKLIGHT;}voidLiquidCrystal_I2C::init(){init_priv();}voidLiquidCrystal_I2C::init_priv(){Wire.begin();_displayfunction=LCD_4BITMODE|LCD_1LINE|LCD_5x8DOTS;begin(_cols,_rows);}voidLiquidCrystal_I2C::begin(uint8_tcols,uint8_tlines,uint8_tdotsize){if(lines1){_displayfunction|=LCD_2LINE;}_numlines=lines;//forsome1linedisplaysyoucanselecta10pixelhighfontif((dotsize!=0)&&(lines==1)){_displayfunction|=LCD_5x10DOTS;}//SEEPAGE45/46FORINITIALIZATIONSPECIFICATION!//accordingtodatasheet,weneedatleast40msafterpowerrisesabove2.7V//beforesendingcommands.Arduinocanturnonwaybefer4.5Vsowe'llwait50delayMicroseconds(50000);//NowwepullbothRSandR/WlowtobegincommandsexpanderWrite(_backlightval);//resetexpanderandturnbacklightoff(Bit8=1)delay(1000);//puttheLCDinto4bitmode//thisisaccordingtothehitachiHD44780datasheet//figure24,pg46//westartin8bitmode,trytoset4bitmodewrite4bits(0x03);delayMicroseconds(4500);//waitmin4.1ms//secondtrywrite4bits(0x03);delayMicroseconds(4500);//waitmin4.1ms//thirdgo!write4bits(0x03);delayMicroseconds(150);//finally,setto4-bitinterfacewrite4bits(0x02);//set#lines,fontsize,etc.command(LCD_FUNCTIONSET|_displayfunction);//turnthedisplayonwithnocursororblinkingdefault_displaycontrol=LCD_DISPLAYON|LCD_CURSOROFF|LCD_BLINKOFF;display();//clearitoffclear();//Initializetodefaulttextdirection(forromanlanguages)_displaymode=LCD_ENTRYLEFT|LCD_ENTRYSHIFTDECREMENT;//settheentrymodecommand(LCD_ENTRYMODESET|_displaymode);home();}/**********highlevelcommands,fortheuser!*/voidLiquidCrystal_I2C::clear(){command(LCD_CLEARDISPLAY);//cleardisplay,setcursorpositiontozerodelayMicroseconds(2000);//thiscommandtakesalongtime!}voidLiquidCrystal_I2C::home(){command(LCD_RETURNHOME);//setcursorpositiontozerodelayMicroseconds(2000);//thiscommandtakesalongtime!}voidLiquidCrystal_I2C::setCursor(uint8_tcol,uint8_trow){introw_offsets[]={0x00,0x40,0x14,0x54};if(row_numlines){row=_numlines-1;//wecountrowsstartingw/0}command(LCD_SETDDRAMADDR|(col+row_offsets[row]));}//Turnthedisplayon/off(quickly)voidLiquidCrystal_I2C::noDisplay(){_displaycontrol&=~LCD_DISPLAYON;command(LCD_DISPLAYCONTROL|_displaycontrol);}voidLiquidCrystal_I2C::display(){_displaycontrol|=LCD_DISPLAYON;command(LCD_DISPLAYCONTROL|_displaycontrol);}//Turnstheunderlinecursoron/offvoidLiquidCrystal_I2C::noCursor(){_displaycontrol&=~LCD_CURSORON;command(LCD_DISPLAYCONTROL|_displaycontrol);}voidLiquidCrystal_I2C::cursor(){_displaycontrol|=LCD_CURSORON;command(LCD_DISPLAYCONTROL|_displaycontrol);}//TurnonandofftheblinkingcursorvoidLiquidCrystal_I2C::noBlink(){_displaycontrol&=~LCD_BLINKON;command(LCD_DISPLAYCONTROL|_displaycontrol);}voidLiquidCrystal_I2C::blink(){_displaycontrol|=LCD_BLINKON;command(LCD_DISPLAYCONTROL|_displaycontrol);}//ThesecommandsscrollthedisplaywithoutchangingtheRAMvoidLiquidCrystal_I2C::scrollDisplayLeft(void){command(LCD_CURSORSHIFT|LCD_DISPLAYMOVE|LCD_MOVELEFT);}voidLiquidCrystal_I2C::scrollDisplayRight(void){command(LCD_CURSORSHIFT|LCD_DISPLAYMOVE|LCD_MOVERIGHT);}//ThisisfortextthatflowsLefttoRightvoidLiquidCrystal_I2C::leftToRight(void){_displaymode|=LCD_ENTRYLEFT;command(LCD_ENTRYMODESET|_displaymode);}//ThisisfortextthatflowsRighttoLeftvoidLiquidCrystal_I2C::rightToLeft(void){_displaymode&=~LCD_ENTRYLEFT;command(LCD_ENTRYMODESET|_displaymode);}//Thiswill'rightjustify'textfromthecursorvoidLiquidCrystal_I2C::autoscroll(void){_displaymode|=LCD_ENTRYSHIFTINCREMENT;command(LCD_ENTRYMODESET|_displaymode);}//Thiswill'leftjustify'textfromthecursorvoidLiquidCrystal_I2C::noAutoscroll(void){_displaymode&=~LCD_ENTRYSHIFTINCREMENT;command(LCD_ENTRYMODESET|_displaymode);}//Allowsustofillthefirst8CGRAMlocations//withcustomcharactersvoidLiquidCrystal_I2C::createChar(uint8_tlocation,uint8_tcharmap[]){location&=0x7;//weonlyhave8locations0-7command(LCD_SETCGRAMADDR|(location3));for(inti=0;i8;i++){write(charmap[i]);}}//Turnthe(optional)backlightoff/onvoidLiquidCrystal_I2C::noBacklight(void){_backlightval=LCD_NOBACKLIGHT;expanderWrite(0);}voidLiquidCrystal_I2C::backlight(void){_backlightval=LCD_BACKLIGHT;expanderWrite(0);}/***********midlevelcommands,forsendingdata/cmds*/inlinevoidLiquidCrystal_I2C::command(uint8_tvalue){send(value,0);}inlinevoidLiquidCrystal_I2C::write(uint8_tvalue){send(value,Rs);}/************lowleveldatapushingcommands**********///writeeithercommandordatavoidLiquidCrystal_I

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功