From 6419eb16d6e3e5d9e9e8775eb330f2e8612e6611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauchmelder23=C2=A7?= Date: Thu, 19 Nov 2020 22:12:36 +0000 Subject: [PATCH] initial commit --- example/example | Bin 0 -> 8452 bytes example/example.c | 14 +++++ raspberrylcd.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++ raspberrylcd.h | 62 ++++++++++++++++++++++ 4 files changed, 203 insertions(+) create mode 100755 example/example create mode 100644 example/example.c create mode 100644 raspberrylcd.c create mode 100644 raspberrylcd.h diff --git a/example/example b/example/example new file mode 100755 index 0000000000000000000000000000000000000000..423a16664db1450033b49d2f4d29ed36648e5f4b GIT binary patch literal 8452 zcmd5>eQ=yb8Q=Hrl3bd!y|!r^+GQxjOYTi_!{zS0 zds7kxX#oYtGOY;KVbny*FgT-Fl|K+$tWtz=fI*5QV{yzS9JI7bkWv^JIDfx)chk)^ zRGHxqce2^%*`{0oS(MiCa3(0xoS zqCPV)Lx_PHmREy-um)5uN6Tj$fn*Ki0amT|L)QVm1O2jI0G^e4h`owNQI@zBeX*7) z88N|`tUj+sA!tNpzkJ$<{~_o@(6f$%XQf_`Ap?8{eY38HoK@?tEVyy7=M+94LT!iM z?Eh`ZSou81){sneHYDTq$waDupgxxE>+O!@(vcNVc^KLtZr$dqC=&Ds$IL<-HK>$P z@4h+IcS-xxi(8+2*MbM1+4tU4kN)kNg)e;d+C?}1bJolq9V_ozdAg}Rvib5e z*S@xI@OL{N|2`iz3&*TW7{oHope27<3I9C!l_5E+keDxi1-@y5pC{zZbpw#qu@>ZzX5;N{(Nyy3I6dC{WnVV!zKJbz?=3nC3=B1zCMvnq)N)2P6>=V$=IOCBvNhZxFh0; zo`f4qUXx9@j?jH=b=>}p@PKWJu53Ezbfr`A9Ea1zAiG$^I@4JfeNv+O?29E*!gk^@ zHwHy#E+^$C&2f1eCl&W-)uIvWHf>ndYF`xLo|eV(;+HYZ=;MwfE&of&Uhq0*Y7z;A zqEDP69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UIqWN>1X}*26)ELc7^xj zlhTX3nB9=G|HJU3>5p$iuHSdm&y=4aJWV`y0C*F>xI|wM-t_mS;4|3Nyi2CNR`A>} zhZVoxz;k{b;B^UuNEtl)zX|*nV}7EcKbQ3`r+d1(8gMVIZm4@w{rpPzb~iLNL?Y^M zDs@-ZT^>n`y11x|eH6wLFxC}IChhB;*mZVy>Z7okqRp(EbNjoyBVD*D)iP$;oC1lU;GJPO`h6=tz1iY^-f>XzWR`qh-~mHN4mLu7bOA z{ode34tKh(>At=gu6A*G+@0v@&pKNYDfs2Qm)H~c#vGQsOrCT_ZQdWBdqFMlw&8hP zLaXcN>thMGjjK^|Ro&ct_QtvN?%a;&(snO~j(c^^e`{CPad5f4;R+ayCsKBQ&WU?d z>~IDquE9BXqD8Nh%s5$_E||Dyx3Q4C^PYUgZr+n;ot)!(9+EfdHm>*e`p{)M#3tSP zd-w3>RM_ciUcrknyH2EK)rNXE)}uIV(@1B30zWJz z;vzz>HTJ5zj3Vm(GeXwxLRUKNLh- z+S?+|Kq5XMB2KRiYzdb!E#aDm(O6%i3mv9mNE$%EhzNF7A2y2c%*9+co9OI!u?Igy zVXlQ&ua_e+AK-6?%)@k{j%5idPw{vytKatIcMhyqWJVywa&Q{Q{9!K)@EfCx#;u0n z4Du@4V_w1UeH=7%I~L+tc}}fDC64(9%%rH1MVwm$?jmlRvam)>y!0-&H3$ZL; zL1kV?9P^V14Agw)07Wxs;+U^$+-}euEBoR397&x1Rsum_0vEh<#r8uQ%8--xH`_X%K_?J*X3eysig+)?Q1W8!$O z&H2BG%J~xKoi7n=QPQgD`R2iE9M4%%;HL85E5K1sdpw^s0cXyGHds!flGc3@w}c8+ zsAGK-6}B=;EVcpHpc%B<9$Ut!F1vx-j*ZIwzZI2ZfXItcZ?2$OPT{iTc0-`;F=IBw N!iAxU^a?tS`wwsHd|3bh literal 0 HcmV?d00001 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