




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、精選精選.精選.modbusC語言程序2021-10-15 09:31:28|分類: HYPERLINK :/blog.163 /li_g888126/blog/ l m=0&t=1&c=fks_087064084084080064084080095095085086082070092082094064086 o 默認分類 默認分類|字號訂閱#include #include/_nop_();#define OSFREQ 11059200char TimeInterval;char MyAddress;char data DI6=1,2,3,4,5,6;char data DO6=6,7,8,
2、9,10,11;char data AI6=11,12,13,14,15,16;char data AO6=16,17,18,19,20,21;unsigned char data ReceiveData14;unsigned char data countnumber;sbit Recenable =P16;/控制端 sbit led =P27;/控制端/*-函數(shù)說明:CRC 高位字節(jié)值表-*/static unsigned char code auchCRCHi = 0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x01,
3、 0 xC0,0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41,0 x00, 0 xC1, 0 x81, 0 x40, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0,0 x80, 0 x41, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40,0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x00, 0 xC1,0 x81, 0 x40, 0 x
4、01, 0 xC0, 0 x80, 0 x41, 0 x01, 0 xC0, 0 x80, 0 x41,0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x00, 0 xC1,0 x81, 0 x40, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41,0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x01, 0 xC0,0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40,
5、 0 x00, 0 xC1, 0 x81, 0 x40,0 x01, 0 xC0, 0 x80, 0 x41, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x00, 0 xC1,0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40,0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x01, 0 xC0,0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x00, 0 xC1, 0 x81, 0
6、x40,0 x01, 0 xC0, 0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0,0 x80, 0 x41, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40,0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x01, 0 xC0,精選精選.精選.0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41,0 x00, 0 xC1
7、, 0 x81, 0 x40, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0,0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41,0 x01, 0 xC0, 0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0,0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x00, 0 xC1, 0 x81, 0 x40,0 x01, 0 xC0, 0 x80, 0 x41, 0 x01, 0
8、 xC0, 0 x80, 0 x41, 0 x00, 0 xC1,0 x81, 0 x40, 0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41,0 x00, 0 xC1, 0 x81, 0 x40, 0 x01, 0 xC0, 0 x80, 0 x41, 0 x01, 0 xC0,0 x80, 0 x41, 0 x00, 0 xC1, 0 x81, 0 x40 ;/*-函數(shù)說明:CRC低位字節(jié)值表-*/static unsigned char code auchCRCLo = 0 x00, 0 xC0, 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B, 0 x69, 0 xA9, 0 xA8, 0 x68,0 x78, 0 xB8, 0 xB9, 0 x79, 0 xBB, 0 x7B, 0 x7A, 0 xBA, 0 xBE, 0 x7E,0 x7F, 0 xBF, 0 x7D, 0 xBD, 0 xBC, 0 x7C, 0 xB4, 0 x74,
13、0 x75, 0 xB5,0 x77, 0 xB7, 0 xB6, 0 x76, 0 x72, 0 xB2, 0 xB3, 0 x73, 0 xB1, 0 x71,0 x70, 0 xB0, 0 x50, 0 x90, 0 x91, 0 x51, 0 x93, 0 x53, 0 x52, 0 x92,0 x96, 0 x56, 0 x57, 0 x97, 0 x55, 0 x95, 0 x94, 0 x54, 0 x9C, 0 x5C,精選精選.精選.0 x5D, 0 x9D, 0 x5F, 0 x9F, 0 x9E, 0 x5E, 0 x5A, 0 x9A, 0 x9B, 0 x5B,0 x
14、99, 0 x59, 0 x58, 0 x98, 0 x88, 0 x48, 0 x49, 0 x89, 0 x4B, 0 x8B,0 x8A, 0 x4A, 0 x4E, 0 x8E, 0 x8F, 0 x4F, 0 x8D, 0 x4D, 0 x4C, 0 x8C,0 x44, 0 x84, 0 x85, 0 x45, 0 x87, 0 x47, 0 x46, 0 x86, 0 x82, 0 x42,0 x43, 0 x83, 0 x41, 0 x81, 0 x80, 0 x40;/*-調(diào)用方式:unsigned int CRC16(unsigned char *puchMsg, unsi
15、gned int usDataLen)函數(shù)說明:CRC校驗-*/unsigned int CRC16(unsigned char *puchMsg, unsigned int usDataLen) unsigned char uchCRCHi = 0 xFF ; / 高CRC字節(jié)初始化 unsigned char uchCRCLo = 0 xFF ; / 低CRC 字節(jié)初始化 unsigned uIndex ; / CRC循環(huán)中的索引 while (usDataLen-) / 傳輸消息緩沖區(qū) uIndex = uchCRCHi *puchMsg+ ; / 計算CRC uchCRCHi = uc
16、hCRCLo auchCRCHiuIndex ; uchCRCLo = auchCRCLouIndex ; return (uchCRCHi 3); temp=*(Myaddress+temp); temp=(Offset); if (temp&0 x01) return 1; else return 0;/*-精選精選.精選.調(diào)用方式:void SendData(unsigned char *output,unsigned char Outlength)函數(shù)說明:發(fā)送數(shù)據(jù)至窗口-*/void SendData(unsigned char *output,unsigned char Outle
17、ngth) ES=0; while(Outlength-) TI=0; ACC=*output; TB8=P; SBUF=*(output+); while(!TI); TI=0; ES=1;/*-調(diào)用方式:void Function12(unsigned char address,unsigned char len)函數(shù)說明:功能碼1,2處理-*/void Function12(unsigned char address,unsigned char len) unsigned int data i; unsigned char data j; unsigned char data lengt
18、h; unsigned char data *send; unsigned int data temp; unsigned char data function; length=0; send=ReceiveData; function=send1; for (i=1;i=len;i+) length+; *(send+2+length)=0; for (j=0;j8;j+) *(send+2+length)=*(send+2+length)|getbit(address,function)len)break; i-; *(send+2)=length; /精選精選.精選.數(shù)據(jù)長度 temp=
19、CRC16(send,length+3); /DI狀態(tài)數(shù)據(jù) *(send+3+length)=(char)(temp8); /CRC校驗高 *(send+4+length)=(char)temp; /CRC校驗低 SendData(send,length+5); /調(diào)用A發(fā)送程序/*-調(diào)用方式:void Function3(unsigned char address,unsigned char len)函數(shù)說明:功能碼3處理-讀取存放器-*/void Function3(unsigned char address,unsigned char len) unsigned char data i;
20、 unsigned char data *send; unsigned int data temp; send=ReceiveData; *(send+2)=2*len; /數(shù)據(jù)長度高 address=2*address; for(i=0;i8); *(send+4+2*len)=(char)temp; SendData(send,2*len+5); /調(diào)用A發(fā)送程序/*-調(diào)用方式:void Function4(unsigned char address,unsigned char len)函數(shù)說明:功能碼處理4-讀取輸入存放器-*/void Function4(unsigned char a
21、ddress,unsigned char len) unsigned char data i; unsigned char data *send; unsigned int data temp; send=ReceiveData; *(send+2)=2*len; address=2*address; for(i=0;i8); *(send+4+2*len)=(char)temp; SendData(send,2*len+5);/*-調(diào)用方式:void Function6(unsigned char address)函數(shù)說明:寫單路存放器-*/void Function6(unsigned c
22、har address) int temp; unsigned char data *WriteData; temp=2*address; WriteData=AO; /將寫入的數(shù)據(jù)進行處理 /your code to add here to deal with the write value *(WriteData+temp)=ReceiveData4; *(WriteData+temp+1)=ReceiveData5; SendData(ReceiveData,countnumber); /調(diào)用發(fā)送程序,返回與主機相同的報文/*-調(diào)用方式:void SendError(char Error
23、Code)函數(shù)說明:-*/void SendError(char ErrorCode) unsigned char data *send; unsigned int data temp; send=ReceiveData; *(send+1)=ReceiveData1|0 x01;/最高位置1 *(send+2)=ErrorCode; temp=CRC16(send,3); *(send+3)=(char)(temp8); *(send+4)=(char)temp; SendData(send,countnumber); /精選精選.精選.調(diào)用發(fā)送程序,返回錯誤代碼/*-調(diào)用方式:void D
24、eal()函數(shù)說明:接收數(shù)據(jù)處理-*/void Deal() unsigned int data temp; unsigned char data address; unsigned char data length; unsigned char data counter; unsigned char data *Pointer; Pointer=ReceiveData; counter=countnumber; if (counter=3 ) return; temp=Pointercounter-28; temp= temp|Pointercounter-1; if( temp=CRC16
25、(Pointer,counter-2) /較驗正確 address=Pointer3; length=Pointer5; if(addressNumber&0 x01=1) return 1; else return 0;/設定某一位的值*unsigned char SetBit(unsigned char Character,unsigned char num,bit boolen) unsigned char code bit_value=1,2,4,8,16,32,64,128; if(boolen) return Character|bit_valuenum; else return
26、Character&bit_valuenum;*/*-調(diào)用方式:void Init_timer2(unsigned int baudrate)函數(shù)說明:定時器2的初始化-*/void Init_timer2(unsigned int baudrate) unsigned int data TimReg2; T2CON = 0 x00; T2MOD = 0 x00; TimReg2=65536-(OSFREQ/384/baudrate*11); RCAP2L=TimReg2&0 x00ff; RCAP2H=(TimReg28); /RCAP2H=TimReg2/256 RCAP2L=0;RCAP2H=0; TL2 = RCAP2L; / TH2 = RCAP2H; / TR2=1; / T2CON.2 start timer精選精選.精選. ET2=0; / 開定時器2中斷/*-調(diào)用方式:void Init_SerialPort(unsigned int baudrate)函數(shù)說明:串口初始化-*/void Init_SerialPort(unsigned int baudrate) unsigned char TimReg2; SCON = 0 xd0; / 方式3, 8-bit U
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 養(yǎng)老器材合同范本
- 年度事跡材料(8篇)
- 貨款抵押合同范本
- 在戰(zhàn)略合作簽字儀式上的講話(9篇)
- 集資投資合同范本
- 光誘導電子給體-受體復合物驅(qū)動的脫羧-脫氨官能化反應研究
- 降水速率對極端暴雨后滿水基坑穩(wěn)定性的影響研究
- 宏觀審慎政策對我國商業(yè)銀行盈利能力的影響研究
- 幾類隨機(偏)微分方程模型長時間動力學行為的數(shù)值分析
- 面向化工園區(qū)的電塔點云三維重建技術研究
- 《現(xiàn)代漢語》語音教學上課用課件
- 采購流程各部門關系圖
- 力士樂工程機械液壓培訓資料(共7篇)課件
- 村光伏發(fā)電申請書
- 支氣管擴張的護理PPT
- 施工現(xiàn)場專項消防安全檢查表
- 學習強國挑戰(zhàn)答題題庫1600題
- 鋼結構廠房吊裝安裝監(jiān)理控制要點演示文稿
- 讓水產(chǎn)動物第一口都吃上蝦奶粉(廖英杰)
- 2023年高考數(shù)學大招9蒙日圓及其證明
- 探究課程之蛇的探究
評論
0/150
提交評論