led.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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, 1, 0, 0}; // 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 first);
  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. case led_Plus:
  55. LED_OUT_PL;
  56. break;
  57. case led_Minus:
  58. LED_OUT_MM;
  59. break;
  60. case led_H:
  61. LED_OUT_H;
  62. break;
  63. case led_O:
  64. LED_OUT_O;
  65. break;
  66. case led_Off:
  67. break;
  68. default:
  69. GPIOD->ODR &= ~(LED_SEG_D);
  70. }
  71. if(LedPoint[ledn] != 0) {
  72. LED_OUT_DP;
  73. }
  74. ledn ++;
  75. if (ledn >= LED_DIGITS_NUM) {
  76. ledn = 0;
  77. }
  78. }
  79. /*
  80. * Select LED Digit.
  81. * If first != 0 - select next, by shift '1' on outputs,
  82. * else select first digit.
  83. * led digits sequence (b - bottom, t - top):
  84. * b1, b2, b4, b3, t1, t2, t4, t3.
  85. */
  86. static void led_SelectDigit (const uint8_t first) {
  87. uint8_t i;
  88. uint8_t data = led_num[first];
  89. for (i=0; i<8; i++) {
  90. GPIO_LOW(SPI_PORT, SPI_SCK); // prepare CLK
  91. if (data & 0x80) { // if msb == 1
  92. GPIO_HIGH(SPI_PORT, SPI_DATA); // DATA = 1
  93. } else {
  94. GPIO_LOW(SPI_PORT, SPI_DATA); // DATA = 0
  95. }
  96. GPIO_HIGH(SPI_PORT, SPI_SCK); // shift bit
  97. data <<= 1;
  98. }
  99. }