#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <wiringPi.h> #include <lcd.h>

// 假设LCD_RS和LCD_EN已经在其他地方定义 //extern int LCD_RS; //extern int LCD_EN;

#define LCD_D4 4 #define LCD_D5 12 #define LCD_D6 13 #define LCD_D7 6

int main() { int lcd; char name[] = 'John Doe'; char studentID[] = '123456789';

// 初始化WiringPi库
wiringPiSetup();

// 初始化LCD
lcd = lcdInit(2, 16, 4, LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0, 0, 0, 0);

if (lcd == -1) {
    printf('无法初始化LCD\n');
    return 1;
}

// 显示姓名
lcdPosition(lcd, 0, 0);
lcdPuts(lcd, name);

// 显示学号
lcdPosition(lcd, 0, 1);
lcdPuts(lcd, studentID);

// 程序延迟一段时间
sleep(5);

// 清空LCD并释放资源
lcdClear(lcd);
lcdHome(lcd);
lcdClose(lcd);

return 0;

}

// 如果LCD_RS和LCD_EN已经在其他地方被定义,您可以按照以下步骤修改程序:

// 1. 在程序的开头,找到以下定义:

// c // #define LCD_RS 3 // #define LCD_EN 14 //

// 2. 将LCD_RS和LCD_EN的定义注释掉或删除。

// 3. 在程序中找到以下代码:

// c // lcd = lcdInit(2, 16, 4, LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0, 0, 0, 0); //

// 4. 修改为使用您先前定义的LCD_RS和LCD_EN的引脚值进行初始化,例如:

// ```c // int LCD_RS = your_RS_pin_number; // int LCD_EN = your_EN_pin_number;

// lcd = lcdInit(2, 16, 4, LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7, 0, 0, 0, 0); // ```

// 请确保将'your_RS_pin_number'和'your_EN_pin_number'替换为您实际使用的引脚号。

// 5. 保存并编译程序,然后再次运行即可。

// 这样,您就可以使用先前定义的LCD_RS和LCD_EN引脚进行初始化,并使程序能够与其他定义的引脚配合使用。

// 请注意,在修改程序时,确保按照正确的引脚定义进行修改,并根据实际硬件连接进行适当的更改。如果您还有其他问题,请随时提问。

C语言LCD显示姓名和学号(Raspberry Pi示例)

原文地址: https://www.cveoy.top/t/topic/mhe 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录