commit 6419eb16d6e3e5d9e9e8775eb330f2e8612e6611 Author: Lauchmelder23ยง Date: Thu Nov 19 22:12:36 2020 +0000 initial commit diff --git a/example/example b/example/example new file mode 100755 index 0000000..423a166 Binary files /dev/null and b/example/example differ diff --git a/example/example.c b/example/example.c new file mode 100644 index 0000000..958e15a --- /dev/null +++ b/example/example.c @@ -0,0 +1,14 @@ +#include "../raspberrylcd.h" + +int main(void) +{ + LCDScreen screen; + configurePins(&screen, 7, 9, 8, 0, 0, 0, 0, 21, 22, 23, 24); + + initScreen(&screen, HALF_BYTE_INTERFACE, TWO_LINES, FONT_5x7, CURSOR_ON | CURSOR_BLINK, LEFT_TO_RIGHT); + sendData(&screen, JAPANESE_QUOTE_START); + sendData(&screen, JAPANESE_QUOTE_END); + resetPins(&screen); + + return 0; +} \ No newline at end of file diff --git a/raspberrylcd.c b/raspberrylcd.c new file mode 100644 index 0000000..84fc7da --- /dev/null +++ b/raspberrylcd.c @@ -0,0 +1,127 @@ +#include "raspberrylcd.h" + +#include +#include + +void waitMs(uint32_t ms) +{ + delay(ms); +} + +LCDScreen* configurePins(LCDScreen* screen, uint8_t RS, uint8_t RW, uint8_t E, + uint8_t D0, uint8_t D1, uint8_t D2, uint8_t D3, + uint8_t D4, uint8_t D5, uint8_t D6, uint8_t D7) +{ + screen->RS = RS; + screen->RW = RW; + screen->E = E; + + screen->D0 = D0; + screen->D1 = D1; + screen->D2 = D2; + screen->D3 = D3; + screen->D4 = D4; + screen->D5 = D5; + screen->D6 = D6; + screen->D7 = D7; + + wiringPiSetup(); + pinMode(screen->RS, OUTPUT); + pinMode(screen->RW, OUTPUT); + pinMode(screen->E, OUTPUT); + + pinMode(screen->D0, OUTPUT); + pinMode(screen->D1, OUTPUT); + pinMode(screen->D2, OUTPUT); + pinMode(screen->D3, OUTPUT); + pinMode(screen->D4, OUTPUT); + pinMode(screen->D5, OUTPUT); + pinMode(screen->D6, OUTPUT); + pinMode(screen->D7, OUTPUT); + + return screen; +} + +void initScreen(LCDScreen* screen, uint8_t interface_bits, uint8_t num_lines, uint8_t fontType, uint8_t cursor, uint8_t writeDirection) +{ + screen->interface_bits = interface_bits; + sendCommand(screen, (FUNCTION_SET | HALF_BYTE_INTERFACE) >> 4); + + sendCommand(screen, FUNCTION_SET | num_lines | fontType); + sendCommand(screen, DISPLAY_SWITCH | DISPLAY_ON | cursor); + sendCommand(screen, SCREEN_CLEAR); + sendCommand(screen, INPUT_SET | writeDirection); +} + +void resetPins(LCDScreen* screen) +{ + digitalWrite(screen->D7, LOW); + digitalWrite(screen->D6, LOW); + digitalWrite(screen->D5, LOW); + digitalWrite(screen->D4, LOW); + digitalWrite(screen->D3, LOW); + digitalWrite(screen->D2, LOW); + digitalWrite(screen->D1, LOW); + digitalWrite(screen->D0, LOW); + + digitalWrite(screen->E, LOW); + digitalWrite(screen->RW, LOW); + digitalWrite(screen->RS, LOW); +} + +void helper_send(LCDScreen* screen, uint8_t byte) +{ + digitalWrite(screen->E, HIGH); + digitalWrite(screen->D7, (byte >> 7) & 0x1); + digitalWrite(screen->D6, (byte >> 6) & 0x1); + digitalWrite(screen->D5, (byte >> 5) & 0x1); + digitalWrite(screen->D4, (byte >> 4) & 0x1); + + if(screen->interface_bits == FULL_BYTE_INTERFACE) + { + digitalWrite(screen->D3, (byte >> 3) & 0x1); + digitalWrite(screen->D2, (byte >> 2) & 0x1); + digitalWrite(screen->D1, (byte >> 1) & 0x1); + digitalWrite(screen->D0, (byte >> 0) & 0x1); + } + else if(screen->interface_bits == HALF_BYTE_INTERFACE) + { + delayMicroseconds(1000); + + digitalWrite(screen->E, LOW); + delayMicroseconds(10000); + + digitalWrite(screen->E, HIGH); + digitalWrite(screen->D7, (byte >> 3) & 0x1); + digitalWrite(screen->D6, (byte >> 2) & 0x1); + digitalWrite(screen->D5, (byte >> 1) & 0x1); + digitalWrite(screen->D4, (byte >> 0) & 0x1); + } + + delayMicroseconds(1000); + + digitalWrite(screen->E, LOW); + delayMicroseconds(10000); +} + +void sendCommand(LCDScreen* screen, uint8_t command) +{ + digitalWrite(screen->RW, LOW); + digitalWrite(screen->RS, LOW); + + helper_send(screen, command); +} + +void sendData(LCDScreen* screen, uint8_t data) +{ + digitalWrite(screen->RW, LOW); + digitalWrite(screen->RS, HIGH); + + helper_send(screen, data); +} + +void sendText(LCDScreen* screen, const char* text) +{ + for(const char* c = text; *c != '\x00'; c++) + sendData(screen, *c); +} \ No newline at end of file diff --git a/raspberrylcd.h b/raspberrylcd.h new file mode 100644 index 0000000..32702a5 --- /dev/null +++ b/raspberrylcd.h @@ -0,0 +1,62 @@ +#ifndef RASPBERRY_LCD_H +#define RASPBERRY_LCD_H + +// Commands +#define SCREEN_CLEAR 0b00000001 +#define CURSOR_RETURN 0b00000010 +#define INPUT_SET 0b00000100 +#define DISPLAY_SWITCH 0b00001000 +#define SHIFT 0b00010000 +#define FUNCTION_SET 0b00100000 +#define CGRAM_AD_SET 0b01000000 +#define DDRAM_AD_SET 0b10000000 + +// Parameters +#define LEFT_TO_RIGHT 0b10 +#define RIGHT_TO_LEFT 0b00 +#define DISPLAY_ON 0b100 +#define DISPLAY_OFF 0b000 +#define CURSOR_ON 0b010 +#define CURSOR_OFF 0b000 +#define CURSOR_BLINK 0b001 +#define CURSOR_STATIC 0b000 +#define DISPLAY_SHIFT 0b1000 +#define CURSOR_SHIFT 0b0000 +#define RIGHT_SHIFT 0b0100 +#define LEFT_SHIFT 0b0000 +#define FULL_BYTE_INTERFACE 0b10000 +#define HALF_BYTE_INTERFACE 0b00000 +#define ONE_LINE 0b00000 +#define TWO_LINES 0b01000 +#define FONT_5x10 0b00100 +#define FONT_5x7 0b00000 + +// Special characters supported by the LCD +#define YEN_CURRENCY 0b01011100 +#define ARROW_RIGHT 0b01111110 +#define ARROW_LEFT 0b01111111 +#define JAPANESE_PERIOD 0b10100001 +#define JAPANESE_QUOTE_START 0b10100010 +#define JAPANESE_QUOTE_END 0b10100011 + +#include + +extern void waitMs(uint32_t ms); + +typedef struct { + uint8_t RS, RW, E, D0, D1, D2, D3, D4, D5, D6, D7; + uint8_t interface_bits; +} LCDScreen; + +extern LCDScreen* configurePins(LCDScreen* screen, uint8_t RS, uint8_t RW, uint8_t E, + uint8_t D0, uint8_t D1, uint8_t D2, uint8_t D3, + uint8_t D4, uint8_t D5, uint8_t D6, uint8_t D7); + +extern void initScreen(LCDScreen* screen, uint8_t interface_bits, uint8_t num_lines, uint8_t fontType, uint8_t cursor, uint8_t writeDirection); +extern void resetPins(LCDScreen* screen); + +extern void sendCommand(LCDScreen* screen, uint8_t command); +extern void sendData(LCDScreen* screen, uint8_t data); +extern void sendText(LCDScreen* screen, const char* text); + +#endif // RASPBERRY_LCD_H \ No newline at end of file