美文网首页
8051驱动 LCD1602 例子程序

8051驱动 LCD1602 例子程序

作者: 缺小小 | 来源:发表于2018-02-11 16:40 被阅读14次

    配套电路图

    image.png

    C程序代码

    #include <8051.h>
    #define true  1
    #define false 0
    #define HIGH  1
    #define LOW   0
    
    /**
     * LCD1602 的测试例子
     *
     * @author 鸿则<hungtcs@163.com>
     * @Date 2018-02-11
     */
    
    // LCD1602 define
    #define LCD1602_E  P2_6
    #define LCD1602_RS P2_4
    #define LCD1602_RW P2_5
    
    // length is 16
    unsigned char line1[] = "  hello world!  ";
    unsigned char line2[] = "~!@#$%^&*()_+[]?";
    
    void delay(unsigned int multiple)
    {
      unsigned int i;
      for(; multiple>0; multiple--)
        for(i=100; i>0; i--);
    }
    
    void writeCommand(unsigned char command)
    {
      LCD1602_E  = LOW;
      LCD1602_RS = LOW;
      LCD1602_RW = LOW;
      P0 = command;
      delay(10);
      LCD1602_E = HIGH;
      delay(20);
      LCD1602_E = LOW;
    }
    
    void writeData(unsigned char data)
    {
      LCD1602_E  = LOW;
      LCD1602_RS = HIGH;
      LCD1602_RW = LOW;
      P0 = data;
      delay(10);
      LCD1602_E = HIGH;
      delay(20);
      LCD1602_E = LOW;
    }
    
    void initLCD1602()
    {
      delay(300);
      writeCommand(0x38);
      delay(100);
      writeCommand(0x38);
      delay(100);
      writeCommand(0x38);
      writeCommand(0x38);
      writeCommand(0x08);
      writeCommand(0x01);
      writeCommand(0x06);
      writeCommand(0x0C);
    }
    
    void setup()
    {
      unsigned char i;
      initLCD1602();
      for(i=0; i<16; i++)
      {
        writeCommand(0x80 + i);
        writeData(line1[i]);
        delay(10);
      }
      for(i=0; i<16; i++)
      {
        writeCommand(0x80 + 0x40 + i);
        writeData(line2[i]);
        delay(10);
      }
    }
    
    void loop()
    {
      
    }
    
    void main()
    {
      setup();
      while(true) loop();
    }
    

    相关文章

      网友评论

          本文标题:8051驱动 LCD1602 例子程序

          本文链接:https://www.haomeiwen.com/subject/irzktftx.html