我实在搞不清USART_GetITStatus()这语句用才好,我用它有时能接收到信息,有时不行
#include "stm32f10x.h" // Device header
#include "stdio.h"
void NVIC_USART_Configuration()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
void USART1_Configuration()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10éèÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//PA9éèÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = 8;
USART_InitStructure.USART_StopBits = 1;
USART_InitStructure.USART_Parity =USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
void delay_ms(u16 ms)
{
int i;
while(--ms)
for(i=0;i<12000;i++);
}
int main()
{
int i;
char TXBUF1[]={"1255?"};
NVIC_USART_Configuration();
USART1_Configuration();
while(1)
{
USART_ClearFlag(USART1,USART_FLAG_TC);
for(i=0;TXBUF1
!=' ';i++)
{
USART_SendData(USART1,TXBUF1);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}
delay_ms(2000);
}
}
void USART1_IRQHandler(void)
{
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ClearITPendingBit(USART1,USART_IT_TC);
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)//
{
USART_SendData(USART1,USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);//
}
}
<------------主要问题出在这
这程序我在串口助手里发送数据,串口接收不到返回的信息
一周热门 更多>