stm32f10x 串口能发不能接受
时间:10-02
整理:3721RD
点击:
和电脑通讯,电脑用的是secureCRT。电脑上能接受到 板子发送的字符,但电脑发的,板子却接受不到。
大神帮忙看看出什么问题了
初始化:
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* ê1?ü USART1 ê±?ó*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/* USART1 ê1ó?IO???ú???? */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?í?íìê?3?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????ê?è?
GPIO_Init(GPIOA, &GPIO_InitStructure); //3?ê??ˉGPIOA
/* USART1 1¤×÷?£ê????? */
USART_InitStructure.USART_BaudRate = 115200; //2¨ì??êéè??£o115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //êy?Y??êyéè??£o8??
USART_InitStructure.USART_StopBits = USART_StopBits_1; //í£?1??éè??£o1??
USART_InitStructure.USART_Parity = USART_Parity_No ; //ê?·?????D£?é£o?T
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ClearFlag(USART1, USART_IT_RXNE);
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ClearFlag(USART1, USART_FLAG_RXNE);
USART_Cmd(USART1, ENABLE);
}
读写方法:
void UART1SendByte(unsigned char SendData)
{
USART_SendData(USART1,SendData);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
unsigned char UART1GetByte(unsigned char* GetData)
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{ return 0;//??óDê?μ?êy?Y
}
*GetData = USART_ReceiveData(USART1);
return 1;//ê?μ?êy?Y
}
用户任务:
void Task_232(void *p_arg)
{
unsigned char *GetData;
while (1)
{
while(UART1GetByte(GetData))
{
UART1SendByte('9');
}
OSTimeDlyHMSM(0, 0,0,1);
}
}
大神帮忙看看出什么问题了
初始化:
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* ê1?ü USART1 ê±?ó*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/* USART1 ê1ó?IO???ú???? */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?′ó?í?íìê?3?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????ê?è?
GPIO_Init(GPIOA, &GPIO_InitStructure); //3?ê??ˉGPIOA
/* USART1 1¤×÷?£ê????? */
USART_InitStructure.USART_BaudRate = 115200; //2¨ì??êéè??£o115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //êy?Y??êyéè??£o8??
USART_InitStructure.USART_StopBits = USART_StopBits_1; //í£?1??éè??£o1??
USART_InitStructure.USART_Parity = USART_Parity_No ; //ê?·?????D£?é£o?T
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ClearFlag(USART1, USART_IT_RXNE);
USART_ClearFlag(USART1, USART_FLAG_TC);
USART_ClearFlag(USART1, USART_FLAG_RXNE);
USART_Cmd(USART1, ENABLE);
}
读写方法:
void UART1SendByte(unsigned char SendData)
{
USART_SendData(USART1,SendData);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
unsigned char UART1GetByte(unsigned char* GetData)
{
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{ return 0;//??óDê?μ?êy?Y
}
*GetData = USART_ReceiveData(USART1);
return 1;//ê?μ?êy?Y
}
用户任务:
void Task_232(void *p_arg)
{
unsigned char *GetData;
while (1)
{
while(UART1GetByte(GetData))
{
UART1SendByte('9');
}
OSTimeDlyHMSM(0, 0,0,1);
}
}
大哥,貌似你的串口接收中断函数没有哦。还有一般情况下,串口的发送还是不用中断的,免得麻烦。
中断部分:
void F_232_1_Handler (void)
{
// OS_CPU_SR cpu_sr;
OSIntEnter();
temp_U1 = 0x0;
//OS_ENTER_CRITICAL(); /* Tell uC/OS-II that we are starting an ISR */
// OSIntNesting++;
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);;
temp_U1 = USART_ReceiveData(USART1);
}
//OS_EXIT_CRITICAL();
OSIntExit(); /* Tell uC/OS-II that we are leaving the ISR */
}
