TM1650.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. www.makerstudio.cc
  3. */
  4. #include "TM1650.h"
  5. #include <Arduino.h>
  6. static int8_t Cmd_Addr[] = {0x68,0x6A,0x6C,0x6E};//LED address
  7. TM1650::TM1650(uint8_t Data, uint8_t Clk)
  8. {
  9. Clkpin = Clk;
  10. Datapin = Data;
  11. }
  12. void TM1650::begin(void)
  13. {
  14. pinMode(Clkpin,OUTPUT);
  15. pinMode(Datapin,OUTPUT);
  16. clearDisplay();
  17. }
  18. //read and output key value
  19. uint8_t TM1650::readKey()
  20. {
  21. uint8_t keyCode = 0;
  22. start();
  23. writeByte(READ_KEY_CMD);
  24. keyCode = readByte();
  25. stop();
  26. return keyCode;
  27. }
  28. //display function.Write to full-screen.
  29. void TM1650::display(uint8_t DispData[])
  30. {
  31. start(); //start signal sent to TM1650 from MCU
  32. writeByte(MODE_CMD);//write command for display
  33. writeByte(BRIGHT_TYPICAL);//set light grade and open display
  34. for(int i=0;i < 4;i ++){
  35. storeDat[i] = DispData[i];
  36. stop(); //
  37. start(); //
  38. writeByte(Cmd_Addr[i]);//
  39. writeByte(storeDat[i]); //
  40. stop(); //
  41. start(); //
  42. }
  43. }
  44. //******************************************
  45. void TM1650::display(uint8_t BitAddr,uint8_t DispData)
  46. {
  47. if(BitAddr >=0 && BitAddr <= 3 ){
  48. storeDat[BitAddr] = DispData;
  49. }
  50. start(); //start signal sent to TM1650 from MCU
  51. writeByte(MODE_CMD);//write command for display
  52. writeByte(BRIGHT_TYPICAL);//set light grade and open display
  53. stop(); //
  54. start(); //
  55. writeByte(Cmd_Addr[BitAddr]);//
  56. writeByte(storeDat[BitAddr]); //
  57. stop(); //
  58. start(); //
  59. }
  60. //whether to light the point in different places
  61. void TM1650::setPoint(uint8_t PointPosition,boolean PointFlag)
  62. {
  63. if(PointPosition >=0 && PointPosition <= 3 ){
  64. if(PointFlag == 1){
  65. storeDat[PointPosition] |= 0x80;
  66. }else if(PointFlag == 0){
  67. storeDat[PointPosition] &= 0x7F;
  68. }
  69. display(PointPosition,storeDat[PointPosition]);
  70. }
  71. }
  72. void TM1650::clearDisplay(void)
  73. {
  74. start(); //start signal sent to TM1650 from MCU
  75. writeByte(MODE_CMD);//write command for display
  76. writeByte(BRIGHT_OFF);//set light grade and open display
  77. for(int i=0;i < 4;i ++){
  78. storeDat[i] = 0x00;
  79. stop(); //
  80. start(); //
  81. writeByte(Cmd_Addr[i]);//
  82. writeByte(storeDat[i]); //
  83. stop(); //
  84. start();
  85. }
  86. }
  87. void TM1650::clearDisplay(uint8_t BitAddr)
  88. {
  89. if(BitAddr >=0 && BitAddr <= 3){
  90. storeDat[BitAddr] &= 0x80;
  91. }
  92. start(); //start signal sent to TM1650 from MCU
  93. writeByte(MODE_CMD);//write command for display
  94. writeByte(BRIGHT_OFF);//set light grade and open display
  95. stop(); //
  96. start(); //
  97. writeByte(Cmd_Addr[BitAddr]);//
  98. writeByte(storeDat[BitAddr]); //
  99. stop(); //
  100. start(); //
  101. }
  102. void TM1650::writeByte(uint8_t wr_data)
  103. {
  104. uint8_t i,count1;
  105. for(i=0;i<8;i++) //sent 8bit data
  106. {
  107. digitalWrite(Clkpin,LOW);
  108. if(wr_data & 0x80)digitalWrite(Datapin,HIGH);//MSB first
  109. else digitalWrite(Datapin,LOW);
  110. wr_data <<= 1;
  111. digitalWrite(Clkpin,HIGH);
  112. }
  113. digitalWrite(Clkpin,LOW);
  114. pinMode(Datapin,INPUT);
  115. while(digitalRead(Datapin)); //wait for the ACK
  116. digitalWrite(Clkpin,HIGH);
  117. pinMode(Datapin,OUTPUT);
  118. }
  119. uint8_t TM1650::readByte()//read 8bit from to TM1650
  120. {
  121. uint8_t rd_data=0;
  122. uint8_t i,count1;
  123. uint8_t temp = 0x80;
  124. pinMode(Datapin,INPUT);
  125. digitalWrite(Clkpin,LOW);
  126. for(i=0;i<8;i++) //read 8bit data
  127. {
  128. digitalWrite(Clkpin,HIGH);
  129. if(digitalRead(Datapin))//MSB first
  130. {
  131. rd_data |= temp;
  132. }
  133. temp >>=1;
  134. digitalWrite(Clkpin,LOW);
  135. }
  136. digitalWrite(Clkpin,LOW); //wait for the ACK
  137. pinMode(Datapin,INPUT);
  138. digitalWrite(Clkpin,HIGH);
  139. return rd_data;
  140. }
  141. //send start signal to TM1650
  142. void TM1650::start(void)
  143. {
  144. pinMode(Datapin,OUTPUT);
  145. digitalWrite(Clkpin,HIGH);//send start signal to TM1650
  146. digitalWrite(Datapin,HIGH);
  147. digitalWrite(Datapin,LOW);
  148. digitalWrite(Clkpin,LOW);
  149. }
  150. //End of transmission
  151. void TM1650::stop(void)
  152. {
  153. pinMode(Datapin,OUTPUT);
  154. digitalWrite(Clkpin,LOW);
  155. digitalWrite(Datapin,LOW);
  156. digitalWrite(Clkpin,HIGH);
  157. digitalWrite(Datapin,HIGH);
  158. }