main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. ******************************************************************************
  3. * @file VAPC-meter
  4. * @author Vladimir N. Shilov <shilow@ukr.net>
  5. * @version V0.0.1
  6. * @date 2015.05.15
  7. * @brief Main program body
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * ----------------------------------------------------------------------------
  12. * "THE BEER-WARE LICENSE" (Revision 42):
  13. * <shilow@ukr.net> wrote this file. As long as you retain this notice you
  14. * can do whatever you want with this stuff. If we meet some day, and you think
  15. * this stuff is worth it, you can buy me a beer in return. Shilov V.N.
  16. * ----------------------------------------------------------------------------
  17. *
  18. ******************************************************************************
  19. */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm8l15x.h"
  22. #include "rtos.h"
  23. #include "max7219.h"
  24. #include "adc.h"
  25. /** @addtogroup STM8L15x_StdPeriph_Template
  26. * @{
  27. */
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* Private define ------------------------------------------------------------*/
  30. #define LED_RED_PORT GPIOC
  31. #define LED_RED_PIN GPIO_Pin_4
  32. #define LED_GREEN_PORT GPIOB
  33. #define LED_GREEN_PIN GPIO_Pin_7
  34. /* Private macro -------------------------------------------------------------*/
  35. #define LED_RED_ON LED_RED_PORT->ODR &= (uint8_t)(~LED_RED_PIN)
  36. #define LED_RED_OFF LED_RED_PORT->ODR |= LED_RED_PIN
  37. #define LED_GREEN_ON LED_GREEN_PORT->ODR &= (uint8_t)(~LED_GREEN_PIN)
  38. #define LED_GREEN_OFF LED_GREEN_PORT->ODR |= LED_GREEN_PIN
  39. /* Private constant ----------------------------------------------------------*/
  40. // перевод числа 0-7 в номер индикатора
  41. static const max7219_reg_t dig[8] = {
  42. Digit0,
  43. Digit1,
  44. Digit2,
  45. Digit3,
  46. Digit4,
  47. Digit5,
  48. Digit6,
  49. Digit7
  50. };
  51. // перевод значения 0x00 - 0x0F в код индикатора
  52. static const max7219_sym_t num[16] = {
  53. Sym_0,
  54. Sym_1,
  55. Sym_2,
  56. Sym_3,
  57. Sym_4,
  58. Sym_5,
  59. Sym_6,
  60. Sym_7,
  61. Sym_8,
  62. Sym_9,
  63. Sym_A,
  64. Sym_b,
  65. Sym_C,
  66. Sym_d,
  67. Sym_E,
  68. Sym_F
  69. };
  70. /* Private variables ---------------------------------------------------------*/
  71. uint16_t Voltage = 0;
  72. uint16_t Current = 0;
  73. uint16_t RefVolt = 0;
  74. extern uint16_t VoltageFastBuffer[];
  75. extern uint16_t CurrentFastBuffer[];
  76. extern uint16_t RefVoltFastBuffer[];
  77. /* Private function prototypes -----------------------------------------------*/
  78. static void GPIO_Config(void);
  79. static void CLK_Config(void);
  80. /* RTOS function prototypes -----------------------------------------------*/
  81. static void ShowTopLineV(void);
  82. static void ShowTopLineC(void);
  83. static void ShowBotLine(void);
  84. static void ProcessFastBuffer(void);
  85. /* Private functions ---------------------------------------------------------*/
  86. /**
  87. * @brief Main program.
  88. * @param None
  89. * @retval None
  90. */
  91. void main(void)
  92. {
  93. /* Clock configuration -----------------------------------------*/
  94. CLK_Config();
  95. /* GPIO Configuration -----------------------------------------*/
  96. GPIO_Config();
  97. /* RTOS Configuration */
  98. RTOS_Init();
  99. /* ADC Configuration and start */
  100. Init_ADC();
  101. /* MAX7219 Configuration */
  102. MAX7219_Init();
  103. /* ROTS tasks */
  104. RTOS_SetTask(ProcessFastBuffer,0,100);
  105. RTOS_SetTask(ShowTopLineV,101,4000);
  106. RTOS_SetTask(ShowTopLineC,2101,4000);
  107. RTOS_SetTask(ShowBotLine,101,100);
  108. /* Infinite loop */
  109. while (1)
  110. {
  111. RTOS_DispatchTask();
  112. Delay(1);
  113. }
  114. }
  115. /*
  116. * Фильтрация и усреднение данных из быстрого буфера,
  117. * вычесление значений, перенос в медленный буфер.
  118. */
  119. static void ProcessFastBuffer(void){
  120. uint16_t volt=0, curr=0, ref=0;
  121. /* Summarize buffers values */
  122. uint8_t i;
  123. for(i=0;i<FAST_BUFFER_SIZE;i++){
  124. volt += VoltageFastBuffer[i];
  125. curr += CurrentFastBuffer[i];
  126. ref += RefVoltFastBuffer[i];
  127. }
  128. /* Calculate voltage value*/
  129. Voltage = ((volt / FAST_BUFFER_SIZE) * ADC_VOLT_RATIO) / 1000;
  130. /* Calculate current value*/
  131. Current = ((curr / FAST_BUFFER_SIZE) * ADC_RATIO) / 1000;
  132. /* Calculate reference voltage value*/
  133. RefVolt = ((ref / FAST_BUFFER_SIZE) * ADC_RATIO) / 1000;
  134. }
  135. /*
  136. * Output to top indicator line
  137. */
  138. static void ShowTopLineV(void){
  139. uint8_t voltage1000 = 0;
  140. uint8_t voltage100 = 0;
  141. uint8_t voltage10 = 0;
  142. uint8_t voltage1 = 0;
  143. /* Thousands voltage value*/
  144. voltage1000 = (uint8_t)(Voltage / 1000);
  145. /* Hundreds voltage value */
  146. voltage100 = (uint8_t)((Voltage % 1000) / 100);
  147. /* Tens voltage value */
  148. voltage10 = (uint8_t)((Voltage % 100 ) / 10);
  149. /* Ones voltage value */
  150. voltage1 = (uint8_t)(Voltage % 10);
  151. LED_RED_ON;
  152. MAX7219_WriteData(dig[0], num[voltage1000]);
  153. MAX7219_WriteData(dig[1], num[voltage100]);
  154. MAX7219_WriteData(dig[2], num[voltage10]);
  155. MAX7219_WriteData(dig[3], num[voltage1]);
  156. }
  157. static void ShowTopLineC(void){
  158. LED_RED_OFF;
  159. uint8_t tmp = Current / 1000;
  160. MAX7219_WriteData(dig[0], num[tmp]);
  161. tmp = (Current % 1000) / 100;
  162. MAX7219_WriteData(dig[1], num[tmp]);
  163. tmp = (Current % 100) / 10;
  164. MAX7219_WriteData(dig[2], num[tmp]);
  165. tmp = Current % 10;
  166. MAX7219_WriteData(dig[3], num[tmp]);
  167. }
  168. /*
  169. * Output to bottom indicator line
  170. */
  171. static void ShowBotLine(void){
  172. uint8_t voltage1000 = 0;
  173. uint8_t voltage100 = 0;
  174. uint8_t voltage10 = 0;
  175. uint8_t voltage1 = 0;
  176. /* Thousands voltage value*/
  177. voltage1000 = (uint8_t)(RefVolt / 1000);
  178. /* Hundreds voltage value */
  179. voltage100 = (uint8_t)((RefVolt % 1000) / 100);
  180. /* Tens voltage value */
  181. voltage10 = (uint8_t)((RefVolt % 100 ) / 10);
  182. /* Ones voltage value */
  183. voltage1 = (uint8_t)(RefVolt % 10);
  184. MAX7219_WriteData(dig[4], num[voltage1000]);
  185. MAX7219_WriteData(dig[5], num[voltage100]);
  186. MAX7219_WriteData(dig[6], num[voltage10]);
  187. MAX7219_WriteData(dig[7], num[voltage1]);
  188. }
  189. /**
  190. * @brief Configure GPIO for button available on the VAPC board
  191. * @param None
  192. * @retval None
  193. */
  194. static void GPIO_Config(void)
  195. {
  196. /* Configure GPIO used to drive LEDs */
  197. GPIO_Init(LED_RED_PORT, LED_RED_PIN, GPIO_Mode_Out_PP_High_Fast);
  198. GPIO_Init(LED_GREEN_PORT, LED_GREEN_PIN, GPIO_Mode_Out_PP_High_Fast);
  199. /* Enable general interrupts */
  200. enableInterrupts();
  201. }
  202. /**
  203. * @brief Configure system clock to run at Maximum clock speed
  204. * @param None
  205. * @retval None
  206. */
  207. static void CLK_Config(void)
  208. {
  209. CLK_DeInit();
  210. /* Configure the clock source */
  211. CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);
  212. /* Configure the System clock frequency to run at 16Mhz */
  213. CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
  214. CLK_HSICmd(ENABLE);
  215. }
  216. #ifdef USE_FULL_ASSERT
  217. /**
  218. * @brief Reports the name of the source file and the source line number
  219. * where the assert_param error has occurred.
  220. * @param file: pointer to the source file name
  221. * @param line: assert_param error line source number
  222. * @retval None
  223. */
  224. void assert_failed(uint8_t* file, uint32_t line)
  225. {
  226. /* User can add his own implementation to report the file name and line number,
  227. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  228. /* Infinite loop */
  229. while (1)
  230. {
  231. }
  232. }
  233. #endif
  234. /**
  235. * @}
  236. */
  237. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/