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: mbed
main.cpp
00001 #ifndef __TTSDISPLAY_H__ 00002 #define __TTSDISPLAY_H__ 00003 00004 #ifndef uchar 00005 #define uchar char 00006 #endif 00007 00008 // definitions for TM1636 00009 #define ADDR_AUTO 0x40 00010 #define ADDR_FIXED 0x44 00011 #define STARTADDR 0xc0 00012 00013 // definitions for the clock point of the digit tube 00014 #define POINT_ON 1 00015 #define POINT_OFF 0 00016 00017 // definitions for brightness 00018 #define BRIGHT_DARKEST 0 00019 #define BRIGHT_TYPICAL 2 00020 #define BRIGHTEST 7 00021 00022 //Special characters index of tube table 00023 #define INDEX_NEGATIVE_SIGH 16 00024 #define INDEX_BLANK 17 00025 00026 00027 class TTSDisplay{ 00028 00029 00030 private: 00031 00032 uchar Cmd_SetData; 00033 uchar Cmd_SetAddr; 00034 uchar Cmd_Dispdisplay; 00035 uchar _PointFlag; //_PointFlag=1:the clock point on 00036 uchar _brightness; 00037 uchar Clkpin; 00038 uchar Datapin; 00039 uchar dtaDisplay[4]; 00040 00041 private: 00042 00043 void writeByte(uchar wr_data); //write 8bit data to tm1637 00044 void start(void); //send start bits 00045 void stop(void); //send stop bits 00046 void set(uchar = BRIGHT_TYPICAL, uchar = 0x40, uchar = 0xc0); //To take effect the next time it displays. 00047 uchar coding(uchar DispData); 00048 00049 00050 //********************************************************************************************************** 00051 //*************************** USER INTERFACE *************************************************************** 00052 //********************************************************************************************************** 00053 public: 00054 00055 TTSDisplay(); 00056 00057 void display(uchar loca, uchar dta); // display a single num(0-9) 00058 void num(int dta); // display a num (0-9999) 00059 void time(uchar hour, uchar min); // display time, such as: 11:26 00060 void clear(); // clear display 00061 00062 void pointOn(); // display : 00063 void pointOff(); // undisplay : 00064 00065 }; 00066 00067 #endif 00068 00069 //#include <Arduino.h> 00070 #include<mbed.h> 00071 //#include <Streaming.h> 00072 00073 //#include "TTSDisplay.h" 00074 00075 //0~9,A,b,C,d,E,F,"-"," " 00076 const uchar TubeTab[] = 00077 { 00078 0x3f,0x06,0x5b,0x4f, 00079 0x66,0x6d,0x7d,0x07, 00080 0x7f,0x6f,0x77,0x7c, 00081 0x39,0x5e,0x79,0x71, 00082 0x40,0x00 00083 }; 00084 00085 //#define PINCLK 7 // pin of clk 00086 //#define PINDTA 8 // pin of data 00087 00088 TTSDisplay::TTSDisplay() 00089 { 00090 DigitalOut Clkpin(D7); 00091 DigitalOut Datapin(D8); 00092 /*Clkpin = PINCLK; 00093 Datapin = PINDTA; 00094 00095 pinMode(Clkpin,OUTPUT); 00096 pinMode(Datapin,OUTPUT);*/ 00097 00098 for(int i=0; i<4; i++) 00099 { 00100 dtaDisplay[i] = 0x00; 00101 } 00102 00103 set(); 00104 //clear(); 00105 } 00106 00107 /********************************************************************************************************* 00108 * Function Name: display 00109 * Description: display a num in certain location 00110 * Parameters: loca - location, 3-2-1-0 00111 * num - number to display 00112 * Return: None 00113 *********************************************************************************************************/ 00114 void TTSDisplay::display(uchar loca, uchar dta) 00115 { 00116 00117 //if(loca > 3 || loca < 0) return; 00118 00119 if(loca > 3) return; 00120 dtaDisplay[loca] = dta; 00121 00122 loca = 3-loca; 00123 00124 uchar segData = coding(dta); 00125 00126 start(); //start signal sent to TM1637 from MCU 00127 writeByte(ADDR_FIXED); 00128 stop(); 00129 00130 start(); 00131 writeByte(loca|0xc0); 00132 writeByte(segData); 00133 stop(); 00134 00135 start(); 00136 writeByte(Cmd_Dispdisplay); 00137 stop(); 00138 00139 } 00140 00141 /********************************************************************************************************* 00142 * Function Name: num 00143 * Description: display a num that 0 - 9999 00144 * Parameters: num - number to display 00145 * Return: None 00146 *********************************************************************************************************/ 00147 void TTSDisplay::num(int dta) 00148 { 00149 if(dta < 0 || dta > 9999) return; // bad data 00150 00151 //clear(); 00152 00153 pointOff(); 00154 if(dta < 10) 00155 { 00156 display(0, dta); 00157 display(1, 0x7f); 00158 display(2, 0x7f); 00159 display(3, 0x7f); 00160 } 00161 else if(dta < 100) 00162 { 00163 display(1, dta/10); 00164 display(0, dta%10); 00165 display(2, 0x7f); 00166 display(3, 0x7f); 00167 } 00168 else if(dta < 1000) 00169 { 00170 display(2, dta/100); 00171 display(1, (dta/10)%10); 00172 display(0, dta%10); 00173 display(3, 0x7f); 00174 } 00175 else 00176 { 00177 display(3, dta/1000); 00178 display(2, (dta/100)%10); 00179 display(1, (dta/10)%10); 00180 display(0, dta%10); 00181 } 00182 } 00183 00184 /********************************************************************************************************* 00185 * Function Name: time 00186 * Description: display time 00187 * Parameters: hour - hour 00188 * min - minutes 00189 * Return: None 00190 *********************************************************************************************************/ 00191 void TTSDisplay::time(uchar hour, uchar min) 00192 { 00193 // if(hour > 24 || hour < 0) return; // bad data 00194 if(hour > 24) return; // bad data 00195 // if(min > 60 || min < 0) return; // bad data 00196 if(min > 60) return; // bad data 00197 00198 00199 display(3, hour/10); 00200 display(2, hour%10); 00201 display(1, min/10); 00202 display(0, min%10); 00203 } 00204 00205 /********************************************************************************************************* 00206 * Function Name: clear 00207 * Description: clear all 00208 * Parameters: None 00209 * Return: None 00210 *********************************************************************************************************/ 00211 void TTSDisplay::clear() 00212 { 00213 display(0x00,0x7f); 00214 display(0x01,0x7f); 00215 display(0x02,0x7f); 00216 display(0x03,0x7f); 00217 } 00218 00219 /********************************************************************************************************* 00220 * Function Name: writeByte 00221 * Description: write a byte to tm1636 00222 * Parameters: wr_data: data to write 00223 * Return: None 00224 *********************************************************************************************************/ 00225 void TTSDisplay::writeByte(uchar wr_data) 00226 { 00227 uchar i,count1; 00228 for(i=0;i<8;i++) // sent 8bit data 00229 { 00230 Clkpin=0; 00231 //digitalWrite(Clkpin,LOW); 00232 //if(wr_data & 0x01)digitalWrite(Datapin,HIGH); // LSB first 00233 if(wr_data & 0x01)Datapin=1; // LSB first 00234 //else digitalWrite(Datapin,LOW); 00235 else Datapin=0; 00236 wr_data >>= 1; 00237 Clkpin=1; 00238 //digitalWrite(Clkpin,HIGH); 00239 } 00240 00241 Clkpin=0; 00242 Datapin=1; 00243 Clkpin=1; 00244 DigitalIn Datapin(D8); 00245 00246 /*digitalWrite(Clkpin,LOW); // wait for the ACK 00247 digitalWrite(Datapin,HIGH); 00248 digitalWrite(Clkpin,HIGH); 00249 pinMode(Datapin,INPUT);*/ 00250 00251 //while(digitalRead(Datapin)) 00252 while(Datapin) 00253 { 00254 count1 += 1; 00255 if(200 == count1) 00256 { 00257 //pinMode(Datapin,OUTPUT); 00258 DigitalOut Datapin(D8); 00259 //digitalWrite(Datapin,LOW); 00260 Datapin=0; 00261 count1 =0; 00262 } 00263 //pinMode(Datapin,INPUT); 00264 DigitalIn Datapin(D8); 00265 } 00266 //pinMode(Datapin,OUTPUT); 00267 //DigitalOut Datapin(D8); 00268 00269 } 00270 00271 /********************************************************************************************************* 00272 * Function Name: start 00273 * Description: send start signal to TTSDisplay 00274 * Parameters: None 00275 * Return: None 00276 *********************************************************************************************************/ 00277 void TTSDisplay::start(void) 00278 { 00279 /*digitalWrite(Clkpin,HIGH); //send start signal to TM1637 00280 digitalWrite(Datapin,HIGH); 00281 digitalWrite(Datapin,LOW); 00282 digitalWrite(Clkpin,LOW);*/ 00283 Clkpin=1; 00284 Datapin=1; 00285 Datapin=0; 00286 Clkpin=0; 00287 } 00288 00289 /********************************************************************************************************* 00290 * Function Name: stop 00291 * Description: send end signal 00292 * Parameters: None 00293 * Return: None 00294 *********************************************************************************************************/ 00295 void TTSDisplay::stop(void) 00296 {/* 00297 digitalWrite(Clkpin,LOW); 00298 digitalWrite(Datapin,LOW); 00299 digitalWrite(Clkpin,HIGH); 00300 digitalWrite(Datapin,HIGH);*/ 00301 Clkpin=0; 00302 Datapin=0; 00303 Clkpin=1; 00304 Datapin=1; 00305 } 00306 00307 /********************************************************************************************************* 00308 * Function Name: set 00309 * Description: init 00310 * Parameters: brightness - brightness 00311 * SetDta - data 00312 * SetAddr - address 00313 * Return: None 00314 *********************************************************************************************************/ 00315 void TTSDisplay::set(uchar brightness,uchar SetData,uchar SetAddr) 00316 { 00317 _brightness = brightness; 00318 Cmd_SetData = SetData; 00319 Cmd_SetAddr = SetAddr; 00320 Cmd_Dispdisplay = 0x88 + brightness; 00321 } 00322 00323 /********************************************************************************************************* 00324 * Function Name: pointOn 00325 * Description: display : 00326 * Parameters: None 00327 * Return: None 00328 *********************************************************************************************************/ 00329 void TTSDisplay::pointOn() 00330 { 00331 _PointFlag = 1; 00332 00333 for(int i=0; i<4; i++) 00334 { 00335 display(i, dtaDisplay[i]); 00336 } 00337 } 00338 00339 /********************************************************************************************************* 00340 * Function Name: pointOff 00341 * Description: no : 00342 * Parameters: None 00343 * Return: None 00344 *********************************************************************************************************/ 00345 void TTSDisplay::pointOff() 00346 { 00347 _PointFlag = 0; 00348 00349 for(int i=0; i<4; i++) 00350 { 00351 display(i, dtaDisplay[i]); 00352 } 00353 } 00354 00355 /********************************************************************************************************* 00356 * Function Name: coding 00357 * Description: coding 00358 * Parameters: None 00359 * Return: None 00360 *********************************************************************************************************/ 00361 uchar TTSDisplay::coding(uchar DispData) 00362 { 00363 00364 uchar PointData = _PointFlag ? 0x80 : 0x00; 00365 DispData = (0x7f == DispData) ? PointData : (TubeTab[DispData]+PointData); 00366 return DispData; 00367 } 00368 00369 00370 /********************************************************************************************************* 00371 END FILE 00372 *********************************************************************************************************/ 00373 00374 00375 //#include <Wire.h> 00376 //#include <TTSDisplay.h> 00377 00378 00379 //TTSDisplay disp; // instantiate an object 00380 00381 00382 /*void setup() 00383 { 00384 // nothing to setup 00385 }*/ 00386 00387 int main() 00388 { 00389 TTSDisplay disp; 00390 while(1) 00391 { 00392 disp.time(11,26); // display time, 11:26 00393 disp.pointOn(); // display : 00394 wait_ms(1000); // wait 1s 00395 } 00396 } 00397 00398 /********************************************************************************************************* 00399 END FILE 00400 *********************************************************************************************************/
Generated on Mon Jul 18 2022 01:32:22 by
1.7.2