led.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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] = {1, 2, 3, 4, 5, 6, 7, 8}; // digits to dsplay
  10. uint8_t LedPoint[LED_DIGITS_NUM] = {0, 1, 0, 0, 0, 0, 0, 1}; // dots for digits
  11. static const uint16_t led_num[8] = {0x10, 0x20, 0x80, 0x40, 0x01, 0x02, 0x08, 0x04};
  12. static void led_SelectDigit (const uint8_t pos);
  13. /*
  14. * Output current value to next led
  15. */
  16. void led_OutputValue(void) {
  17. static uint8_t ledn = 0;
  18. /* all off */
  19. LED_OUT_OFF;
  20. /* Fire on nex led */
  21. led_SelectDigit(ledn);
  22. /* out next value */
  23. switch (LedDigits[ledn]) {
  24. case 0:
  25. LED_OUT_0;
  26. break;
  27. case 1:
  28. LED_OUT_1;
  29. break;
  30. case 2:
  31. LED_OUT_2;
  32. break;
  33. case 3:
  34. LED_OUT_3;
  35. break;
  36. case 4:
  37. LED_OUT_4;
  38. break;
  39. case 5:
  40. LED_OUT_5;
  41. break;
  42. case 6:
  43. LED_OUT_6;
  44. break;
  45. case 7:
  46. LED_OUT_7;
  47. break;
  48. case 8:
  49. LED_OUT_8;
  50. break;
  51. case 9:
  52. LED_OUT_9;
  53. break;
  54. /*
  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_O:
  83. LED_OUT_O;
  84. break;
  85. case led_Off:
  86. break;
  87. */
  88. case led_Minus:
  89. LED_OUT_MM;
  90. break;
  91. default:
  92. LED_OUT_MM;
  93. }
  94. if(LedPoint[ledn] != 0) {
  95. LED_OUT_DP;
  96. }
  97. ledn ++;
  98. if (ledn >= LED_DIGITS_NUM) {
  99. ledn = 0;
  100. }
  101. }
  102. /*
  103. * Select LED Digit.
  104. * led digits sequence (b - bottom, t - top):
  105. * t1, t2, t4, t3, b1, b2, b3, b4.
  106. */
  107. static void led_SelectDigit (const uint8_t pos) {
  108. uint8_t i;
  109. uint8_t data = led_num[pos];
  110. for (i=0; i<8; i++) {
  111. GPIO_LOW(SPI_PORT, SPI_SCK); // prepare CLK
  112. if (data & 0x80) { // if msb == 1
  113. GPIO_HIGH(SPI_PORT, SPI_DATA); // DATA = 1
  114. } else {
  115. GPIO_LOW(SPI_PORT, SPI_DATA); // DATA = 0
  116. }
  117. GPIO_HIGH(SPI_PORT, SPI_SCK); // shift bit
  118. data <<= 1;
  119. }
  120. }