main.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #define VALUES_BUFFER_SIZE 10
  35. /* Private macro -------------------------------------------------------------*/
  36. #define LED_RED_ON LED_RED_PORT->ODR &= (uint8_t)(~LED_RED_PIN)
  37. #define LED_RED_OFF LED_RED_PORT->ODR |= LED_RED_PIN
  38. #define LED_GREEN_ON LED_GREEN_PORT->ODR &= (uint8_t)(~LED_GREEN_PIN)
  39. #define LED_GREEN_OFF LED_GREEN_PORT->ODR |= LED_GREEN_PIN
  40. /* Private constant ----------------------------------------------------------*/
  41. // перевод числа 0-7 в номер индикатора
  42. static const max7219_reg_t dig[8] = {
  43. RegDigit0, RegDigit1, RegDigit2, RegDigit3,
  44. RegDigit4, RegDigit5, RegDigit6, RegDigit7
  45. };
  46. // перевод значения 0x00 - 0x0F в код индикатора
  47. static const max7219_sym_t num[16] = {
  48. Sym_0, Sym_1, Sym_2, Sym_3, Sym_4, Sym_5, Sym_6, Sym_7,
  49. Sym_8, Sym_9, Sym_A, Sym_b, Sym_C, Sym_d, Sym_E, Sym_F
  50. };
  51. /* Private variables ---------------------------------------------------------*/
  52. static uint16_t bufVoltage[VALUES_BUFFER_SIZE];
  53. static uint16_t bufCurrent[VALUES_BUFFER_SIZE];
  54. /* averaged values for 1 sek */
  55. static uint16_t Voltage;
  56. static uint16_t Current;
  57. static uint32_t Power;
  58. static uint32_t CapacityAH = 0;
  59. static uint32_t CapacityWH = 0;
  60. /* Private function prototypes -----------------------------------------------*/
  61. static void GPIO_Config(void);
  62. static void CLK_Config(void);
  63. /* RTOS function prototypes -----------------------------------------------*/
  64. static void getADCValues(void);
  65. static void calculateValues(void);
  66. static void showTopLineU(void);
  67. static void showBotLineI(void);
  68. /* Private functions ---------------------------------------------------------*/
  69. /**
  70. * @brief Main program.
  71. * @param None
  72. * @retval None
  73. */
  74. void main(void)
  75. {
  76. /* Clock configuration -----------------------------------------*/
  77. CLK_Config();
  78. /* GPIO Configuration -----------------------------------------*/
  79. GPIO_Config();
  80. /* RTOS Configuration */
  81. RTOS_Config();
  82. /* ADC Configuration and start */
  83. ADC_Config();
  84. /* MAX7219 Configuration */
  85. MAX7219_Config();
  86. /* ROTS tasks */
  87. RTOS_SetTask(getADCValues,100,100);
  88. RTOS_SetTask(calculateValues,101,1000);
  89. RTOS_SetTask(showTopLineU,102,250);
  90. RTOS_SetTask(showBotLineI,103,250);
  91. /* Infinite loop */
  92. while (1) {
  93. RTOS_DispatchTask();
  94. wfi();
  95. }
  96. }
  97. /**
  98. * Забираем обработанные данные из быстрого буфера
  99. * и складываем в медленный.
  100. */
  101. static void getADCValues(void) {
  102. static uint8_t idxVal=0;
  103. uint16_t * avgVal;
  104. avgVal = ADC_GetValues();
  105. bufVoltage[idxVal] = *avgVal;
  106. bufCurrent[idxVal] = *(avgVal + 1);
  107. idxVal ++;
  108. if (idxVal == VALUES_BUFFER_SIZE) {
  109. idxVal = 0;
  110. }
  111. }
  112. /**
  113. * Average values from slow buffer.
  114. * Calculate Power, Capacitance IH and WH.
  115. */
  116. static void calculateValues(void) {
  117. uint8_t i;
  118. uint16_t c=0;
  119. uint32_t v=0;
  120. for (i=0; i<VALUES_BUFFER_SIZE; i++) {
  121. v += bufVoltage[i];
  122. c += bufCurrent[i];
  123. }
  124. Current = (c + (VALUES_BUFFER_SIZE/2)) / VALUES_BUFFER_SIZE;
  125. Voltage = (v + (VALUES_BUFFER_SIZE/2)) / VALUES_BUFFER_SIZE;
  126. Power = ((Voltage * Current) + 500) / 1000;
  127. CapacityAH += Current;
  128. CapacityWH += Power;
  129. }
  130. /**
  131. * Output voltage values to top indicator
  132. */
  133. static void showTopLineU(void) {
  134. uint32_t vlt = 0;
  135. uint8_t tmp;
  136. for (tmp=0; tmp<VALUES_BUFFER_SIZE; tmp++) {
  137. vlt += bufVoltage[tmp];
  138. }
  139. vlt /= VALUES_BUFFER_SIZE;
  140. // LED_RED_ON;
  141. if(vlt >= 10000){
  142. /* Tens Thousands voltage value*/
  143. tmp = (uint8_t)(vlt / 10000);
  144. vlt %= 10000;
  145. MAX7219_WriteData(dig[0], num[tmp]);
  146. /* Thousands voltage value*/
  147. tmp = (uint8_t)(vlt / 1000);
  148. vlt %= 1000;
  149. MAX7219_WriteData(dig[1], (num[tmp] | SYM_DOT));
  150. /* Hundreds voltage value */
  151. tmp = (uint8_t)(vlt / 100);
  152. vlt %= 100;
  153. MAX7219_WriteData(dig[2], num[tmp]);
  154. /* Tens voltage value */
  155. tmp = (uint8_t)(vlt / 10);
  156. MAX7219_WriteData(dig[3], num[tmp]);
  157. } else {
  158. /* Thousands voltage value*/
  159. tmp = (uint8_t)(vlt / 1000);
  160. vlt %= 1000;
  161. MAX7219_WriteData(dig[0], (num[tmp] | SYM_DOT));
  162. /* Hundreds voltage value */
  163. tmp = (uint8_t)(vlt / 100);
  164. vlt %= 100;
  165. MAX7219_WriteData(dig[1], num[tmp]);
  166. /* Tens voltage value */
  167. tmp = (uint8_t)(vlt / 10);
  168. vlt %= 10;
  169. MAX7219_WriteData(dig[2], num[tmp]);
  170. /* Ones voltage value */
  171. MAX7219_WriteData(dig[3], num[(uint8_t)vlt]);
  172. }
  173. }
  174. /**
  175. * Output current values to bottom indicator
  176. */
  177. static void showBotLineI(void){
  178. // LED_RED_OFF; // индикация режима
  179. uint16_t crnt = 0;
  180. uint8_t tmp;
  181. for (tmp=0; tmp<VALUES_BUFFER_SIZE; tmp++) {
  182. crnt += bufCurrent[tmp];
  183. }
  184. crnt /= VALUES_BUFFER_SIZE;
  185. if (crnt >= 1000) {
  186. tmp = crnt / 1000;
  187. crnt %= 1000;
  188. } else {
  189. tmp = 0;
  190. }
  191. MAX7219_WriteData(dig[4], (num[tmp] | SYM_DOT));
  192. if (crnt >= 100) {
  193. tmp = crnt / 100;
  194. crnt %= 100;
  195. } else {
  196. tmp = 0;
  197. }
  198. MAX7219_WriteData(dig[5], num[tmp]);
  199. if (crnt >= 10) {
  200. tmp = crnt / 10;
  201. crnt %= 10;
  202. } else {
  203. tmp = 0;
  204. }
  205. MAX7219_WriteData(dig[6], num[tmp]);
  206. MAX7219_WriteData(dig[7], num[crnt]);
  207. }
  208. /**
  209. * @brief Configure GPIO for button available on the VAPC board
  210. * @param None
  211. * @retval None
  212. */
  213. static void GPIO_Config(void)
  214. {
  215. /* Configure GPIO used to drive LEDs */
  216. GPIO_Init(LED_RED_PORT, LED_RED_PIN, GPIO_Mode_Out_PP_High_Fast);
  217. GPIO_Init(LED_GREEN_PORT, LED_GREEN_PIN, GPIO_Mode_Out_PP_High_Fast);
  218. /* Enable general interrupts */
  219. enableInterrupts();
  220. }
  221. /**
  222. * @brief Configure system clock to run at Maximum clock speed
  223. * @param None
  224. * @retval None
  225. */
  226. static void CLK_Config(void)
  227. {
  228. CLK_DeInit();
  229. /* Configure the clock source */
  230. CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);
  231. /* Configure the System clock frequency to run at 16Mhz */
  232. CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
  233. CLK_HSICmd(ENABLE);
  234. }
  235. #ifdef USE_FULL_ASSERT
  236. /**
  237. * @brief Reports the name of the source file and the source line number
  238. * where the assert_param error has occurred.
  239. * @param file: pointer to the source file name
  240. * @param line: assert_param error line source number
  241. * @retval None
  242. */
  243. void assert_failed(uint8_t* file, uint32_t line)
  244. {
  245. /* User can add his own implementation to report the file name and line number,
  246. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  247. /* Infinite loop */
  248. while (1)
  249. {
  250. }
  251. }
  252. #endif
  253. /**
  254. * @}
  255. */
  256. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/