Test USB

Dependencies:   USBDevice mbed

Committer:
Tomoseec
Date:
Tue Dec 25 11:02:43 2012 +0000
Revision:
0:0ca2c7e9dc81
USB TEST

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tomoseec 0:0ca2c7e9dc81 1 /* mbed TextLCD Library
Tomoseec 0:0ca2c7e9dc81 2 * Copyright (c) 2007-2009 sford
Tomoseec 0:0ca2c7e9dc81 3 * Released under the MIT License: http://mbed.org/license/mit
Tomoseec 0:0ca2c7e9dc81 4 *
Tomoseec 0:0ca2c7e9dc81 5 * TODO: Needs serious rework/neatening up!
Tomoseec 0:0ca2c7e9dc81 6 */
Tomoseec 0:0ca2c7e9dc81 7
Tomoseec 0:0ca2c7e9dc81 8 #include "TextLCD.h"
Tomoseec 0:0ca2c7e9dc81 9
Tomoseec 0:0ca2c7e9dc81 10 #include "mbed.h"
Tomoseec 0:0ca2c7e9dc81 11 #include "error.h"
Tomoseec 0:0ca2c7e9dc81 12
Tomoseec 0:0ca2c7e9dc81 13 using namespace mbed;
Tomoseec 0:0ca2c7e9dc81 14
Tomoseec 0:0ca2c7e9dc81 15 /*
Tomoseec 0:0ca2c7e9dc81 16 * useful info found at http://www.a-netz.de/lcd.en.php
Tomoseec 0:0ca2c7e9dc81 17 *
Tomoseec 0:0ca2c7e9dc81 18 *
Tomoseec 0:0ca2c7e9dc81 19 * Initialisation
Tomoseec 0:0ca2c7e9dc81 20 * ==============
Tomoseec 0:0ca2c7e9dc81 21 *
Tomoseec 0:0ca2c7e9dc81 22 * After attaching the supply voltage/after a reset, the display needs to be brought in to a defined state
Tomoseec 0:0ca2c7e9dc81 23 *
Tomoseec 0:0ca2c7e9dc81 24 * - wait approximately 15 ms so the display is ready to execute commands
Tomoseec 0:0ca2c7e9dc81 25 * - Execute the command 0x30 ("Display Settings") three times (wait 1,64ms after each command, the busy flag cannot be queried now).
Tomoseec 0:0ca2c7e9dc81 26 * - The display is in 8 bit mode, so if you have only connected 4 data pins you should only transmit the higher nibble of each command.
Tomoseec 0:0ca2c7e9dc81 27 * - If you want to use the 4 bit mode, now you can execute the command to switch over to this mode now.
Tomoseec 0:0ca2c7e9dc81 28 * - Execute the "clear display" command
Tomoseec 0:0ca2c7e9dc81 29 *
Tomoseec 0:0ca2c7e9dc81 30 * Timing
Tomoseec 0:0ca2c7e9dc81 31 * ======
Tomoseec 0:0ca2c7e9dc81 32 *
Tomoseec 0:0ca2c7e9dc81 33 * Nearly all commands transmitted to the display need 40us for execution.
Tomoseec 0:0ca2c7e9dc81 34 * Exceptions are the commands "Clear Display and Reset" and "Set Cursor to Start Position"
Tomoseec 0:0ca2c7e9dc81 35 * These commands need 1.64ms for execution. These timings are valid for all displays working with an
Tomoseec 0:0ca2c7e9dc81 36 * internal clock of 250kHz. But I do not know any displays that use other frequencies. Any time you
Tomoseec 0:0ca2c7e9dc81 37 * can use the busy flag to test if the display is ready to accept the next command.
Tomoseec 0:0ca2c7e9dc81 38 *
Tomoseec 0:0ca2c7e9dc81 39 * _e is kept high apart from calling clock
Tomoseec 0:0ca2c7e9dc81 40 * _rw is kept 0 (write) apart from actions that uyse it differently
Tomoseec 0:0ca2c7e9dc81 41 * _rs is set by the data/command writes
Tomoseec 0:0ca2c7e9dc81 42 */
Tomoseec 0:0ca2c7e9dc81 43
Tomoseec 0:0ca2c7e9dc81 44 TextLCD::TextLCD(PinName rs, PinName rw, PinName e, PinName d0, PinName d1,
Tomoseec 0:0ca2c7e9dc81 45 PinName d2, PinName d3, int columns, int rows) : _rw(rw), _rs(rs),
Tomoseec 0:0ca2c7e9dc81 46 _e(e), _d(d0, d1, d2, d3), _columns(columns), _rows(rows) {
Tomoseec 0:0ca2c7e9dc81 47
Tomoseec 0:0ca2c7e9dc81 48 // _rows = 2;
Tomoseec 0:0ca2c7e9dc81 49 // _columns = 16;
Tomoseec 0:0ca2c7e9dc81 50 // Mon, 27 Apr 2009 23:32:34 +0200
Tomoseec 0:0ca2c7e9dc81 51 // Kevin Konradt:
Tomoseec 0:0ca2c7e9dc81 52 // When using a LCD with 1 row x 16 characters
Tomoseec 0:0ca2c7e9dc81 53 // instead of 2x16, try changing _columns to 8.
Tomoseec 0:0ca2c7e9dc81 54 // (display seems to split the 16 characters into
Tomoseec 0:0ca2c7e9dc81 55 // 2 virtual rows with 8 characters each.)
Tomoseec 0:0ca2c7e9dc81 56
Tomoseec 0:0ca2c7e9dc81 57 _rw = 0;
Tomoseec 0:0ca2c7e9dc81 58 _e = 1;
Tomoseec 0:0ca2c7e9dc81 59 _rs = 0; // command mode
Tomoseec 0:0ca2c7e9dc81 60
Tomoseec 0:0ca2c7e9dc81 61 // Should theoretically wait 15ms, but most things will be powered up pre-reset
Tomoseec 0:0ca2c7e9dc81 62 // so i'll disable that for the minute. If implemented, could wait 15ms post reset
Tomoseec 0:0ca2c7e9dc81 63 // instead
Tomoseec 0:0ca2c7e9dc81 64 // wait(0.015);
Tomoseec 0:0ca2c7e9dc81 65
Tomoseec 0:0ca2c7e9dc81 66 // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus)
Tomoseec 0:0ca2c7e9dc81 67 for(int i=0; i<3; i++) {
Tomoseec 0:0ca2c7e9dc81 68 writeNibble(0x3);
Tomoseec 0:0ca2c7e9dc81 69 wait(0.00164); // this command takes 1.64ms, so wait for it
Tomoseec 0:0ca2c7e9dc81 70 }
Tomoseec 0:0ca2c7e9dc81 71 writeNibble(0x2); // 4-bit mode
Tomoseec 0:0ca2c7e9dc81 72
Tomoseec 0:0ca2c7e9dc81 73 writeCommand(0x28); // Function set 001 BW N F - -
Tomoseec 0:0ca2c7e9dc81 74 writeCommand(0x0C);
Tomoseec 0:0ca2c7e9dc81 75 writeCommand(0x6); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes
Tomoseec 0:0ca2c7e9dc81 76
Tomoseec 0:0ca2c7e9dc81 77 cls();
Tomoseec 0:0ca2c7e9dc81 78 }
Tomoseec 0:0ca2c7e9dc81 79
Tomoseec 0:0ca2c7e9dc81 80 int TextLCD::_putc(int value) {
Tomoseec 0:0ca2c7e9dc81 81 if(value == '\n') {
Tomoseec 0:0ca2c7e9dc81 82 newline();
Tomoseec 0:0ca2c7e9dc81 83 } else {
Tomoseec 0:0ca2c7e9dc81 84 writeData(value);
Tomoseec 0:0ca2c7e9dc81 85 }
Tomoseec 0:0ca2c7e9dc81 86 return value;
Tomoseec 0:0ca2c7e9dc81 87 }
Tomoseec 0:0ca2c7e9dc81 88
Tomoseec 0:0ca2c7e9dc81 89 int TextLCD::_getc() {
Tomoseec 0:0ca2c7e9dc81 90 return 0;
Tomoseec 0:0ca2c7e9dc81 91 }
Tomoseec 0:0ca2c7e9dc81 92
Tomoseec 0:0ca2c7e9dc81 93 void TextLCD::newline() {
Tomoseec 0:0ca2c7e9dc81 94 _column = 0;
Tomoseec 0:0ca2c7e9dc81 95 _row++;
Tomoseec 0:0ca2c7e9dc81 96 if(_row >= _rows) {
Tomoseec 0:0ca2c7e9dc81 97 _row = 0;
Tomoseec 0:0ca2c7e9dc81 98 }
Tomoseec 0:0ca2c7e9dc81 99 locate(_column, _row);
Tomoseec 0:0ca2c7e9dc81 100 }
Tomoseec 0:0ca2c7e9dc81 101
Tomoseec 0:0ca2c7e9dc81 102 void TextLCD::locate(int column, int row) {
Tomoseec 0:0ca2c7e9dc81 103 if(column < 0 || column >= _columns || row < 0 || row >= _rows) {
Tomoseec 0:0ca2c7e9dc81 104 error("locate(%d,%d) out of range on %dx%d display", column, row, _columns, _rows);
Tomoseec 0:0ca2c7e9dc81 105 return;
Tomoseec 0:0ca2c7e9dc81 106 }
Tomoseec 0:0ca2c7e9dc81 107
Tomoseec 0:0ca2c7e9dc81 108 _row = row;
Tomoseec 0:0ca2c7e9dc81 109 _column = column;
Tomoseec 0:0ca2c7e9dc81 110 int address = 0x80 + (_row * 40) + _column; // memory starts at 0x80, and is 40 chars long per row
Tomoseec 0:0ca2c7e9dc81 111 writeCommand(address);
Tomoseec 0:0ca2c7e9dc81 112 }
Tomoseec 0:0ca2c7e9dc81 113
Tomoseec 0:0ca2c7e9dc81 114 void TextLCD::cls() {
Tomoseec 0:0ca2c7e9dc81 115 writeCommand(0x01); // Clear Display
Tomoseec 0:0ca2c7e9dc81 116 wait(0.00164f); // This command takes 1.64 ms
Tomoseec 0:0ca2c7e9dc81 117 locate(0, 0);
Tomoseec 0:0ca2c7e9dc81 118 }
Tomoseec 0:0ca2c7e9dc81 119
Tomoseec 0:0ca2c7e9dc81 120 void TextLCD::reset() {
Tomoseec 0:0ca2c7e9dc81 121 cls();
Tomoseec 0:0ca2c7e9dc81 122 }
Tomoseec 0:0ca2c7e9dc81 123
Tomoseec 0:0ca2c7e9dc81 124 void TextLCD::clock() {
Tomoseec 0:0ca2c7e9dc81 125 wait(0.000040f);
Tomoseec 0:0ca2c7e9dc81 126 _e = 0;
Tomoseec 0:0ca2c7e9dc81 127 wait(0.000040f); // most instructions take 40us
Tomoseec 0:0ca2c7e9dc81 128 _e = 1;
Tomoseec 0:0ca2c7e9dc81 129 }
Tomoseec 0:0ca2c7e9dc81 130
Tomoseec 0:0ca2c7e9dc81 131 void TextLCD::writeNibble(int value) {
Tomoseec 0:0ca2c7e9dc81 132 _d = value;
Tomoseec 0:0ca2c7e9dc81 133 clock();
Tomoseec 0:0ca2c7e9dc81 134 }
Tomoseec 0:0ca2c7e9dc81 135
Tomoseec 0:0ca2c7e9dc81 136 void TextLCD::writeByte(int value) {
Tomoseec 0:0ca2c7e9dc81 137 writeNibble(value >> 4);
Tomoseec 0:0ca2c7e9dc81 138 writeNibble(value >> 0);
Tomoseec 0:0ca2c7e9dc81 139 }
Tomoseec 0:0ca2c7e9dc81 140
Tomoseec 0:0ca2c7e9dc81 141 void TextLCD::writeCommand(int command) {
Tomoseec 0:0ca2c7e9dc81 142 _rs = 0;
Tomoseec 0:0ca2c7e9dc81 143 writeByte(command);
Tomoseec 0:0ca2c7e9dc81 144 }
Tomoseec 0:0ca2c7e9dc81 145
Tomoseec 0:0ca2c7e9dc81 146 void TextLCD::writeData(int data) {
Tomoseec 0:0ca2c7e9dc81 147 _rs = 1;
Tomoseec 0:0ca2c7e9dc81 148 writeByte(data);
Tomoseec 0:0ca2c7e9dc81 149 _column++;
Tomoseec 0:0ca2c7e9dc81 150 if(_column >= _columns) {
Tomoseec 0:0ca2c7e9dc81 151 newline();
Tomoseec 0:0ca2c7e9dc81 152 }
Tomoseec 0:0ca2c7e9dc81 153 }