//串口5的初始化
void Init_Uart5(u16 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART5|LIN_BUS_RX_GPIO_CLK|LIN_BUS_TX_GPIO_CLK|RCC_APB2Periph_AFIO, ENABLE); //使能USART1,GPIOA时钟
USART_DeInit(UART5); //复位串口5
//UART5_TX PC.12
GPIO_InitStructure.GPIO_Pin = LIN_BUS_TX_PIN; //PC.12
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(LIN_BUS_TX_GPIO, &GPIO_InitStructure); //初始化PC12
//UART5_RX PD.2
GPIO_InitStructure.GPIO_Pin = LIN_BUS_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(LIN_BUS_RX_GPIO, &GPIO_InitStructure); //初始化PD2
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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(UART5, &USART_InitStructure); //初始化串口
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
USART_ITConfig(UART5, USART_IT_TC, ENABLE);
USART_Cmd(UART5, ENABLE); //使能串口
}
void UART5_IRQHandler(void) //串口1中断服务程序
{
u8 Res;
if(USART_GetFlagStatus(UART5, USART_FLAG_NE) != RESET)
{//同 @arg USART_IT_NE : Noise Error interrupt
USART_ClearFlag(UART5, USART_FLAG_NE);
}
if(USART_GetFlagStatus(UART5, USART_FLAG_FE) != RESET)
{//同 @arg USART_IT_FE : Framing Error interrupt
USART_ClearFlag(UART5, USART_FLAG_FE);
}
if(USART_GetFlagStatus(UART5, USART_FLAG_PE) != RESET)
{//同 @arg USART_IT_PE : Parity Error interrupt
USART_ClearFlag(UART5, USART_FLAG_PE);
}
//注意!不能使用if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)来判断
if (USART_GetFlagStatus(UART5, USART_FLAG_ORE) != RESET)
{
Res =USART_ReceiveData(UART5);//(USART1->DR); //
USART_ClearITPendingBit(UART5, USART_IT_ORE);
}
if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) //
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
Res =USART_ReceiveData(UART5);//(USART1->DR); //读取接收到的数据
if(Pc_rx_sta)
{
if((Res!=UART_STAR_CHAR)&&(Res!=UART_STOP_CHAR))
checksum+=Res;
if(Res==UART_MID_CHAR)
{
switch(Pc_rx_buf[Pc_RxLen-1])
{
case 0x00:Pc_rx_buf[Pc_RxLen-1]=0x1a;break;
case 0x01:Pc_rx_buf[Pc_RxLen-1]=0x1b;break;
case 0x02:Pc_rx_buf[Pc_RxLen-1]=0x1c;break;
default :Pc_rx_buf[Pc_RxLen ]=Res;Pc_RxLen++;break;
}
}else if(Res==UART_STOP_CHAR)
{
//收到 地址+数据+校验
if(Pc_ok_**==0)
{
memcpy(Pc_rx_buf1,Pc_rx_buf,Pc_RxLen);
Pc_RxLen1=Pc_RxLen;
Pc_ok_**=1;
}
}else if(Res==UART_STAR_CHAR)
{
Pc_rx_sta=1;
Pc_RxLen=0;
checksum=0;
}else
{
Pc_rx_buf[Pc_RxLen++] = Res; //将接收到的字符串存到缓存中
}
//缓存指针向后移动
if(Pc_RxLen>=USART_RX_LEN) //如果缓存满,将缓存指针指向缓存的首地址
{
Pc_RxLen = 0;
Pc_rx_sta=0;
checksum=0;
}
}else if(Res==UART_STAR_CHAR)
{
Pc_rx_sta=1;
Pc_RxLen=0;
checksum=0;
}
}
Pc_Zhong++;
if(USART_GetITStatus(UART5,USART_IT_TC))
{
//Pc_Zhong++;
USART_ClearITPendingBit(UART5,USART_IT_TC);
if(Pc_busyflag)
{
if(Pc_send_cnt >=USART_REC_LEN)
{
Pc_send_cnt=1;
Pc_sendlen=0;
Pc_busyflag=0;
}else if(Pc_send_cnt <Pc_sendlen)
{
USART_SendData(UART5,Pc_tx_buf[Pc_send_cnt++]);
}
else
{
Pc_send_cnt=1;
Pc_sendlen=0;
Pc_busyflag=0;
}
}else
{
Pc_send_cnt=1;
Pc_sendlen=0;
Pc_busyflag=0;
}
}
}
中断函数
|