版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、在CCS3.3,DSP/BIOS環(huán)境下,應(yīng)用TMS320F28335的SCI功能,編寫了MODBUS-RTU從站程序,可以使用03功能碼和16功能碼,機(jī)器上應(yīng)用通過(guò)以下為源碼,共四個(gè)文件/* * file S_Usart.h * author ww * version * date 2015年12月1日 * brief F28335的SCI初始化及使用 * history*/#ifndef _S_USART_H_#define _S_USART_H_/ Prototype statements for functions found within this file.void scib_gpi
2、oInit();void setADM2587_RE(void);void setADM2587_DE(void);void clearADM2587_RE(void);void clearADM2587_DE(void);void scib_echoback_init(void);void scic_echoback_init(void);void scib_fifo_init(void);void scib_xmit(int a);void scib_msg(char *msg);#endif/* * file S_Usart.c * author ww * version * date
3、2015年12月1日 * brief F28335的sci功能初始化及使用, * history*/#include DSP2833x_Device.h / DSP2833x Headerfile Include File#include DSP2833x_Examples.h / DSP2833x Examples Include File#include S_Gpio.h#include S_Usart.h#include #include #include #include #include #include / Prototype statements for functions fo
4、und within this file.void scib_gpioInit();void setADM2587_RE(void);void setADM2587_DE(void);void clearADM2587_RE(void);void clearADM2587_DE(void);void scib_echoback_init(void);void scib_fifo_init(void);void scib_xmit(int a);void scib_msg(char *msg);#define ADM2587_RE GPIO25#define ADM2587_DEGPIO24/
5、Global counts used in this exampleUint16 LoopCount;Uint16 ErrorCount;void phase_ScibTest(Arg id_arg)Uint16 ReceivedChar;char *msg;/ For this example, only init the pins for the SCI-A port./ This function is found in the DSP2833x_Sci.c file.scib_gpioInit();InitScibGpio();/ Step 5. User specific code:
6、LoopCount = 0;ErrorCount = 0;scib_fifo_init(); / Initialize the SCI FIFOscib_echoback_init(); / Initalize SCI for echobacksetADM2587_DE();setADM2587_RE();msg = rnnnHello Yan Xu!0;scib_msg(msg);setADM2587_DE();setADM2587_RE();msg = rnYou will enter a character, and the DSP will echo it back! n0;scib_
7、msg(msg);for (;)setADM2587_DE();setADM2587_RE();msg = rnEnter a character: 0;scib_msg(msg);clearADM2587_DE();clearADM2587_RE();/ Wait for inc characterwhile (ScibRegs.SCIRXST.bit.RXRDY != 1) / wait for XRDY =1 for empty state/ Get characterReceivedChar = ScibRegs.SCIRXBUF.all;setADM2587_DE();setADM2
8、587_RE();/ Echo character backmsg = You sent: 0;scib_msg(msg);clearADM2587_DE();clearADM2587_RE();scib_xmit(ReceivedChar);LoopCount+;void scib_gpioInit()EALLOW;/初始化DM2587_RE的GPIOGpioCtrlRegs.GPAMUX2.bit.ADM2587_RE = 0;GpioCtrlRegs.GPADIR.bit.ADM2587_RE = 1;/IO為輸出模式GpioCtrlRegs.GPAPUD.bit.ADM2587_RE
9、= 0;/使能內(nèi)部上拉GpioCtrlRegs.GPAQSEL2.bit.ADM2587_RE = 0;/與系統(tǒng)時(shí)鐘同步/初始化DM2587_DE的GPIOGpioCtrlRegs.GPAMUX2.bit.ADM2587_DE = 0;GpioCtrlRegs.GPADIR.bit.ADM2587_DE = 1;GpioCtrlRegs.GPAPUD.bit.ADM2587_DE = 0;GpioCtrlRegs.GPAQSEL2.bit.ADM2587_DE = 0;EDIS;/name: setLedvoid setADM2587_RE(void)GpioDataRegs.GPASET.b
10、it.ADM2587_RE = 1;/name: setLedvoid setADM2587_DE(void)GpioDataRegs.GPASET.bit.ADM2587_DE = 1;/name: clearLedvoid clearADM2587_RE(void)GpioDataRegs.GPACLEAR.bit.ADM2587_RE = 1;/name: clearLedvoid clearADM2587_DE(void)GpioDataRegs.GPACLEAR.bit.ADM2587_DE = 1;/ Test 1,SCIA DLB, 8-bit word, baud rate 0
11、x000F, default, 1 STOP bit, no parityvoid scib_echoback_init()/ Note: Clocks were turned on to the SCIA peripheral/ in the InitSysCtrl() functionScibRegs.SCICCR.all = 0x0007; / 1 stop bit, No loopback/ No parity,8 char bits,/ async mode, idle-line protocolScibRegs.SCICTL1.all = 0x0003; / enable TX,
12、RX, internal SCICLK,/ Disable RX ERR, SLEEP, TXWAKEScibRegs.SCICTL2.all = 0x0003;ScibRegs.SCICTL2.bit.TXINTENA = 1;ScibRegs.SCICTL2.bit.RXBKINTENA = 1;#if (CPU_FRQ_150MHZ)ScibRegs.SCIHBAUD = 0x0001; / 9600 baud LSPCLK = 37.5MHz.ScibRegs.SCILBAUD = 0x00E7;#endif#if (CPU_FRQ_100MHZ)ScibRegs.SCIHBAUD =
13、 0x0001; / 9600 baud LSPCLK = 20MHz.ScibRegs.SCILBAUD = 0x0044;#endifScibRegs.SCICTL1.all = 0x0023; / Relinquish SCI from Reset/ Test 1,SCIA DLB, 8-bit word, baud rate 0x000F, default, 1 STOP bit, no parityvoid scic_echoback_init()/ Note: Clocks were turned on to the SCIc peripheral/ in the InitSysC
14、trl() functionScicRegs.SCICCR.all = 0x0007; / 1 stop bit, No loopback/ No parity,8 char bits,/ async mode, idle-line protocolScicRegs.SCICTL1.all = 0x0003; / enable TX, RX, internal SCICLK,/ Disable RX ERR, SLEEP, TXWAKEScicRegs.SCICTL2.all = 0x0003;ScicRegs.SCICTL2.bit.TXINTENA = 1;ScicRegs.SCICTL2
15、.bit.RXBKINTENA = 1;#if (CPU_FRQ_150MHZ)ScicRegs.SCIHBAUD = 0x0001; / 9600 baud LSPCLK = 37.5MHz.ScicRegs.SCILBAUD = 0x00E7;#endif#if (CPU_FRQ_100MHZ)ScicRegs.SCIHBAUD = 0x0001; / 9600 baud LSPCLK = 20MHz.ScicRegs.SCILBAUD = 0x0044;#endifScicRegs.SCICTL1.all = 0x0023; / Relinquish SCI from Reset/ Tr
16、ansmit a character from the SCIvoid scib_xmit(int a)while (ScibRegs.SCICTL2.bit.TXRDY = 0) ScibRegs.SCITXBUF = a;void scib_msg(char * msg)int i;i = 0;while (msgi != 0)scib_xmit(msgi);i+;/ Initalize the SCI FIFOvoid scib_fifo_init()ScibRegs.SCIFFTX.all = 0x8000;/=/ No more./=/* * file S_Modbus.h * au
17、thor ww * version * date 2015年12月1日 * brief F28335的modbus rtu初始化及使用 * history*/#ifndef _S_MODBUS_H_#define _S_MODBUS_H_struct MODBUS_MODULE Uint16 Buf256;Uint16 TxLen;Uint16 RxLen;Uint16Point;Uint16ReTryTime;Uint16TimeoutReg;Uint16 Tbl_SET1;Uint16Tbl_READ1;Uint16Tbl_SET2;Uint16 Tbl_READ2;Uint16Tbl_C
18、OIL_SET1;Uint16Tbl_COIL_READ1;Uint16ID;Uint16FunCode;Uint16RegAddr;Uint16RegNum;Uint16 Status;/ Prototype statements for functions found within this file.void ConfigureModbus (void)void ConfigureModbus (void);interrupt void ScicTxInt (void);interrupt void ScicRxInt (void) ;Uint16 GetCRC16 (Uint16 vo
19、latile * ptr, Uint16 len);void ModbusSlaveSetNHldRegAnswer ( Uint16 board_adr, Uint16 start_address, Uint16 lenth);void ModbusSlaveReadHldRegAnswer ( Uint16 board_adr, Uint16 *com_buf, Uint16 lenth);Uint16 RTUSlaveFrameAnalyse (Uint16 *dest_p);void ModbusRTUSlaveRun (void);#endif/-/ FILE: S_Modbus.c
20、/ 程序分兩部分:串口操作:中斷接收和發(fā)送,采用緩沖區(qū)發(fā)送/ 協(xié)議處理:分主戰(zhàn)和從站兩部分,具體的站需要宏定義指定。/-/ Ver | dd mmm yyyy | Who | Description of changes/ =|=|=|=/ 1.00| 9 Dec 2015 | WXJ | start version03,16兩命令分別讀寫/ 內(nèi)部寄存器/-/ Load DSP lib#include DSP2833x_Device.h / DSP2833x Headerfile Include File#include DSP2833x_Examples.h / DSP2833x Examp
21、les Include File#include S_Usart.h#include S_Modbus.h#include #include #include #include #include #include #pragma CODE_SECTION(ScicRxInt, ramfuncs);#pragma CODE_SECTION(ScicTxInt, ramfuncs);#define IDLE_WAIT 0x00#define RECE_START 0x01#define RECE_END0x02#define SEND_START0x03#define SEND_END0x04#d
22、efine SEND_EXCHANGE0x05#define SET_N_HLD_REG0x10#define READ_HLD_REG0x03/-主從站協(xié)議選擇-#define MODBUS_RTU1#define MODBUS_SLAVE 1/ #define MODBUS_MASTER0/-/ij:i-(從站號(hào)1);j-對(duì)應(yīng)從站號(hào)通訊起始地址的偏移量Uint16 WRData2100; /從站1(HMI):500599; 從站2(DSP2):600699Uint16 REData2100; /從站1(HMI):700799; 從站2(DSP2):800899Uint16 WRBit220
23、; /從站1(HMI):000320; 從站2Uint16 REBit220; /從站1(HMI):400720; 從站2volatile struct MODBUS_MODULE ModbusModule;/-Uint16 WORD_HI(Uint16 _arg);Uint16 WORD_LO(Uint16 _arg);void ConfigureModbus(void);Uint16 RTUFrameAnalyse(Uint16 *dest_p);void ConstructFrame_RTUReadHldReg( Uint16 board_adr, Uint16 start_addres
24、s, Uint16 lenth);void ConstructFrame_RTUSetNHldReg( Uint16 board_adr, Uint16 *com_buf, Uint16 start_address, Uint16 lenth);extern Uint16 ramfuncs_loadstart;extern Uint16 ramfuncs_loadsize;extern Uint16 ramfuncs_runstart;/-/ 程序第一部分:串口程序/ 串口初始化/ 串口收發(fā)數(shù)據(jù)/-/ -/-void modbus_task(Arg id_arg)memcpy(&ramfunc
25、s_runstart, &ramfuncs_loadstart, (Uint32)&ramfuncs_loadsize);/ scic_gpioInit();/使能輸入輸出使能IOInitScicGpio();ScicRegs.SCIFFTX.all = 0x8000; / Initialize the SCI FIFOscic_echoback_init(); / Initalize SCI for echobackConfigureModbus();while (1)ModbusRTUSlaveRun();TSK_sleep(2);ModbusModule.TimeoutReg+;Uint
26、16 WORD_HI(Uint16 _arg)_arg = (_arg 8) & 0x0ff;return _arg;Uint16 WORD_LO(Uint16 _arg)_arg = _arg & 0x0ff;return _arg;/-void ConfigureModbus (void)Uint16 i;for (i = 0; i 100; i+) WRData0i = i;WRData1i = i;REData0i = i;REData1i = i;for (i = 0; i 20; i+) WRBit0i = 0;REBit0i = 0;WRBit1i = 0;REBit1i = 0
27、;for ( i = 0; i 0 )ScicRegs.SCITXBUF = ModbusModule.BufModbusModule.Point+;ModbusModule.TxLen-;/ ModbusModule.Point+;/ ModbusModule.Point %= 128; /else/發(fā)送完畢ModbusModule.Status = SEND_END; /狀態(tài)切換ModbusModule.Point = 0;ModbusModule.TimeoutReg = 0;/-/ ScicRxInt MODBUS RTU/ 以兩字符時(shí)間停頓為3.5個(gè)字符時(shí)間/ 程序用5ms時(shí)間判斷
28、對(duì)應(yīng)96005個(gè)字符時(shí)間/ 程序幀結(jié)束數(shù)據(jù)在主程序里面處理/-interrupt void ScicRxInt (void) / SCI-CUint16 temp;PieCtrlRegs.PIEACK.bit.ACK8 = 1; /ACK復(fù)位temp = ScicRegs.SCIRXBUF.all;switch ( ModbusModule.Status ) case SEND_END: /4ModbusModule.Status = RECE_START;ModbusModule.Point = 0;case RECE_START: /1ModbusModule.BufModbusModul
29、e.Point = temp;ModbusModule.Point+;ModbusModule.RxLen = ModbusModule.Point;break;ModbusModule.TimeoutReg = 0;/-/ 程序第二部分:MODBUS 協(xié)議解析與構(gòu)造/ CRC16/ 協(xié)議幀構(gòu)造/ 協(xié)議幀解析/-/-/ CRC16 calculate/ -Modbus Protocol English pdf/ ptr calculate start point/ len calculate data length/-Uint16 GetCRC16 (Uint16 volatile * ptr
30、, Uint16 len)Uint16 i;Uint16 crc = 0xFFFF;if (len = 0) len = 1;while (len-) crc = (*ptr);for (i = 0; i = 1;crc = 0xA001; else crc = 1;ptr+;return (crc);/ return 1;#ifdef MODBUS_SLAVE/-/ RTU Set N Hold Register/ CMD=16 SET_N_HLD_REG/ Constructe Answer Frame/-void ModbusSlaveSetNHldRegAnswer ( Uint16
31、board_adr, Uint16 start_address, Uint16 lenth)Uint16 i = 0, j = 0;ModbusModule.Bufi+ = board_adr;ModbusModule.Bufi+ = SET_N_HLD_REG;ModbusModule.Bufi+ = WORD_HI(start_address);ModbusModule.Bufi+ = WORD_LO(start_address);ModbusModule.Bufi+ = WORD_HI(lenth);ModbusModule.Bufi+ = WORD_LO(lenth);j = GetC
32、RC16(ModbusModule.Buf, i);ModbusModule.Bufi+ = WORD_HI(j);ModbusModule.Bufi+ = WORD_LO(j);ModbusModule.TxLen = i;ModbusModule.Point = 0;/-/ RTU Read Hold Register/ CMD=03 READ_HLD_REG/ Constructe Answer Frame/-void ModbusSlaveReadHldRegAnswer ( Uint16 board_adr, Uint16 *com_buf, Uint16 lenth)Uint16
33、i = 0, j = 0;ModbusModule.Bufi+ = board_adr;ModbusModule.Bufi+ = READ_HLD_REG;ModbusModule.Bufi+ = lenth 1;for (j = 0; j lenth; j+) ModbusModule.Bufi+ = WORD_HI( * (com_buf + j) );ModbusModule.Bufi+ = WORD_LO( * (com_buf + j) );j = GetCRC16(ModbusModule.Buf, i);ModbusModule.Bufi+ = WORD_HI(j);Modbus
34、Module.Bufi+ = WORD_LO(j);ModbusModule.TxLen = i;ModbusModule.Point = 0;/-/ RTU 從站接收分析/ 3 READ_HLD_REG/ 16 SET_N_HLD_REG/ 返回值: 0 OK/ 1 CRC校驗(yàn)錯(cuò)誤/ 2 站號(hào)不匹配/ 4 16寫地址不匹配/ 5 16寫數(shù)據(jù)字?jǐn)?shù)不匹配/-Uint16 RTUSlaveFrameAnalyse (Uint16 *dest_p)Uint16 i;Uint16 crc_result, crc_tmp;Uint16 RegAddr, RegNum;crc_tmp = (ModbusM
35、odule.BufModbusModule.RxLen - 2 8);crc_tmp += ModbusModule.BufModbusModule.RxLen - 1;crc_result = GetCRC16(ModbusModule.Buf, ModbusModule.RxLen - 2);if ( crc_tmp != crc_result ) / CRC 校驗(yàn)正確return 1;if ( ModbusModule.ID != ModbusModule.Buf0 ) return 2;/ if ( ModbusModule.FunCode != ModbusModule.Buf1 )/ return 3;/ Mod
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 網(wǎng)絡(luò)直播平臺(tái)賽事組織合同
- 網(wǎng)絡(luò)游戲防沉迷系統(tǒng)設(shè)計(jì)與實(shí)施計(jì)劃
- 網(wǎng)絡(luò)游戲虛擬物品交易監(jiān)管制度預(yù)案
- 網(wǎng)絡(luò)安全防護(hù)設(shè)備采購(gòu)合同
- 城市公園景觀更新項(xiàng)目施工協(xié)議2024
- 2024高效企業(yè)員工接送包車協(xié)議
- 智能物流配送系統(tǒng)開發(fā)與運(yùn)營(yíng)合同
- 智慧城市規(guī)劃與設(shè)計(jì)咨詢合同
- 2024年度統(tǒng)一格式勞動(dòng)協(xié)議樣本
- 2024年個(gè)人人民幣貸款協(xié)議條款
- 浙江省小升初數(shù)學(xué)試卷及答案二
- 教學(xué)評(píng)一體化
- 2024年高考語(yǔ)文備考之現(xiàn)代文閱讀史鐵生《我二十一歲那年》(附習(xí)題+答案)
- 外國(guó)新聞傳播史 課件 第21-23章 新西蘭等國(guó)的新聞傳播事業(yè)、巴西的新聞傳播事業(yè)、墨西哥的新聞傳播事業(yè)
- 大數(shù)據(jù)與會(huì)計(jì)職業(yè)生涯規(guī)劃
- 寧德時(shí)代2024年社招測(cè)評(píng)題庫(kù)
- 2023年度省綜合專家?guī)煸u(píng)標(biāo)專家繼續(xù)教育培訓(xùn)考試試題(三套)
- 電力配網(wǎng)安全培訓(xùn)課件
- 數(shù)字信號(hào)處理試卷及答案
- 電子商務(wù)運(yùn)營(yíng) 教案 項(xiàng)目三 網(wǎng)店推廣
- 血栓風(fēng)險(xiǎn)評(píng)估及個(gè)體化干預(yù)(遺傳性易栓癥風(fēng)險(xiǎn)基因檢測(cè))
評(píng)論
0/150
提交評(píng)論