【树莓派+arduino实验记录18】I2C_LCD1602液晶

2020-09-09  本文已影响0人  Geekero

Arduino

//include the libarary code
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
/*****************************************/
char array1[]="Hellow World!             "; //the string to print on the LCD
char array2[]="Hellow World!             ";
int tim = 500; //the value of the delay time

// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x27, 16, 2); //set the LCD address to 0x27 for a 16 chars and 2 line display
/****************************************************/

void setup()
{
    lcd.init(); //initialize the led
    lcd.backlight(); //open the backlight
}

/**********************************************************/
void loop()
{
    lcd.setCursor(15, 0); //set the cursor to column 15, line 0
    for (int positionCounter1 = 0; positionCounter1 < 26; positionCounter1++)
    {
      lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
      lcd.print(array1[positionCounter1]); //Print a massage to the LCD.
      delay(tim); //wait for 250 microseconds
    }
    lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
    lcd.setCursor(15, 1); //Set the cursor to column 15, line 1
    for (int positionCounter = 0; positionCounter < 26; positionCounter++)
    {
      lcd.scrollDisplayLeft(); //Scrolls the contents of the display one space to the left.
      lcd.print(array2[positionCounter]); //Print a message to the LCD.
      delay(tim); //wait for 250 microseconds
    }
    lcd.clear(); //Clears the LCD screen and positions the cursor in the upper-left corner.
}

树莓派

C

#include <stdio.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <string.h>

int LCDAddr = 0x27;
int BLEN = 1;
int fd;

void write_word(int data){
    int temp = data;
    if (BLEN == 1)
            temp |= 0x08;
    else
            temp &= 0xF7;
    wiringPiI2CWrite(fd, temp);
}

void send_command(int comm){
    int buf;
    //Send bit7-4 firstly
    buf = comm & 0xF0;
    buf |= 0x04;    //RS = 0, RW = 0, EN = 1
    write_word(buf);
    delay(2);
    buf &= 0xFB; //Make EN = 0
    write_word(buf);
    //Send bit3-0 secondly
    buf = (comm & 0x0F) << 4;
    buf |= 0x04;  //RS = 0, RW = 0, EN = 1
    write_word(buf);
    delay(2);
    buf &= 0xFB;  //Make EN = 0
    write_word(buf);
}

void send_data(int data){
    int buf;
    //Send bit7-4 firstly
    buf = data & 0xF0;
    buf |= 0x05;  //RS = 1, RW = 0, EN = 1
    write_word(buf);
    delay(2);
    buf  &= 0xFB;  //Make EN = 0;
    write_word(buf);
    //Send bit 3-0 secondly
    buf = (data & 0x0F) << 4;
    buf |= 0x05; //RS = 1, RW = 0, EN = 1;
    write_word(buf);
    delay(2);
    buf &= 0xFB; //Make EN = 0;
    write_word(buf);
}

void init()
{
    send_command(0x33); //Must initialize to 8-line mode at first
    delay(5);
    send_command(0x32); //Then initialize to 4-line mode 
    delay(5);
    send_command(0x28); //2 Lines & 5*7 dots
    delay(5);
    send_command(0x0C); //Enable display without cursor
    delay(5);
    send_command(0x01); //Clear the screen
    wiringPiI2CWrite(fd, 0x08);
}

void clear(){
    send_command(0x01); //clear the screen
}

void write(int x, int y, char data[]){
    int addr, i;
    int tmp;
    if (x < 0) x = 0;
    if (x > 15) x = 15;
    if (y < 0) y = 0;
    if (y > 1) y = 1;
    // Move cursor
    addr = 0x80 + 0x40 * y + x;
    send_command(addr);

    tmp = strlen(data);
    for (i = 0; i < tmp; i++){
         send_data(data[i]);
    }
}

void main(){
    fd = wiringPiI2CSetup(LCDAddr);
    init();
    write(0, 0, "Greeting!");
    write(1, 1, "From Robin");
    delay(2000);
    clear();
}

