communication through usart interrupt

Discussion in 'Embedded Systems and Microcontrollers' started by anju.m, Nov 1, 2016.

  1. anju.m

    Thread Starter New Member

    Nov 1, 2016
    4
    0
    Hi Everyone,
    Iam doing a project using the STM32F103VCT6 microcontroller and the board used is HY-MiniSTM32V.The softwares used are STM32CubeMX(for generating c initialisation code) , Atollic TrueStudioLite (as the integrated development envirnment) ,Flash Loader Demonstrator(for flashing the code to the controller) and the Tera Term(for getting the output).I am currently doing usart interrupt program and i have kept my code along with this.But the program is not working as an interrupt can anyone suggest me how to fix this?Eagerly waiting for all your replies.


    Code (Text):
    1. /**
    2. ******************************************************************************
    3. * File Name : main.c
    4. * Description : Main program body
    5. ******************************************************************************
    6. *
    7. * COPYRIGHT(c) 2016 STMicroelectronics
    8. *
    9. * Redistribution and use in source and binary forms, with or without modification,
    10. * are permitted provided that the following conditions are met:
    11. * 1. Redistributions of source code must retain the above copyright notice,
    12. * this list of conditions and the following disclaimer.
    13. * 2. Redistributions in binary form must reproduce the above copyright notice,
    14. * this list of conditions and the following disclaimer in the documentation
    15. * and/or other materials provided with the distribution.
    16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
    17. * may be used to endorse or promote products derived from this software
    18. * without specific prior written permission.
    19. *
    20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    30. *
    31. ******************************************************************************
    32. */
    33. /* Includes ------------------------------------------------------------------*/
    34. #include "stm32f1xx_hal.h"
    35.  
    36. /* USER CODE BEGIN Includes */
    37.  
    38. /* USER CODE END Includes */
    39.  
    40. /* Private variables ---------------------------------------------------------*/
    41. UART_HandleTypeDef huart1;
    42.  
    43. /* USER CODE BEGIN PV */
    44. /* Private variables ---------------------------------------------------------*/
    45.  
    46. /* USER CODE END PV */
    47.  
    48. /* Private function prototypes -----------------------------------------------*/
    49. void SystemClock_Config(void);
    50. void Error_Handler(void);
    51. static void MX_GPIO_Init(void);
    52. static void MX_USART1_UART_Init(void);
    53.  
    54. /* USER CODE BEGIN PFP */
    55. /* Private function prototypes -----------------------------------------------*/
    56.  
    57. /* USER CODE END PFP */
    58.  
    59. /* USER CODE BEGIN 0 */
    60.  
    61. /* USER CODE END 0 */
    62.  
    63. int main(void)
    64. {
    65.  
    66. /* USER CODE BEGIN 1 */
    67.  
    68. /* USER CODE END 1 */
    69.  
    70. /* MCU Configuration----------------------------------------------------------*/
    71.  
    72. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    73. HAL_Init();
    74.  
    75. /* Configure the system clock */
    76. SystemClock_Config();
    77.  
    78. /* Initialize all configured peripherals */
    79. MX_GPIO_Init();
    80. MX_USART1_UART_Init();
    81.  
    82. /* USER CODE BEGIN 2 */
    83.  
    84. /* USER CODE END 2 */
    85.  
    86. /* Infinite loop */
    87. /* USER CODE BEGIN WHILE */
    88. while (1)
    89.  
    90. {
    91. /* USER CODE END WHILE */
    92.  
    93. /* USER CODE BEGIN 3 */
    94. uint8_t data[1];
    95.  
    96. if(HAL_UART_Transmit_IT(&huart1,data,1)==0)
    97. {
    98. HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,1);
    99. HAL_UART_Receive(&huart1,data,1,0);
    100. HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,0);
    101. }
    102.  
    103. }
    104. /* USER CODE END 3 */
    105.  
    106. }
    107.  
    108. /** System Clock Configuration
    109. */
    110. void SystemClock_Config(void)
    111. {
    112.  
    113. RCC_OscInitTypeDef RCC_OscInitStruct;
    114. RCC_ClkInitTypeDef RCC_ClkInitStruct;
    115.  
    116. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    117. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    118. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
    119. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    120. {
    121. Error_Handler();
    122. }
    123.  
    124. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
    125. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    126. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
    127. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    128. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    129. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    130. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
    131. {
    132. Error_Handler();
    133. }
    134.  
    135. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
    136.  
    137. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
    138.  
    139. /* SysTick_IRQn interrupt configuration */
    140. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
    141. }
    142.  
    143. /* USART1 init function */
    144. static void MX_USART1_UART_Init(void)
    145. {
    146.  
    147. huart1.Instance = USART1;
    148. huart1.Init.BaudRate = 115200;
    149. huart1.Init.WordLength = UART_WORDLENGTH_8B;
    150. huart1.Init.StopBits = UART_STOPBITS_1;
    151. huart1.Init.Parity = UART_PARITY_NONE;
    152. huart1.Init.Mode = UART_MODE_TX_RX;
    153. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    154. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
    155. if (HAL_UART_Init(&huart1) != HAL_OK)
    156. {
    157. Error_Handler();
    158. }
    159.  
    160. }
    161.  
    162. /** Configure pins as
    163. * Analog
    164. * Input
    165. * Output
    166. * EVENT_OUT
    167. * EXTI
    168. */
    169. static void MX_GPIO_Init(void)
    170. {
    171.  
    172. GPIO_InitTypeDef GPIO_InitStruct;
    173.  
    174. /* GPIO Ports Clock Enable */
    175. __HAL_RCC_GPIOB_CLK_ENABLE();
    176. __HAL_RCC_GPIOA_CLK_ENABLE();
    177.  
    178. /*Configure GPIO pin Output Level */
    179. HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
    180.  
    181. /*Configure GPIO pin : LED1_Pin */
    182. GPIO_InitStruct.Pin = LED1_Pin;
    183. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    184. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    185. HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);
    186.  
    187. }
    188.  
    189. /* USER CODE BEGIN 4 */
    190.  
    191. /* USER CODE END 4 */
    192.  
    193. /**
    194. * @brief This function is executed in case of error occurrence.
    195. * [USER=120004]@Param[/USER] None
    196. * @retval None
    197. */
    198. void Error_Handler(void)
    199. {
    200. /* USER CODE BEGIN Error_Handler */
    201. /* User can add his own implementation to report the HAL error return state */
    202. while(1)
    203. {
    204. }
    205. /* USER CODE END Error_Handler */
    206. }
    207.  
    208. #ifdef USE_FULL_ASSERT
    209.  
    210. /**
    211. * @brief Reports the name of the source file and the source line number
    212. * where the assert_param error has occurred.
    213. * [USER=120004]@Param[/USER] file: pointer to the source file name
    214. * [USER=120004]@Param[/USER] line: assert_param error line source number
    215. * @retval None
    216. */
    217. void assert_failed(uint8_t* file, uint32_t line)
    218. {
    219. /* USER CODE BEGIN 6 */
    220. /* User can add his own implementation to report the file name and line number,
    221. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    222. /* USER CODE END 6 */
    223.  
    224. }
    225.  
    226. #endif
    227.  
    228. /**
    229. * @}
    230. */
    231.  
    232. /**
    233. * @}
    234. */
    235.  
    236. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    237.  
    238.  
    239.  
    240.  
    241.  
    242.  
    243. /**
    244. ******************************************************************************
    245. * @file stm32f1xx_it.c
    246. * @brief Interrupt Service Routines.
    247. ******************************************************************************
    248. *
    249. * COPYRIGHT(c) 2016 STMicroelectronics
    250. *
    251. * Redistribution and use in source and binary forms, with or without modification,
    252. * are permitted provided that the following conditions are met:
    253. * 1. Redistributions of source code must retain the above copyright notice,
    254. * this list of conditions and the following disclaimer.
    255. * 2. Redistributions in binary form must reproduce the above copyright notice,
    256. * this list of conditions and the following disclaimer in the documentation
    257. * and/or other materials provided with the distribution.
    258. * 3. Neither the name of STMicroelectronics nor the names of its contributors
    259. * may be used to endorse or promote products derived from this software
    260. * without specific prior written permission.
    261. *
    262. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    263. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    264. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    265. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    266. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    267. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    268. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    269. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    270. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    271. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    272. *
    273. ******************************************************************************
    274. */
    275. /* Includes ------------------------------------------------------------------*/
    276. #include "stm32f1xx_hal.h"
    277. #include "stm32f1xx.h"
    278. #include "stm32f1xx_it.h"
    279. uint8_t data[4];
    280. /* USER CODE BEGIN 0 */
    281.  
    282. /* USER CODE END 0 */
    283.  
    284. /* External variables --------------------------------------------------------*/
    285. extern UART_HandleTypeDef huart1;
    286.  
    287. /******************************************************************************/
    288. /* Cortex-M3 Processor Interruption and Exception Handlers */
    289. /******************************************************************************/
    290.  
    291. /**
    292. * @brief This function handles Non maskable interrupt.
    293. */
    294. void NMI_Handler(void)
    295. {
    296. /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
    297.  
    298. /* USER CODE END NonMaskableInt_IRQn 0 */
    299. /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
    300.  
    301. /* USER CODE END NonMaskableInt_IRQn 1 */
    302. }
    303.  
    304. /**
    305. * @brief This function handles Hard fault interrupt.
    306. */
    307. void HardFault_Handler(void)
    308. {
    309. /* USER CODE BEGIN HardFault_IRQn 0 */
    310.  
    311. /* USER CODE END HardFault_IRQn 0 */
    312. while (1)
    313. {
    314. }
    315. /* USER CODE BEGIN HardFault_IRQn 1 */
    316.  
    317. /* USER CODE END HardFault_IRQn 1 */
    318. }
    319.  
    320. /**
    321. * @brief This function handles Memory management fault.
    322. */
    323. void MemManage_Handler(void)
    324. {
    325. /* USER CODE BEGIN MemoryManagement_IRQn 0 */
    326.  
    327. /* USER CODE END MemoryManagement_IRQn 0 */
    328. while (1)
    329. {
    330. }
    331. /* USER CODE BEGIN MemoryManagement_IRQn 1 */
    332.  
    333. /* USER CODE END MemoryManagement_IRQn 1 */
    334. }
    335.  
    336. /**
    337. * @brief This function handles Prefetch fault, memory access fault.
    338. */
    339. void BusFault_Handler(void)
    340. {
    341. /* USER CODE BEGIN BusFault_IRQn 0 */
    342.  
    343. /* USER CODE END BusFault_IRQn 0 */
    344. while (1)
    345. {
    346. }
    347. /* USER CODE BEGIN BusFault_IRQn 1 */
    348.  
    349. /* USER CODE END BusFault_IRQn 1 */
    350. }
    351.  
    352. /**
    353. * @brief This function handles Undefined instruction or illegal state.
    354. */
    355. void UsageFault_Handler(void)
    356. {
    357. /* USER CODE BEGIN UsageFault_IRQn 0 */
    358.  
    359. /* USER CODE END UsageFault_IRQn 0 */
    360. while (1)
    361. {
    362. }
    363. /* USER CODE BEGIN UsageFault_IRQn 1 */
    364.  
    365. /* USER CODE END UsageFault_IRQn 1 */
    366. }
    367.  
    368. /**
    369. * @brief This function handles System service call via SWI instruction.
    370. */
    371. void SVC_Handler(void)
    372. {
    373. /* USER CODE BEGIN SVCall_IRQn 0 */
    374.  
    375. /* USER CODE END SVCall_IRQn 0 */
    376. /* USER CODE BEGIN SVCall_IRQn 1 */
    377.  
    378. /* USER CODE END SVCall_IRQn 1 */
    379. }
    380.  
    381. /**
    382. * @brief This function handles Debug monitor.
    383. */
    384. void DebugMon_Handler(void)
    385. {
    386. /* USER CODE BEGIN DebugMonitor_IRQn 0 */
    387.  
    388. /* USER CODE END DebugMonitor_IRQn 0 */
    389. /* USER CODE BEGIN DebugMonitor_IRQn 1 */
    390.  
    391. /* USER CODE END DebugMonitor_IRQn 1 */
    392. }
    393.  
    394. /**
    395. * @brief This function handles Pendable request for system service.
    396. */
    397. void PendSV_Handler(void)
    398. {
    399. /* USER CODE BEGIN PendSV_IRQn 0 */
    400.  
    401. /* USER CODE END PendSV_IRQn 0 */
    402. /* USER CODE BEGIN PendSV_IRQn 1 */
    403.  
    404. /* USER CODE END PendSV_IRQn 1 */
    405. }
    406.  
    407. /**
    408. * @brief This function handles System tick timer.
    409. */
    410. void SysTick_Handler(void)
    411. {
    412. /* USER CODE BEGIN SysTick_IRQn 0 */
    413.  
    414. /* USER CODE END SysTick_IRQn 0 */
    415. HAL_IncTick();
    416. HAL_SYSTICK_IRQHandler();
    417. /* USER CODE BEGIN SysTick_IRQn 1 */
    418.  
    419.  
    420. /* USER CODE END SysTick_IRQn 1 */
    421. }
    422.  
    423. /******************************************************************************/
    424. /* STM32F1xx Peripheral Interrupt Handlers */
    425. /* Add here the Interrupt Handlers for the used peripherals. */
    426. /* For the available peripheral interrupt handler names, */
    427. /* please refer to the startup file (startup_stm32f1xx.s). */
    428. /******************************************************************************/
    429.  
    430. /**
    431. * @brief This function handles USART1 global interrupt.
    432. */
    433. void USART1_IRQHandler(void)
    434. {
    435. /* USER CODE BEGIN USART1_IRQn 0 */
    436.  
    437. /* USER CODE END USART1_IRQn 0 */
    438. HAL_UART_IRQHandler(&huart1);
    439. /* USER CODE BEGIN USART1_IRQn 1 */
    440.  
    441.  
    442.  
    443. /* USER CODE END USART1_IRQn 1 */
    444.  
    445. }
    446. /* USER CODE BEGIN 1 */
    447.  
    448. /* USER CODE END 1 */
    449. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
    Moderator edit: inserted code tags
     
  2. MrChips

    Moderator

    Oct 2, 2009
    18,522
    5,880
    You need to initialize and enable USART1 IRQ.
     
  3. anju.m

    Thread Starter New Member

    Nov 1, 2016
    4
    0
    hi,
    Can you please tell me how to initialize it? And also where should I initialize it,is it inside main.c?
     
  4. anju.m

    Thread Starter New Member

    Nov 1, 2016
    4
    0
    hi,
    Please do help me on this ,I am new to this software and controller.
     
  5. dannyf

    Well-Known Member

    Sep 13, 2015
    2,196
    421
    because you didn't write your code to do what you wanted.

    the general approach is to initialize the transmission; and then in the isr, detect if the string has been finished; if yes, turn off the interrupt; otherwise, load the char to the uart and advance to the next char.
     
  6. anju.m

    Thread Starter New Member

    Nov 1, 2016
    4
    0
    Can you please make corrections in the code and send it back to me? I tried to do but I am not getting it.Do help me on this.
     
Loading...