程序如下:
void bsp_RS485_IRQHandler(void)//串口中断部分
{
//判断有无溢出
if(USART_GetITStatus(RS485_usart, USART_IT_ORE)==SET)
{
USART_ClearITPendingBit(RS485_usart, USART_IT_ORE);
USART_ReceiveData(RS485_usart);
}
if(USART_GetITStatus(RS485_usart, USART_IT_RXNE) != RESET)
{
uart_buff[uart_p] = USART_ReceiveData(RS485_usart);
uart_p++;
}
USART_ClearITPendingBit(RS485_usart, USART_IT_RXNE);
}
if(uart_p == 9)
{
recivestate = 1;//接收到完整的数据时,将接受分析标志位置一
len = uart_p;
for(i = 0;i<uart_p;i++)//´«³öÊý¾Ý
{
uart_rx_buff[i] = uart_buff[i];//转移数据
}
//clean_rebuff();
}
}
void main()
{
RS485_init();
u16 sum=0;
void test()
{
u8 txbuff[1024];
u8 buff[2] = {0x01,0x02};
u16 i,cnt = 0;
if(recivestate == 1)
{
recivestate = 0;
for(i=1;i
{
sum += uart_rx_buff[i];
}
if(uart_rx_buff[cnt] == title1_send && uart_rx_buff[cnt+1] == title2_send)
{
if(uart_rx_buff[cnt+5] == (sum&0xff) && uart_rx_buff[cnt+6] == ((sum>>8)&0xff))
{
switch(uart_rx_buff[cnt+2])
{
case 0x08: //选择功能
RS485_TX_EN();//切换到发送
RESEND(0x08,len);
//RS485_delay(10000);
RS485_RX_EN();
break;
}
}
}
void RESEND(u8 cmd,u8 L)//发送内容
{
u8 data_send[50];
u8 cnt = 0;
u16 i ,sum;
data_send[cnt++] = title1_send;
data_send[cnt++] = title2_send;
data_send[cnt++] = cmd;
data_send[cnt++] = L;
for(i=1;i
{
sum += data_send[i];
}
data_send[cnt++] = (sum&0xff);
data_send[cnt++] = ((sum>>8)&0xff);
data_send[cnt++] = 0X0D;
data_send[cnt++] = 0X0A;
USART_send_array(RS485_usart,data_send,cnt-1);
}
//发送数组
void USART_send_array(USART_TypeDef *pUSARTx,u8 *array,u8 len)
{
u8 i=0;
while(i<len)
{
USART_SendData(pUSARTx,array[i]);
while( USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)!= SET);
i++;
}
}
}
配置文件
void RS485_configure()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//配置时钟
RCC_APB2PeriphClockCmd(RS485_usart_clk,ENABLE);
RCC_APB2PeriphClockCmd(RS485_USART_RX_GPIO_CLK,ENABLE);
RCC_APB2PeriphClockCmd(RS485_USART_TX_GPIO_CLK,ENABLE);
RCC_APB2PeriphClockCmd(RS485_USART_PE_GPIO_CLK,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//配置引脚
GPIO_InitStructure.GPIO_Pin=RS485_usart_TX_PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(RS485_USART_TX_GPIO_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=RS485_usart_RX_pin;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(RS485_USART_RX_GPIO_PORT,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=RS485_RE_PIN;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(RS485_RE_GPIO_PORT,&GPIO_InitStructure);
//配置中断优先级
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = RS485_INT_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//使能中断
USART_ITConfig(RS485_usart, USART_IT_RXNE, ENABLE);
USART_ITConfig(RS485_usart, USART_IT_IDLE, ENABLE);
USART_ITConfig(RS485_usart, USART_IT_ORE, ENABLE);
//设置为接收状态
GPIO_ResetBits(RS485_RE_GPIO_PORT,RS485_RE_PIN);
}
中断里接收到的数据是完整的,比如接收到了3A 16 08 01 00 .... 0D 0A,我想mcu返回3A 16 09 02.....,全速运行后结果只返回了一个3A,然后程序卡住了,之后再怎么发都不返回了,停下来就发现一直在中断里面。我查了下数据,数据也有传到uart_rx_buff[]里面了。求各位大神帮帮忙!!!!!!(还有一点,我按照网上的说法在发送后面加了个延时函数,就发送了3A 16,之后无论怎么加大延时函数也是发这两个。)