k og / HT1621

Dependents:   Demo_HT1621

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HT1621.cpp Source File

HT1621.cpp

00001 #include "HT1621.h"
00002 
00003 #define ADDR_MAX 128
00004 
00005 #define TAKE_CS()    _cs = 0
00006 #define RELEASE_CS() _cs = 1
00007 
00008 HT1621::HT1621(
00009     PinName data, PinName wr, PinName rd, PinName cs
00010     ):
00011     _data(data), _wr(wr), _rd(rd), _cs(cs)
00012 {
00013     _data.output();
00014     _data = 1;
00015     _wr = 1;
00016     _rd = 1;
00017     _cs = 1;
00018     sendCommand(SYS_DIS);
00019     wait_ms(500);
00020     //if (! testMem())
00021     //    return false;
00022 
00023     memset(0, 0, ADDR_MAX);
00024     sendCommand(SYS_EN);
00025     sendCommand(LCD_ON);
00026 }
00027 
00028 
00029 inline bool HT1621::testMem()
00030 {
00031     uint8_t test = 10;
00032     writeMem(0x5a, test);
00033     if (readMem(0x5a) != test)
00034         return false;
00035     return true;
00036 }
00037 
00038 
00039 void HT1621::writeBits(uint8_t data, uint8_t cnt)
00040 {
00041     while (cnt) {
00042         _wr = 0;
00043         uint8_t bitval = (data & (1 << (cnt - 1))) ? 1 : 0;
00044         _data = bitval;
00045         //wait_ms(1);
00046         _wr = 1;
00047         cnt--;
00048     }
00049 }
00050 
00051 uint8_t HT1621::readBits(uint8_t cnt)
00052 {
00053     uint8_t data = 0;
00054     _data.input();
00055     while (cnt) {
00056         _rd = 0;
00057         data += _data << (cnt - 1);
00058         //wait_ms(1);
00059         _rd= 1;
00060         cnt--;
00061     }
00062     _data.output();
00063     return data;
00064 }
00065 
00066 #define COMMAND_MODE 0b100
00067 void HT1621::sendCommand(uint8_t cmd, bool first /*= true*/, bool last /*= true*/)
00068 {
00069     if (first) {
00070         TAKE_CS();
00071         writeBits(COMMAND_MODE, 3);
00072     }
00073     writeBits(cmd, 8);
00074     writeBits(0, 1); //Last bit - don't care
00075     if (last)
00076         RELEASE_CS();
00077 }
00078 
00079 #define WRITE_MODE 0b101
00080 void HT1621::writeMem(uint8_t address, uint8_t data)
00081 {
00082     TAKE_CS();
00083     writeBits(WRITE_MODE, 3);
00084     writeBits(address, 6);
00085     writeBits(data, 4);
00086     RELEASE_CS();
00087 }
00088 
00089 void HT1621::memset(uint8_t address, uint8_t data, uint8_t cnt)
00090 {
00091   TAKE_CS();
00092   writeBits(WRITE_MODE, 3);
00093   writeBits(address, 6);
00094   for (uint8_t i = 0; i < cnt; i++)
00095     writeBits(data, 4);
00096   RELEASE_CS();
00097 }
00098 
00099 //Write up to 256 values starting from address
00100 //Note: Data is 8-bit aligned. This is not vary efficient
00101 void HT1621::write(uint8_t address, uint8_t *data, uint8_t cnt)
00102 {
00103     TAKE_CS();
00104     writeBits(WRITE_MODE, 3);
00105     writeBits(address, 6);
00106     for (uint8_t i = 0; i < cnt; i++)
00107         writeBits(data[i], 4);
00108     RELEASE_CS();
00109 }
00110 
00111 #define READ_MODE 0b110
00112 uint8_t HT1621::readMem(uint8_t address)
00113 {
00114     uint8_t data;
00115     TAKE_CS();
00116     writeBits(READ_MODE, 3);
00117     writeBits(address, 6);
00118     data = readBits(4);
00119     RELEASE_CS();
00120     return data;
00121 }
00122 
00123 #define READ_MODE 0b110
00124 void HT1621::read(uint8_t address, uint8_t *data, uint8_t cnt)
00125 {
00126     TAKE_CS();
00127     writeBits(READ_MODE, 3);
00128     writeBits(address, 6);
00129     for (uint8_t i = 0; i < cnt; i++)
00130         data[i] = readBits(4);
00131     RELEASE_CS();
00132 }