Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: pat9125_mbed mbed
pixart_lcm/pixart_lcm.cpp@9:53b4b362cbb8, 2017-10-20 (annotated)
- Committer:
- pixus_mbed
- Date:
- Fri Oct 20 01:47:37 2017 +0000
- Revision:
- 9:53b4b362cbb8
- Parent:
- 5:61318505e528
For NUCLEO-L476RG
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pixus_mbed | 0:411244c71423 | 1 | #include "pixart_lcm.h" |
pixus_mbed | 0:411244c71423 | 2 | |
pixus_mbed | 0:411244c71423 | 3 | |
pixus_mbed | 0:411244c71423 | 4 | |
pixus_mbed | 0:411244c71423 | 5 | typedef struct |
pixus_mbed | 0:411244c71423 | 6 | { |
pixus_mbed | 0:411244c71423 | 7 | SPI *pSPI ; |
pixus_mbed | 0:411244c71423 | 8 | DigitalOut *pCSB; |
pixus_mbed | 0:411244c71423 | 9 | DigitalOut *pRSTB; |
pixus_mbed | 0:411244c71423 | 10 | DigitalOut *pRS; |
pixus_mbed | 0:411244c71423 | 11 | } pixart_lcm_state_s; |
pixus_mbed | 0:411244c71423 | 12 | |
pixus_mbed | 0:411244c71423 | 13 | static pixart_lcm_state_s g_state ; |
pixus_mbed | 0:411244c71423 | 14 | |
pixus_mbed | 0:411244c71423 | 15 | |
pixus_mbed | 0:411244c71423 | 16 | #define LOW 0 |
pixus_mbed | 0:411244c71423 | 17 | #define HIGH 1 |
pixus_mbed | 0:411244c71423 | 18 | #define PIN_LCM_RS g_state.pRS |
pixus_mbed | 0:411244c71423 | 19 | #define PIN_LCM_RSTB g_state.pRSTB |
pixus_mbed | 0:411244c71423 | 20 | #define PIN_LCM_CSB g_state.pCSB |
pixus_mbed | 0:411244c71423 | 21 | #define digitalWrite(pin,level) *pin = level |
pixus_mbed | 0:411244c71423 | 22 | #define LCM_RS_LO digitalWrite(PIN_LCM_RS,LOW) |
pixus_mbed | 0:411244c71423 | 23 | #define LCM_RS_HI digitalWrite(PIN_LCM_RS,HIGH) |
pixus_mbed | 0:411244c71423 | 24 | #define LCM_RSTB_LO digitalWrite(PIN_LCM_RSTB,LOW) |
pixus_mbed | 0:411244c71423 | 25 | #define LCM_RSTB_HI digitalWrite(PIN_LCM_RSTB,HIGH) |
pixus_mbed | 0:411244c71423 | 26 | #define LCM_CSB_LO digitalWrite(PIN_LCM_CSB,LOW) |
pixus_mbed | 0:411244c71423 | 27 | #define LCM_CSB_HI digitalWrite(PIN_LCM_CSB,HIGH) |
pixus_mbed | 0:411244c71423 | 28 | |
pixus_mbed | 0:411244c71423 | 29 | #define I2C_ADDRESS (0x73 << 1) |
pixus_mbed | 0:411244c71423 | 30 | #define delayMicroseconds(us) wait_us(us) |
pixus_mbed | 0:411244c71423 | 31 | #define delay(ms) wait_ms(ms) |
pixus_mbed | 0:411244c71423 | 32 | |
pixus_mbed | 0:411244c71423 | 33 | //----------------------------------------------------------------------- |
pixus_mbed | 0:411244c71423 | 34 | unsigned char hex2dec_nibble(unsigned char hex_nibble) |
pixus_mbed | 0:411244c71423 | 35 | { |
pixus_mbed | 0:411244c71423 | 36 | unsigned char dec; |
pixus_mbed | 0:411244c71423 | 37 | |
pixus_mbed | 0:411244c71423 | 38 | switch(hex_nibble) |
pixus_mbed | 0:411244c71423 | 39 | { |
pixus_mbed | 0:411244c71423 | 40 | case 0xA: dec=10; break; |
pixus_mbed | 0:411244c71423 | 41 | case 0xB: dec=11; break; |
pixus_mbed | 0:411244c71423 | 42 | case 0xC: dec=12; break; |
pixus_mbed | 0:411244c71423 | 43 | case 0xD: dec=13; break; |
pixus_mbed | 0:411244c71423 | 44 | case 0xE: dec=14; break; |
pixus_mbed | 0:411244c71423 | 45 | case 0xF: dec=15; break; |
pixus_mbed | 0:411244c71423 | 46 | default: dec=hex_nibble; break; |
pixus_mbed | 0:411244c71423 | 47 | } |
pixus_mbed | 0:411244c71423 | 48 | |
pixus_mbed | 0:411244c71423 | 49 | return (dec); |
pixus_mbed | 0:411244c71423 | 50 | } |
pixus_mbed | 0:411244c71423 | 51 | |
pixus_mbed | 0:411244c71423 | 52 | unsigned int hex2dec_word(unsigned int hex_word) |
pixus_mbed | 0:411244c71423 | 53 | { |
pixus_mbed | 0:411244c71423 | 54 | unsigned char dec_nb[4]; |
pixus_mbed | 0:411244c71423 | 55 | unsigned char nibble3=(hex_word>>12)&0x000f; |
pixus_mbed | 0:411244c71423 | 56 | unsigned char nibble2=(hex_word>>8)&0x000f; |
pixus_mbed | 0:411244c71423 | 57 | unsigned char nibble1=(hex_word>>4)&0x000f; |
pixus_mbed | 0:411244c71423 | 58 | unsigned char nibble0=hex_word&0x000f; |
pixus_mbed | 0:411244c71423 | 59 | |
pixus_mbed | 0:411244c71423 | 60 | dec_nb[3]=hex2dec_nibble(nibble3); |
pixus_mbed | 0:411244c71423 | 61 | dec_nb[2]=hex2dec_nibble(nibble2); |
pixus_mbed | 0:411244c71423 | 62 | dec_nb[1]=hex2dec_nibble(nibble1); |
pixus_mbed | 0:411244c71423 | 63 | dec_nb[0]=hex2dec_nibble(nibble0); |
pixus_mbed | 0:411244c71423 | 64 | return ((dec_nb[3]<<12)+(dec_nb[2]<<8)+(dec_nb[1]<<4)+dec_nb[0]); |
pixus_mbed | 0:411244c71423 | 65 | } |
pixus_mbed | 0:411244c71423 | 66 | |
pixus_mbed | 0:411244c71423 | 67 | void LCM_WriteCom(unsigned char Command) |
pixus_mbed | 0:411244c71423 | 68 | { |
pixus_mbed | 0:411244c71423 | 69 | LCM_CSB_LO; |
pixus_mbed | 0:411244c71423 | 70 | LCM_RS_LO; |
pixus_mbed | 0:411244c71423 | 71 | //SPI.transfer(Command); |
pixus_mbed | 0:411244c71423 | 72 | g_state.pSPI->write(Command); |
pixus_mbed | 0:411244c71423 | 73 | LCM_CSB_HI; |
pixus_mbed | 0:411244c71423 | 74 | |
pixus_mbed | 0:411244c71423 | 75 | delayMicroseconds(30); |
pixus_mbed | 0:411244c71423 | 76 | } |
pixus_mbed | 0:411244c71423 | 77 | |
pixus_mbed | 0:411244c71423 | 78 | void LCM_WriteData(unsigned char Ascii) |
pixus_mbed | 0:411244c71423 | 79 | { |
pixus_mbed | 0:411244c71423 | 80 | LCM_CSB_LO; |
pixus_mbed | 0:411244c71423 | 81 | LCM_RS_HI; |
pixus_mbed | 0:411244c71423 | 82 | //SPI.transfer(Ascii); |
pixus_mbed | 0:411244c71423 | 83 | g_state.pSPI->write(Ascii); |
pixus_mbed | 0:411244c71423 | 84 | LCM_CSB_HI; |
pixus_mbed | 0:411244c71423 | 85 | |
pixus_mbed | 0:411244c71423 | 86 | delayMicroseconds(30); |
pixus_mbed | 0:411244c71423 | 87 | } |
pixus_mbed | 0:411244c71423 | 88 | |
pixus_mbed | 0:411244c71423 | 89 | void LCM_Init(void) |
pixus_mbed | 0:411244c71423 | 90 | { |
pixus_mbed | 0:411244c71423 | 91 | LCM_RSTB_LO; |
pixus_mbed | 0:411244c71423 | 92 | delay(3); |
pixus_mbed | 0:411244c71423 | 93 | LCM_RSTB_HI; |
pixus_mbed | 0:411244c71423 | 94 | delay(20); |
pixus_mbed | 0:411244c71423 | 95 | LCM_WriteCom(0x30); //wake up |
pixus_mbed | 0:411244c71423 | 96 | delay(3); |
pixus_mbed | 0:411244c71423 | 97 | LCM_WriteCom(0x30); //wake up |
pixus_mbed | 0:411244c71423 | 98 | LCM_WriteCom(0x30); //wake up |
pixus_mbed | 0:411244c71423 | 99 | LCM_WriteCom(0x39); //function set |
pixus_mbed | 0:411244c71423 | 100 | LCM_WriteCom(0x14); //internal osc frequency |
pixus_mbed | 0:411244c71423 | 101 | LCM_WriteCom(0x56); //Contrast set |
pixus_mbed | 0:411244c71423 | 102 | LCM_WriteCom(0x6D); //follower control |
pixus_mbed | 0:411244c71423 | 103 | LCM_WriteCom(0x75); //contrast// |
pixus_mbed | 0:411244c71423 | 104 | LCM_WriteCom(0x0C); //display on |
pixus_mbed | 0:411244c71423 | 105 | LCM_WriteCom(0x06); //entry mode |
pixus_mbed | 0:411244c71423 | 106 | LCM_WriteCom(0x01); //clear |
pixus_mbed | 0:411244c71423 | 107 | |
pixus_mbed | 0:411244c71423 | 108 | delay(10); |
pixus_mbed | 0:411244c71423 | 109 | } |
pixus_mbed | 0:411244c71423 | 110 | |
pixus_mbed | 0:411244c71423 | 111 | void LCM_Clear(void) |
pixus_mbed | 0:411244c71423 | 112 | { |
pixus_mbed | 0:411244c71423 | 113 | LCM_WriteCom(0x01); |
pixus_mbed | 0:411244c71423 | 114 | |
pixus_mbed | 0:411244c71423 | 115 | delay(2); |
pixus_mbed | 0:411244c71423 | 116 | } |
pixus_mbed | 0:411244c71423 | 117 | |
pixus_mbed | 0:411244c71423 | 118 | void LCM_SetPosition(unsigned char line, unsigned char position)//line=1 or 2; position=1~16 |
pixus_mbed | 0:411244c71423 | 119 | { |
pixus_mbed | 0:411244c71423 | 120 | unsigned char address; |
pixus_mbed | 0:411244c71423 | 121 | |
pixus_mbed | 0:411244c71423 | 122 | address = ((line-1) * 0x40) + (position-1); |
pixus_mbed | 0:411244c71423 | 123 | address = 0x80 + (address & 0x7F); |
pixus_mbed | 0:411244c71423 | 124 | |
pixus_mbed | 0:411244c71423 | 125 | LCM_WriteCom(address); |
pixus_mbed | 0:411244c71423 | 126 | } |
pixus_mbed | 0:411244c71423 | 127 | |
pixus_mbed | 0:411244c71423 | 128 | void LCM_DisplayString(unsigned char line, unsigned char position, const char *ptr) |
pixus_mbed | 0:411244c71423 | 129 | { |
pixus_mbed | 0:411244c71423 | 130 | LCM_SetPosition(line,position); |
pixus_mbed | 0:411244c71423 | 131 | |
pixus_mbed | 0:411244c71423 | 132 | while (*ptr) |
pixus_mbed | 0:411244c71423 | 133 | { |
pixus_mbed | 0:411244c71423 | 134 | LCM_WriteData(*ptr++); |
pixus_mbed | 0:411244c71423 | 135 | } |
pixus_mbed | 0:411244c71423 | 136 | } |
pixus_mbed | 0:411244c71423 | 137 | |
pixus_mbed | 0:411244c71423 | 138 | void LCM_DisplayDecimal(unsigned char line, unsigned char position, unsigned int hex_word, unsigned char digits) |
pixus_mbed | 0:411244c71423 | 139 | { |
pixus_mbed | 0:411244c71423 | 140 | unsigned char sign;//0:positive, 1:negative |
pixus_mbed | 0:411244c71423 | 141 | unsigned int dec_num; |
pixus_mbed | 0:411244c71423 | 142 | unsigned char digit[5]; |
pixus_mbed | 0:411244c71423 | 143 | signed char ii; |
pixus_mbed | 0:411244c71423 | 144 | |
pixus_mbed | 0:411244c71423 | 145 | if(hex_word & 0x8000) |
pixus_mbed | 0:411244c71423 | 146 | sign=1; |
pixus_mbed | 0:411244c71423 | 147 | else |
pixus_mbed | 0:411244c71423 | 148 | sign=0; |
pixus_mbed | 0:411244c71423 | 149 | |
pixus_mbed | 0:411244c71423 | 150 | if(sign==1) |
pixus_mbed | 0:411244c71423 | 151 | hex_word=~hex_word+1; |
pixus_mbed | 0:411244c71423 | 152 | |
pixus_mbed | 0:411244c71423 | 153 | dec_num=hex2dec_word(hex_word); |
pixus_mbed | 0:411244c71423 | 154 | digit[4]=dec_num/10000; |
pixus_mbed | 0:411244c71423 | 155 | digit[3]=(dec_num%10000)/1000; |
pixus_mbed | 0:411244c71423 | 156 | digit[2]=(dec_num%1000)/100; |
pixus_mbed | 0:411244c71423 | 157 | digit[1]=(dec_num%100)/10; |
pixus_mbed | 0:411244c71423 | 158 | digit[0]=dec_num%10; |
pixus_mbed | 0:411244c71423 | 159 | |
pixus_mbed | 0:411244c71423 | 160 | LCM_SetPosition(line,position); |
pixus_mbed | 0:411244c71423 | 161 | |
pixus_mbed | 0:411244c71423 | 162 | if(sign==1) |
pixus_mbed | 0:411244c71423 | 163 | LCM_WriteData('-'); |
pixus_mbed | 0:411244c71423 | 164 | else |
pixus_mbed | 0:411244c71423 | 165 | LCM_WriteData('+'); |
pixus_mbed | 0:411244c71423 | 166 | |
pixus_mbed | 0:411244c71423 | 167 | for(ii=(digits-1);ii>=0;ii--) |
pixus_mbed | 0:411244c71423 | 168 | { |
pixus_mbed | 0:411244c71423 | 169 | LCM_WriteData(digit[ii] | 0x30);//decimal to ascii |
pixus_mbed | 0:411244c71423 | 170 | } |
pixus_mbed | 0:411244c71423 | 171 | |
pixus_mbed | 0:411244c71423 | 172 | } |
pixus_mbed | 0:411244c71423 | 173 | |
pixus_mbed | 0:411244c71423 | 174 | void LCM_ClearLine(unsigned char line)// line: 1 or 2 |
pixus_mbed | 0:411244c71423 | 175 | { |
pixus_mbed | 0:411244c71423 | 176 | LCM_DisplayString(line,1," "); |
pixus_mbed | 0:411244c71423 | 177 | } |
pixus_mbed | 0:411244c71423 | 178 | |
pixus_mbed | 0:411244c71423 | 179 | void LCM_DisplayString_Reset(void) |
pixus_mbed | 0:411244c71423 | 180 | { |
pixus_mbed | 0:411244c71423 | 181 | LCM_DisplayString(1,1,"SHAFT"); LCM_DisplayString(1,8,"U/D");LCM_DisplayDecimal(1,12,0x000,4); |
pixus_mbed | 0:411244c71423 | 182 | LCM_DisplayString(2,1,"SPRING");LCM_DisplayString(2,8,"P/R");LCM_DisplayDecimal(2,12,0x000,4); |
pixus_mbed | 0:411244c71423 | 183 | } |
pixus_mbed | 0:411244c71423 | 184 | |
pixus_mbed | 0:411244c71423 | 185 | void LCM_DisplayString_Boot(boolean sen_status) |
pixus_mbed | 0:411244c71423 | 186 | { |
pixus_mbed | 0:411244c71423 | 187 | LCM_DisplayString(1,1,"PixArt Shaft EVK"); |
pixus_mbed | 5:61318505e528 | 188 | LCM_DisplayString(2,1,"PAT9125 FW V2.40"); |
pixus_mbed | 0:411244c71423 | 189 | delay(2000); |
pixus_mbed | 0:411244c71423 | 190 | |
pixus_mbed | 0:411244c71423 | 191 | LCM_ClearLine(1); |
pixus_mbed | 0:411244c71423 | 192 | LCM_ClearLine(2); |
pixus_mbed | 0:411244c71423 | 193 | |
pixus_mbed | 0:411244c71423 | 194 | if(sen_status == true) |
pixus_mbed | 0:411244c71423 | 195 | { |
pixus_mbed | 0:411244c71423 | 196 | LCM_DisplayString_Reset(); |
pixus_mbed | 0:411244c71423 | 197 | } |
pixus_mbed | 0:411244c71423 | 198 | else |
pixus_mbed | 0:411244c71423 | 199 | { |
pixus_mbed | 0:411244c71423 | 200 | LCM_DisplayString(2,1,"Read Sensor Fail"); |
pixus_mbed | 5:61318505e528 | 201 | while(1);//stop here if read sensor fail as a warning. |
pixus_mbed | 0:411244c71423 | 202 | } |
pixus_mbed | 0:411244c71423 | 203 | } |
pixus_mbed | 0:411244c71423 | 204 | |
pixus_mbed | 0:411244c71423 | 205 | //----------------------------------------------------------------------- |
pixus_mbed | 0:411244c71423 | 206 | |
pixus_mbed | 0:411244c71423 | 207 | pixart_lcm::pixart_lcm(SPI *pSPI, DigitalOut *pCSB, DigitalOut *pRSTB, DigitalOut *pRS) |
pixus_mbed | 0:411244c71423 | 208 | { |
pixus_mbed | 0:411244c71423 | 209 | g_state.pSPI = pSPI; |
pixus_mbed | 0:411244c71423 | 210 | g_state.pCSB = pCSB; |
pixus_mbed | 0:411244c71423 | 211 | g_state.pRSTB = pRSTB; |
pixus_mbed | 0:411244c71423 | 212 | g_state.pRS = pRS; |
pixus_mbed | 0:411244c71423 | 213 | *g_state.pRS = 1; |
pixus_mbed | 0:411244c71423 | 214 | LCM_Init(); |
pixus_mbed | 0:411244c71423 | 215 | } |
pixus_mbed | 0:411244c71423 | 216 | |
pixus_mbed | 0:411244c71423 | 217 | void pixart_lcm::LCM_DisplayString(unsigned char line, unsigned char position, const char *ptr) |
pixus_mbed | 0:411244c71423 | 218 | { |
pixus_mbed | 0:411244c71423 | 219 | ::LCM_DisplayString(line, position, ptr); |
pixus_mbed | 0:411244c71423 | 220 | } |
pixus_mbed | 0:411244c71423 | 221 | void pixart_lcm::LCM_DisplayDecimal(unsigned char line, unsigned char position, unsigned int hex_word, unsigned char digits) |
pixus_mbed | 0:411244c71423 | 222 | { |
pixus_mbed | 0:411244c71423 | 223 | ::LCM_DisplayDecimal(line, position, hex_word, digits); |
pixus_mbed | 0:411244c71423 | 224 | } |
pixus_mbed | 0:411244c71423 | 225 | void pixart_lcm::LCM_DisplayString_Reset(void) |
pixus_mbed | 0:411244c71423 | 226 | { |
pixus_mbed | 0:411244c71423 | 227 | ::LCM_DisplayString_Reset(); |
pixus_mbed | 0:411244c71423 | 228 | } |
pixus_mbed | 0:411244c71423 | 229 | void pixart_lcm::LCM_DisplayString_Boot(boolean sen_status) |
pixus_mbed | 0:411244c71423 | 230 | { |
pixus_mbed | 0:411244c71423 | 231 | ::LCM_DisplayString_Boot(sen_status); |
pixus_mbed | 0:411244c71423 | 232 | } |