Contains necessary classes and functions for ELEC351

/media/uploads/Luka_Danilovic/elec_315_prototype_assembly.jpg

displayMaster/displayMaster.cpp

Committer:
Luka_Danilovic
Date:
2017-12-28
Revision:
4:d463eafbabed
Parent:
3:e84fa53173e6
Child:
5:becb1545229d

File content as of revision 4:d463eafbabed:

#include "mbed.h"
#include "displayMaster.hpp"

/* Instructions on using the 16x2 LCD panel are found here:
[https://www.8051projects.net/lcd-interfacing/lcd-4-bit.php] */

/* N.B. This class contains blocking function */

C_displayMaster::C_displayMaster(PinName D1, PinName D2, PinName D3 , PinName D4 , PinName RS , PinName RW, PinName EN)
    :
    _commsBus(D1, D2, D3, D4),
    _registerSel(RS),
    _modeSel(RW),
    _enable(EN)
{
    _enable      = DISABLE;          // DISABLE at start
    _commsBus[3].output();           // Reconfigure pin as output (MSB)
    Thread::wait(20);                // Powerup time
    _commsBus = 0x03;                // Pannel config instruction 1
    _enable      = ENABLE;           // Validate instruction
    _enable      = DISABLE;
    Thread::wait(15);                // Wait since BUSY flag not available yet
    _commsBus = 0x03;                // Pannel config instruction 2
    _enable      = ENABLE;           // Validate instruction
    _enable      = DISABLE;
    Thread::wait(5);                 // Wait since BUSY flag not available yet
    _commsBus = 0x03;                // Pannel config instruction 3
    _enable      = ENABLE;           // Validate instruction
    _enable      = DISABLE;
    Thread::wait(2);                 // Wait since BUSY flag not available yet
    _commsBus = 0x02;                // Pannel config instruction (4 bit mode)
    _enable      = ENABLE;           // Validate instruction
    _enable      = DISABLE;
    Thread::wait(2);                 // Wait since BUSY flag not available yet
    writeChar(0x28, INSTRUCTION);    // Function Set: 4-bit, 2 Line, 5x7 Dots
    //writeChar(0x01, INSTRUCTION);    // Clear Display (also clear DDRAM content)
    writeChar(0x0C, INSTRUCTION);    // Display = on, Cursor = off
    writeChar(0x06, INSTRUCTION);    // Entry Mode (Auto increment)
    
    writeChar(0x23, DATA);

}

void C_displayMaster::busyCheck()
{
    _enable      = DISABLE;          // DISABLE at start
    _commsBus[3].input();            // Input to listen for BUSY flag (MSB)
    _registerSel = INSTRUCTION;      // Comunicate with INSTRUCTION register
    _modeSel     = READ;             // READ from register
    _enable      = ENABLE;           // Listen for BUSY flag
    while(_commsBus[3]) {
        _enable != _enable;          // Repeat read while BUSY flag is set
    }
    _enable      = DISABLE;          // Leave DISABLED upon exit
    _modeSel     = WRITE;            // Return to WRITE mode
    _commsBus[3].output();           // Reconfigure pin as output (MSB)
}

void C_displayMaster::writeChar(char instructions, bool REGISTER)
{
    _enable      = DISABLE;          // DISABLE at start
    _commsBus[3].output();           // Reconfigure pin as output (MSB)
    _registerSel = REGISTER;         // Chose register to comunicate with
    _modeSel     = WRITE;            // WRITE to register
    _commsBus    = (instructions>>4);// Top nibble
    _enable      = ENABLE;           // validate signal
    Thread::wait(20);
   // busyCheck();
    _commsBus    = (instructions);   // Bottom nibble
    _enable      = ENABLE;           // validate signal
   // busyCheck();
   Thread::wait(20);
}