#include "rs485.h"
#include "stm32f10x.h"
#include "misc.h"
#include "stm32f10x_rcc.h"
#if EN_USART2_RX //如果使能了接收
u8 RS485_RX_BUF[64]; //接收缓冲区
u8 RS485_RX_CNT=0;
//串口1中断服务函数
void USART2_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
res=USART_ReceiveData(USART2);//读取接收到的数据
if(RS485_RX_CNT<64)
{
RS485_RX_BUF[RS485_RX_CNT]=res;
RS485_RX_CNT++;
}
}
}
#endif
void delay(u32 nTimer)
{ int k,j;
for (k=0;k<nTimer;k++)
for (j=0;j<100;j++);
}
//初始化串口
//bound 波特率
void RS485_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //PA4为推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //PA2复用推挽
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;//PA3浮空输入
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE); //复位串口2
RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止复位
USART_InitStructure.USART_BaudRate=bound; //波特率
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(USART2,&USART_InitStructure);
USART_Cmd(USART2,ENABLE); //使能串口
USART_ClearFlag(USART2, USART_FLAG_TC);//清除标志位
#ifdef EN_USART2_RX //如果使能了接收
//USART2 NVIC配置
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断分组2 16个优先级,高两位抢占式,低两位响应
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn; //使能串口2中断
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority=3;//子优先级3
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //开启中断
#endif
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //默认为接收模式 ,即端口输出低电平
}
//RS485发送len个字节,其中buf为发送首地址,len为发送字节数(不要超过64个)
void RS485_Send_data(u8 *buf,u8 len)
{
u8 t;
GPIO_SetBits(GPIOA,GPIO_Pin_4); //设置为发送模式
for(t=0;t<len;t++)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
USART_SendData(USART2,buf[t]);
}
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
RS485_RX_CNT=0;
GPIO_ResetBits(GPIOA,GPIO_Pin_4); //设置为接收模式
}
//RS485查询接受到的数据,其中buf为接收缓存首地址,len为读到的数据长度
void RS485_Receive_data(u8 *buf,u8 *len)
{
u8 rxlen=RS485_RX_CNT;
u8 i=0;
*len=0;
delay(5000); //这里还要改??? //等待10ms,连续超过10ms没有接收到一个数据,就定义为接收结束
if(rxlen==RS485_RX_CNT&&rxlen)
{
for(i=0;i<rxlen;i++)
{
buf[i]=RS485_RX_BUF[i];
}
}
*len=RS485_RX_CNT;
RS485_RX_CNT=0;
}
|