A basic library for the Freetronics arduino LCD shield.

Fork of freetronicsLCDShield by Koen Kempeneers

Committer:
KKempeneers
Date:
Tue Oct 08 08:46:39 2013 +0000
Revision:
0:01f3d38f8b6d
Child:
1:ddcefddda4a7
First attempt at publishing a library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
KKempeneers 0:01f3d38f8b6d 1 #include "mbed.h"
KKempeneers 0:01f3d38f8b6d 2 #include "freetronicsLCDShield.h"
KKempeneers 0:01f3d38f8b6d 3
KKempeneers 0:01f3d38f8b6d 4 #define PULSE_E wait(0.000001f); \
KKempeneers 0:01f3d38f8b6d 5 _e = 0; \
KKempeneers 0:01f3d38f8b6d 6 wait(0.000001f); \
KKempeneers 0:01f3d38f8b6d 7 _e = 1;
KKempeneers 0:01f3d38f8b6d 8
KKempeneers 0:01f3d38f8b6d 9
KKempeneers 0:01f3d38f8b6d 10 freetronicsLCDShield::freetronicsLCDShield (PinName rs, PinName e, PinName d0, PinName d1, PinName d2, PinName d3, PinName bl, PinName a0)
KKempeneers 0:01f3d38f8b6d 11 : _rs(rs), _e(e), _d(d0, d1, d2, d3), _bl(bl), _a0(a0) {
KKempeneers 0:01f3d38f8b6d 12 // Class constructor
KKempeneers 0:01f3d38f8b6d 13 // Init the display, wait 15ms to insure the power is up
KKempeneers 0:01f3d38f8b6d 14 _e = true;
KKempeneers 0:01f3d38f8b6d 15 _rs = false;
KKempeneers 0:01f3d38f8b6d 16 wait(0.015f);
KKempeneers 0:01f3d38f8b6d 17
KKempeneers 0:01f3d38f8b6d 18 for (int i = 0; i < 3; i++){
KKempeneers 0:01f3d38f8b6d 19 writeByte(0x3);
KKempeneers 0:01f3d38f8b6d 20 wait(0.00164f); // this command takes 1.64ms, so wait for it
KKempeneers 0:01f3d38f8b6d 21 }
KKempeneers 0:01f3d38f8b6d 22
KKempeneers 0:01f3d38f8b6d 23 writeByte(0x2); // 4-bit mode
KKempeneers 0:01f3d38f8b6d 24 writeCommand(0x28); // Function set 001 BW N F - -
KKempeneers 0:01f3d38f8b6d 25 writeCommand(0x0C); // Display on/off controll 0000 1 D C B (D(isplay) On/Off C(ursor) On/Off B(link) On/Off
KKempeneers 0:01f3d38f8b6d 26 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
KKempeneers 0:01f3d38f8b6d 27 cls();
KKempeneers 0:01f3d38f8b6d 28
KKempeneers 0:01f3d38f8b6d 29 // Set the PWM period to 20ms
KKempeneers 0:01f3d38f8b6d 30 _bl.period_ms(1);
KKempeneers 0:01f3d38f8b6d 31 _bl.write(0.0);
KKempeneers 0:01f3d38f8b6d 32 }
KKempeneers 0:01f3d38f8b6d 33
KKempeneers 0:01f3d38f8b6d 34 void freetronicsLCDShield::setCursorPosition (int line, int col) {
KKempeneers 0:01f3d38f8b6d 35 // Set the new cursorposition
KKempeneers 0:01f3d38f8b6d 36 writeCommand(0x80 + (line * 0x40) + col);
KKempeneers 0:01f3d38f8b6d 37 }
KKempeneers 0:01f3d38f8b6d 38
KKempeneers 0:01f3d38f8b6d 39 void freetronicsLCDShield::setBackLight (bool blStatus) {
KKempeneers 0:01f3d38f8b6d 40 // Switch the backlight on (true) or off (false)
KKempeneers 0:01f3d38f8b6d 41 _bl = (blStatus) ? 1.0 : 0.0;
KKempeneers 0:01f3d38f8b6d 42 }
KKempeneers 0:01f3d38f8b6d 43
KKempeneers 0:01f3d38f8b6d 44 void freetronicsLCDShield::setBackLight (float blIntensity) {
KKempeneers 0:01f3d38f8b6d 45 // Switch the backlight on (true) or off (false)
KKempeneers 0:01f3d38f8b6d 46 _bl = blIntensity;
KKempeneers 0:01f3d38f8b6d 47 }
KKempeneers 0:01f3d38f8b6d 48
KKempeneers 0:01f3d38f8b6d 49 void freetronicsLCDShield::setCursor (bool cStatus, bool blink) {
KKempeneers 0:01f3d38f8b6d 50 int tmp = 0;
KKempeneers 0:01f3d38f8b6d 51
KKempeneers 0:01f3d38f8b6d 52 if (blink) tmp = 0x01;
KKempeneers 0:01f3d38f8b6d 53 if (cStatus) tmp |= 0x02;
KKempeneers 0:01f3d38f8b6d 54 writeCommand(0x0C + tmp);
KKempeneers 0:01f3d38f8b6d 55 }
KKempeneers 0:01f3d38f8b6d 56
KKempeneers 0:01f3d38f8b6d 57 void freetronicsLCDShield::shift (bool left) {
KKempeneers 0:01f3d38f8b6d 58 int tmp = 0;
KKempeneers 0:01f3d38f8b6d 59
KKempeneers 0:01f3d38f8b6d 60 if (left) tmp = 0x04;
KKempeneers 0:01f3d38f8b6d 61 writeCommand(0x18 + tmp);
KKempeneers 0:01f3d38f8b6d 62 }
KKempeneers 0:01f3d38f8b6d 63
KKempeneers 0:01f3d38f8b6d 64 void freetronicsLCDShield::cls(void) {
KKempeneers 0:01f3d38f8b6d 65 // Clear the display and place the cursor at 0, 0
KKempeneers 0:01f3d38f8b6d 66 writeCommand(0x01);
KKempeneers 0:01f3d38f8b6d 67 wait(0.00164f);
KKempeneers 0:01f3d38f8b6d 68 }
KKempeneers 0:01f3d38f8b6d 69
KKempeneers 0:01f3d38f8b6d 70 void freetronicsLCDShield::home(void) {
KKempeneers 0:01f3d38f8b6d 71 // Undo shift operations and place cursor at 0,0
KKempeneers 0:01f3d38f8b6d 72 writeCommand(0x02);
KKempeneers 0:01f3d38f8b6d 73 wait(0.00164f);
KKempeneers 0:01f3d38f8b6d 74 }
KKempeneers 0:01f3d38f8b6d 75
KKempeneers 0:01f3d38f8b6d 76 // Low level output functions
KKempeneers 0:01f3d38f8b6d 77 void freetronicsLCDShield::writeByte (int byte) {
KKempeneers 0:01f3d38f8b6d 78 // Split the byte in high and low nibble, write high nibble first
KKempeneers 0:01f3d38f8b6d 79 _d = byte >> 4;
KKempeneers 0:01f3d38f8b6d 80 PULSE_E;
KKempeneers 0:01f3d38f8b6d 81 _d = byte >> 0;
KKempeneers 0:01f3d38f8b6d 82 PULSE_E;
KKempeneers 0:01f3d38f8b6d 83 // Most instructions take 40us
KKempeneers 0:01f3d38f8b6d 84 wait(0.000040f);
KKempeneers 0:01f3d38f8b6d 85 }
KKempeneers 0:01f3d38f8b6d 86
KKempeneers 0:01f3d38f8b6d 87 void freetronicsLCDShield::writeData (int data) {
KKempeneers 0:01f3d38f8b6d 88 _rs = true;
KKempeneers 0:01f3d38f8b6d 89 writeByte(data);
KKempeneers 0:01f3d38f8b6d 90 }
KKempeneers 0:01f3d38f8b6d 91
KKempeneers 0:01f3d38f8b6d 92 void freetronicsLCDShield::writeCommand (int command) {
KKempeneers 0:01f3d38f8b6d 93 _rs = false;
KKempeneers 0:01f3d38f8b6d 94 writeByte(command);
KKempeneers 0:01f3d38f8b6d 95 }
KKempeneers 0:01f3d38f8b6d 96
KKempeneers 0:01f3d38f8b6d 97 float freetronicsLCDShield::readButton(void) {
KKempeneers 0:01f3d38f8b6d 98 return(_a0.read());
KKempeneers 0:01f3d38f8b6d 99 }
KKempeneers 0:01f3d38f8b6d 100
KKempeneers 0:01f3d38f8b6d 101 // Virtual functions for stream
KKempeneers 0:01f3d38f8b6d 102 int freetronicsLCDShield::_putc(int value) {
KKempeneers 0:01f3d38f8b6d 103 writeData(value);
KKempeneers 0:01f3d38f8b6d 104 return value;
KKempeneers 0:01f3d38f8b6d 105 }
KKempeneers 0:01f3d38f8b6d 106
KKempeneers 0:01f3d38f8b6d 107 int freetronicsLCDShield::_getc() {
KKempeneers 0:01f3d38f8b6d 108 return -1;
KKempeneers 0:01f3d38f8b6d 109 }