在线时间1 小时
UID417263
ST金币0
蝴蝶豆0
注册时间2013-2-20
新手上路
- 最后登录
- 2018-7-12
|
a0a.1 0b0c
各位我用查询方式接收串口数据,结果接收不到:期待高人指点。
串口配置如下:
/* USART1 config 9600 8-N-1 */
USART_InitTypeDef USART_InitStructure;
/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/* USART1 GPIO config */
/* Configure USART1 Rx PA10 input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA9) as alternate 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);
/* USART1 mode config */
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
// USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
USART_Cmd(USART1, ENABLE);
代码如下:
void Commun_232_Receive(void)
{
static uint16_t Data_Rec[4]={0,0,0,0};
static uint16_t FrameHead=0;
static uint16_t Data_Num=0;
static uint16_t i=0,j=0,k=0;
uint16_t crc=0,crc1=0;
//===============================
// Receive_Status= USART_GetITStatus(USART1, USART_IT_RXNE);
Receive_Status= USART_GetFlagStatus(USART1, USART_FLAG_RXNE);
// if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) = 1)
if(Receive_Status==1)//有数据
{
Data_receive = USART_ReceiveData(USART1);//接收数据
Data_receive&=0xFF;
// USART_ClearFlag(USART1,USART_FLAG_RXNE);//清除接收非空标志位
if(FrameHead==0&&Data_receive==0x35)//找帧头
{
FrameHead=1; //找到帧头
}
else if(FrameHead==1)
{
Data_Rec[Data_Num]=Data_receive;
Data_Num++;
if(Data_Num>4)
{
Data_Num=0;
FrameHead=0;
Data_Rec[2] = Data_Rec[2] BSRR = 0x00000008;
/* Set PA3 */
Switch_Status.bit.TA0=1;
}
else if(Data_Rec[1]==1)
{
GPIOA->BRR = 0x00000008;
/* Reset PA3 */
Switch_Status.bit.TA1=1;
}
break;
}
case 2:
{
if(Data_Rec[1]==0)
{
GPIOA->BSRR = 0x00000010;
/* Set PA4 */
Switch_Status.bit.TA2=1;
}
else if(Data_Rec[1]==1)
{
GPIOA->BRR = 0x00000010;
/* Reset PA4 */
Switch_Status.bit.TA3=1;
}
break;
}
case 3:
{
if(Data_Rec[1]==0)
{
GPIOA->BSRR = 0x00000020;
/* Set PA5 */
Switch_Status.bit.TA4=1;
}
else if(Data_Rec[1]==1)
{
GPIOA->BRR = 0x00000020;
/* Reset PA5 */
Switch_Status.bit.TA5=1;
}
break;
}
case 4:
{
if(Data_Rec[1]==0)
{
GPIOA->BSRR = 0x00000040;
/* Set PA6 */
Switch_Status.bit.TA6=1;
}
else if(Data_Rec[1]==1)
{
GPIOA->BRR = 0x00000040;
/* Reset PA6 */
Switch_Status.bit.TA7=1;
}
break;
}
case 5:
{
if(Data_Rec[1]==0)
{
GPIOA->BSRR = 0x00000080;
/* Set PA7 */
Switch_Status.bit.TA8=1;
}
else if(Data_Rec[1]==1)
{
GPIOA->BRR = 0x00000080;
/* Reset PA7 */
Switch_Status.bit.TA9=1;
}
break;
}
}
}
}
} |
|