zoukankan      html  css  js  c++  java
  • Arduino IIC lcd1602

    /*
    ** Example Arduino sketch for SainSmart I2C LCD Screen 16x2
    ** based on https://bitbucket.org/celem/sainsmart-i2c-lcd/src/3adf8e0d2443/sainlcdtest.ino
    ** by
    ** Edward Comer
    ** LICENSE: GNU General Public License, version 3 (GPL-3.0)
    
    ** This example uses F Malpartida's NewLiquidCrystal library. Obtain from:
    ** https://bitbucket.org/fmalpartida/new-liquidcrystal
    
    ** Modified – Ian Brennan ianbren at hotmail.com 23-10-2012 to support Tutorial posted to Arduino.cc
    
    ** Written for and tested with Arduino 1.0
    **
    ** NOTE: Tested on Arduino Uno whose I2C pins are A4==SDA, A5==SCL
    
    */
    #include <Wire.h>
    #include <LCD.h>
    #include <LiquidCrystal_I2C.h>
    
    #define I2C_ADDR    0x27 // <<----- Add your address here.  Find it from I2C Scanner
    #define BACKLIGHT_PIN     3
    #define En_pin  2
    #define Rw_pin  1
    #define Rs_pin  0
    #define D4_pin  4
    #define D5_pin  5
    #define D6_pin  6
    #define D7_pin  7
    // Creat a set of new characters
    const uint8_t charBitmap[][8] = {
    {0x10,0x10,0xfd,0x10,0x7c,0x44,0x7c,0x44},
    {0x7d,0x10,0xfe,0x10,0x10,0x10,0x10,0x10},
    {0x20,0x20,0xfe,0x20,0x20,0xfc,0x20,0x20},
    {0xfe,0x22,0x22,0x22,0x2a,0x24,0x20,0x20},
    {0x01,0x01,0xef,0x01,0x07,0xc4,0x07,0x04},
    {0xe7,0x21,0x2f,0x21,0xa1,0x41,0x01,0x01}, 
    {0x02, 0x02, 0xdf, 0x02, 0xc2, 0x4f, 0xc2, 0x42},
     {0xdf, 0x02, 0xe2, 0x02, 0x02, 0x02, 0x02, 0x02}
    
    };
    /*
    const uint8_t charBitmap[][8] = {
       { 0xc, 0x12, 0x12, 0xc, 0, 0, 0, 0 },  
        { 0x6, 0x9, 0x9, 0x6, 0, 0, 0, 0 },  
         { 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0, 0x0 },  
          { 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0, 0x0 }, 
            { 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0x0 },  
             { 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0x0 },  
              { 0x0, 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0x0 }, 
                { 0x0, 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0x0 }
    
    };
    */
    int n = 1;
    
    LiquidCrystal_I2C	lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
    
    void setup()
    {
      lcd.begin (16,2); //  <<----- My LCD was 16x2
     int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0]));
      for ( int i = 0; i < charBitmapSize; i++ )
       {
          lcd.createChar ( i, (uint8_t *)charBitmap[i] );
       }
    // Switch on the backlight
    lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
    lcd.setBacklight(HIGH);
    lcd.home (); // go home
    
      lcd.print("I2C16x2");
      lcd.setCursor (12,0);
       lcd.print (char(4));
       lcd.print (char(0));
       lcd.print (char(6));
         lcd.print (char(2));
         lcd.setCursor (12,1);
          lcd.print (char(5));
             lcd.print (char(1));
              lcd.print (char(7));
         lcd.print (char(3));
    }
    
    void loop()
    {
      // Backlight on/off every 3 seconds
      lcd.setCursor (0,1);        // go to start of 2nd line
      lcd.print(n++,DEC);
      lcd.setCursor (10,1);
       lcd.print (char(random(7)));
      lcd.setBacklight(LOW);      // Backlight off
      delay(3000);
      lcd.setBacklight(HIGH);     // Backlight on
      delay(3000);
    }
    

    使用I2c链接lcd1602A ,下载较新的的库文件,使用I2cScaner 获得 地址为 0x27,随便做了姓氏的字模,不过没有考虑到字符时5*8,没有找到单独制作5*8点阵的软件,使用了八个自定义字符显示16*16的汉字,有些浪费。

    总结,arduino下开发更加容易,因为有很多demo和先驱者

  • 相关阅读:
    P1908 逆序对
    P3834 【模板】可持久化线段树 1(主席树)
    BZOJ 4300: 绝世好题
    Codevs 2185【模板】最长公共上升子序列
    P1439 【模板】最长公共子序列
    P3865 【模板】ST表
    【转】良心的可持久化线段树教程
    Codevs 1299 切水果
    P3388 【模板】割点(割顶)&& 桥
    P3805 【模板】manacher算法
  • 原文地址:https://www.cnblogs.com/cndavy/p/3450630.html
Copyright © 2011-2022 走看看