STM32F107串口1通訊modbus學(xué)習(xí)示例_第1頁
STM32F107串口1通訊modbus學(xué)習(xí)示例_第2頁
STM32F107串口1通訊modbus學(xué)習(xí)示例_第3頁
STM32F107串口1通訊modbus學(xué)習(xí)示例_第4頁
STM32F107串口1通訊modbus學(xué)習(xí)示例_第5頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx*1*1*1*1*1*1*1*1*1*1*TxTxTxTxTxTxTxTxTxTx*文件名稱:*功能描述:STM32F107串口1通訊*參數(shù):None*返回值:None*作者:汪仁海*日期:2013年1月28日*修改人:*日期:*TxTxTxTxTxTxTxTxTxTxTxTxTxTx

2、TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx*1*1*1*1*1*1*1*1*1*1*/TxTxTxTxTxTxTxTxTxTx/#includemain.h#defineUSART1_USART1_Addr0 x01定義設(shè)備地址#defineUSART1_GetDataNum50/定義接收數(shù)據(jù)最大長度#defineUSART1_OutDataNum50/定義發(fā)送數(shù)據(jù)最大長度#de

3、fineUSART1_RTU_03ReadOrder0 x04/定義mobusRTU03讀指令#defineUSART1_RTU_04ReadOrder0 x03/定義mobusRTU04讀指令#defineUSART1_RTU_04WriteOrder0 x10定義mobusRTU04寫指令,在定義地址寄存器內(nèi)操作#defineUSART1_TIM2_COUNT3/串口接收數(shù)據(jù)超計數(shù)常數(shù)u8USART1_GetDataUSART1_GetDataNum;定義接收數(shù)組u8USART1_GetDataCpyeUSART1_GetDataNum;定義接收數(shù)組有效數(shù)據(jù)暫存區(qū)u8USART1_OutD

4、ataUSART1_OutDataNum;定義發(fā)送數(shù)組u16USART1_MobusWordUSART1_MobusWordNum;/MOBUSRTU寄存器16bit字節(jié)數(shù)組u8USART1_GetDataCount;/串口接收數(shù)據(jù)個數(shù)u8USART1_GetDataCountCpye;串口接收數(shù)據(jù)個數(shù)轉(zhuǎn)存區(qū)u8USART1_OutDataCount;串口發(fā)送數(shù)據(jù)個數(shù)u8USART1_GetDataFlag;/串口接收數(shù)據(jù)繼續(xù)有效標識符,0為無效,大于0為有效u16USART1_GetMobusAddr;串口接收到指令操作地址u16USART1_GetMobusAddrCpye;串口接收到指令

