初级会员

- 积分
- 81
- 金钱
- 81
- 注册时间
- 2016-12-19
- 在线时间
- 50 小时
|
5金钱
如题:
用的stm32f103c8t6,串口usart1往PC上发数据,大概快一分钟开始乱码(见附件图片),点一下“手动发送”会恢复正常,过个快一分钟又开始乱码,再点“手动发送”又恢复正常,一直这样。
求大大们帮我看看我的代码是不是有问题,还有可能是哪里出了问题,目前猜想是不是中断的问题,可是不知道中断跟usart有什么关系。
PS:
波特率设置的9600,一开始设置的115200数据完全乱码。
这是我的代码:
[mw_shl_code=c,true]int main(void)
{
char temp = 0;
char buf[] = {"Usart_SendData function!"};
/* 设置时钟频率为 70M */
SystemInit();
/* USART1 config 9600 8-N-1 */
USART1_Config();
while (1)
{
for (temp=0; temp<24; temp++)
{
USART_SendData(USART1, buf[temp]);
while( USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET );
}
mydelay_ms(2000);
}
}
[/mw_shl_code]
[mw_shl_code=c,true]void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 9600;
//USART_InitStructure.USART_BaudRate = 115200;
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);
}[/mw_shl_code]
|
-
最佳答案
查看完整内容[请看2#楼]
#include "stm32f10x.h"
此代码是103C8T6 供参考 如果还有问题 查一下时钟
void UART1_init(void)
{
/* The following example illustrates how to configure the USART1 */
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USA ...
|