如题:
用的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]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
此代码是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;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
// USART_InitStructure.USART_Clock = USART_Clock_Disable;
// USART_InitStructure.USART_CPOL = USART_CPOL_High;
// USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
// USART_InitStructure.USART_LastBit = USART_LastBit_Enable;
USART_Init(USART1, &USART_InitStructure);
USART_SetPrescaler(USART1,8); //USART1分频系数
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能串口接受中断
USART_Cmd(USART1, ENABLE);
}
void UART1_NVIC_config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the Priority Grouping with 1 bit */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
/* Enable and set EXTI11 Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UART1_send_data(u8 data)
{
USART_SendData(USART1, data);
while(!USART_GetFlagStatus(USART1, USART_FLAG_TC)); //清除标志的第一步
}
编译有问题,不知道怎么解决,啊,心好累。谢谢你帮我看了,分数还是给你吧。
一周热门 更多>