5、操作地址u8USART1_GetMobusOrder;串口接收到指令操作命令u8USART1_GetMobusOrderCpye;串口接收到指令操作命令u8USART1_GetMobusLen;串口接收到指令操作數(shù)據(jù)長度u8USART1_GetMobusOrderFlag;串口接收到指令操作數(shù)據(jù)是否有效標記符/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx/*對外輸入輸出接口

6、函數(shù)部分*/u8USARTl_GetMobusOrder_Flag(void)串口接收到指令操作數(shù)據(jù)是否有效標記符returnUSARTl_GetMobusOrderFlag;voidUSART1_GetMobusOrder_SetFlag(u8ch)/設(shè)置串口指令操作數(shù)據(jù)標記符USART1_GetMobusOrderFlag=ch;u16ReadMobusWord(u16addr)/讀Mobus數(shù)組指定地址的數(shù)據(jù)if(addr=USART1_MobusWordAddr)&(addr=USART1_MobusWordAddr)&(addr(USART1_MobusWordAddr+USART1

7、_MobusWordNum)USART1_MobusWordaddr-USART1_MobusWordAddr=data;return1;elsereturn0;/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx/*初始化函數(shù)*/*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*

8、1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx/*數(shù)據(jù)初始化*/voidUSART1_InitData(void)u16i=0;for(i=0;iUSART1_GetDataNum;i+)USART1_GetDatai=0;USART1_GetDataCp

9、yei=0;for(i=0;iUSART1_OutDataNum;i+)USART1_OutDatai=0;for(i=0;iUSART1_MobusWordNum;i+)USART1_MobusWordi=i;/USART1_MobusWordi=0;USARTl_MobusWord0=USARTl_USARTl_Addr;起始寄存器為該設(shè)備的地址USARTl_GetDataCount=0;串口接收數(shù)據(jù)個數(shù)USART1_GetDataCountCpye=0;串口接收數(shù)據(jù)個數(shù)轉(zhuǎn)存區(qū)USART1_GetDataFlag=0;串口接收數(shù)據(jù)繼續(xù)有效標識符USART1_GetMobusLen=0;串口

10、接收到指令操作數(shù)據(jù)長度/*串口過時接收復(fù)位函數(shù)*/voidUSART1_IsReset(void)串口過時接收復(fù)位函數(shù)if(USART1_GetDataFlag1)USART1_GetDataCount=0;串口接收數(shù)據(jù)個數(shù)清零elseUSART1_GetDataFlag-;/串口接收數(shù)據(jù)標識符減/TxTxTxTxTx/*指令有效判斷函數(shù)*/*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1

11、*1*1*1*/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx/*返回指定數(shù)值指定長度字符串的CRC值*/u16GetCRC16(u8ch,u16len)/返回指定數(shù)值指定長度字符串的CRC值u16i,j;u16crc=0 xFFFF;if(len=0)len=1;for(j=0;jlen;j+)crcA=chj;for(i=0;i=1;crcA=0 xA001;elsecr

12、c=1;return(crc);/*判斷指定數(shù)值,指定長度的最后兩位CRC值是否正確*/ul6IsCRC16(u8ch,ul6len)/判斷指定數(shù)值,指定長度的最后兩位CRC值是否正確u16a,b;b=0 x0000;a=GetCRC16(ch,len-2);b=(b|chlen-1)8)|chlen-2;if(a=b)return1;elsereturn0;u8IsGetCharAddr(void)/判斷接收到得指令地址是否有效u8flagAddr=0,flagCRC=0;u16GetCharAddr=0 x0000;GetCharAddr=USART1_GetDataCpye2;GetCh

13、arAddr=(GetCharAddr=USART1_MobusWordAddr)&(GetCharAddr8)&0 xff;ch4=data&0 xff;data=GetCRC16(ch,5);/返回指定數(shù)值指定長度字符串的CRC值ch6=(data8)&0 xff;ch5=data&0 xff;Uart1PutCharNum(ch,7);break;caseUSART1_RTU_04WriteOrder:/04指令寫操作if(USART1_GetDataCpye4=0 x00)&(USART1_GetDataCpye5=0 x01)/只允許寫一個字符data=USART1_GetDataC

14、pye7;WriteMobusWord(USART1_GetMobusAddrCpye,(data8)&0 xff;ch3=data&0 xff;ch4=0 x00;ch5=0 x01;data=GetCRC16(ch,6);/返回指定數(shù)值指定長度字符串的CRC值ch7=(data8)&0 xff;ch6=data&0 xff;Uart1PutCharNum(ch,8);break;default:break;/*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1

15、*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx/*串口處理函數(shù)*/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx

16、TxTxTxTxTxTxTxTxTxTxTx/函數(shù)名:USARTl_Configuration輸入:輸出:功能說明:初始化串口硬件設(shè)備,啟用中斷配置步驟:打開GPIO和USART1的時鐘設(shè)置USART1兩個管腳GPIO模式配置USART1數(shù)據(jù)格式、波特率等參數(shù)使能USART1接收中斷功能(5)最后使能USART1功能/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx/voi

17、dUSART1_Configuration(void)GPIO_InitTypeDefGPIO_InitStructure;USART_InitTypeDefUSART_InitStructure;/RS485控制引腳配置GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);/*第1步:打開GPIO和USART部件

18、的時鐘*/*第2步:將USARTTx的GPIO配置為推挽復(fù)用模式*/GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);/*第3步:將USARTRx的GPIO配置為浮空輸入模式由于CPU復(fù)位后,GPIO缺省都是浮空輸入模式,因此下面這個步驟不是必須的但是,我還是建議加上便于閱讀,并且防止其它地方修改了這個口線的設(shè)置

19、參數(shù)*/GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);/*第4步:配置USART1參數(shù)BaudRate=115200baudWordLength=8BitsOneStopBitNoparityHardwareflowcontroldisabled(RTSandCTSsignals)Receive

20、andtransmitenabled*/USART_InitStructure.USART_BaudRate=2400;USART_InitStructure.USART_WordLength=USART_WordLength_8b;USART_InitStructure.USART_StopBits=USART_StopBits_1;USART_InitStructure.USART_Parity=USART_Parity_No;USART_InitStructure.USART_HardwareFlowControlUSART_HardwareFlowControl_None;USART_

