main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. ******************************************************************************
  3. * @file Project/main.c
  4. * @author MCD Application Team
  5. * @version V2.3.0
  6. * @date 16-June-2017
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  12. *
  13. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14. * You may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at:
  16. *
  17. * http://www.st.com/software_license_agreement_liberty_v2
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. ******************************************************************************
  26. */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "stm8s.h"
  29. #include "hw_init.h"
  30. #include "sc7a20.h"
  31. #include "key.h"
  32. #include "led.h"
  33. #include "tasks.h"
  34. /* Private defines -----------------------------------------------------------*/
  35. uint32_t Global_Timing_Counter;
  36. uint8_t PeriodCnt_5ms;
  37. uint8_t PeriodCnt_20ms;
  38. uint8_t PeriodCnt_100ms;
  39. uint16_t AD_Value;
  40. uint8_t AD_FinishFlag = 0;
  41. /* Private function prototypes -----------------------------------------------*/
  42. /* Private functions ---------------------------------------------------------*/
  43. void main(void)
  44. {
  45. /* Hardware Init */
  46. HW_Init();
  47. SC7A20_Init();
  48. /* Task Init */
  49. Global_Timing_Counter = 0U;
  50. PeriodCnt_5ms = 0;
  51. PeriodCnt_20ms = 0;
  52. PeriodCnt_100ms = 0;
  53. /* Infinite loop */
  54. while (1)
  55. {
  56. //5ms
  57. if(PeriodCnt_5ms >= 5)
  58. {
  59. PeriodCnt_5ms = 0;
  60. taskA(); //按键扫描
  61. }
  62. //10ms
  63. if(PeriodCnt_20ms >= 20)
  64. {
  65. PeriodCnt_20ms = 0;
  66. taskB(); //加速度读取
  67. }
  68. //100ms
  69. if(PeriodCnt_100ms >= 100)
  70. {
  71. PeriodCnt_100ms = 0;
  72. taskC(); //AD采集,电量指示灯
  73. }
  74. }
  75. }
  76. INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
  77. {
  78. /* In order to detect unexpected events during development,
  79. it is recommended to set a breakpoint on the following instruction.
  80. */
  81. if(Global_Timing_Counter == 0x9A7EC800) //30days
  82. {
  83. Global_Timing_Counter = 0U;
  84. }
  85. else
  86. {
  87. Global_Timing_Counter++;
  88. }
  89. PeriodCnt_5ms++;
  90. PeriodCnt_20ms++;
  91. PeriodCnt_100ms++;
  92. Tim4_Isr_CallBack();
  93. TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
  94. }
  95. INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
  96. {
  97. /* In order to detect unexpected events during development,
  98. it is recommended to set a breakpoint on the following instruction.
  99. */
  100. ADC1->CSR &= ~0x80; // 转换结束标志位清零 EOC
  101. //默认左对齐 读数据时先读高高8位 再读低8位
  102. AD_Value = (ADC1->DRH << 2);
  103. AD_Value += ADC1->DRL;
  104. AD_FinishFlag = 1; // ADC中断标志 置1
  105. }
  106. #ifdef USE_FULL_ASSERT
  107. /**
  108. * @brief Reports the name of the source file and the source line number
  109. * where the assert_param error has occurred.
  110. * @param file: pointer to the source file name
  111. * @param line: assert_param error line source number
  112. * @retval : None
  113. */
  114. void assert_failed(u8* file, u32 line)
  115. {
  116. /* User can add his own implementation to report the file name and line number,
  117. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  118. /* Infinite loop */
  119. while (1)
  120. {
  121. }
  122. }
  123. #endif
  124. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/