中级会员
 
- 积分
- 317
- 金钱
- 317
- 注册时间
- 2015-11-4
- 在线时间
- 51 小时
|

楼主 |
发表于 2016-4-25 10:29:02
|
显示全部楼层
485.c:
void USART2_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
//LED2(ON);
res =USART_ReceiveData(USART2);
// USART_SendData(USART1,res);
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
if(Uart1_rev_flag != 1)
{
if(recenum < 12)
{
ReceBuf[recenum] = res;
// USART_SendData(USART1,ReceBuf[recenum]);
recenum++;
usDataLen = recenum - 2;
TIM_Cmd(TIM3, ENABLE);
TIM_SetCounter(TIM3, 0);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
}
else
{
recenum=0;
TIM_Cmd(TIM3, ENABLE);
TIM_SetCounter(TIM3, 0);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
}
}
else
{
USART_ReceiveData(USART2);
}
}
if(USART_GetFlagStatus(USART2,USART_FLAG_ORE)!=RESET)
{
USART_ClearFlag(USART2,USART_FLAG_ORE);
USART_ReceiveData(USART2);
}
}
//#endif
void RS485_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOG,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = bound;ë
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(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2, USART_FLAG_TC);
#if EN_USART2_RX
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
RS485_TX_EN=0;
} |
|