|
- #include "sys.h"
- #include "rs485.h"
- #include "delay.h"
- #include "CRC16.h"
- #include "math.h"
- #include "stdio.h"
- #define pi acos(-1)
- #ifdef EN_USART2_RX //如果使能了接收
- //接收緩存區(qū)
- u8 RS485_RX_BUF[64]; //接收緩沖,最大64個(gè)字節(jié).
- //接收到的數(shù)據(jù)長(zhǎng)度
- u8 RS485_RX_CNT=0;
- void USART2_IRQHandler(void)
- {
- u8 res;
-
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收到數(shù)據(jù)
- {
-
- res =USART_ReceiveData(USART2); //讀取接收到的數(shù)據(jù)
- if(RS485_RX_CNT<64)
- {
- RS485_RX_BUF[RS485_RX_CNT]=res; //記錄接收到的值
- RS485_RX_CNT++; //接收數(shù)據(jù)增加1
- }
- }
- }
- #endif
- //初始化IO 串口2
- //pclk1:PCLK1時(shí)鐘頻率(Mhz)
- //bound:波特率
- void RS485_Init(u32 bound)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOD, ENABLE);//使能GPIOA,D時(shí)鐘
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2時(shí)鐘
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //PD7端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;//PA3
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,ENABLE);//復(fù)位串口2
- RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2,DISABLE);//停止復(fù)位
-
-
- #ifdef EN_USART2_RX //如果使能了接收
- USART_InitStructure.USART_BaudRate = bound;//波特率設(shè)置
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位數(shù)據(jù)長(zhǎng)度
- USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個(gè)停止位
- USART_InitStructure.USART_Parity = USART_Parity_No;///奇偶校驗(yàn)位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無(wú)硬件數(shù)據(jù)流控制
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//收發(fā)模式
- USART_Init(USART2, &USART_InitStructure); ; //初始化串口
-
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //使能串口2中斷
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占優(yōu)先級(jí)2級(jí)
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //從優(yōu)先級(jí)2級(jí)
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中斷通道
- NVIC_Init(&NVIC_InitStructure); //根據(jù)NVIC_InitStruct中指定的參數(shù)初始化外設(shè)NVIC寄存器
-
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//開(kāi)啟中斷
-
- USART_Cmd(USART2, ENABLE); //使能串口
- #endif
- RS485_TX_EN=0; //默認(rèn)為接收模式
-
- }
- //RS485發(fā)送len個(gè)字節(jié).
- //buf:發(fā)送區(qū)首地址
- //len:發(fā)送的字節(jié)數(shù)(為了和本代碼的接收匹配,這里建議不要超過(guò)64個(gè)字節(jié))
- void RS485_Send_Data(u8 *buf,u8 len)
- {
- u8 t;
- RS485_TX_EN=1; //設(shè)置為發(fā)送模式
- for(t=0;t<len;t++) //循環(huán)發(fā)送數(shù)據(jù)
- {
- 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;
- RS485_TX_EN=0; //設(shè)置為接收模式
- }
- //RS485查詢(xún)接收到的數(shù)據(jù)
- //buf:接收緩存首地址
- //len:讀到的數(shù)據(jù)長(zhǎng)度
- void RS485_Receive_Data(u8 *buf,u8 *len)
- {
- u8 rxlen=RS485_RX_CNT;
- u8 i=0;
- *len=0; //默認(rèn)為0
- delay_ms(10); //等待10ms,連續(xù)超過(guò)10ms沒(méi)有接收到一個(gè)數(shù)據(jù),則認(rèn)為接收結(jié)束
- if(rxlen==RS485_RX_CNT&&rxlen)//接收到了數(shù)據(jù),且接收完成了
- {
- for(i=0;i<rxlen;i++)
- {
- buf[i]=RS485_RX_BUF[i];
- }
- *len=RS485_RX_CNT; //記錄本次數(shù)據(jù)長(zhǎng)度
- RS485_RX_CNT=0; //清零
- }
- }
- u8 RS485_correspondence(u8 addr)
- {
- u8 rs485buf[8];
- u8 crc485[5];
- u8 data1,data2;
- u8 len1,len2;
- u8 k,i;
- u8 cnt;
- u16 crc;
- u8 crc1,crc2;
- u8 crc3,crc4;
- rs485buf[0] = addr;
- RS485_Send_Data(rs485buf,1); //地址發(fā)送
- RS485_Receive_Data(rs485buf,&len1); //接收從機(jī)回饋
- if(len1)
- {
- for(i=0;i<7;i++)
- {
- printf("%d\r\t",rs485buf[i]);
- }
- data1 = rs485buf[0];
- if(addr == data1)//判斷從機(jī)是否準(zhǔn)備就緒
- {
- printf("接收到從機(jī)信號(hào)\r\n");
- data2 = rs485buf[1];
- len2 = rs485buf[2];//測(cè)量數(shù)據(jù)長(zhǎng)度
- if(data2 == 0xAA)
- {
- for(k = 0;k < len2;k++)
- {
- cnt=cnt+rs485buf[3+k];
- crc485[k] = rs485buf[3+k];
- }
- }
- if(data2 == 0xBB)
- {
- for(k = 0;k < len2;k++)
- {
- cnt=cnt+rs485buf[3+k];
- crc485[k] = rs485buf[3+k];
- cnt = -cnt;
- }
- }
- crc=GetCRC16(crc485,len2);
- crc2 = crc>>8;
- crc1 = crc&0xff;
- crc3 = rs485buf[3+k];
- crc4 = rs485buf[4+k];
- if(crc1==crc3&&crc2==crc4)
- {
- printf("CRC16校驗(yàn)正確\r\n");
- }
- else
- {
- static int flag3;
- printf("CRC16校驗(yàn)錯(cuò)誤,重新發(fā)送數(shù)據(jù)\r\n");
- flag3++;
- if(flag3 == 3)
- {
- printf("數(shù)據(jù)發(fā)送有誤,請(qǐng)重新檢查\r\n");
- flag3 = 0;
- }
- return 0;
- }
- }
- else //從機(jī)錯(cuò)誤
- {
- static int flag1;
- printf("從機(jī)未準(zhǔn)備就緒\r\n");
- flag1++;
- if(flag1 == 3)
- {
- printf("通信錯(cuò)誤,請(qǐng)重新檢查設(shè)備\r\n");
- flag1 = 0;
- }
- return 0;
- }
- len1 = 0;
- }
- return cnt;
- }
復(fù)制代碼 |
|