21、InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;USART_Init(USART1,&USART_InitStructure);/*若接收數(shù)據(jù)寄存器滿,則產(chǎn)生中斷*/USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);/USART_ITConfig(USART1,USART_IT_TXE,ENABLE);/*第5步:使能USART1,配置完畢*/USART_Cmd(USART1,ENABLE);/*如下語句解決第1個字節(jié)無法正確發(fā)送出去的問題*/USART_ClearFlag(USART1,USART

22、_FLAG_TC);/清標志USARTl_InitData();初始化數(shù)據(jù)/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx/TOC o 1-5 h z/*/*STM32串口1的RS485讀寫設(shè)置*/*0:讀外部數(shù)據(jù)*/*1:向外發(fā)送數(shù)據(jù)*/u8SetUart1_RS485(u8ch)u32i;if(ch=1)/發(fā)送狀態(tài)GPIO_SetBits(GPIOA,GPIO_Pin_8

23、);for(i=0;iv15000;i+)延時1050nselse/接收狀態(tài)for(i=0;iv15000;i+)延時1050nsGPIO_ResetBits(GPIOA,GPIO_Pin_8);returnGPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8);/*/*/*/*/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx/voidUart1_P

24、utChar(u8ch)SetUart1_RS485(1);USART_SendData(USART1,(u8)ch);while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)=RESET);SetUart1_RS485(0);/*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*/TxTxTxTxTxTxTxTxTxTxTxTxTxTxT

25、xTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx/*/*STM32向串口1發(fā)送一串字節(jié)/*/*/*/*/*/u8Uart1PutCharNum(u8ch,u8num)u8i;SetUart1_RS485(1);for(i=0;inum;i+)USART_SendData(USART1,chi);while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)=RESET);SetUart1_RS485(

26、0);returni;/*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*1*/TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTx/*/*STM32在串口1接收1字節(jié)

27、/*說明:串口1接收中斷/*/*/*/*/voidUSART1_IRQHandler(void)u8USART1GetData,i;/*串口接收寄存器滿中斷處/*STM32向串口1發(fā)送1字節(jié)/*if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)/若接收數(shù)據(jù)寄存器滿USART1GetData=USART_ReceiveData(USART1);USARTl_GetDataFlag=USARTl_TIM2_C0UNT;串口接收數(shù)據(jù)繼續(xù)有效標識符/接收數(shù)據(jù)到緩沖區(qū)if(USARTl_GetDataCountUSARTl_GetDataNum)USAR

28、Tl_GetDataUSARTl_GetDataCount+=USARTlGetData;elseUSARTl_GetDataCount=0;USARTl_GetDataUSARTl_GetDataCount+=USARTlGetData;/判斷接收數(shù)據(jù)指令設(shè)備地址if(USARTl_GetDataCount=l)if(USARTl_GetData0!=USARTl_USARTl_Addr)USART1_GetDataCount=0;串口接收數(shù)據(jù)復(fù)位/判斷指令命令類型if(USART1_GetDataCount=2)if(USART1_GetData1=USART1_RTU_04ReadOrd

29、er)/4x讀命令USART1_GetMobusLen=8;串口接收到指令操作數(shù)據(jù)長度USART1_GetMobusOrderCpye=USART1_RTU_04ReadOrder;串口接收到指令操作命令elseif(USART1_GetData1=USART1_RTU_04WriteOrder)/4x寫命令USART1_GetMobusLen=11;串口接收到指令操作數(shù)據(jù)長度USART1_GetMobusOrderCpye=USART1_RTU_04WriteOrder;串口接收到指令操作命令elseUSART1_GetDataCount=0;串口過時接收復(fù)位函數(shù)/接收完一個讀寫指令TxTx

30、TxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxTxif(USART1_GetDataCount=USART1_GetMobusLen)/USART1_GetDataCountCpye=USART1_GetMobusLen;for(i=0;iUSART1_GetDataCountCpye;i+)USART1_GetDataCpyei=USAR

31、T1_GetDatai;USARTl_GetMobusOrderFlag=l;串口接收到指令操作數(shù)據(jù)是否有效標記符/Uartl_PutChar(USARTlGetData);/*文件名稱*功能描述*參數(shù)SysTick時鐘處理串口1數(shù)據(jù)例None*返回None*作者:汪仁海*日期:2013年1月28日*修改人:*日期:*#includemain.hu16SysTick_count;voidSysTick_Configuration(void)/運行狀態(tài)指示燈閃爍引腳定義GPIO_InitTypeDefGPIO_InitStructure;/控制引腳配置GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;GPIO_

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論