UART effecting “character LCD(I2C)” in stm32f103
UART effecting “character LCD(I2C)” in stm32f103
16 Nov 2016, 09:39
I'm using HAL API for programming, I programed if UART got some certain text then display the counter in character LCD, Then increment counter, If again got that sentence repeat these (Increment counter and displaying that).(I'm getting by interrupt) When I send about 200 char, two times, everything are right, But (sending) after this ,LCD(Used with I2C expander) missing it's functionality . & if I sending less character in each time I can send characters more than two time .
LCD is based on HD44780A00.
This is schematic:
And this is code:(switch is for detecting that certain code,access indicators are LS and LF)
/* Includes ------------------------------------------------------------------*/ #include "stm32f1xx_hal.h" /* USER CODE BEGIN Includes */ #include "hd44780.h" /* USER CODE END Includes */ /* Private variables ---------------------------------------------------------*/ I2C_HandleTypeDef hi2c1; UART_HandleTypeDef huart1; DMA_HandleTypeDef hdma_usart1_tx; DMA_HandleTypeDef hdma_usart1_rx; /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ unsigned short flag_acc=0;/**/uint8_t index_tok[12],index_tag[6]/*buffer localization*/, c=0/*index_tag counter*/ ,rx_index=0/*for locar reception*/,chsumt=0;short err=0/*error occurence*/ ,errtemp=0/*extreme erorr occurence check*/; char /*gl_rx_sz,*/rx_data[1],tx_buf[100],rx_buf[200],chck,test=0,RXNE=-1; short LS/*Last Sentence*/=0,LF=0/*is in Line Feed char??*/,i2cer=0,ii=0; typedef enum{ gsv_off=0, gsv_on, rmc_off, gll_off, gga_off, gsa_off, gla_off }ubx; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); void Error_Handler(void); static void MX_GPIO_Init(void); static void MX_DMA_Init(void); static void MX_USART1_UART_Init(void); static void MX_I2C1_Init(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ void gps_baud(); /************************************************* ******************************************************* ****************************************************/ //#ifdef __GNUC__ // /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf // set to 'Yes') calls __io_putchar() */ // #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) //#else // #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f) //#endif /* __GNUC__ */ ///** // * @brief Retargets the C library printf function to the USART. // * @param None // * @retval None // */ //PUTCHAR_PROTOTYPE //{ // /* Place your implementation of fputc here */ // /* e.g. write a character to the USART */ // HAL_UART_Transmit_DMA(&huart1, (uint8_t *)&ch,1); // HAL_Delay(1); // return ch; //} ///********************************************** //*************************************** //**********************************************/ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart){// maybe some algorithm needed to declare the error NVIC_SystemReset(); } void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *huart){// maybe some algorithm needed to declare the error NVIC_SystemReset(); } //Interrupt callback routine void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART1) //current UART { rx_buf[rx_index++]=rx_data[0]; //receiving process if (rx_data[0]==0x24/*$*/){index_tag[c++]=rx_index-1;LS=1;}//LF indicate end of buffer else { if(rx_data[0]!=0x0D/*LF*/){ switch (LS) { case 1: if (rx_data[0]=='G')LS+=1; break; case 2: if (rx_data[0]=='P')LS++; break; case 3: if (rx_data[0]=='G')LS++;else LS=0; break; case 4: if (rx_data[0]=='S')LS++;else LS=0; break; case 5: if (rx_data[0]=='A')LS=-1;else LS=0;/*last sentense reached*/ break; default: break;} } else LF=1; }//obtain indexes tag HAL_UART_Receive_DMA(&huart1, rx_data, 1); /*write flag. it must implement in main while :)*/ } } /* USER CODE END PFP */ /* USER CODE BEGIN 0 */ void ubx_message(ubx ubx); /* USER CODE END 0 */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_USART1_UART_Init(); MX_I2C1_Init(); /* USER CODE BEGIN 2 */ // gps_baud(); ubx_message(gsv_off);while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)==0);HAL_Delay(5); ubx_message(rmc_off);while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)==0);HAL_Delay(5); ubx_message(gll_off); LCD_PCF8574_HandleTypeDef lcd; lcd.pcf8574.PCF_I2C_ADDRESS = 0x27; lcd.pcf8574.PCF_I2C_TIMEOUT = 50; lcd.pcf8574.i2c.Instance = I2C1; lcd.pcf8574.i2c.Init.ClockSpeed = 40000; lcd.NUMBER_OF_LINES = NUMBER_OF_LINES_2; lcd.type = TYPE0; LCD_Init(&lcd); LCD_SetLocation(&lcd,0,0); LCD_WriteString(&lcd,"/*/*/*/*/*/*/*/*"); HAL_UART_Receive_DMA(&huart1, rx_data, 1); //activate uart every time receive 1 bit __HAL_UART_ENABLE_IT(&huart1,UART_IT_ERR);//error interupt enabled __HAL_UART_ENABLE_IT(&huart1,UART_IT_PE); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { //error handler *** if(0x01==__HAL_UART_GET_FLAG(&huart1,UART_FLAG_ORE)||0x01==__HAL_UART_GET_FLAG(&huart1,UART_FLAG_PE)||err!=errtemp){ NVIC_SystemReset(); }//error handler end *** if(LS==-1&&LF==1) { // HAL_UART_Transmit_DMA(&huart1, tx_buf,100); LCD_SetLocation(&lcd,0,0); LCD_WriteString(&lcd,"we detecting sth."); LCD_SetLocation(&lcd,0,1); LCD_WriteFloat(&lcd,ii++,2); LS=0; LF=0;/*refresh Line Feed for next recieve*/ rx_index=0; } HAL_UART_Transmit_DMA(&huart1, "|",1); HAL_Delay(50); HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_3); /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** System Clock Configuration */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); /* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); } /* I2C1 init function */ static void MX_I2C1_Init(void) { hi2c1.Instance = I2C1; hi2c1.Init.ClockSpeed = 100000; hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2; hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); } } /* USART1 init function */ static void MX_USART1_UART_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 9600; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } } /** * Enable DMA controller clock */ static void MX_DMA_Init(void) { /* DMA controller clock enable */ __HAL_RCC_DMA1_CLK_ENABLE(); /* DMA interrupt init */ /* DMA1_Channel4_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn); /* DMA1_Channel5_IRQn interrupt configuration */ HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn); } /** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_RESET); /*Configure GPIO pins : PA2 PA3 */ GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ void ubx_message(ubx ublx) { switch (ublx) { case gsv_off: HAL_UART_Transmit_DMA(&huart1, "\xB5\x62\x06\x01\x03\x00\xF0\x03\x00\xFD\x15",11); break; case rmc_off: HAL_UART_Transmit_DMA(&huart1, "\xB5\x62\x06\x01\x03\x00\xF0\x04\x00\xFE\x17",11); break; case gll_off: HAL_UART_Transmit_DMA(&huart1, "\xB5\x62\x06\x01\x03\x00\xF0\x01\x00\xFB\x11",11); break; case gsv_on: {uint8_t mess[11]={0xB5,0x62,0x06,0x01,0x03,0x00,0xF0,0x03,0x01,0xFE,0x16}; HAL_UART_Transmit_DMA(&huart1, mess,11);} break; default: break; } } void gps_baud() { HAL_UART_Transmit_DMA(&huart1, "$PUBX,41,1,0007,0003,115200,0*18\n\r",35); while(__HAL_UART_GET_FLAG(&huart1,UART_FLAG_TC)==0); huart1.Init.BaudRate =115200; HAL_UART_Init(&huart1) ; } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @param None * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler */ /* User can add his own implementation to report the HAL error return state */ while(1) { } /* USER CODE END Error_Handler */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t* file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /** * @} */ /** * @} */