|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
基于MODBUS协议的单片机主程序 测试通过
( ^/ J$ m) s+ o$ |+ f8 |, c
! g ^3 e' _" Q7 D: U
# v: D& p) H/ u; z5 v4 `' q$ a* g/ \$ J- w- H4 {" O/ j
% Q D! u/ z2 u& S+ o* M8 E单片机源程序:+ w: C; l u u2 z' V
#include"reg51.h"* A D6 u9 _4 N2 V2 N" Z4 U
#include"lcd.h"" C" [' o% X! a* y, a8 Z
#include"address.h"+ z5 C; V# J: c; m: \6 n
#include"intrins.h"1 j* X8 ]) `. v6 Y# ^( O! ?$ L
#include"crc.h"; t, a1 } m1 e
#define uint unsigned int: ~! w; J" L9 a0 E; D
#define uchar unsigned char4 ^8 t4 y& t0 k1 ?
uchar code sendbuf[]={0x01,0x03,0x00,0x0b,0x00,0x01,0xf5,0xc8};
& B5 p( a0 y' _. f. z$ p1 m//0x01 从站地址,0x03功能码,0x00寄存器地址高位,0x09寄存器地址低位,0x00数据个数高位,0x01数据个数低位,0x54CRC校验码高位,0x08CRC校验码低位1 m6 l9 c0 J9 e. S, `% a3 ?9 n
uchar code tab[]={'0','1','2','3','4','5','6','7','8','9'};
7 o9 [/ w/ V. f- s0 E Huchar code error[]={"error!"};5 Y' ^6 V0 N; n
uchar tem[5],tem1[8];
* L& r# W6 }8 t7 b4 tuchar resvbuf[7];
* @) ] F {7 c; kint i,j;
/ @! g7 P) k0 g! `8 V( tuchar resvend,temp2;5 o+ w6 p2 s$ p! @# a
uint crctemp,temp,temp1;. S2 j! P6 j+ ]) X/ T' ~
void convert10(uint dat). K! J5 M5 `/ Q( @: M( w6 y: a
{
$ b$ i5 L$ A9 c5 m2 a5 `. l! Y uchar i;
. ~, x- _# [3 \ tem1[0]='U';
2 |7 d1 y( l+ h+ F9 a; W tem1[1]='=';: x+ {* @4 P: h7 M1 v% p; j; b `
tem1[7]='V';
- e% D+ u! C8 T; _) c: Q5 J tem[0]=dat/10000;
) S' K, B& a6 ^9 U( q5 _; \5 ?* t dat=dat%10000;
% S7 Q8 g. A; |( ~/ N tem[1]=dat/1000;
% ~, V. D, k1 k; `$ s dat=dat%1000;* j9 I7 {$ [4 U- i1 u, Q6 T8 G: W1 y
tem[2]=dat/100;
2 ^( N: {+ x4 {' u" K, u: \$ s dat=dat%100;4 S/ [; q' x$ U4 k! C
tem[3]=dat/10;
3 p% }, g; }0 f' J3 z. d* o3 @ tem[4]=dat%10;
4 D3 m0 f0 K) r for (i=0;i<5;i++)
4 z6 l& v/ E; c( [" M) m {( i0 `- @% G C( H* H
tem1[i+2]=tab[tem];! S0 g- Y1 N8 l' w2 A9 V
}$ ^9 I5 c" N5 V: f; L |; v/ N! s
}
' l- ]! g2 f; t: M$ t" w Yvoid serial_int() interrupt 4 using 1
4 ~, T$ ^/ a; i1 J{
, @2 `0 ^9 O9 g; |: l if(RI)
: n! c& ^1 N, V+ A {
+ T3 U9 V8 L1 S z& Z+ ] RI=0;
, c3 q$ Z3 d5 U% `* c; D, Z //temp2=SBUF;5 o! n t9 B7 K4 u( l3 d2 P' t
resvbuf[j++]=SBUF; ( \ j% i- x! v
if(j==7)
# v2 m" p! ]$ u! ] {
7 {$ j2 m# L/ g. q$ ^ j=0;5 ^! J+ B9 x% V5 @
resvend=1;4 T2 n! {* E& o- M0 e% m, }5 U
} 0 c5 w& L7 v9 Y( k# B" |4 u
} k- T+ [ S0 y/ w* E
}
: H! Q$ m5 ^4 |% \1 D7 }* b+ Dvoid initserial ()& ^! |) t# n0 m# w- Q
{
9 N* L) q [. |/ c resvend=0;
% ?# n. I [2 Z. e6 l# d TMOD=0x20;
1 {+ p7 p6 d" m" B: | PCON=0x00;
, F2 a9 f, Y# b0 a# L4 b& [ SCON=0xd8;3 K, ~; {. Q% w7 a0 Z& x
TL1=0xfd;( `5 A% k2 Q% V- O5 W \2 _& ~* e
TH1=0xfd;
; I8 d3 {4 x' f3 i TR1=1;$ V; c, Z% u% |7 i
ES=1;, m7 h" ^0 h3 k8 x5 G: E# E( L6 P
}
% a: G$ w3 m; i0 T- l% |- j' lvoid displayout()* d. w0 d$ R5 Y9 _; Z
{
! [# ^1 E: u. I crctemp=crccheck(resvbuf,5);2 y% r. _, Z0 Y/ h
temp=resvbuf[5];
+ `: e, J' ?/ h8 ^ temp=temp << 8 | resvbuf[6];
" O+ y3 e% r q6 x$ D& L if(temp==crctemp)8 l& w0 L! Y1 ?3 P4 _
{4 ~+ ^' }* L: A/ g
temp1=0; I4 W' X( p. U
temp1 |=resvbuf[3];
- t" D9 u; }" H, ~ temp1= temp1<<8 | resvbuf[4];
) j: p3 W6 A' d' \ convert10(temp1);2 N& h3 R8 u$ U# @5 ^! S; Y. ~8 X
printf(tem1,8);
( V- ?. f8 W3 U* @ C5 X4 A& R }
5 V& Q' C; j f! Q+ h6 g# y: e% \ else4 `& A& E" H7 k) J( c4 y
{) t" @& @. W# K& f& Z
printf(error,16);
9 ] B2 S2 i" [% B* S+ [( ^! @ }
. B$ r% R# `# u& u9 L1 B) u( p} 0 y' i- @% ?3 k9 C& B" K" j
void delay_10us(uchar n)3 T9 e* N8 E6 M* a& Q+ t2 q5 C3 \
{- }6 t) Q/ [( w; M( U
do
4 n/ k: a" F7 b' A5 {. I% X {9 m5 I2 w5 R* G2 a3 }
_nop_();# N u5 @$ q* _
_nop_();
! l$ t, h& C4 m" h9 y$ ~* u _nop_();
3 N$ w' o( n: K% X' H. c1 O _nop_();
# L5 V8 x1 l5 c D! ~ _nop_();
2 X! l& I/ C% R }while(--n);$ j& F5 w) ?: Y- E
}- r6 v4 z. \1 U" s
void delay_ms(uint n)
% q4 R% J; n& J6 ]! \# k{1 T8 k" F* \' p
do( ?! c5 y% ~0 r- x' s
{
5 g. Z) D4 K! i2 m% A+ O5 T* P delay_10us(131); o E+ I! d, w$ I) c1 c1 j: k
}while(--n);- |; S, q0 _ d" v
}
+ {; y0 q( z8 k0 ~: ~# H
+ M) a$ K& g& C* g o! } Xvoid main()( \1 E3 ?# C8 W% G
{
0 w, B7 M2 x( w! n: B) Y) e initlcd();) P$ A3 t) s+ V7 G9 V3 x
initserial();
u4 L5 Y5 S# S6 O EA=1;
+ {2 x, R P; m2 _ while(1)
1 D/ A8 X' h) R' u2 A; s* u- s: m5 h {
/ D$ `6 z/ ?+ @ H( V i=0;- r6 m7 m5 G: Y6 G, f+ y
; |! f! ~; R V V# J2 q) n( Y% p7 ^5 m…………限于本文篇幅 余下代码请从论坛下载附件…………
: s: h8 @7 `$ D9 M+ c% `3 O
; q4 d! f4 H1 Y* |6 y, U2 X$ U. o, ]$ x, W
|
|