RS485通讯实验
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? RS485通訊實(shí)驗(yàn)
1.硬件:
STM32的串口管教加一個485芯片控制IO:
還需要一個USB 轉(zhuǎn)485轉(zhuǎn)換器,一端接485AB,USB口插PC上用串口助手查看數(shù)據(jù)。
注意點(diǎn):以板子為核心,PC12置1為向外發(fā)送數(shù)據(jù),PC12置0為向內(nèi)接收數(shù)據(jù)。
2.代碼,效果就是串口3發(fā)送5字節(jié)數(shù)據(jù)后收到同樣5字節(jié)數(shù)據(jù)的返回:
rs485.h文件: #ifndef RS485_RS485_H_ #define RS485_RS485_H_#include "Sys.h"u8 RS485_RX_CNT; //接收緩存區(qū) u8 RS485_RX_BUF[64]; //接收緩沖,最大64個字節(jié).;void RS485_Send_Data(u8 *buf,u8 len); void RS485_Receive_Data(u8 *buf,u8 *len);#endif /* RS485_RS485_H_ */rs485.c文件: #include "rs485.h" #include "Uart.h" volatile unsigned char RS485_REC_Flag = 0; //接收緩存區(qū) u8 RS485_RX_BUF[64]; //接收緩沖,最大64個字節(jié). //接收到的數(shù)據(jù)長度 u8 RS485_RX_CNT=0;//RS485發(fā)送len個字節(jié). //buf:發(fā)送區(qū)首地址 //len:發(fā)送的字節(jié)數(shù)(為了和本代碼的接收匹配,這里建議不要超過64個字節(jié)) void RS485_Send_Data(u8 *buf,u8 len) {u8 t;GPIO_SetBits(GPIOC,GPIO_Pin_12);for(t=0;t<len;t++) //循環(huán)發(fā)送數(shù)據(jù){while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);USART_SendData(USART3,buf[t]);}while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);RS485_RX_CNT=0;GPIO_ResetBits(GPIOC,GPIO_Pin_12); } //RS485查詢接收到的數(shù)據(jù) //buf:接收緩存首地址 //len:讀到的數(shù)據(jù)長度 void RS485_Receive_Data(u8 *buf,u8 *len) {u8 rxlen=RS485_RX_CNT;u8 i=0;u8 lenx = 5;*len=0; //默認(rèn)為0delay_ms(100); //等待10ms,連續(xù)超過10ms沒有接收到一個數(shù)據(jù),則認(rèn)為接收結(jié)束if(rxlen==RS485_RX_CNT&&rxlen)//接收到了數(shù)據(jù),且接收完成了{(lán)for(i=0;i<rxlen;i++){buf[i]=RS485_RX_BUF[i];}RS485_Send_Data(buf,lenx);*len=RS485_RX_CNT; //記錄本次數(shù)據(jù)長度RS485_RX_CNT=0; //清零} }void USART3_IRQHandler(void) {u8 res;if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //接收到數(shù)據(jù){res =USART_ReceiveData(USART3); //讀取接收到的數(shù)據(jù)if(RS485_RX_CNT<64){RS485_RX_BUF[RS485_RX_CNT]=res; //記錄接收到的值RS485_RX_CNT++; //接收數(shù)據(jù)增加1}} }Uart.h文件: #ifndef UART_UART_H_ #define UART_UART_H_#include "Sys.h"#define USART_REC_LEN 200 //定義最大接收字節(jié)數(shù) 200u16 USART_RX_STA; //接收狀態(tài)標(biāo)記 u8 USART_RX_BUF[USART_REC_LEN]; //接收緩沖,最大USART_REC_LEN個字節(jié).void uart_init(u32 bound); void uart3_init(u32 bound);#endif /* UART_UART_H_ */Uart.c文件: #include "Uart.h" void uart_init(u32 bound) {//USART 初始化設(shè)置USART_InitTypeDef USART_InitStructure;USART_InitStructure.USART_BaudRate = bound;//串口波特率USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位數(shù)據(jù)格式USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗(yàn)位USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式USART_Init(UART5, &USART_InitStructure); //初始化串口1USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//開啟串口接受中斷USART_Cmd(UART5, ENABLE); //使能串口1}void uart3_init(u32 bound) {//USART 初始化設(shè)置USART_InitTypeDef USART_InitStructure;USART_InitStructure.USART_BaudRate = bound;//串口波特率USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位數(shù)據(jù)格式USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗(yàn)位USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式USART_Init(USART3, &USART_InitStructure); //初始化串口1USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//開啟串口接受中斷USART_Cmd(USART3, ENABLE); //使能串口1USART_ClearITPendingBit(USART3, USART_IT_TC);//清除中斷TC位}Sys.h文件: #ifndef SYS_SYS_H_ #define SYS_SYS_H_#include <stdio.h> #include <string.h> #include "stm32f10x.h"#define True (1) #define False (0) typedef uint32_t u32;///32位void delay_init(void); void RCC_Configuration(void); void GPIO_RemapConfig(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void systemInit(void); void delay_ms(u16 nms);#endif /* SYS_SYS_H_ */Sys.c文件: #include "Sys.h" #include "Uart.h"static u8 fac_us=0; //us延時倍乘數(shù) static u16 fac_ms=0; //ms延時倍乘數(shù),在ucos下,代表每個節(jié)拍的ms數(shù) //初始化延遲函數(shù) //當(dāng)使用OS的時候,此函數(shù)會初始化OS的時鐘節(jié)拍 //SYSTICK的時鐘固定為HCLK時鐘的1/8 //SYSCLK:系統(tǒng)時鐘 void delay_init() {SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); //選擇外部時鐘 HCLK/8fac_us=SystemCoreClock/8000000; //為系統(tǒng)時鐘的1/8fac_ms=(u16)fac_us*1000; //非OS下,代表每個ms需要的systick時鐘數(shù) }void RCC_Configuration(void) {RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC, ENABLE); //使能USART1,GPIOA時鐘RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);}void GPIO_Configuration(void) {GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOA.9//USART1_RX GPIOA.10初始化GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOA.10//USART1_RX GPIOA.10初始化GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//浮空輸入GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIOA.10}void GPIO_RemapConfig(void) {GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); }void NVIC_Configuration(void) {NVIC_InitTypeDef NVIC_InitStructure;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設(shè)置NVIC中斷分組2:2位搶占優(yōu)先級,2//中斷優(yōu)先級NVIC設(shè)置NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; //TIM3中斷NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占優(yōu)先級0級NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //從優(yōu)先級3級NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器}//延時nms //注意nms的范圍 //SysTick->LOAD為24位寄存器,所以,最大延時為: //nms<=0xffffff*8*1000/SYSCLK //SYSCLK單位為Hz,nms單位為ms //對72M條件下,nms<=1864 void delay_ms(u16 nms) {u32 temp;SysTick->LOAD=(u32)nms*fac_ms; //時間加載(SysTick->LOAD為24bit)SysTick->VAL =0x00; //清空計(jì)數(shù)器SysTick->CTRL|=SysTick_CTRL_ENABLE_Msk ; //開始倒數(shù)do{temp=SysTick->CTRL;}while((temp&0x01)&&!(temp&(1<<16))); //等待時間到達(dá)SysTick->CTRL&=~SysTick_CTRL_ENABLE_Msk; //關(guān)閉計(jì)數(shù)器SysTick->VAL =0X00; //清空計(jì)數(shù)器 }void systemInit(void) {SystemInit();delay_init();RCC_Configuration(); // RCC配置GPIO_Configuration(); // GPIO配置GPIO_RemapConfig(); // GPIO重映射NVIC_Configuration(); // NVIC配置uart3_init(9600); }main.c文件: #include "Sys.h" #include "Uart.h" #include "rs485.h"void getSysClk(void) {RCC_ClocksTypeDef get_rcc_clock;RCC_GetClocksFreq(&get_rcc_clock); }int main(void) {TimerFlagCounter = 0x00;TimerFlag = False;USART_RX_STA = 0;RS485_RX_CNT = 0;u8 rs485buf[5];u8 key = 0x05;systemInit();while(1){RS485_Receive_Data(rs485buf,&key);}return 0; }?
總結(jié)
- 上一篇: 关于人工智能不会使大脑变懒惰的议论文_模
- 下一篇: Frammer X for mac(ma