新手上路
- 积分
- 43
- 金钱
- 43
- 注册时间
- 2020-11-3
- 在线时间
- 10 小时
|

楼主 |
发表于 2021-7-26 17:02:09
|
显示全部楼层
void USART3_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART3,&USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(USART3, ENABLE);//使能串口
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断,接收中断数据
}
void USART3_IRQHandler(void)
{
static uint8_t rebuf[9]={0},i=0;
if(USART_GetITStatus(USART3,USART_IT_RXNE) != RESET)
{
num1=1;
rebuf[i++]=USART_ReceiveData(USART3);
// if(rebuf[0]!=0x5A)
// i=0;
// if((i==2)&&(rebuf[1]!=0x55))
// i=0;
if(i>=8)
{
for(i = 0;i <= 8;i++)
{
LAN_Rx_BUF = rebuf;
}
i = 0;
}
USART_ClearFlag(USART3,USART_FLAG_RXNE);
}
} |
|