Python

#!/usr/bin/env python
import LCD1602
import time

def setup():
    LCD1602.init(0x27, 1) #init (salve address, background light)
    LCD1602.write(0, 0, 'Greetings!')
    LCD1602.write(1, 1, 'from Robin')
    time.sleep(2)

def loop():
    space = '                '
    greetings = 'Thank you for buying Sensor Kit for RaspberryPi! ^_^'
    greetings = space + greetings
    while True:
        tmp = greetings
        for i in range(0, len(greetings)):
            LCD1602.write(0, 0, tmp)
            tmp = tmp[1:]
            time.sleep(0.8)
            LCD1602.clear()

def destroy():
    pass


if __name__ == "__main__":
    try:
        setup()
        while True:
            loop()
    except KeyboardInterrupt:
        LCD1602.clear()

LCD1602.py:

#!/usr/bin/env python

import time
import smbus

BUS = smbus.SMBus(1)

def write_word(addr, data):
    global BLEN
    temp = data
    if BLEN == 1:
        temp |= 0x08
    else:
        temp &= 0xF7
    BUS.write_byte(addr ,temp)

def send_command(comm):
    # Send bit7-4 firstly
    buf = comm & 0xF0
    buf |= 0x04               # RS = 0, RW = 0, EN = 1
    write_word(LCD_ADDR ,buf)
    time.sleep(0.002)
    buf &= 0xFB               # Make EN = 0
    write_word(LCD_ADDR ,buf)

    # Send bit3-0 secondly
    buf = (comm & 0x0F) << 4
    buf |= 0x04               # RS = 0, RW = 0, EN = 1
    write_word(LCD_ADDR ,buf)
    time.sleep(0.002)
    buf &= 0xFB               # Make EN = 0
    write_word(LCD_ADDR ,buf)

def send_data(data):
    # Send bit7-4 firstly
    buf = data & 0xF0
    buf |= 0x05               # RS = 1, RW = 0, EN = 1
    write_word(LCD_ADDR ,buf)
    time.sleep(0.002)
    buf &= 0xFB               # Make EN = 0
    write_word(LCD_ADDR ,buf)

    # Send bit3-0 secondly
    buf = (data & 0x0F) << 4
    buf |= 0x05               # RS = 1, RW = 0, EN = 1
    write_word(LCD_ADDR ,buf)
    time.sleep(0.002)
    buf &= 0xFB               # Make EN = 0
    write_word(LCD_ADDR ,buf)

def init(addr, bl):
#   global BUS
#   BUS = smbus.SMBus(1)
    global LCD_ADDR
    global BLEN
    LCD_ADDR = addr
    BLEN = bl
    try:
        send_command(0x33) # Must initialize to 8-line mode at first
        time.sleep(0.005)
        send_command(0x32) # Then initialize to 4-line mode
        time.sleep(0.005)
        send_command(0x28) # 2 Lines & 5*7 dots
        time.sleep(0.005)
        send_command(0x0C) # Enable display without cursor
        time.sleep(0.005)
        send_command(0x01) # Clear Screen
        BUS.write_byte(LCD_ADDR, 0x08)
    except:
        return False
    else:
        return True

def clear():
    send_command(0x01) # Clear Screen

def openlight():  # Enable the backlight
    BUS.write_byte(0x27,0x08)
    BUS.close()

def write(x, y, str):
    if x < 0:
        x = 0
    if x > 15:
        x = 15
    if y <0:
        y = 0
    if y > 1:
        y = 1

    # Move cursor
    addr = 0x80 + 0x40 * y + x
    send_command(addr)

    for chr in str:
        send_data(ord(chr))

if __name__ == '__main__':
    init(0x27, 1)
    write(4, 0, 'Hello')
    write(7, 1, 'world!')
上一篇 下一篇

猜你喜欢

热点阅读