[原创] F7评测④——串口打印数据

65387468@qq.com   2016-12-13 10:20 楼主
1.首先需要一个建立好的工程,这边就不多说了,可以看之前的帖子https://bbs.eeworld.com.cn/thread-508163-1-1.html 2.配置系统时钟,用demo自带的函数即可,配置到216MHz static void SystemClock_Config(void) { RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; HAL_StatusTypeDef ret = HAL_OK; /* Enable Power Control clock */ __HAL_RCC_PWR_CLK_ENABLE(); /* The voltage scaling allows optimizing the power consumption when the device is clocked below the maximum system frequency, to update the voltage scaling value regarding system frequency refer to product datasheet. */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /* Enable HSE Oscillator and activate PLL with HSE as source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 25; RCC_OscInitStruct.PLL.PLLN = 432; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 9; RCC_OscInitStruct.PLL.PLLR = 7; ret = HAL_RCC_OscConfig(&RCC_OscInitStruct); if(ret != HAL_OK) { while(1) { ; } } /* Activate the OverDrive to reach the 216 MHz Frequency */ ret = HAL_PWREx_EnableOverDrive(); if(ret != HAL_OK) { while(1) { ; } } /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7); if(ret != HAL_OK) { while(1) { ; } } } 3.初始化串口引脚,我们ST-Link自带一个虚拟串口, QQ截图20161213100731.jpg QQ截图20161213100752.jpg 需要把A9 A10配置成 GPIO_InitTypeDef gpio_init_structure; __HAL_RCC_GPIOA_CLK_ENABLE();使能 __USART1_CLK_ENABLE(); gpio_init_structure.Pin = ((uint32_t)GPIO_PIN_9)|((uint32_t)GPIO_PIN_10); gpio_init_structure.Mode = GPIO_MODE_AF_PP;//复用推挽输出 gpio_init_structure.Pull = GPIO_NOPULL; gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;//高速 gpio_init_structure.Alternate = GPIO_AF7_USART1;//复用串口1 HAL_GPIO_Init(((GPIO_TypeDef*)GPIOA), &gpio_init_structure);//完成初始化 4.下面需要对串口的波特率,停止位,校验位,数据位等进行配置, UART_HandleTypeDef UartHandle; UartHandle.Instance = USART1; //串口1 UartHandle.Init.BaudRate = 115200;//波特率115200 UartHandle.Init.WordLength = UART_WORDLENGTH_8B;//数据位8 UartHandle.Init.StopBits = UART_STOPBITS_1;//停止位1 UartHandle.Init.Parity = UART_PARITY_NONE;校验位无 UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; UartHandle.Init.Mode = UART_MODE_TX_RX; HAL_UART_Init(&UartHandle); 5.在while(1)中添加 HAL_Delay(500); HAL_UART_Transmit(&UartHandle,"1",1,2); 打开串口助手就能看到现象了 QQ截图20161213101345.jpg 6.如果需要用prinf()函数,我们需要添加下面一段函数 int fputc(int ch, FILE *f) { uint8_t temp[1]={ch}; HAL_UART_Transmit(&UartHandle,temp,1,2); return ch; } 改成printf("123\r\n"); 就能看到串口现象了,注意勾选 QQ截图20161213102746.jpg QQ截图20161213102028.jpg 点击此处,查看STM32F769I开发板官方资源。 本帖最后由 65387468@qq.com 于 2016-12-13 10:28 编辑

回复评论 (1)

学习了,正好要用到串口
点赞  2016-12-21 14:08
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复