led_blink.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "stm32l1xx_hal.h"
  2. #include "stm32l1xx_hal_gpio.h"
  3. unsigned int blinkLed = 0;
  4. unsigned int blinkTxLed = 0;
  5. // Interface function to blink the green LED
  6. void blinkTheLED() {
  7. blinkLed++;
  8. }
  9. // Interface function to blink the blue LED
  10. void blinkTheTxLED() {
  11. blinkTxLed++;
  12. }
  13. enum {
  14. MODE_INACTIVE,
  15. MODE_ON,
  16. MODE_OFF,
  17. MODE_SWITCH, // Just have been off
  18. };
  19. void rxLed() {
  20. static unsigned long timer = 0;
  21. static int mode = MODE_INACTIVE;
  22. switch( mode ) {
  23. case MODE_INACTIVE:
  24. if( blinkLed > 0 ) {
  25. blinkLed--;
  26. mode = MODE_ON;
  27. timer = 1;
  28. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,(GPIO_PinState)1);
  29. }
  30. break;
  31. case MODE_ON:
  32. timer--;
  33. if( timer == 0 ) {
  34. mode = MODE_OFF;
  35. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,(GPIO_PinState)0);
  36. timer = 1;
  37. }
  38. break;
  39. case MODE_OFF:
  40. timer--;
  41. if( timer == 0 ) {
  42. mode = MODE_INACTIVE;
  43. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,(GPIO_PinState)0);
  44. break;
  45. }
  46. }
  47. }
  48. void txLed() {
  49. static unsigned long timer = 0;
  50. static int mode = MODE_INACTIVE;
  51. switch( mode ) {
  52. case MODE_INACTIVE:
  53. if( blinkTxLed > 0 ) {
  54. blinkTxLed--;
  55. mode = MODE_ON;
  56. timer = 1;
  57. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,(GPIO_PinState)1);
  58. }
  59. break;
  60. case MODE_ON:
  61. timer--;
  62. if( timer == 0 ) {
  63. mode = MODE_OFF;
  64. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,(GPIO_PinState)0);
  65. timer = 1;
  66. }
  67. break;
  68. case MODE_OFF:
  69. timer--;
  70. if( timer == 0 ) {
  71. mode = MODE_INACTIVE;
  72. HAL_GPIO_WritePin(GPIOB,GPIO_PIN_6,(GPIO_PinState)0);
  73. break;
  74. }
  75. }
  76. }
  77. // 10Hz timer interrupt function
  78. void led_interrupt(){
  79. rxLed();
  80. txLed();
  81. }