Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.99 KB | None | 0 0
  1.  
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2018 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41. #include "stm32f4xx_hal.h"
  42.  
  43.  
  44. /* USER CODE BEGIN Includes */
  45. #include "math.h"
  46. /* USER CODE END Includes */
  47.  
  48. /* Private variables ---------------------------------------------------------*/
  49. ADC_HandleTypeDef hadc1;
  50.  
  51. UART_HandleTypeDef huart2;
  52.  
  53. /* USER CODE BEGIN PV */
  54. /* Private variables ---------------------------------------------------------*/
  55.  
  56. // Atramine 3,3V. Max reiksme 4095
  57. // Tuomet 1mV atitinka:
  58. #define LSB 3300/4095
  59.  
  60. // Polinomu kiekis.
  61. #define J_Kiekis 5
  62. #define K_Kiekis 5
  63.  
  64. // Para valandomis
  65. #define para_valandomis 72
  66.  
  67.  
  68. // Cia ADC kvantai
  69. uint32_t g_ADCValue;
  70. uint32_t offset=2;
  71.  
  72. // Polinomu koeficientai
  73. const double coef_J[J_Kiekis][3]={
  74. {-2.157, 3.5834, -36.24},
  75. {-0.3979, 19.528, -0.176},
  76. {-0.1505, 19.729, 0.0175},
  77. {0.0002, 18.068, 5.095},
  78. {-0.0814, 22.466, -54.51}
  79. };
  80.  
  81. const double coef_K[K_Kiekis][3]={
  82. {-7.4218, -29.137, -113.71},
  83. {-1.3281, 22.835, -2.0492},
  84. {-0.1942, 25.208, -0.1319},
  85. {-0.0648, 25.822, -5.7738},
  86. {0.0282, 22.15, 30.866}
  87. };
  88.  
  89. // Temperaturu ribos, apspresti kuri polinoma pasirinkti. Nuo, iki.
  90. const double TempJ_intervalai[J_Kiekis][2]={
  91. {-7.890, -2.893},
  92. {-2.893, 0},
  93. {0, 5.269},
  94. {5.269, 28.516},
  95. {28.516, 39.132}
  96. };
  97.  
  98. const double TempK_intervalai[K_Kiekis][2]={
  99. {-5.891, -4.138},
  100. {-3.554, -1.527},
  101. {-0.778, 4.096},
  102. {4.92, 20.644},
  103. {21.497, 29.129}
  104. };
  105.  
  106. // Apskaiciuojamos itampos kintamieji
  107. float Itampa;
  108. float Itampa_J_Potenciometru;
  109. float Itampa_K_Potenciometru;
  110. float tempK;
  111. float tempJ;
  112.  
  113. // Visos temperaturos per 3 paras (72 valandas).
  114. float temperaturos[para_valandomis];
  115. int temperaturos_n = 0; // Siuo metu skaitomas atskaitos taskas
  116. float temperaturu_suma = 0;
  117. float temperaturu_vidurkis = 0;
  118.  
  119. // Laikmaciai
  120. uint16_t tt; //UART laikmatis, nustatome kas kiek laiko issiuncia zinute
  121. uint16_t tt1; // Kas kiek laiko temp. reiksme imam. Siuo momentu kas 200ms
  122. uint32_t tt2; // Sitas laikmatis isjungtas. Cia yra laikmatis pagal kuri temp. reiksme imama kas 1 val.
  123.  
  124. // UART stringas, kuri siunciam
  125. char m[30];
  126.  
  127. // Flag'ai
  128. int uzsipilde_tmasyvas=0; // Jeigu temperaturu masyvas uzsipilde
  129. int sild_on; // Sildymas ijungtas, isjungtas.
  130. uint8_t TXFLAG, Ready, Ready1;
  131.  
  132. /* USER CODE END PV */
  133.  
  134. /* Private function prototypes -----------------------------------------------*/
  135. void SystemClock_Config(void);
  136. static void MX_GPIO_Init(void);
  137. static void MX_ADC1_Init(void);
  138. static void MX_USART2_UART_Init(void);
  139.  
  140. /* USER CODE BEGIN PFP */
  141. /* Private function prototypes -----------------------------------------------*/
  142.  
  143. double power_J(double input, int pol_funkcija) {
  144. double temp=0;
  145. temp = coef_J[pol_funkcija][0]*pow(input,2)+coef_J[pol_funkcija][1]*input+coef_J[pol_funkcija][2];
  146. return temp;
  147. }
  148.  
  149. double power_K(double input, int pol_funkcija) {
  150. double temp=0;
  151. temp = coef_K[pol_funkcija][0]*pow(input,2)+coef_K[pol_funkcija][1]*input+coef_K[pol_funkcija][2];
  152. return temp;
  153. }
  154.  
  155. double TempJ(double input) {
  156. double temp=0;
  157. for(int i=0;i<J_Kiekis;i++) {
  158. if(input>=TempJ_intervalai[i][0] && input<=TempJ_intervalai[i][1]) {
  159. temp = power_J(input, i);
  160. }
  161. }
  162. return temp;
  163. }
  164.  
  165. double TempK(double input) {
  166. double temp=0;
  167. for(int i=0;i<K_Kiekis;i++) {
  168. if(input>=TempK_intervalai[i][0] && input<=TempK_intervalai[i][1]) {
  169. temp = power_K(input, i);
  170. }
  171. }
  172. return temp;
  173. }
  174.  
  175. /* USER CODE END PFP */
  176.  
  177. /* USER CODE BEGIN 0 */
  178. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
  179. {
  180. TXFLAG=0;
  181. }
  182. /* USER CODE END 0 */
  183.  
  184. /**
  185. * @brief The application entry point.
  186. *
  187. * @retval None
  188. */
  189. int main(void)
  190. {
  191. /* USER CODE BEGIN 1 */
  192.  
  193. /* USER CODE END 1 */
  194.  
  195. /* MCU Configuration----------------------------------------------------------*/
  196.  
  197. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  198. HAL_Init();
  199.  
  200. /* USER CODE BEGIN Init */
  201.  
  202. /* USER CODE END Init */
  203.  
  204. /* Configure the system clock */
  205. SystemClock_Config();
  206.  
  207. /* USER CODE BEGIN SysInit */
  208.  
  209. /* USER CODE END SysInit */
  210.  
  211. /* Initialize all configured peripherals */
  212. MX_GPIO_Init();
  213. MX_ADC1_Init();
  214. MX_USART2_UART_Init();
  215. /* USER CODE BEGIN 2 */
  216.  
  217. // Nunulinam temperaturos masyva
  218. for (int i = 0; i < para_valandomis; i++) {
  219. temperaturos[i] = 0;
  220. }
  221.  
  222. /* USER CODE END 2 */
  223.  
  224. /* Infinite loop */
  225. /* USER CODE BEGIN WHILE */
  226. while (1)
  227. {
  228. HAL_ADC_Start(&hadc1);
  229. if(HAL_ADC_PollForConversion(&hadc1, 100)== HAL_OK) {
  230.  
  231. // Nuimam offseta
  232. g_ADCValue = HAL_ADC_GetValue(&hadc1);
  233.  
  234. // Paverciame ADC kvantus i itampa
  235. Itampa = (float) g_ADCValue * LSB;
  236. //Itampa_tikra=Itampa/189.789; //J termoporai
  237.  
  238. // Potenciometrais
  239. Itampa_J_Potenciometru=(((TempJ_intervalai[J_Kiekis-1][1]-TempJ_intervalai[0][0])/3300)*Itampa)+TempJ_intervalai[0][0];
  240. Itampa_K_Potenciometru=(((TempK_intervalai[K_Kiekis-1][1]-TempK_intervalai[0][0])/3300)*Itampa)+TempK_intervalai[0][0];
  241.  
  242. // Paverciam i temperatura
  243. tempK = TempK(Itampa_K_Potenciometru);
  244. tempJ = TempJ(Itampa_J_Potenciometru);
  245.  
  246. if(Ready1==1) {
  247. // Atimama seniausia reiksme. (sitas galioja tik tuo atveju, kai masyvas jau yra pilnai uzsipildes)
  248. temperaturu_suma = temperaturu_suma - temperaturos[temperaturos_n];
  249. // Irasome temperatura i masyva
  250. temperaturos[temperaturos_n] = tempJ;
  251. // Sumuojame temperaturas
  252. temperaturu_suma = temperaturu_suma + temperaturos[temperaturos_n];
  253. // Prie temperaturos atskaitos n, pridedame +1.
  254. temperaturos_n = temperaturos_n + 1;
  255.  
  256. // Jeigu pasiekem temperaturos masyvo pabaiga
  257. if (temperaturos_n >= para_valandomis) {
  258. // Nustatome temperaturos atskaitos n i pradzia.
  259. temperaturos_n= 0;
  260. // Ijungiame flag, jog masyvas uzsipilde.
  261. uzsipilde_tmasyvas=1;
  262. }
  263.  
  264. // Jeigu masyvas yra uzsipildes. Yra 72 reiksmes jame.
  265. if(uzsipilde_tmasyvas==1){
  266. temperaturu_vidurkis = temperaturu_suma / para_valandomis;
  267. }
  268.  
  269. // Jeigu masyvas dar nespejo uzsipildyti
  270. else {
  271. // Dalyba is 0 negalima
  272. if(temperaturos_n==0) {
  273. temperaturu_vidurkis = temperaturu_suma;
  274. }
  275. // Daliname is tiek reiksmiu, kiek yra masyve
  276. else {
  277. temperaturu_vidurkis = temperaturu_suma / temperaturos_n;
  278. }
  279.  
  280. }
  281.  
  282. Ready1=0;
  283. if(temperaturu_vidurkis<10){
  284. //GPIOB->BSRR=GPIO_PIN_12; // Isvedam 1 i Pin'a
  285. sild_on=1;
  286. } else {
  287. // GPIOB->BRR=GPIO_PIN_12; // Isvedam 0 i Pin'a
  288. sild_on=0;
  289. }
  290.  
  291.  
  292. }
  293.  
  294.  
  295.  
  296. if((Ready==1)&&(TXFLAG==0)){
  297.  
  298. if(sild_on==1){
  299. sprintf(m,"Temperatura=%5.4f, vidurkis=%5.4f,SILDYMAS IJUNGTAS\n\r",tempJ, temperaturu_vidurkis); // convert to string
  300. }
  301. else{
  302. sprintf(m,"Temperatura=%5.4f, vidurkis=%5.4f,NESILDO \n\r",tempJ, temperaturu_vidurkis); // convert to string
  303. }
  304.  
  305. HAL_UART_Transmit_IT(&huart1, (uint8_t*)m, 100);
  306. Ready=0;
  307. TXFLAG=1;
  308. }
  309.  
  310.  
  311. }
  312. /* USER CODE END WHILE */
  313.  
  314. /* USER CODE BEGIN 3 */
  315.  
  316. }
  317. /* USER CODE END 3 */
  318.  
  319. }
  320.  
  321. /**
  322. * @brief System Clock Configuration
  323. * @retval None
  324. */
  325. void SystemClock_Config(void)
  326. {
  327.  
  328. RCC_OscInitTypeDef RCC_OscInitStruct;
  329. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  330.  
  331. /**Configure the main internal regulator output voltage
  332. */
  333. __HAL_RCC_PWR_CLK_ENABLE();
  334.  
  335. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  336.  
  337. /**Initializes the CPU, AHB and APB busses clocks
  338. */
  339. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  340. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  341. RCC_OscInitStruct.HSICalibrationValue = 16;
  342. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  343. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  344. RCC_OscInitStruct.PLL.PLLM = 8;
  345. RCC_OscInitStruct.PLL.PLLN = 50;
  346. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
  347. RCC_OscInitStruct.PLL.PLLQ = 7;
  348. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  349. {
  350. _Error_Handler(__FILE__, __LINE__);
  351. }
  352.  
  353. /**Initializes the CPU, AHB and APB busses clocks
  354. */
  355. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  356. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  357. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  358. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  359. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8;
  360. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;
  361.  
  362. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  363. {
  364. _Error_Handler(__FILE__, __LINE__);
  365. }
  366.  
  367. /**Configure the Systick interrupt time
  368. */
  369. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  370.  
  371. /**Configure the Systick
  372. */
  373. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  374.  
  375. /* SysTick_IRQn interrupt configuration */
  376. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  377. }
  378.  
  379. /* ADC1 init function */
  380. static void MX_ADC1_Init(void)
  381. {
  382.  
  383. ADC_ChannelConfTypeDef sConfig;
  384.  
  385. /**Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  386. */
  387. hadc1.Instance = ADC1;
  388. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
  389. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  390. hadc1.Init.ScanConvMode = DISABLE;
  391. hadc1.Init.ContinuousConvMode = DISABLE;
  392. hadc1.Init.DiscontinuousConvMode = DISABLE;
  393. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  394. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  395. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  396. hadc1.Init.NbrOfConversion = 1;
  397. hadc1.Init.DMAContinuousRequests = DISABLE;
  398. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  399. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  400. {
  401. _Error_Handler(__FILE__, __LINE__);
  402. }
  403.  
  404. /**Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  405. */
  406. sConfig.Channel = ADC_CHANNEL_0;
  407. sConfig.Rank = 1;
  408. sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES;
  409. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  410. {
  411. _Error_Handler(__FILE__, __LINE__);
  412. }
  413.  
  414. }
  415.  
  416. /* USART2 init function */
  417. static void MX_USART2_UART_Init(void)
  418. {
  419.  
  420. huart2.Instance = USART2;
  421. huart2.Init.BaudRate = 9600;
  422. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  423. huart2.Init.StopBits = UART_STOPBITS_1;
  424. huart2.Init.Parity = UART_PARITY_NONE;
  425. huart2.Init.Mode = UART_MODE_TX_RX;
  426. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  427. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  428. if (HAL_UART_Init(&huart2) != HAL_OK)
  429. {
  430. _Error_Handler(__FILE__, __LINE__);
  431. }
  432.  
  433. }
  434.  
  435. /** Pinout Configuration
  436. */
  437. static void MX_GPIO_Init(void)
  438. {
  439.  
  440. /* GPIO Ports Clock Enable */
  441. __HAL_RCC_GPIOA_CLK_ENABLE();
  442.  
  443. }
  444.  
  445. /* USER CODE BEGIN 4 */
  446.  
  447. /* USER CODE END 4 */
  448.  
  449. /**
  450. * @brief This function is executed in case of error occurrence.
  451. * @param file: The file name as string.
  452. * @param line: The line in file as a number.
  453. * @retval None
  454. */
  455. void _Error_Handler(char *file, int line)
  456. {
  457. /* USER CODE BEGIN Error_Handler_Debug */
  458. /* User can add his own implementation to report the HAL error return state */
  459. while(1)
  460. {
  461. }
  462. /* USER CODE END Error_Handler_Debug */
  463. }
  464.  
  465. #ifdef USE_FULL_ASSERT
  466. /**
  467. * @brief Reports the name of the source file and the source line number
  468. * where the assert_param error has occurred.
  469. * @param file: pointer to the source file name
  470. * @param line: assert_param error line source number
  471. * @retval None
  472. */
  473. void assert_failed(uint8_t* file, uint32_t line)
  474. {
  475. /* USER CODE BEGIN 6 */
  476. /* User can add his own implementation to report the file name and line number,
  477. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  478. /* USER CODE END 6 */
  479. }
  480. #endif /* USE_FULL_ASSERT */
  481.  
  482. /**
  483. * @}
  484. */
  485.  
  486. /**
  487. * @}
  488. */
  489.  
  490. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement