使用51芯片来驱动TM1650芯片
    使用该芯片应注意LED灯需共阴极的数码管,共阳极无法正常使用
    image.png

    1. /*****************************************************************************
    2. *版权信息:
    3. *文 件 名:
    4. *当前版本:V1.0
    5. *MCU 型号:STC12C5608AD
    6. *开发环境:Keil uVision4
    7. *晶震频率:11.0592MHZ
    8. *完成日期:2013-07-29
    9. *程序功能:1.上电8段4位共阴数码管显示1、2、3、4.
    10. 2.按下K11与DIG1,K12与DIG2 ,K13与DIG3之间的按键,数码管第一位分别显示5、6、7。
    11. *免责声明:
    12. ********************************************************************************/
    13. #include<reg52.h> //MCU头文件
    14. #include<intrins.h> //包含nop指令头文件
    15. #define uint unsigned int //数据类型宏定义
    16. #define uchar unsigned char //数据类型宏定义
    17. #define nop _nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); //宏定义
    18. /********************定义控制端口**********************/
    19. sbit SCL=P2^1; //时钟线
    20. sbit SDA=P2^0; //数据线
    21. uchar keya; //定义读出按键返回值
    22. /*************1ms延时*晶振11.0592M********************/
    23. void delay(uint n)
    24. {
    25. uint i;
    26. while(n--)
    27. for(i=0;i<550;i++);
    28. }
    29. /**************共阴数码管显示0-F**********************/
    30. uchar display[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}; //共阴极字段码
    31. /************ START信号*******************************/
    32. void FZH110_START()
    33. {
    34. SCL=1;
    35. SDA=1;
    36. nop;
    37. SDA=0;
    38. nop;
    39. SCL=0;
    40. }
    41. /******************** STOP信号************************/
    42. void FZH110_STOP()
    43. {
    44. SDA=0;
    45. nop;
    46. SCL=1;
    47. nop;
    48. SDA=1;
    49. nop;
    50. SCL=0;
    51. SDA=0;
    52. }
    53. /****************写1个字节给FZH110********************/
    54. void write_8bit( uchar dat)
    55. {
    56. uchar i;
    57. SCL=0;
    58. for(i=0;i<8;i++)
    59. {
    60. if(dat&0x80)
    61. {
    62. SDA=1;
    63. nop;
    64. nop;
    65. SCL=1;
    66. nop;
    67. nop;
    68. nop;
    69. nop;
    70. nop;
    71. SCL=0;
    72. }
    73. else
    74. {
    75. SDA=0;
    76. nop;
    77. nop;
    78. SCL=1;
    79. nop;
    80. nop;
    81. nop;
    82. nop;
    83. nop;
    84. SCL=0;
    85. }
    86. dat<<=1;
    87. }
    88. SDA=1; //ACK信号
    89. nop;
    90. nop;
    91. nop;
    92. nop;
    93. SCL=1;
    94. nop;
    95. nop;
    96. nop;
    97. nop;
    98. nop;
    99. SCL=0;
    100. nop;
    101. nop;
    102. }
    103. /**********************读8bit**************************/
    104. uchar read_8bit()
    105. {
    106. uchar dat,i;
    107. SDA=1;
    108. dat=0;
    109. for(i=0;i<8;i++)
    110. {
    111. SCL=1; //时钟上沿
    112. nop;
    113. nop;
    114. nop;
    115. dat<<=1;
    116. if(SDA)
    117. dat++;
    118. SCL=0;
    119. nop;
    120. nop;
    121. nop;
    122. nop;
    123. }
    124. SDA=0; //ACK信号
    125. nop;
    126. nop;
    127. nop;
    128. SCL=1;
    129. nop;
    130. nop;
    131. nop;
    132. nop;
    133. SCL=0;
    134. nop;
    135. return dat ;
    136. }
    137. /*******************读按键命令************************/
    138. uchar FZH110_read()
    139. {
    140. uchar key;
    141. FZH110_START();
    142. write_8bit(0x4F);//读按键指令
    143. key=read_8bit();
    144. FZH110_STOP();
    145. return key;
    146. }
    147. /*****************发送命令信号***********************/
    148. void FZH110_send(uchar date1,uchar date2)
    149. {
    150. FZH110_START();
    151. write_8bit(date1);
    152. write_8bit(date2);
    153. FZH110_STOP();
    154. }
    155. /*****************显示函数***********************/
    156. void disp()
    157. {
    158. FZH110_send(0x48,0x01); // 开启显示模式:8段显示,1级亮度
    159. FZH110_send(0X68,display[1]); //GID1
    160. FZH110_send(0X6A,display[2]); //GID2
    161. FZH110_send(0X6C,display[3]); //GID3
    162. FZH110_send(0X6E,display[4]); //GID4
    163. }
    164. /**************主函数**************************/
    165. void main(void)
    166. {
    167. disp(); //上电显示1、2、3、4
    168. delay(1);
    169. while(1)
    170. {
    171. keya=FZH110_read();
    172. delay(50);
    173. if (keya!=0)
    174. {
    175. switch(keya) //控制数码管显示数字
    176. {
    177. case 0x44:FZH110_send(0X68,display[5]); break; //K11与DIG1
    178. case 0x4D:FZH110_send(0X68,display[6]); break; //K12与DIG2
    179. case 0x56:FZH110_send(0X68,display[7]); break; //K13与DIG3
    180. default: break;
    181. }
    182. }
    183. }
    184. }