版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、8.5 軟件設計本系統(tǒng)的軟件設計主要包括以下幾個部分:A/D轉換子程序、時鐘芯片DS12887初始化子程序、看門狗芯片的初始化及復位子程序、外部控制接點輸出子程序、查詢外部開關量的狀態(tài)子程序、網絡部分子程序等。其中網絡部分是本模塊軟件設計相對復雜的部分,也是軟件設計的重點部分。網絡部分主要包括以下幾個部分:GPRS MODEM撥號部分、LCP協商部分、IPCP協商部分、查找UDP包部分、UDP解包部分、UDP包的發(fā)送部分、短信報警部分等。下面分別給出主要部分的程序流程圖以及相應的源代碼,供讀者參考。8.5.1 主程序#include#include#include#include#define
2、 uchar unsigned char #define uint unsigned int#define ulong unsigned long#define Add_h 0x00#define Add_l 0x01extern uchar data backup_P1;extern uchar xdata receive_buff256;extern uchar xdata count;extern uint xdata crc;extern uint xdata ppp_s_id;extern uchar xdata local_ip4;extern uchar xdata host_i
3、p4;void reset_dog(void);void set_system_time(void);void out_io(void);void sd_connected_prove(void);void send_ad(void);uchar data backup_P1;/中斷時用來保存片選uchar xdata receive_buff256 _at_ 0x0101;/256字節(jié)的接收緩沖區(qū)uchar xdata local_ip4;uchar xdata host_ip4;uint xdata crc=0;uchar xdata count=0;/接受數組下表,自動轉頭uint xd
4、ata ppp_s_id=0x3600;void csh_X25045(void);void main(void)uchar xdata h;ppp_s_id=0x3600;P1=0x1c;/選中ramcsh_X25045();baud();/設置波特率P1=0x1c;/-緩沖區(qū)清0-/for(h=0;h255;h+)receive_buffh=0x00;reset_dog();initstat_mc35();/set mc35msg();/找到短信dail();confg();/ 上網p_service();sd_connected_prove();while(1)P1=0x1c;reset
5、_dog();read_io(); /讀4路io口狀態(tài),放入io_state4數組read_ad(); /讀4路AD數值,放入ad_value4數組 P1=0x0D;/選中ds12887,讀系統(tǒng)時間,放入date6數組r_ds();P1=0x1c;for(h=0;h100;h+)delays();find_config_frame();/找udp包switch (receive_config_frame35)case 0x50:send_ad(); break;/發(fā)送對應模擬開關量case 0x70:set_system_time(); break;/設置系統(tǒng)時間set_system_time
6、.ccase 0x95:out_io(); break;/執(zhí)行開關量io_out.cdefault:_nop_();8.5.2 相關的硬件驅動程序1. 延時子程序void delay(uchar data a) /延時程序,延時a個機器周期 uchar data b; for(b=0;b=a;b+) _nop_(); 2. A/D轉換子程序sbit sa_clk=P15;sbit sa_in=P17;sbit sa_out=P16;void delay(uchar);uint read2543(uchar data channel) /A/D轉換子程序 read2543 uchar data
7、al=0,ah=0; uint data ad; uint data i; switch (channel) /通道轉換 case 0x01:channel=0x00; break;case 0x02:channel=0x01; break;case 0x03:channel=0x04; break;case 0x04:channel=0x05; break;default :channel=0x00; break; channel=4; P1=0x08; /使cs=1 P1=0x0E; /使cs=0 sa_out=0; sa_clk=0; sa_in=0; for (i=0;i12;i+)/
8、把控制字打入2543 sa_in=(bit)(channel&0x80); sa_clk=1; _nop_();/delay(0x00); channel=1; sa_clk=0; _nop_();/delay(0x00); for (i=0;i4;i+) /取D11,D10,D9,D8 sa_out=1; sa_clk=1; _nop_();/delay(0x00); ah=1;if (sa_out) ah|=0x01;al=1; if (sa_out) al|=0x01; sa_clk=0; _nop_(); /delay(0x00); P1=0x1c; /選中ram ad=(uint)a
9、h;ad=8;ad|=al;/得到AD值 return (ad); 3. 外部控制接點輸出子程序sbit s_out=P17;sbit s_clk=P15;void out_io(void) s_out=(int)receive_config_frame39; /第四路輸出開關狀態(tài),開是0,關是1 s_clk=0; s_clk=1; s_out=(int)receive_config_frame38; /第三路輸出開關狀態(tài),開是0,關是1 s_clk=0; s_clk=1;s_out=(int)receive_config_frame37; /第二路輸出開關狀態(tài),開是0,關是1 s_clk=0
10、; s_clk=1;s_out=(int)receive_config_frame36; /第一路輸出開關狀態(tài),開是0,關是1 s_clk=0; s_clk=1; P1=0x0a; /373的LE有效,鎖存 P1=0x0a; delay(0x08); P1=0x09; /373輸出P1=0x09 delay(0x08); P1=0x1c;/選中ramreceive_config_frame35=0x00;4. 外部開關量查詢子程序sbit D11=P33;sbit D12=P32;sbit D13=P35;sbit D14=P34;uchar xdata io_state4;void read
11、_io(void)D11=1; /讀INT1(IO1)狀態(tài)if(D11)io_state0=0x01;elseio_state0=0x00;D12=1;/讀INT0(IO2)狀態(tài)if(D12)io_state1=0x01;elseio_state1=0x00;D13=1;/讀T1(IO3)狀態(tài)if(D13)io_state2=0x01;elseio_state2=0x00;D14=1;/讀T0(IO4)狀態(tài)if(D14)io_state3=0x01;elseio_state3=0x00;8.5.3 網絡部分子程序網絡部分軟件設計是本項目的設計重點,下面將給出詳細的軟件流程圖,同時給出相應的源程
12、序。參考前面的關鍵技術分析部分對網絡部分的分析,再對照給出的軟件流程,相信能給讀者提供一定的啟發(fā)。程序的流程圖如圖8-26所示。圖8-26 程序流程圖網絡部分的源代碼如下:#define _DATATPYE_Htypedef unsigned char U8;typedef unsigned int U16;typedef unsigned long U32;typedef signed char S8;typedef signed int S16;typedef signed long S32;#define TRUE 1#define FALSE 0#define OK1#define F
13、AIL0#define _LIB_H#define DEALY1S 1s#define UART0 0#define UART1 1void Delay(volatile U16 time);void Prints(S8 * ptr , U8 port);void Print(S8 * ptr , U8 len , U8 port);void InitMc35(void);void Prints(S8 * ptr , U8 port)while (* str)UartSend(*ptr+ , port);void Print(S8 * ptr , U8 len , U8 port)U8 i ;
14、for ( i=0; ilen; i+)UartSend(*ptr+, port)void PrintBuff(U8 * ptr , U8 port)for (U8 bb=0; bb2; ) if (*ptr = 0x7e)bb+;UartSend(*ptr+, port);void InitMc35(void)static S8 splus=+rn;static S8 sATH=ATHrn;static S8 sATE0V0Q0=ATE0V0Q0rn;static S8 sAT_CMGF=AT+CMGF=1rn;static S8 sAT_CGDCONT=AT+CGDCONT=1,IP,gz
15、eunt.gdrn;static S8 sATDT=ATDT*99*1#rn;/*send + twice*/Prints(splus, UART0);Delay(DELAYTIME);Prints(splus, UART0);Delay(DEALY1S);/*sendATH,wait MC35 answer 300D*/doPrints(sATH , UART0);Delay(DEALY1S);while(!( (uart0RevBuffuart0Count-2 = 0x30) & (uart0_revbuffuart0_count-1 = 0x0D) );uart0RevBuffuart0
16、Count-2 = 0;uart0RevBuffuart0Count-1 = 0;/*send ATE0V0Q0,wait mc35 answer 300D*/doPrints(sATE0V0Q0, UART0);Dealy(DELAY1S);while(!( (uart0RevBuffuart0Count-2 = 0x30) & (uart0_revbuffuart0_count-1 = 0x0D) );uart0RevBuffuart0Count-2 = 0;uart0RevBuffuart0Count-1 = 0;/*set MSM message format to TEXT mode
17、l AT+CMGF=1 */doPrints(sAT_CMGF, UART0);Delay(DEALY1S);while(!( (uart0RevBuffuart0Count-2 = 0x30) & (uart0_revbuffuart0_count-1 = 0x0D) );uart0RevBuffuart0Count-2 = 0;uart0RevBuffuart0Count-1 = 0;/*Set APNAT+CGDCONT=1,IP,gzeunt.gdrn*/doPrints(sAT_CGDCONT, UART0);Delay(DEALY1S);while(!( (uart0RevBuff
18、uart0Count-2 = 0x30) & (uart0_revbuffuart0_count-1 = 0x0D) );uart0RevBuffuart0Count-2 = 0;uart0RevBuffuart0Count-1 = 0;/*set GPRS sevice “ATDT*99*1#” */doPrints(sATDT , UART0);Delay(DEALY1S);while(!( (uart0RevBuffuart0Count-2 = 0x30) & (uart0_revbuffuart0_count-1 = 0x0D) );uart0RevBuffuart0Count-2 =
19、 0;uart0RevBuffuart0Count-1 = 0;/*transfer meaning*/* author: muyi date:2004.4.20 * rework: date:2004.11.1 * function:the datas between the start and end 7e not including the start and end themselves which are 0x7e 0x7d or 0; len-)if ( (*p_nontransfer = 0x7e) | (*p_nontransfer = 0x7d) | (*p_nontrans
20、fer 0) fcs = (fcs 8) crctab16(fcs *pData) & 0xff; nLength-; pData+; return fcs; U8 IsCrc16Good(const U8 *pData, U8 nLength) U16 fcs = 0xffff; pData+; while(nLength0) fcs = (fcs 8) crctab16(fcs *pData) & 0xff; nLength-; pData+; return (fcs = 0xf0b8); / 0xf0b8 is CRC-ITUsMagic Value/*find buff*/* auth
21、or: muyi date:2004.10.20 * rework: date:2004.11.1 * function: find out the frame in uart0_revbuff256;check the crc ,if the crc is right ,delete the ff 03 * after the start 7e *IO: return 1 when crc ok ,0 when crc false*/U8 Find_Frame(void)U8 back_count0 = uart0_count;U8 flag_start7e = FAIL;U8 flag_e
22、nd7e = FAIL;U8 start7e = 0;U8 end7e = 0;U8 i = 0;U8 j = 0;U8 k = 0;U8 temp150;for (k=0; k255; k+,back_count0-)/ find the end 7eif (uart0_revbuffback_count0 = 0x7e)if (uart0_revbuffback_count0-1 = 0x7e)/in case of.7e7e.back_count0-;end7e=back_count0;/mark the end 7eflag_end7e = OK;break;elseend7e = b
23、ack_count0;/mark the end 7eflag_end7e = OK;break; back_count0-;for (k=0; k255; k+,back_count0-)/find the start 7eif (uart0_revbuffback_count0 = 0x7e)start7e = back_count0;/mark the start 7eflag_start7e = OK;break;if (flag_start7e = FAIL | flag_end7e = FAIL)/if there is no frame , go to the endgoto E
24、ND;k=0;/copy the found frame from uart0_recbuff to tempfor (i=start7e,j=0; k0x02; i+,j+)tempj = uart0_revbuffi;if (uart0_revbuffi = 0x7e)k+; receiveFrame0 = temp0;/start7e for (i=1,j=1; tempi!=0x7e; i+,j+)/transfer meaningif (tempi = 0x7d)receiveFramej=(tempi+1&0x20)?(tempi+1&0xdf):(tempi+1|0x20);i+
25、;elsereceiveFramej = tempi;receiveFramej = tempi;if ( IsCrc16Good(receiveFrame , j-1) )uart0_revbuffstart7e = 0;/ clear the found frame in the revbuff0uart0_revbuffend7e = 0;if (receiveFrame1 = 0xff)for (i=1; i149; i+)/if receiveFrame1=0xff ,then drop itreceiveFramei = receiveFramei+1;if (receiveFra
26、me1 = 0x03)/if receiveFrame1=0x03 ,then drop itfor (i=1; i149; i+)receiveFramei = receiveFramei+1;return 1;else/ if crc check fail then clear the receiveFramefor (i=0; i150; i+)receiveFramei = 0;return 0;END:return 0 ;/*lcp*/void Lcp(void)U8 *p_lcp=receiveFrame;U16 crc16;U8 flag=0;U8 i;Static U8;cop
27、y1=0x7e,0xff,0x03,0xc0,0x21,0x03,0x00,0x00,0x08,0x03,0x04,0xc0,0x23,0x00,0x00,0x7e;/papU8 temp50;U8 flag_sd=1;p_lcp=p_lcp+3;switch(*p_lcp)case 0x01:/requestp_lcp=p_lcp+4;doswitch (*p_lcp)case 0x01:/Maximum-Receive-Unitpassp_lcp=p_lcp+4;break;case 0x02:/Async-Control-Character-Map passp_lcp=p_lcp+6;b
28、reak;case 0x03:/Authentication-Protocol chap to papif ( *(p_lcp+1) = 0x05 )flag=1;p_lcp=p_lcp+5;for (i=0; i10; i+)tempi=copy1i;temp6=receiveFrame4;/indentiferelse p_lcp=p_lcp+4;break;case 0x05:/Magic-Numberp_lcp=p_lcp+6;break;case 0x07:/Protocol-Field-Compressionp_lcp=p_lcp+2;break;case 0x08:/Addres
29、s-and-Control-Field-Compressionp_lcp=p_lcp+2;break;default:break;while( (flag = 0x00) & ( *(p_lcp+2) != 0x7e) );if (flag = 0x00 )flagLcpServ = OK;temp0=0x7e; temp1=0xff;temp2=0x03;for(i=1; i6+receiveFrame6; i+)tempi+2=receiveFramei;temp5=0x02;/ackbreak;case 0x02:flagLcpClient = OK;flag_sd = 0;case 0
30、x03:temp0=0x7e; temp1=0xff;temp2=0x03;for(i=1; i 8) & 0xff;Transfer_Meaning( temp , temp8+8 );PrintBuff(uart0SendBuff , UART0);/*Pap*/void Pap(void)if (receiveFrame3 = 0x02 )flagPap = OK; /*Ipcp*/void Ipcp(void)U8 *p_ipcp = receiveFrame;U16 crc16;U8 i;U8 temp50;U8 flag_sd=1;p_ipcp = p_ipcp+3;switch
31、(*p_ipcp)case 0x01:for (i=0; ireceiveFrame6+6; i+)/copy receiveFrame to temp and set it to acktempi = receiveFramei;temp3 = 0x02;if ( *(p_ipcp+3) = 0x0a)flagIpcpServ = OK;break;case 0x02:flagIpcpClient = OK;/get IP hereflag_sd = 0;break;case 0x03:for (i=0; i 8) & 0xff;Transfer_Meaning( temp , temp6+
32、6 );PrintBuff(uart0SendBuff , UART0);/*Dail Gprs*/U8 DailGprs(void)U8 flagIpcpClient = FAIL;U8 flagIpcpServ = FAIL;U8 flagLcpServ=FAIL;U8 flagLcpClient=FAIL;U8 flagPap=FAIL;U8 receiveFrame256;do ClientConfig(); Delay(DELAY1S); while ( FindFrame() ); Protocol(); while ( !(flagIpcpClient & flagIpcpServ) );/*UDP*/void GprsSendData(S8 * pSendData , U8 len)U8 *pCheckSum;U16 a,b
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 重點環(huán)節(jié)應急管
- 沈陽理工大學《含能運載材料》2023-2024學年第一學期期末試卷
- 沈陽理工大學《操作系統(tǒng)》2022-2023學年期末試卷
- 沈陽理工大學《環(huán)境工程項目管理》2023-2024學年第一學期期末試卷
- 海南小產權房買賣合同
- 2025屆高考數學統(tǒng)考二輪復習第二部分專題5解析幾何第1講直線與圓教師用書教案理1
- 2024部門經理入職發(fā)言部門經理入職合同范本
- 2024職工住房抵押借款合同范本
- 2024網絡安全服務合同
- 2024水庫承包合同范本范文
- 2024年浙江杭州錢塘新區(qū)城市發(fā)展集團限公司招聘30人公開引進高層次人才和急需緊缺人才筆試參考題庫(共500題)答案詳解版
- 成人住院患者靜脈血栓栓塞癥Caprini、Padua風險評估量表
- 排球《正面上手發(fā)球》教案
- 股骨粗隆間骨折
- 小班安全我要跟著老師走
- (正式版)JBT 14795-2024 內燃機禁用物質要求
- 基于核心素養(yǎng)初中數學跨學科教學融合策略
- 200TEU 長江集裝箱船設計
- 辦公樓物業(yè)服務管理的培訓
- 智慧能源管理平臺建設項目解決方案
- JTG∕T F30-2014 公路水泥混凝土路面施工技術細則
評論
0/150
提交評論