項(xiàng)目開發(fā)中,當(dāng)出現(xiàn)bug時(shí),由于不知道某個(gè)變量的值,所以很難定位問題,針對此問題,串口調(diào)試脫穎而出。通過串口printf()實(shí)時(shí)將需要顯示的信息打印出來,這樣就很方便的定位問題。
串口設(shè)置方法
1.購買調(diào)試器pwlink2。參考STM32F103C8T6程序燒錄方法_stm32f103c8t6如何燒錄_流浪法師解剖魚的博客-CSDN博客
2.下載Power??Writer燒錄調(diào)試軟件。
3.編寫代碼,配置串口,編寫要打印的東西,通過printf();代碼部分見最后。
4.連線,STM32F103C8T6我配置的PA9 和PA10分別位TX RX,連接到燒錄器pwlink的RX和TX端。
5.下載程序。
6.打開Power??Writer,點(diǎn)擊連接設(shè)備。成功后點(diǎn)擊串口。
?7.配置串口波特率為9600,再高顯示不出東西。然后打開串口即可。
都看到這里了幫忙點(diǎn)個(gè)贊?。?!
下面是串口的配置代碼。
usart.c
#include "usart.h"
int fputc(int ch,FILE *p)
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
void USART1_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //TX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //RX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=bound;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void)
{
u8 r;
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)
{
r=USART_ReceiveData(USART1);
USART_SendData(USART1,r);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET)
{
}
}
USART_ClearFlag(USART1,USART_FLAG_TC);
}
usart.h文章來源:http://www.zghlxwxcb.cn/news/detail-521865.html
#ifndef _usart_H
#define _usart_H
#include "system.h"
#include "stdio.h"
void USART1_Init(u32 bound);
#endif
main文章來源地址http://www.zghlxwxcb.cn/news/detail-521865.html
Init();
printf("starting stm32 system loding\r\n");
到了這里,關(guān)于STM32F103C8T6串口調(diào)試篇的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!