tm1637.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * tm1637.c
  3. *
  4. * Copyright 2022 dh33ex <dh33ex@riseup.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301, USA or see <http://www.gnu.org/licenses/>.
  20. *
  21. *
  22. */
  23. #ifndef __msp430_h_
  24. #include <msp430.h>
  25. #endif
  26. #include "tm1637.h"
  27. static const unsigned char digit_HEX[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x00};
  28. void TM1637_start(TM1637 *disp);
  29. void TM1637_stop(TM1637 *disp);
  30. unsigned char TM1637_send_byte(TM1637 *disp, unsigned char data);
  31. void TM1637_send_array(TM1637 *disp, unsigned char *send_data);
  32. void TM1637_init(TM1637 *disp) {
  33. P1DIR |= disp->clk;
  34. P1DIR |= disp->dio;
  35. }
  36. void TM1637_display(TM1637 *disp, unsigned char disp_data[4]) {
  37. unsigned char seg_data[4];
  38. unsigned char i;
  39. for (i = 0; i < 4; i++) {
  40. seg_data[i] = digit_HEX[disp_data[i]] + 0x80 * disp->colon;
  41. }
  42. TM1637_send_array(disp, seg_data);
  43. }
  44. void TM1637_send_array(TM1637 *disp, unsigned char send_data[]) {
  45. TM1637_start(disp);
  46. TM1637_send_byte(disp, 0x40);
  47. TM1637_stop(disp);
  48. TM1637_start(disp);
  49. TM1637_send_byte(disp, 0xC0);
  50. unsigned char i;
  51. for (i = 0; i < 4; i++) {
  52. TM1637_send_byte(disp, send_data[i]);
  53. }
  54. TM1637_stop(disp);
  55. TM1637_start(disp);
  56. TM1637_send_byte(disp, 0x88 + disp->brightness);
  57. TM1637_stop(disp);
  58. }
  59. void TM1637_start(TM1637 *disp) {
  60. P1OUT |= disp->clk;
  61. P1OUT |= disp->dio;
  62. P1OUT &= ~disp->dio;
  63. P1OUT &= ~disp->clk;
  64. }
  65. void TM1637_stop(TM1637 *disp) {
  66. P1OUT &= ~disp->clk;
  67. P1OUT &= ~disp->dio;
  68. P1OUT |= disp->clk;
  69. P1OUT |= disp->dio;
  70. }
  71. unsigned char TM1637_send_byte(TM1637 *disp, unsigned char data) {
  72. unsigned char i;
  73. for (i = 0; i < 8; i++) {
  74. P1OUT &= ~disp->clk;
  75. if (data & 0x01) {
  76. P1OUT |= disp->dio;
  77. } else {
  78. P1OUT &= ~disp->dio;
  79. }
  80. data >>= 1;
  81. P1OUT |= disp->clk;
  82. }
  83. P1OUT &= ~disp->clk;
  84. P1OUT |= disp->dio;
  85. P1OUT |= disp->clk;
  86. P1DIR &= ~disp->dio;
  87. __delay_cycles(1000);
  88. unsigned char ack = P1IN & disp->dio;
  89. if (ack == 0) {
  90. P1DIR |= disp->dio;
  91. P1OUT &= ~disp->dio;
  92. }
  93. __delay_cycles(1000);
  94. P1DIR |= disp->dio;
  95. __delay_cycles(1000);
  96. return ack;
  97. }