Project C accelerometer
Fork of N5110 by
Revision 0:d563e74f0ae9, committed 2014-01-26
- Comitter:
- eencae
- Date:
- Sun Jan 26 18:55:16 2014 +0000
- Child:
- 1:df68f34cd32d
- Commit message:
- Initial commit of library.; ; Testing and documentation to do.
Changed in this revision
N5110.cpp | Show annotated file Show diff for this revision Revisions of this file |
N5110.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/N5110.cpp Sun Jan 26 18:55:16 2014 +0000 @@ -0,0 +1,218 @@ +#include "mbed.h" +#include "N5110.h" + +N5110::N5110(PinName pwrPin, PinName ledPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin) +{ + + spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise + initSPI(); + + led = new PwmOut(ledPin); + pwr = new DigitalOut(pwrPin); + sce = new DigitalOut(scePin); + rst = new DigitalOut(rstPin); + dc = new DigitalOut(dcPin); + +} + +void N5110::init() +{ + turnOn(); // power up + reset(); // reset LCD + + // function set - extended + sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); + + sendCommand(CMD_VOP_7V38); // operating voltage + sendCommand(CMD_TC_TEMP_2); // temperature control + sendCommand(CMD_BI_MUX_48); // bias + + // function set - basic + sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); + sendCommand(CMD_DC_NORMAL_MODE); // black on white + //sendLCDCommand(CMD_DC_INVERT_VIDEO); // white on black + + // RAM is undefined at power-up so clear + clearRAM(); + +} + +// function to power up the LCD and backlight +void N5110::turnOn() +{ + // set brightness of LED - 0.0 to 1.0 - default is 50% + setBrightness(0.5); + pwr->write(1); // apply power +} + +// function to power down LCD +void N5110::turnOff() +{ + + setBrightness(0.0); // turn backlight off + clearRAM(); // clear RAM to ensure specified current consumption + sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); + sendCommand(CMD_DC_CLEAR_DISPLAY); + sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); + pwr->write(0); + +} + +// function to change LED backlight brightness +void N5110::setBrightness(float brightness) +{ + // check whether brightness is within range + if (brightness < 0.0) + brightness = 0.0; + if (brightness > 1.0) + brightness = 1.0; + // set PWM duty cycle + led->write(brightness); +} + + +// pulse the active low reset line +void N5110::reset() +{ + rst->write(0); // reset the LCD + rst->write(1); +} + +// function to initialise SPI peripheral +void N5110::initSPI() +{ + spi->format(8,1); // 8 bits, Mode 1 - polarity 0, phase 1 - base value of clock is 0, data captured on falling edge/propagated on rising edge + spi->frequency(4000000); // maximum of screen is 4 MHz +} + +void N5110::sendCommand(unsigned char command) +{ + dc->write(0); // set DC low for command + sce->write(0); // set CE low to begin frame + spi->write(command); // send command + dc->write(1); // turn back to data by default + sce->write(1); // set CE high to end frame (expected for transmission of single byte) + +} + +void N5110::sendData(unsigned char data) +{ + sce->write(0); // set CE low to begin frame + spi->write(data); + sce->write(1); // set CE high to end frame (expected for transmission of single byte) +} + +// this function writes 0 to the 504 bytes to clear the RAM +void N5110::clearRAM() +{ + int i; + sce->write(0); //set CE low to begin frame + for(i = 0; i < 504; i++) { // 48 x 84 bits = 504 bytes + spi->write(0x00); // send 0's + } + sce->write(1); // set CE high to end frame + +} + +void N5110::setXYAddress(int x, int y) +{ + + // check whether address is in range + if (x > 83) + x=83; + if (y > 5) + y=5; + + sendCommand(0x80 | x); + sendCommand(0x40 | y); +} + +void N5110::setPixel(int x, int y) +{ + // calculate bank and shift 1 to required position + buffer[x][y/8] |= (1 << y%8); +} + +void N5110::clearPixel(int x, int y) +{ + // calculate bank and shift 1 to required position (using bit clear) + buffer[x][y/8] &= ~(1 << y%8); +} + +unsigned char N5110::getPixel(int x, int y) +{ + // return relevant bank and mask required bit + return buffer[x][y/8] & (1 << y%8); + +} + +void N5110::refreshDisplay() +{ + + int i,j; + sce->write(0); //set CE low to begin frame + + for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing + for(i = 0; i < 84; i++) { + spi->write(buffer[i][j]); // send buffer + } + } + sce->write(1); // set CE high to end frame + +} + +void N5110::randomiseBuffer() +{ + + int i,j; + + for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing + for(i = 0; i < 84; i++) { + buffer[i][j] = rand()%256; // generate random byte + } + } + +} + +// function to print 5x7 font +void N5110::printChar(char c) +{ + + // loop through 5 columns + for (int j = 0; j < 5 ; j++ ) { + sendData(font5x7[(c - 32)*5 + j]); + // array is offset by 32 relative to ASCII, each character is 5 pixels wide + } + +} + +// function to print string at specified position +void N5110::printString(const char * str,int x,int y) +{ + int n = 0 ; // counter for number of characters in string + // loop through string and print character + while(*str) { + + setXYAddress(x+6*n,y); // leave 1 pixel (6 = 5 + 1) between each character + printChar(*str); + str++; // go to next character in string + n++; // increment index + } + +} + +void N5110::clear() +{ + clearBuffer(); + refreshDisplay(); +} + +void N5110::clearBuffer() +{ + int i,j; + for (i=0; i<84; i++) { + for (j=0; j<6; j++) { + buffer[i][j]=0; + } + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/N5110.h Sun Jan 26 18:55:16 2014 +0000 @@ -0,0 +1,182 @@ +/* +Simple library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) + +Uses PCD8544 Controller - 48 rows, 84 columns + +Chris Yan's Nokia5110 Library proved a useful starting point. + +Revision 1.0 + +Craig A. Evans +January 2014 + +*/ + +#ifndef N5110_H +#define N5110_H + +// Command Bytes - taken from Chris Yan's library +// H = 0 +#define CMD_DC_CLEAR_DISPLAY 0x08 +#define CMD_DC_NORMAL_MODE 0x0C +#define CMD_DC_FILL_DISPLAY 0x09 +#define CMD_DC_INVERT_VIDEO 0x0D +#define CMD_FS_HORIZONTAL_MODE 0x00 +#define CMD_FS_VERTICAL_MODE 0x02 +#define CMD_FS_BASIC_MODE 0x00 +#define CMD_FS_EXTENDED_MODE 0x01 +#define CMD_FS_ACTIVE_MODE 0x00 +#define CMD_FS_POWER_DOWN_MODE 0x04 +// H = 1 +#define CMD_TC_TEMP_0 0x04 +#define CMD_TC_TEMP_1 0x05 +#define CMD_TC_TEMP_2 0x06 +#define CMD_TC_TEMP_3 0x07 +#define CMD_BI_MUX_24 0x15 +#define CMD_BI_MUX_48 0x13 +#define CMD_BI_MUX_100 0x10 +#define CMD_VOP_6V06 0xB2 +#define CMD_VOP_7V38 0xC8 + +#include "mbed.h" + +class N5110 { + + public: + /** Create instance of N5110 LCD with specified pins */ + N5110(PinName pwr, PinName led, PinName sce, PinName rst, PinName dc, PinName mosi, PinName sclk); + void init(); + void turnOff(); + void clear(); + void setBrightness(float brightness); + void setXYAddress(int x, int y); + void printString(const char * str,int x,int y); + void printChar(char c); + void setPixel(int x, int y); + void clearPixel(int x, int y); + unsigned char getPixel(int x, int y); + void refreshDisplay(); + void clearBuffer(); + void randomiseBuffer(); + + private: + void initSPI(); + void turnOn(); + void reset(); + void clearRAM(); + void sendCommand(unsigned char command); + void sendData(unsigned char data); + + public: + unsigned char buffer[84][6]; // screen buffer - the 6 is for the banks - each one is 8 bits; + + private: // private variables + SPI* spi; + PwmOut* led; + DigitalOut* pwr; + DigitalOut* sce; + DigitalOut* rst; + DigitalOut* dc; + +}; + +const unsigned char font5x7[480] = { + 0x00, 0x00, 0x00, 0x00, 0x00,// (space) + 0x00, 0x00, 0x5F, 0x00, 0x00,// ! + 0x00, 0x07, 0x00, 0x07, 0x00,// " + 0x14, 0x7F, 0x14, 0x7F, 0x14,// # + 0x24, 0x2A, 0x7F, 0x2A, 0x12,// $ + 0x23, 0x13, 0x08, 0x64, 0x62,// % + 0x36, 0x49, 0x55, 0x22, 0x50,// & + 0x00, 0x05, 0x03, 0x00, 0x00,// ' + 0x00, 0x1C, 0x22, 0x41, 0x00,// ( + 0x00, 0x41, 0x22, 0x1C, 0x00,// ) + 0x08, 0x2A, 0x1C, 0x2A, 0x08,// * + 0x08, 0x08, 0x3E, 0x08, 0x08,// + + 0x00, 0x50, 0x30, 0x00, 0x00,// , + 0x08, 0x08, 0x08, 0x08, 0x08,// - + 0x00, 0x60, 0x60, 0x00, 0x00,// . + 0x20, 0x10, 0x08, 0x04, 0x02,// / + 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0 + 0x00, 0x42, 0x7F, 0x40, 0x00,// 1 + 0x42, 0x61, 0x51, 0x49, 0x46,// 2 + 0x21, 0x41, 0x45, 0x4B, 0x31,// 3 + 0x18, 0x14, 0x12, 0x7F, 0x10,// 4 + 0x27, 0x45, 0x45, 0x45, 0x39,// 5 + 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6 + 0x01, 0x71, 0x09, 0x05, 0x03,// 7 + 0x36, 0x49, 0x49, 0x49, 0x36,// 8 + 0x06, 0x49, 0x49, 0x29, 0x1E,// 9 + 0x00, 0x36, 0x36, 0x00, 0x00,// : + 0x00, 0x56, 0x36, 0x00, 0x00,// ; + 0x00, 0x08, 0x14, 0x22, 0x41,// < + 0x14, 0x14, 0x14, 0x14, 0x14,// = + 0x41, 0x22, 0x14, 0x08, 0x00,// > + 0x02, 0x01, 0x51, 0x09, 0x06,// ? + 0x32, 0x49, 0x79, 0x41, 0x3E,// @ + 0x7E, 0x11, 0x11, 0x11, 0x7E,// A + 0x7F, 0x49, 0x49, 0x49, 0x36,// B + 0x3E, 0x41, 0x41, 0x41, 0x22,// C + 0x7F, 0x41, 0x41, 0x22, 0x1C,// D + 0x7F, 0x49, 0x49, 0x49, 0x41,// E + 0x7F, 0x09, 0x09, 0x01, 0x01,// F + 0x3E, 0x41, 0x41, 0x51, 0x32,// G + 0x7F, 0x08, 0x08, 0x08, 0x7F,// H + 0x00, 0x41, 0x7F, 0x41, 0x00,// I + 0x20, 0x40, 0x41, 0x3F, 0x01,// J + 0x7F, 0x08, 0x14, 0x22, 0x41,// K + 0x7F, 0x40, 0x40, 0x40, 0x40,// L + 0x7F, 0x02, 0x04, 0x02, 0x7F,// M + 0x7F, 0x04, 0x08, 0x10, 0x7F,// N + 0x3E, 0x41, 0x41, 0x41, 0x3E,// O + 0x7F, 0x09, 0x09, 0x09, 0x06,// P + 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q + 0x7F, 0x09, 0x19, 0x29, 0x46,// R + 0x46, 0x49, 0x49, 0x49, 0x31,// S + 0x01, 0x01, 0x7F, 0x01, 0x01,// T + 0x3F, 0x40, 0x40, 0x40, 0x3F,// U + 0x1F, 0x20, 0x40, 0x20, 0x1F,// V + 0x7F, 0x20, 0x18, 0x20, 0x7F,// W + 0x63, 0x14, 0x08, 0x14, 0x63,// X + 0x03, 0x04, 0x78, 0x04, 0x03,// Y + 0x61, 0x51, 0x49, 0x45, 0x43,// Z + 0x00, 0x00, 0x7F, 0x41, 0x41,// [ + 0x02, 0x04, 0x08, 0x10, 0x20,// "\" + 0x41, 0x41, 0x7F, 0x00, 0x00,// ] + 0x04, 0x02, 0x01, 0x02, 0x04,// ^ + 0x40, 0x40, 0x40, 0x40, 0x40,// _ + 0x00, 0x01, 0x02, 0x04, 0x00,// ` + 0x20, 0x54, 0x54, 0x54, 0x78,// a + 0x7F, 0x48, 0x44, 0x44, 0x38,// b + 0x38, 0x44, 0x44, 0x44, 0x20,// c + 0x38, 0x44, 0x44, 0x48, 0x7F,// d + 0x38, 0x54, 0x54, 0x54, 0x18,// e + 0x08, 0x7E, 0x09, 0x01, 0x02,// f + 0x08, 0x14, 0x54, 0x54, 0x3C,// g + 0x7F, 0x08, 0x04, 0x04, 0x78,// h + 0x00, 0x44, 0x7D, 0x40, 0x00,// i + 0x20, 0x40, 0x44, 0x3D, 0x00,// j + 0x00, 0x7F, 0x10, 0x28, 0x44,// k + 0x00, 0x41, 0x7F, 0x40, 0x00,// l + 0x7C, 0x04, 0x18, 0x04, 0x78,// m + 0x7C, 0x08, 0x04, 0x04, 0x78,// n + 0x38, 0x44, 0x44, 0x44, 0x38,// o + 0x7C, 0x14, 0x14, 0x14, 0x08,// p + 0x08, 0x14, 0x14, 0x18, 0x7C,// q + 0x7C, 0x08, 0x04, 0x04, 0x08,// r + 0x48, 0x54, 0x54, 0x54, 0x20,// s + 0x04, 0x3F, 0x44, 0x40, 0x20,// t + 0x3C, 0x40, 0x40, 0x20, 0x7C,// u + 0x1C, 0x20, 0x40, 0x20, 0x1C,// v + 0x3C, 0x40, 0x30, 0x40, 0x3C,// w + 0x44, 0x28, 0x10, 0x28, 0x44,// x + 0x0C, 0x50, 0x50, 0x50, 0x3C,// y + 0x44, 0x64, 0x54, 0x4C, 0x44,// z + 0x00, 0x08, 0x36, 0x41, 0x00,// { + 0x00, 0x00, 0x7F, 0x00, 0x00,// | + 0x00, 0x41, 0x36, 0x08, 0x00,// } + 0x08, 0x08, 0x2A, 0x1C, 0x08,// -> + 0x08, 0x1C, 0x2A, 0x08, 0x08 // <- +}; + +#endif \ No newline at end of file