Luka Danilovic
/
ELEC351_10497267_SUBMISSION
ELEC351 SUBMISSION - Same as on the DLE
ELEC351_LIBRARY/displayMaster/displayMaster.cpp@0:c66224a27cf8, 2018-01-10 (annotated)
- Committer:
- Luka_Danilovic
- Date:
- Wed Jan 10 09:49:43 2018 +0000
- Revision:
- 0:c66224a27cf8
ELEC351 SUBMISSION - SAme as on the DLE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Luka_Danilovic | 0:c66224a27cf8 | 1 | #include "mbed.h" |
Luka_Danilovic | 0:c66224a27cf8 | 2 | #include "displayMaster.hpp" |
Luka_Danilovic | 0:c66224a27cf8 | 3 | |
Luka_Danilovic | 0:c66224a27cf8 | 4 | //////////////////////////////////////////////////////////////////////////////// |
Luka_Danilovic | 0:c66224a27cf8 | 5 | // INCOMPLETE - RUN OUT OF TIME TO DEBUG // |
Luka_Danilovic | 0:c66224a27cf8 | 6 | //////////////////////////////////////////////////////////////////////////////// |
Luka_Danilovic | 0:c66224a27cf8 | 7 | |
Luka_Danilovic | 0:c66224a27cf8 | 8 | /* Instructions on using the 16x2 LCD panel are found here: |
Luka_Danilovic | 0:c66224a27cf8 | 9 | [https://www.8051projects.net/lcd-interfacing/lcd-4-bit.php] */ |
Luka_Danilovic | 0:c66224a27cf8 | 10 | /* N.B. I did the code from scratch since I do not have the code from ELEC230 */ |
Luka_Danilovic | 0:c66224a27cf8 | 11 | /* N.B. This class contains blocking function so it goes into its own thread */ |
Luka_Danilovic | 0:c66224a27cf8 | 12 | |
Luka_Danilovic | 0:c66224a27cf8 | 13 | C_displayMaster::C_displayMaster(PinName D1, PinName D2, PinName D3 , PinName D4 , PinName RS , PinName RW, PinName EN) |
Luka_Danilovic | 0:c66224a27cf8 | 14 | : // Initialisation of the instance |
Luka_Danilovic | 0:c66224a27cf8 | 15 | _commsBus(D1, D2, D3, D4), |
Luka_Danilovic | 0:c66224a27cf8 | 16 | _registerSel(RS), |
Luka_Danilovic | 0:c66224a27cf8 | 17 | _modeSel(RW), |
Luka_Danilovic | 0:c66224a27cf8 | 18 | _enable(EN) |
Luka_Danilovic | 0:c66224a27cf8 | 19 | { |
Luka_Danilovic | 0:c66224a27cf8 | 20 | _registerSel = INSTRUCTION; // command mode |
Luka_Danilovic | 0:c66224a27cf8 | 21 | _modeSel = WRITE; // write mode |
Luka_Danilovic | 0:c66224a27cf8 | 22 | |
Luka_Danilovic | 0:c66224a27cf8 | 23 | Thread::wait(20); // Wait 20ms to ensure powered up |
Luka_Danilovic | 0:c66224a27cf8 | 24 | |
Luka_Danilovic | 0:c66224a27cf8 | 25 | for (int i=0; i<3; i++) { // Three times |
Luka_Danilovic | 0:c66224a27cf8 | 26 | _enable = DISABLE; // Invalidate signal |
Luka_Danilovic | 0:c66224a27cf8 | 27 | _commsBus = 0x03; // Send command for powerup |
Luka_Danilovic | 0:c66224a27cf8 | 28 | _enable = ENABLE; // Validate signal |
Luka_Danilovic | 0:c66224a27cf8 | 29 | Thread::wait(20); // Wait since BUSY flag is not yet available |
Luka_Danilovic | 0:c66224a27cf8 | 30 | } |
Luka_Danilovic | 0:c66224a27cf8 | 31 | |
Luka_Danilovic | 0:c66224a27cf8 | 32 | _enable = DISABLE; // Invalidate signal |
Luka_Danilovic | 0:c66224a27cf8 | 33 | _commsBus = 0x03; // Send command for 4 bit mode |
Luka_Danilovic | 0:c66224a27cf8 | 34 | _enable = ENABLE; // Validate signal |
Luka_Danilovic | 0:c66224a27cf8 | 35 | Thread::wait(20); // Wait since BUSY flag is not yet available |
Luka_Danilovic | 0:c66224a27cf8 | 36 | |
Luka_Danilovic | 0:c66224a27cf8 | 37 | // BUSY flag is now available, Can use "writeChar" // |
Luka_Danilovic | 0:c66224a27cf8 | 38 | |
Luka_Danilovic | 0:c66224a27cf8 | 39 | writeChar(0x28, INSTRUCTION); // 4-bit, 2 Line, 5x7 Dots |
Luka_Danilovic | 0:c66224a27cf8 | 40 | writeChar(0x0C, INSTRUCTION); // Display on Cursor off |
Luka_Danilovic | 0:c66224a27cf8 | 41 | writeChar(0x04, INSTRUCTION); // Entry mode - no increment & no display shift |
Luka_Danilovic | 0:c66224a27cf8 | 42 | clear(); // Clear display for writing (DDRAM cleared) |
Luka_Danilovic | 0:c66224a27cf8 | 43 | |
Luka_Danilovic | 0:c66224a27cf8 | 44 | // writeChar(0b00100011, DATA); // Write Test "#" symbol |
Luka_Danilovic | 0:c66224a27cf8 | 45 | // Thread::wait(1000); // Wait 1 second |
Luka_Danilovic | 0:c66224a27cf8 | 46 | // clear(); // Clear Test "#" symbol |
Luka_Danilovic | 0:c66224a27cf8 | 47 | } |
Luka_Danilovic | 0:c66224a27cf8 | 48 | |
Luka_Danilovic | 0:c66224a27cf8 | 49 | |
Luka_Danilovic | 0:c66224a27cf8 | 50 | //****************************************************************************// |
Luka_Danilovic | 0:c66224a27cf8 | 51 | void C_displayMaster::writeChar(int Char, bool REGISTER) |
Luka_Danilovic | 0:c66224a27cf8 | 52 | { |
Luka_Danilovic | 0:c66224a27cf8 | 53 | _enable = DISABLE; // DISABLE at start |
Luka_Danilovic | 0:c66224a27cf8 | 54 | _commsBus[3].output(); // Reconfigure pin as output (MSB) |
Luka_Danilovic | 0:c66224a27cf8 | 55 | _registerSel = REGISTER; // Chose register to comunicate with |
Luka_Danilovic | 0:c66224a27cf8 | 56 | _modeSel = WRITE; // WRITE to register |
Luka_Danilovic | 0:c66224a27cf8 | 57 | |
Luka_Danilovic | 0:c66224a27cf8 | 58 | _commsBus = (Char>>4); // Top nibble |
Luka_Danilovic | 0:c66224a27cf8 | 59 | _enable = ENABLE; // Validate signal |
Luka_Danilovic | 0:c66224a27cf8 | 60 | _enable = DISABLE; // Invalidate signal |
Luka_Danilovic | 0:c66224a27cf8 | 61 | busyCheck(); // Chek if clear to write more |
Luka_Danilovic | 0:c66224a27cf8 | 62 | |
Luka_Danilovic | 0:c66224a27cf8 | 63 | _commsBus = (Char>>0); // Bottom nibble |
Luka_Danilovic | 0:c66224a27cf8 | 64 | _enable = ENABLE; // Validate signal |
Luka_Danilovic | 0:c66224a27cf8 | 65 | _enable = DISABLE; // Invalidate signal |
Luka_Danilovic | 0:c66224a27cf8 | 66 | busyCheck(); // Chek if clear to write more |
Luka_Danilovic | 0:c66224a27cf8 | 67 | } |
Luka_Danilovic | 0:c66224a27cf8 | 68 | |
Luka_Danilovic | 0:c66224a27cf8 | 69 | |
Luka_Danilovic | 0:c66224a27cf8 | 70 | void C_displayMaster::busyCheck() |
Luka_Danilovic | 0:c66224a27cf8 | 71 | { |
Luka_Danilovic | 0:c66224a27cf8 | 72 | _enable = DISABLE; // DISABLE at start |
Luka_Danilovic | 0:c66224a27cf8 | 73 | _commsBus[3].input(); // Input to listen for BUSY flag (MSB) |
Luka_Danilovic | 0:c66224a27cf8 | 74 | _registerSel = INSTRUCTION; // Comunicate with INSTRUCTION register |
Luka_Danilovic | 0:c66224a27cf8 | 75 | _modeSel = READ; // READ from register |
Luka_Danilovic | 0:c66224a27cf8 | 76 | _enable = ENABLE; // Listen for BUSY flag |
Luka_Danilovic | 0:c66224a27cf8 | 77 | while(_commsBus[3]) { |
Luka_Danilovic | 0:c66224a27cf8 | 78 | _enable != _enable; // Repeat read while BUSY flag is set |
Luka_Danilovic | 0:c66224a27cf8 | 79 | } |
Luka_Danilovic | 0:c66224a27cf8 | 80 | _enable = DISABLE; // Leave DISABLED upon exit |
Luka_Danilovic | 0:c66224a27cf8 | 81 | _modeSel = WRITE; // Return to WRITE mode |
Luka_Danilovic | 0:c66224a27cf8 | 82 | _commsBus[3].output(); // Reconfigure pin as output (MSB) |
Luka_Danilovic | 0:c66224a27cf8 | 83 | } |
Luka_Danilovic | 0:c66224a27cf8 | 84 | |
Luka_Danilovic | 0:c66224a27cf8 | 85 | |
Luka_Danilovic | 0:c66224a27cf8 | 86 | void C_displayMaster::calcDDRAM(int col, int row) |
Luka_Danilovic | 0:c66224a27cf8 | 87 | { |
Luka_Danilovic | 0:c66224a27cf8 | 88 | /* The following two IF statments create a continous display meaning that |
Luka_Danilovic | 0:c66224a27cf8 | 89 | running of the right edge return you to the left edge and running of the |
Luka_Danilovic | 0:c66224a27cf8 | 90 | bottom edge return you to the top row. */ |
Luka_Danilovic | 0:c66224a27cf8 | 91 | |
Luka_Danilovic | 0:c66224a27cf8 | 92 | if (col>16) { // If coloumn is outside of Right edge |
Luka_Danilovic | 0:c66224a27cf8 | 93 | col = 1; // Go back to start of row |
Luka_Danilovic | 0:c66224a27cf8 | 94 | } else if (col<1) { // OR If coloumn is outside of Left edge |
Luka_Danilovic | 0:c66224a27cf8 | 95 | col = 1; // Go back to start of row |
Luka_Danilovic | 0:c66224a27cf8 | 96 | } else { // Othetwise |
Luka_Danilovic | 0:c66224a27cf8 | 97 | col = col; // No change in col |
Luka_Danilovic | 0:c66224a27cf8 | 98 | } |
Luka_Danilovic | 0:c66224a27cf8 | 99 | |
Luka_Danilovic | 0:c66224a27cf8 | 100 | if (row>2) { // If row is outside of Bottom edge |
Luka_Danilovic | 0:c66224a27cf8 | 101 | row = 1; // Go back to top row |
Luka_Danilovic | 0:c66224a27cf8 | 102 | } else if (row<1) { // OR If row is outside of Top edge |
Luka_Danilovic | 0:c66224a27cf8 | 103 | row = 1; // Go back to top row |
Luka_Danilovic | 0:c66224a27cf8 | 104 | } else { // Othetwise |
Luka_Danilovic | 0:c66224a27cf8 | 105 | row = row; // No change in row |
Luka_Danilovic | 0:c66224a27cf8 | 106 | } |
Luka_Danilovic | 0:c66224a27cf8 | 107 | |
Luka_Danilovic | 0:c66224a27cf8 | 108 | _ADR = (1<<7)+((1<<6)*row-1)+col-1; // Recalculate DDRAM address |
Luka_Danilovic | 0:c66224a27cf8 | 109 | } |
Luka_Danilovic | 0:c66224a27cf8 | 110 | |
Luka_Danilovic | 0:c66224a27cf8 | 111 | |
Luka_Danilovic | 0:c66224a27cf8 | 112 | void C_displayMaster::clear() |
Luka_Danilovic | 0:c66224a27cf8 | 113 | { |
Luka_Danilovic | 0:c66224a27cf8 | 114 | writeChar(0x01, INSTRUCTION); // Clear display for writing (DDRAM cleared) |
Luka_Danilovic | 0:c66224a27cf8 | 115 | _col = 1; // First coloumn |
Luka_Danilovic | 0:c66224a27cf8 | 116 | _row = 1; // First row |
Luka_Danilovic | 0:c66224a27cf8 | 117 | calcDDRAM(_col, _row); // Get DDRAM address for writing next char |
Luka_Danilovic | 0:c66224a27cf8 | 118 | } |
Luka_Danilovic | 0:c66224a27cf8 | 119 | |
Luka_Danilovic | 0:c66224a27cf8 | 120 | |
Luka_Danilovic | 0:c66224a27cf8 | 121 | int C_displayMaster::_getc() |
Luka_Danilovic | 0:c66224a27cf8 | 122 | { |
Luka_Danilovic | 0:c66224a27cf8 | 123 | return -1; // Return Fail value since function has no purpose but is needed for character Stream |
Luka_Danilovic | 0:c66224a27cf8 | 124 | } |
Luka_Danilovic | 0:c66224a27cf8 | 125 | |
Luka_Danilovic | 0:c66224a27cf8 | 126 | |
Luka_Danilovic | 0:c66224a27cf8 | 127 | int C_displayMaster::_putc(int character) |
Luka_Danilovic | 0:c66224a27cf8 | 128 | { |
Luka_Danilovic | 0:c66224a27cf8 | 129 | if (character=='\n') { // IF new line |
Luka_Danilovic | 0:c66224a27cf8 | 130 | _row++; // Go to new row |
Luka_Danilovic | 0:c66224a27cf8 | 131 | _col = 1; // Go to start of row |
Luka_Danilovic | 0:c66224a27cf8 | 132 | } else if (character=='\r') { // IF carridge return |
Luka_Danilovic | 0:c66224a27cf8 | 133 | _col = 1; // Go to start of line |
Luka_Danilovic | 0:c66224a27cf8 | 134 | } else { // ELSE |
Luka_Danilovic | 0:c66224a27cf8 | 135 | |
Luka_Danilovic | 0:c66224a27cf8 | 136 | calcDDRAM(_col, _row); // Get cursor new address (N.B this function also creates a continous display. More details in function). |
Luka_Danilovic | 0:c66224a27cf8 | 137 | writeChar(_ADR, INSTRUCTION); // Set cursor to position |
Luka_Danilovic | 0:c66224a27cf8 | 138 | writeChar(character, DATA); // Write character |
Luka_Danilovic | 0:c66224a27cf8 | 139 | |
Luka_Danilovic | 0:c66224a27cf8 | 140 | _col++; // Increment cursor position |
Luka_Danilovic | 0:c66224a27cf8 | 141 | if (_col>16) { // IF end of row go to new row |
Luka_Danilovic | 0:c66224a27cf8 | 142 | _row++; // Go to new row |
Luka_Danilovic | 0:c66224a27cf8 | 143 | _col = 1; // Go to start of row |
Luka_Danilovic | 0:c66224a27cf8 | 144 | } |
Luka_Danilovic | 0:c66224a27cf8 | 145 | } |
Luka_Danilovic | 0:c66224a27cf8 | 146 | return character; // Return what was written to indicate sucess |
Luka_Danilovic | 0:c66224a27cf8 | 147 | } |