led.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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[8] = {1, 2, 3, 4, 5, 6, 7, 8}; // digits to dsplay
  10. uint8_t LedPoint[8] = {0, 0, 0, 1, 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 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. default:
  55. LED_OUT_MM;
  56. }
  57. /*
  58. if(LedPoint[ledn] != 0) {
  59. LED_OUT_DP;
  60. }
  61. */
  62. ledn ++;
  63. if (ledn > 7) {
  64. ledn = 0;
  65. }
  66. }
  67. /*
  68. * Select LED Digit.
  69. * If first != 0 - select next, by shift '1' on outputs,
  70. * else select first digit.
  71. * led digits sequence (b - bottom, t - top):
  72. * b1, b2, b4, b3, t1, t2, t4, t3.
  73. */
  74. static void led_SelectDigit (const uint8_t first) {
  75. uint8_t i;
  76. uint8_t data = led_num[first];
  77. for (i=0; i<8; i++) {
  78. GPIO_LOW(SPI_PORT, SPI_SCK); // prepare CLK
  79. if (data & 0x80) { // if msb == 1
  80. GPIO_HIGH(SPI_PORT, SPI_DATA); // DATA = 1
  81. } else {
  82. GPIO_LOW(SPI_PORT, SPI_DATA); // DATA = 0
  83. }
  84. GPIO_HIGH(SPI_PORT, SPI_SCK); // shift bit
  85. data <<= 1;
  86. }
  87. }