本节我们测试NUCLEO-F412ZG的串口,在测试工程中由于没有注意,发现串口发送的数据乱码,后来才发现是数据位数和奇偶检验的问题,大家在运用过程中要注意是否使用奇偶校验,奇偶校验为包含在数据长度中。程序说明如下
int main(void)
{
/* STM32F4xx HAL library initialization:
- Configure the Flash prefetch
- Systick timer is configured by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and
handled in milliseconds basis.
- Set NVIC Group Priority to 4
- Low Level Initialization
*/
HAL_Init();//初始化HAL库
/* Configure the system clock to 100 MHz */
SystemClock_Config();//配置系统时钟为100MHz
/* Initialize BSP Led for LED2 */
BSP_LED_Init(LED2);//初始化LED2
/*##-1- Configure the UART peripheral ######################################*/
/* Put the USART peripheral in the Asynchronous mode (UART Mode) */
/* UART configured as follows:
- Word Length = 8 Bits (7 data bit + 1 parity bit) :
BE CAREFUL : Program 7 data bits + 1 parity bit in PC HyperTerminal
- Stop Bit = One Stop bit
- Parity = ODD parity
- BaudRate = 9600 baud
- Hardware flow control disabled (RTS and CTS signals) */
UartHandle.Instance = USARTx;//串口号为USARTx
/* Output a message on Hyperterminal using printf function */
printf("nr UART Printf Example: retarget the C library printf function to the UARTnr");//打印数据
printf("** Test finished successfully. ** nr");