led.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Драйвер модуля LED-индикаторов
  3. */
  4. #include "stm8s.h"
  5. #include "led.h"
  6. #define GPIO_HIGH(a,b) a->ODR |= b
  7. #define GPIO_LOW(a,b) a->ODR &= ~b
  8. #define GPIO_TOGGLE(a,b) a->ODR ^= b
  9. uint8_t LedDigits[LED_DIGITS_NUM] = {0xff}; // digits to dsplay
  10. uint8_t LedPoint[LED_DIGITS_NUM] = {0, 1, 0, 0, 0, 0, 0, 1}; // dots for digits
  11. /* 1 2 3 4 5 6 7 8 */
  12. static const uint8_t led_num[LED_DIGITS_NUM] = {0x08, 0x04, 0x02, 0x01, 0x10, 0x20, 0x40, 0x80};
  13. static void led_SelectDigit (const uint8_t pos);
  14. /*
  15. * Output current value to next led
  16. */
  17. void led_OutputValue(void) {
  18. static uint8_t ledn = 0;
  19. /* all off */
  20. LED_OUT_OFF;
  21. /* Fire on nex led */
  22. led_SelectDigit(ledn);
  23. /* out next value */
  24. switch (LedDigits[ledn]) {
  25. case 0:
  26. LED_OUT_0;
  27. break;
  28. case 1:
  29. LED_OUT_1;
  30. break;
  31. case 2:
  32. LED_OUT_2;
  33. break;
  34. case 3:
  35. LED_OUT_3;
  36. break;
  37. case 4:
  38. LED_OUT_4;
  39. break;
  40. case 5:
  41. LED_OUT_5;
  42. break;
  43. case 6:
  44. LED_OUT_6;
  45. break;
  46. case 7:
  47. LED_OUT_7;
  48. break;
  49. case 8:
  50. LED_OUT_8;
  51. break;
  52. case 9:
  53. LED_OUT_9;
  54. break;
  55. case led_A:
  56. LED_OUT_A;
  57. break;
  58. case led_B:
  59. LED_OUT_B;
  60. break;
  61. case led_C:
  62. LED_OUT_C;
  63. break;
  64. case led_D:
  65. LED_OUT_D;
  66. break;
  67. case led_E:
  68. LED_OUT_E;
  69. break;
  70. case led_F:
  71. LED_OUT_F;
  72. break;
  73. case led_H:
  74. LED_OUT_H;
  75. break;
  76. case led_U:
  77. LED_OUT_U;
  78. break;
  79. case led_Plus:
  80. LED_OUT_PL;
  81. break;
  82. case led_Minus:
  83. LED_OUT_MM;
  84. break;
  85. case led_O:
  86. LED_OUT_O;
  87. break;
  88. case led_Off:
  89. break;
  90. default:
  91. LED_OUT_MM;
  92. }
  93. if(LedPoint[ledn] != 0) {
  94. LED_OUT_DP;
  95. }
  96. ledn ++;
  97. if (ledn >= LED_DIGITS_NUM) {
  98. ledn = 0;
  99. }
  100. }
  101. /*
  102. * Select LED Digit.
  103. * led digits sequence (b - bottom, t - top):
  104. * t1, t2, t4, t3, b1, b2, b3, b4.
  105. */
  106. static void led_SelectDigit (const uint8_t pos) {
  107. uint8_t i;
  108. uint8_t data = led_num[pos];
  109. for (i=0; i<8; i++) {
  110. GPIO_LOW(SPI_PORT, SPI_SCK); // prepare CLK
  111. if (data & 0x80) { // if msb == 1
  112. GPIO_HIGH(SPI_PORT, SPI_DATA); // DATA = 1
  113. } else {
  114. GPIO_LOW(SPI_PORT, SPI_DATA); // DATA = 0
  115. }
  116. GPIO_HIGH(SPI_PORT, SPI_SCK); // shift bit
  117. data <<= 1;
  118. }
  119. /* hack for LATCH */
  120. GPIO_LOW(SPI_PORT, SPI_DATA);
  121. GPIO_LOW(SPI_PORT, SPI_SCK);
  122. GPIO_HIGH(SPI_PORT, SPI_SCK);
  123. }