Skip to main content
ilyaz yilmaz
Associate II
May 9, 2020
Solved

Nucleo F401RE Usart Tx Blink Led

  • May 9, 2020
  • 4 replies
  • 1244 views

I have Nucleo F401RE board.

I want to Turn On Led When Usart Tx pin high and Turn Off When Usart Tx pin is low.

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t data_tx[5] = {0x41 , 0x42 ,0x43 ,0x44 , 0x45};
uint8_t data_rx[5];
/* USER CODE END 0 */
 
/**
 * @brief The application entry point.
 * @retval int
 */
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();
 
 /* USER CODE BEGIN Init */
 
 /* USER CODE END Init */
 
 /* Configure the system clock */
 SystemClock_Config();
 
 /* USER CODE BEGIN SysInit */
 
 /* USER CODE END SysInit */
 
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_DMA_Init();
 MX_USART2_UART_Init();
 MX_USART1_UART_Init();
 /* USER CODE BEGIN 2 */
 
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
 
 /* USER CODE BEGIN 3 */
	 //HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
	 //hdma_usart1_tx.Instance->CR |= (1<<0);
	 //HAL_UART_Receive_DMA(&huart1, data_rx, 5);
	 HAL_UART_Transmit_DMA(&huart1, data_tx, 5);
	 if((GPIOA->ODR & GPIO_PIN_9)==1 | (GPIOA->ODR & GPIO_PIN_10)==1)
	 {
		 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin , 1);
	 }else
	 {
		 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin , 0);
	 }
	 //HAL_GPIO_ReadPin(LD2_GPIO_Port, LD2_Pin);
	 //HAL_Delay(50);
 
 }
 /* USER CODE END 3 */
}

 The code is above and I always read Tx Pin low...

Is there any other way or solution?

This topic has been closed for replies.
Best answer by Nikita91

I don't know, you have to dig the reference manual.

4 replies

Nikita91
Lead II
May 9, 2020

if you use a mask to check a bit you must compare against 0 not 1: if ((GPIOA->ODR & mask) != 0)

If the bit you check is not the LSB the result will not be 1...

ilyaz yilmaz
Associate II
May 9, 2020

Hi Nikita ,

it's my mistake thank you so much.

Actually I change like below just a test . But GPIOA-> ODR data always zero doesnt change.

USART1 TX pin is connnected PA9... And it transmit data. But GPIOA-> ODR value doesnt change.

I think GPIO ode in AF mode doesnt change ODR register status. Is there any way to read Pin state in AF Mode ????

	 if(GPIOA->ODR != 0)
	 {
		 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin , 1);
	 }else
	 {
		 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin , 0);
	 }

Nikita91
Nikita91Best answer
Lead II
May 9, 2020

I don't know, you have to dig the reference manual.

ilyaz yilmaz
Associate II
May 9, 2020

In the Referance manual , it 's written . Below code Works For Nucleo F401RE

	 if((GPIOA->IDR & GPIO_PIN_9)!= 0)
	 {
		 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin , 0);
	 }else
	 {
		 HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin , 1);
	 }

0693W000000X9GdQAK.png