blablabla

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SLCD.cpp Source File

SLCD.cpp

00001 #include "SLCD.h"
00002 #include "LCDconfig.h"
00003 
00004 
00005 const uint8_t WF_ORDERING_TABLE[ ] = {
00006     CHAR1a,   // LCD81 --- Pin:5  LCDnAddress=51
00007     CHAR1b,   // LCD82 --- Pin:6  LCDnAddress=52
00008     CHAR2a,   // LCD83 --- Pin:7  LCDnAddress=53
00009     CHAR2b,   // LCD84 --- Pin:8  LCDnAddress=54
00010     CHAR3a,   // LCD85 --- Pin:9  LCDnAddress=55
00011     CHAR3b,   // LCD86 --- Pin:10 LCDnAddress=56
00012     CHAR4a,   // LCD87 --- Pin:11 LCDnAddress=57
00013     CHAR4b,   // LCD88 --- Pin:12 LCDnAddress=58
00014     CHARCOM0, // LCD77 --- Pin:1  LCDnAddress=4D
00015     CHARCOM1, // LCD78 --- Pin:2  LCDnAddress=4E
00016     CHARCOM2, // LCD79 --- Pin:3  LCDnAddress=4F
00017     CHARCOM3, // LCD80 --- Pin:4  LCDnAddress=50
00018 };
00019 
00020 const char ASCII_TO_WF_CODIFICATION_TABLE [ ] = {
00021 
00022     /*
00023                    segA
00024                  ________
00025                 |        |
00026            segF |        | segB
00027                 |        |
00028                  -segG--
00029                 |        |
00030            segE |        | segC
00031                 |________|
00032                    segD
00033     */
00034 
00035     ( SEGD+ SEGE+ SEGF+!SEGG) , ( SEGC+ SEGB+ SEGA) ,//Char = 0,   offset=0
00036     (!SEGD+!SEGE+!SEGF+!SEGG) , ( SEGC+ SEGB+!SEGA) ,//Char = 1,   offset=4
00037     ( SEGD+ SEGE+!SEGF+ SEGG) , (!SEGC+ SEGB+ SEGA) ,//Char = 2,   offset=8
00038     ( SEGD+!SEGE+!SEGF+ SEGG) , ( SEGC+ SEGB+ SEGA) ,//Char = 3,   offset=12
00039     (!SEGD+!SEGE+ SEGF+ SEGG) , ( SEGC+ SEGB+!SEGA) ,//Char = 4,   offset=16
00040     ( SEGD+!SEGE+ SEGF+ SEGG) , ( SEGC+!SEGB+ SEGA) ,//Char = 5,   offset=20
00041     ( SEGD+ SEGE+ SEGF+ SEGG) , ( SEGC+!SEGB+ SEGA) ,//Char = 6,   offset=24
00042     (!SEGD+!SEGE+!SEGF+!SEGG) , ( SEGC+ SEGB+ SEGA) ,//Char = 7,   offset=28
00043     ( SEGD+ SEGE+ SEGF+ SEGG) , ( SEGC+ SEGB+ SEGA) ,//Char = 8,   offset=32
00044     ( SEGD+!SEGE+ SEGF+ SEGG) , ( SEGC+ SEGB+ SEGA) ,//Char = 9,   offset=36
00045     (!SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+!SEGA) ,//Char = :,   offset=40
00046     (!SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+!SEGA) ,//Char = ;,   offset=44
00047     (!SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+!SEGA) ,//Char = <,   offset=48
00048     ( SEGD+!SEGE+!SEGF+ SEGG) , (!SEGC+!SEGB+!SEGA) ,//Char = =,   offset=52
00049     (!SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+!SEGA) ,//Char = >,   offset=56
00050     (!SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+ SEGA) ,//Char = ?,   offset=60
00051     ( SEGD+ SEGE+ SEGF+ SEGG) , ( SEGC+ SEGB+ SEGA) ,//Char = @,   offset=64
00052     (!SEGD+ SEGE+ SEGF+ SEGG) , ( SEGC+ SEGB+ SEGA) ,//Char = A,   offset=68
00053     ( SEGD+ SEGE+ SEGF+ SEGG) , ( SEGC+!SEGB+!SEGA) ,//Char = B,   offset=72
00054     ( SEGD+ SEGE+ SEGF+!SEGG) , (!SEGC+!SEGB+ SEGA) ,//Char = C,   offset=76
00055     ( SEGD+ SEGE+!SEGF+ SEGG) , ( SEGC+ SEGB+!SEGA) ,//Char = D,   offset=80
00056     ( SEGD+ SEGE+ SEGF+ SEGG) , (!SEGC+!SEGB+ SEGA) ,//Char = E,   offset=84
00057     (!SEGD+ SEGE+ SEGF+ SEGG) , (!SEGC+!SEGB+ SEGA) ,//Char = F,   offset=88
00058     ( SEGD+ SEGE+ SEGF+ SEGG) , ( SEGC+!SEGB+ SEGA) ,//Char = G,   offset=92
00059     (!SEGD+ SEGE+ SEGF+ SEGG) , ( SEGC+ SEGB+!SEGA) ,//Char = H,   offset=96
00060     (!SEGD+!SEGE+!SEGF+!SEGG) , ( SEGC+!SEGB+!SEGA) ,//Char = I,   offset=100
00061     ( SEGD+ SEGE+!SEGF+!SEGG) , ( SEGC+ SEGB+!SEGA) ,//Char = J,   offset=104
00062     (!SEGD+ SEGE+ SEGF+ SEGG) , ( SEGC+!SEGB+ SEGA) ,//Char = K,   offset=108
00063     ( SEGD+ SEGE+ SEGF+!SEGG) , (!SEGC+!SEGB+!SEGA) ,//Char = L,   offset=112
00064     (!SEGD+ SEGE+ SEGF+!SEGG) , ( SEGC+ SEGB+ SEGA) ,//Char = M,   offset=116
00065     (!SEGD+ SEGE+!SEGF+ SEGG) , ( SEGC+!SEGB+!SEGA) ,//Char = N,   offset=120
00066     ( SEGD+ SEGE+!SEGF+ SEGG) , ( SEGC+!SEGB+!SEGA) ,//Char = O,   offset=124
00067     (!SEGD+ SEGE+ SEGF+ SEGG) , (!SEGC+ SEGB+ SEGA) ,//Char = P,   offset=128
00068     ( SEGD+!SEGE+ SEGF+ SEGG) , ( SEGC+ SEGB+ SEGA) ,//Char = Q,   offset=132
00069     (!SEGD+ SEGE+!SEGF+ SEGG) , (!SEGC+!SEGB+!SEGA) ,//Char = R,   offset=136
00070     ( SEGD+!SEGE+ SEGF+ SEGG) , ( SEGC+!SEGB+ SEGA) ,//Char = S,   offset=140
00071     ( SEGD+ SEGE+ SEGF+ SEGG) , (!SEGC+!SEGB+!SEGA) ,//Char = T,   offset=144
00072     ( SEGD+ SEGE+ SEGF+!SEGG) , ( SEGC+ SEGB+!SEGA) ,//Char = U,   offset=148
00073     ( SEGD+ SEGE+!SEGF+!SEGG) , ( SEGC+!SEGB+!SEGA) ,//Char = V,   offset=152
00074     ( SEGD+ SEGE+ SEGF+!SEGG) , ( SEGC+ SEGB+!SEGA) ,//Char = W,   offset=156
00075     (!SEGD+ SEGE+ SEGF+ SEGG) , ( SEGC+ SEGB+!SEGA) ,//Char = X,   offset=160
00076     ( SEGD+!SEGE+ SEGF+ SEGG) , ( SEGC+ SEGB+!SEGA) ,//Char = Y,   offset=164
00077     ( SEGD+!SEGE+!SEGF+ SEGG) , (!SEGC+!SEGB+!SEGA) ,//Char = Z,   offset=168
00078     ( SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+ SEGA) ,//Char = [,   offset=172
00079     ( SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+ SEGA) ,//Char = \,   offset=176
00080     ( SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+ SEGA) ,//Char = ],   offset=180
00081     ( SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+ SEGA) ,//Char = ^,   offset=184
00082     ( SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+ SEGA) ,//Char = _,   offset=188
00083     ( SEGD+!SEGE+!SEGF+!SEGG) , (!SEGC+!SEGB+ SEGA) ,//Char = `,   offset=192
00084     (!SEGD+!SEGE+!SEGF+ SEGG) , (!SEGC+!SEGB+!SEGA) ,//Char = -,   offset=196 //LG+ added '-'
00085 };
00086 
00087 SLCD::SLCD()
00088 {
00089     init();
00090     CharPosition = 0;
00091 }
00092 
00093 void SLCD::init()
00094 {
00095     SIM->SCGC5 |= SIM_SCGC5_SLCD_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK;
00096 
00097     // configure pins for LCD operation
00098     PORTC->PCR[20] = 0x00000000;     //VLL2
00099     PORTC->PCR[21] = 0x00000000;     //VLL1
00100     PORTC->PCR[22] = 0x00000000;     //VCAP2
00101     PORTC->PCR[23] = 0x00000000;     //VCAP1
00102     // Enable IRCLK
00103     MCG->C1  |= MCG_C1_IRCLKEN_MASK | MCG_C1_IREFSTEN_MASK;
00104     MCG->C2  |= MCG_C2_IRCS_MASK ;  //0 32KHZ internal reference clock; 1= 4MHz irc
00105 
00106     //Check if the Fast reference has its divide by 2 enabled (default):
00107     if ((MCG->SC & MCG_SC_FCRDIV_MASK) != 1<<1)
00108         error("Invalid clock configuration for SLCD\n");
00109     LCD->GCR = 0x0;
00110     LCD->AR  = 0x0;
00111 
00112     // LCD configurartion
00113     LCD->GCR =   ( LCD_GCR_RVEN_MASK*_LCDRVEN
00114                    | LCD_GCR_RVTRIM(_LCDRVTRIM)         //0-15
00115                    | LCD_GCR_CPSEL_MASK*_LCDCPSEL
00116                    | LCD_GCR_LADJ(_LCDLOADADJUST)       //0-3
00117                    | LCD_GCR_VSUPPLY_MASK*_LCDSUPPLY    //0-1
00118                    |!LCD_GCR_FDCIEN_MASK
00119                    | LCD_GCR_ALTDIV(1)         //divide by something
00120                    |!LCD_GCR_LCDDOZE_MASK
00121                    |!LCD_GCR_LCDSTP_MASK
00122                    |!LCD_GCR_LCDEN_MASK                 //WILL BE ENABLE ON SUBSEQUENT STEP
00123                    | LCD_GCR_SOURCE_MASK*1
00124                    | LCD_GCR_ALTSOURCE_MASK*0
00125                    | LCD_GCR_LCLK(0)    //0-7
00126                    | LCD_GCR_DUTY(_LCDDUTY)   //0-7
00127                  );
00128     uint8_t i;
00129     uint32_t *p_pen;
00130     uint8_t pen_offset;   // 0 or 1
00131     uint8_t pen_bit;      // 0 to 31
00132     LCD->PEN[0] = 0x0;
00133     LCD->PEN[1] = 0x0;
00134     LCD->BPEN[0] = 0x0;
00135     LCD->BPEN[1] = 0x0;
00136     p_pen = (uint32_t *)&LCD->PEN[0];
00137     for (i=0; i<_LCDUSEDPINS; i++) {
00138         pen_offset = WF_ORDERING_TABLE[i]/32;
00139         pen_bit    = WF_ORDERING_TABLE[i]%32;
00140         p_pen[pen_offset] |= 1 << pen_bit;
00141         if (i>= _LCDFRONTPLANES) {  // Pin is a backplane
00142             p_pen[pen_offset+2] |= 1 << pen_bit;  // Enable  BPEN
00143             LCD->WF8B[(uint8_t)WF_ORDERING_TABLE[i]] = 1 << (i - _LCDFRONTPLANES);   // fill with 0x01, 0x02, etc
00144         }
00145     }
00146     LCD->GCR |= LCD_GCR_LCDEN_MASK;
00147 }
00148 
00149 int SLCD::_putc(int c)
00150 {
00151     Write_Char(c);
00152     return 0;
00153 }
00154 
00155 void SLCD::Write_Char (char lbValue)
00156 {
00157     if (CharPosition >= _CHARNUM)
00158         CharPosition = 0;
00159 
00160     if (lbValue == '.') {
00161         // Use built-in dot
00162         DP(CharPosition-1, true);
00163         return;
00164     }
00165 
00166     uint8_t char_val;
00167     uint8_t temp;
00168     uint8_t *lbpLCDWF;
00169     uint8_t lbCounter;
00170     uint16_t arrayOffset;
00171     uint8_t position;
00172 
00173     lbpLCDWF = (uint8_t *)&LCD->WF8B[0];
00174     /* only ascii character if value not writeable write as @ */
00175     if (lbValue>='a' && lbValue<='z') {
00176         lbValue -= 32; // UpperCase
00177     }
00178     if (lbValue<ASCCI_TABLE_START || lbValue >ASCCI_TABLE_END) {
00179       if (lbValue!='-') //LG+
00180         lbValue = BLANK_CHARACTER;  // default value as space
00181     }
00182     //if(lbValue=='-') lbValue += (-ASCCI_TABLE_START+3+(196)/4); else //LG+
00183     if(lbValue=='-') lbValue = 196/4; else //LG+
00184     lbValue -=ASCCI_TABLE_START;        // Remove the offset to search in the ascci table
00185     arrayOffset = (lbValue * _CHAR_SIZE); // Compensate matrix offset
00186     // ensure bLCD position is in valid limit
00187     lbCounter = 0;  //number of writings to complete one char
00188     while (lbCounter<_CHAR_SIZE) {
00189         position = (CharPosition) *_LCDTYPE + lbCounter;
00190         temp=0;
00191         if (lbCounter==1) {
00192             temp = lbpLCDWF[WF_ORDERING_TABLE[position]] & 0x01;//bit 0 has the special symbol information
00193         }
00194         char_val = ASCII_TO_WF_CODIFICATION_TABLE[arrayOffset + lbCounter];
00195         lbpLCDWF[WF_ORDERING_TABLE[position]] = char_val | temp;
00196         //  if (char_val==0) lbCounter = _CHAR_SIZE; //end of this character
00197         lbCounter++;
00198     }
00199     CharPosition++;
00200 }
00201 
00202 void SLCD::Home (void)
00203 {
00204     CharPosition =  0;
00205 }
00206 
00207 void SLCD::Contrast (uint8_t lbContrast)
00208 {
00209     lbContrast &= 0x0F;              //Forced to the only values accepted
00210     LCD->GCR |= LCD_GCR_RVTRIM(lbContrast);
00211 }
00212 
00213 void SLCD::clear()
00214 {
00215     All_Segments(0);
00216 }
00217 
00218 void SLCD::All_Segments (int mode)
00219 {
00220     uint8_t lbTotalBytes = _CHARNUM * _LCDTYPE;
00221     uint8_t lbCounter=0;
00222     uint8_t *lbpLCDWF;
00223 
00224     lbpLCDWF = (uint8_t *)&LCD->WF8B[0];
00225     while (lbCounter < lbTotalBytes) {
00226         if (mode==1) {
00227             lbpLCDWF[(uint8_t)WF_ORDERING_TABLE[lbCounter++]]=_ALLON;
00228         } else {
00229             lbpLCDWF[WF_ORDERING_TABLE[lbCounter++]]=0;
00230         }
00231     }
00232 }
00233 
00234 void SLCD::DP(int pos, bool on)
00235 {
00236     uint8_t *lbpLCDWF;
00237     int tableLoc;
00238     switch (pos) {
00239         case 0:
00240             tableLoc = 1;
00241             break;
00242         case 1:
00243             tableLoc = 3;
00244             break;
00245         case 2:
00246             tableLoc = 5;
00247             break;
00248         default:
00249             return; // Bad position
00250     }
00251     lbpLCDWF = (uint8_t *)&LCD->WF8B[0];
00252     if (on) {
00253         lbpLCDWF[(uint8_t)WF_ORDERING_TABLE[tableLoc]]|=1;
00254     } else {
00255         lbpLCDWF[(uint8_t)WF_ORDERING_TABLE[tableLoc]]&=~1;
00256     }
00257 }
00258 
00259 void SLCD::DP1 (int mode)
00260 {
00261     DP(0, mode==1);
00262 }
00263 
00264 void SLCD::DP2 (int mode)
00265 {
00266     DP(1, mode==1);
00267 }
00268 
00269 void SLCD::DP3 (int mode)
00270 {
00271     DP(2, mode==1);
00272 }
00273 
00274 void SLCD::Colon (int mode)
00275 {
00276     uint8_t *lbpLCDWF;
00277     lbpLCDWF = (uint8_t *)&LCD->WF8B[0];
00278     if (mode==1) {
00279         lbpLCDWF[(uint8_t)WF_ORDERING_TABLE[7]]|=1;
00280     } else {
00281         lbpLCDWF[(uint8_t)WF_ORDERING_TABLE[7]]&=~1;
00282     }
00283 }
00284 
00285 void SLCD::blink(int blink)
00286 {
00287     if (( blink > 7) || (blink < 0))
00288         LCD->AR &= ~LCD_AR_BLINK_MASK;
00289     else
00290         LCD->AR |= LCD_AR_BLINK_MASK | blink;
00291 }
00292 
00293 void SLCD::deepsleepEnable(bool enable)
00294 {
00295     MCG->C1 &= ~MCG_C1_IREFSTEN_MASK;
00296     MCG->C1 |= enable << MCG_C1_IREFSTEN_SHIFT;
00297     LCD->GCR &= ~LCD_GCR_LCDSTP_MASK;
00298     LCD->GCR |= (!enable) << LCD_GCR_LCDSTP_SHIFT;
00299 }