PCD8544 Driver for an LCD with an PCD8544 controller (e.g. Nokia 3310, 3390)

Dependents:   PCD8544_LCD

Committer:
carlosftm
Date:
Sun Jan 02 11:56:06 2011 +0000
Revision:
1:9948e71af151
Parent:
0:358e68b0535a
Library with Text support only (14x6 characters)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
carlosftm 0:358e68b0535a 1 /* ********************************************
carlosftm 0:358e68b0535a 2 * PCD8544 Driver for an LCD with an PCD8544
carlosftm 0:358e68b0535a 3 * controller.
carlosftm 0:358e68b0535a 4 * Note: Text mode supported only.
carlosftm 0:358e68b0535a 5 *
carlosftm 0:358e68b0535a 6 * Created on: 12/31/2010 at 19:48
carlosftm 0:358e68b0535a 7 * Created by: CarlosFTM
carlosftm 0:358e68b0535a 8 ********************************************** */
carlosftm 0:358e68b0535a 9
carlosftm 0:358e68b0535a 10 #include "mbed.h"
carlosftm 0:358e68b0535a 11 #include "pcd8544_drv.hpp"
carlosftm 0:358e68b0535a 12
carlosftm 0:358e68b0535a 13 pcd8544::pcd8544(PinName pin_sclk, PinName pin_mosi, PinName pin_dc, PinName pin_sce, PinName pin_reset) : _sclk(pin_sclk),
carlosftm 0:358e68b0535a 14 _mosi(pin_mosi),
carlosftm 0:358e68b0535a 15 _dc(pin_dc),
carlosftm 0:358e68b0535a 16 _sce(pin_sce),
carlosftm 0:358e68b0535a 17 _reset(pin_reset)
carlosftm 0:358e68b0535a 18 {
carlosftm 0:358e68b0535a 19 /* Set all pins to initial state */
carlosftm 0:358e68b0535a 20 _sce = HIGH;
carlosftm 0:358e68b0535a 21 _reset = HIGH;
carlosftm 0:358e68b0535a 22 _sclk = LOW;
carlosftm 0:358e68b0535a 23 }
carlosftm 0:358e68b0535a 24
carlosftm 0:358e68b0535a 25 /* Transmits a single byte to the LCD */
carlosftm 0:358e68b0535a 26 void pcd8544::sendByte(char byte, bool command)
carlosftm 0:358e68b0535a 27 {
carlosftm 0:358e68b0535a 28 int loop;
carlosftm 0:358e68b0535a 29 _dc = !command;
carlosftm 0:358e68b0535a 30 _sce = LOW;
carlosftm 0:358e68b0535a 31 for (loop = 0; loop < 8; loop++)
carlosftm 0:358e68b0535a 32 {
carlosftm 0:358e68b0535a 33 _mosi = ((byte << loop) & 0x80); // TX starts from MSB
carlosftm 0:358e68b0535a 34 clockTick(TICK);
carlosftm 0:358e68b0535a 35 }
carlosftm 0:358e68b0535a 36 _sce = HIGH;
carlosftm 0:358e68b0535a 37 }
carlosftm 0:358e68b0535a 38
carlosftm 0:358e68b0535a 39 /* Writes a data byte into LCD RAM*/
carlosftm 0:358e68b0535a 40 void pcd8544::writeData(char data)
carlosftm 0:358e68b0535a 41 {
carlosftm 0:358e68b0535a 42 sendByte(data, false);
carlosftm 0:358e68b0535a 43 }
carlosftm 0:358e68b0535a 44
carlosftm 0:358e68b0535a 45 /* Sends a command to the LCD */
carlosftm 0:358e68b0535a 46 void pcd8544::writeCmd(char cmd)
carlosftm 0:358e68b0535a 47 {
carlosftm 0:358e68b0535a 48 sendByte(cmd, true);
carlosftm 0:358e68b0535a 49 }
carlosftm 0:358e68b0535a 50
carlosftm 0:358e68b0535a 51 /* Writes a ASCII character into LCD RAM */
carlosftm 0:358e68b0535a 52 void pcd8544::writeChar(char character)
carlosftm 0:358e68b0535a 53 {
carlosftm 0:358e68b0535a 54 char loop = 0;
carlosftm 0:358e68b0535a 55
carlosftm 0:358e68b0535a 56 if ((character >= 32) && (character <= 122))
carlosftm 0:358e68b0535a 57 {
carlosftm 0:358e68b0535a 58 character = character - 32;
carlosftm 0:358e68b0535a 59 }
carlosftm 0:358e68b0535a 60 else
carlosftm 0:358e68b0535a 61 {
carlosftm 0:358e68b0535a 62 character = 0x00;
carlosftm 0:358e68b0535a 63 }
carlosftm 0:358e68b0535a 64
carlosftm 0:358e68b0535a 65 for (loop = 0; loop < 5; loop++)
carlosftm 0:358e68b0535a 66 {
carlosftm 0:358e68b0535a 67 sendByte(ascii_table[character][loop], false);
carlosftm 0:358e68b0535a 68 }
carlosftm 0:358e68b0535a 69 sendByte(0x00, false);
carlosftm 0:358e68b0535a 70 }
carlosftm 0:358e68b0535a 71
carlosftm 0:358e68b0535a 72 /* Writes a string of characters into LCD RAM */
carlosftm 0:358e68b0535a 73 void pcd8544::writeString(char* character)
carlosftm 0:358e68b0535a 74 {
carlosftm 0:358e68b0535a 75 while (*character)
carlosftm 0:358e68b0535a 76 {
carlosftm 0:358e68b0535a 77 writeChar(char(*character));
carlosftm 0:358e68b0535a 78 character++;
carlosftm 0:358e68b0535a 79 }
carlosftm 0:358e68b0535a 80 }
carlosftm 0:358e68b0535a 81
carlosftm 0:358e68b0535a 82 /* Resets the LCD*/
carlosftm 0:358e68b0535a 83 void pcd8544::resetLCD()
carlosftm 0:358e68b0535a 84 {
carlosftm 0:358e68b0535a 85 int loop;
carlosftm 0:358e68b0535a 86 _sce = LOW;
carlosftm 0:358e68b0535a 87 wait_us(TICK);
carlosftm 0:358e68b0535a 88 _reset = LOW;
carlosftm 0:358e68b0535a 89 for (loop = 0; loop < RESET_TICKS; loop++)
carlosftm 0:358e68b0535a 90 {
carlosftm 0:358e68b0535a 91 clockTick(TICK);
carlosftm 0:358e68b0535a 92 }
carlosftm 0:358e68b0535a 93 _reset = HIGH;
carlosftm 0:358e68b0535a 94 wait_us(TICK);
carlosftm 0:358e68b0535a 95 _sce = HIGH;
carlosftm 0:358e68b0535a 96 }
carlosftm 0:358e68b0535a 97
carlosftm 0:358e68b0535a 98 /* Clear the LCD RAM*/
carlosftm 0:358e68b0535a 99 void pcd8544::clearLCD()
carlosftm 0:358e68b0535a 100 {
carlosftm 0:358e68b0535a 101 writeCmd(0x80); // Set X ram-address to 0x00
carlosftm 0:358e68b0535a 102 writeCmd(0x40); // Set Y ram-address to 0x00
carlosftm 0:358e68b0535a 103
carlosftm 0:358e68b0535a 104 int loop = 0;
carlosftm 0:358e68b0535a 105
carlosftm 0:358e68b0535a 106 for (loop = 0; loop < (MAX_PIX_X * CHAR_PIX_X); loop++)
carlosftm 0:358e68b0535a 107 {
carlosftm 0:358e68b0535a 108 writeData(0x00); // Write empty character
carlosftm 0:358e68b0535a 109 }
carlosftm 0:358e68b0535a 110 /* Set cursor to position 0,0 */
carlosftm 0:358e68b0535a 111 writeCmd(0x80); // Set X ram-address to 0x00
carlosftm 0:358e68b0535a 112 writeCmd(0x40); // Set Y ram-address to 0x00
carlosftm 0:358e68b0535a 113 }
carlosftm 0:358e68b0535a 114
carlosftm 0:358e68b0535a 115 /* Initialize the LCD with default values */
carlosftm 0:358e68b0535a 116 void pcd8544::initLCD(void)
carlosftm 0:358e68b0535a 117 {
carlosftm 0:358e68b0535a 118 writeCmd(0x90); // Set Vop as in PCD8544 specs example
carlosftm 0:358e68b0535a 119 writeCmd(0x20); // Normal instruction set. Horizontal addressing.
carlosftm 0:358e68b0535a 120 writeCmd(0x0C); // display in normal mode
carlosftm 0:358e68b0535a 121
carlosftm 0:358e68b0535a 122 /* Set cursor to position 0,0 */
carlosftm 0:358e68b0535a 123 writeCmd(0x80); // Set X ram-address to 0x00
carlosftm 0:358e68b0535a 124 writeCmd(0x40); // Set Y ram-address to 0x00
carlosftm 0:358e68b0535a 125 }
carlosftm 0:358e68b0535a 126
carlosftm 0:358e68b0535a 127
carlosftm 0:358e68b0535a 128 /* Sends a clock tick to the LCD */
carlosftm 0:358e68b0535a 129 inline void pcd8544::clockTick(unsigned short useg)
carlosftm 0:358e68b0535a 130 {
carlosftm 0:358e68b0535a 131 _sclk = LOW;
carlosftm 0:358e68b0535a 132 wait_us(useg);
carlosftm 0:358e68b0535a 133 _sclk = HIGH;
carlosftm 0:358e68b0535a 134 wait_us(useg);
carlosftm 0:358e68b0535a 135 }
carlosftm 0:358e68b0535a 136
carlosftm 0:358e68b0535a 137 /* Set the cursor position on specific character position x,y*/
carlosftm 0:358e68b0535a 138 void pcd8544::setCursorXY(char x, char y)
carlosftm 0:358e68b0535a 139 {
carlosftm 0:358e68b0535a 140 x = (((x % MAX_CHAR_X) * CHAR_PIX_X) | SET_ADDRES_X);
carlosftm 0:358e68b0535a 141 y = ((y % MAX_CHAR_Y) | SET_ADDRES_Y);
carlosftm 0:358e68b0535a 142 writeCmd(x); // Set X ram-address to x
carlosftm 0:358e68b0535a 143 writeCmd(y); // Set Y ram-address to y
carlosftm 0:358e68b0535a 144 }