#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;
}
|