官方下载freemodbus-v1.6.zip源码,然后把源码中的modbus文件夹、demo\BARE\port文件夹导入工程。3 r# L. d9 _& c, u( i( L$ o
; g9 b1 [& |0 |% U1 ^ u先是一波无脑导,把文件夹里所有文件导入。: F: k; R: v1 |3 P8 x0 v
) ]6 t* G" _% q1 p; Y: ?" W& M7 o2 x& u
存储区配置,单片机上定义的起始地址要比实际通信过程中读写的地址+1。
4 R4 }& Y0 ~6 c: y Z- #include "mb.h"
; _. H( \4 F* `$ C - #include "mbport.h"2 ]8 L/ B- m( m; g) v2 a( F
- #include "mbutils.h"
2 C* E0 k: L/ c& e( q - //输入寄存器4 v( I2 p' ~, l/ f% x |0 ~! v
- #define REG_INPUT_START 10013 G5 M- F& _0 U, ~! ?; X: U
- #define REG_INPUT_NREGS 2
! X$ S1 _; F4 X* S& p
! ?9 n9 k1 Q; o2 x) t5 X7 M5 u- static USHORT usRegInputStart = REG_INPUT_START;3 x% }/ H& y9 V7 E* R( A
- static USHORT usRegInputBuf[REG_INPUT_NREGS];
( D) p# ?) ?2 m6 K2 W3 J" i1 w - 5 D6 k6 [* k/ Z, r6 c
- //保持寄存器0 f l. P. h* U( @1 [7 [9 ]
- #define REG_HOLDING_START 2001
8 Z2 d# x: \3 t% x0 V7 i( j5 y - #define REG_HOLDING_NREGS 19 D% c) Y0 Z1 g5 p
: p; K) k, a2 I% F8 G- static USHORT usRegHoldingStart = REG_HOLDING_START;
+ O5 b6 i* v* P. ?$ O - static USHORT usRegHoldingBuf[REG_HOLDING_NREGS];* T) R$ S4 S$ r; o7 S
- t0 T9 X7 w }- //输出线圈点7 p6 n4 U/ b* d4 _& E" \4 Q
- #define REG_COILS_START 3001
; S% ~ P2 O- i6 [ - #define REG_COILS_SIZE 4' U& n( X; ^+ }6 a& H) ~7 Z- [. z
- 4 Q( y" }. m: I, A f+ S
- static USHORT usRegCoilsStart = REG_COILS_START;
6 ~8 {! A/ D1 K- s - static UCHAR ucRegCoilsBuf[REG_COILS_SIZE/8+(REG_COILS_SIZE%8?1:0)];
, B$ b' p& d& [" z' v9 H" [& w
6 a, U9 b! |7 M) q. l% | m- //离散输入点! B9 l6 ^9 f$ P) Z0 [
- #define REG_DISCRETE_START 4001+ y E, _7 o) `, ]8 ]% W( Y1 }
- #define REG_DISCRETE_SIZE 8: u- x1 J; v( p" c
- 9 S9 ], R$ Y/ B9 X. x
- static USHORT usRegDiscreteStart = REG_DISCRETE_START;
: \" ], B$ J2 E7 T: K } - static UCHAR ucRegDiscreteBuf[REG_DISCRETE_SIZE/8+(REG_DISCRETE_SIZE%8?1:0)];# W1 X! m7 `+ b9 ?+ W# ]$ U
- 8 C! |9 o5 @0 n" M* H
复制代码 Q7 a0 q9 k+ |' R- K( l6 D
主程序就是标准的运行三连+IO读写。
4 X u/ S- z9 T- [' F8 B- eStatus = eMBInit( MB_RTU, addr, 0, 9600, MB_PAR_EVEN );, y8 x8 ]% G) y) p! U
( P% ?. m) k4 r+ [% b- /* Enable the Modbus Protocol Stack. */
6 p+ U& _$ r6 i' C/ |. d e - eStatus = eMBEnable( );5 I3 S2 h; f2 v3 |* O
& _1 e# c# s: @* c! D- for( ;; )
- \5 \# G2 z% b8 i$ O, B! R - {
6 P2 I; E6 Y0 @3 h! t1 N - ( void )eMBPoll( );- M& c n+ R8 b
- usRegInputBuf[0]=Get_Adc_Average(8,10);
; H$ l' F5 K# O& e$ F7 A - usRegInputBuf[1]=Get_Adc_Average(9,10);: d6 k4 O" k! u3 _0 v1 Z P* W1 ^
- 9 r4 W' c& v o1 ^ h. ~! d/ W* _: e+ E
- ucRegDiscreteBuf[0]=(GPIOB->IDR&0xFFFFFF00)>>8;1 F3 m9 S/ n+ r7 g3 R$ v
- GPIOB->ODR=GPIOB->ODR&0xFFFFFF0F;
1 d" v* _' }) v. j - GPIOB->ODR=GPIOB->ODR|ucRegCoilsBuf[0]<<4;
% B* N% l4 n8 f( g% ` - }- Z. k2 G; _1 p3 J4 n
复制代码 & C" H7 j2 L* z; h) n
支持的4种读写function扔在主函数后面就行了。
3 P( m0 G6 V4 X7 \' ^- /*** s+ p0 T- S1 ]9 H! A
- * @brief 输入寄存器处理函数,输入寄存器可读,但不可写。
: k. |, w9 j4 M1 Q) u4 S - * @param pucRegBuffer 返回数据指针
3 b9 Q- j' R% C8 ] k - * usAddress 寄存器起始地址
+ Y2 B2 O8 l. g Q- L" x - * usNRegs 寄存器长度 c8 n1 _0 v2 _6 S# U
- * @retval eStatus 寄存器状态
4 v/ f- Q" O1 v - */3 O( O5 x' q; n
- eMBErrorCode
- b( Q' v% X! I, ~3 ^' B/ Z& _ - eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )# y6 F( F, v4 |$ j; l. |) }5 a4 d$ x
- {/ X( ]9 E$ f9 k" G# }$ K
- eMBErrorCode eStatus = MB_ENOERR;
# }" H3 h1 X2 p2 l* r: o$ z - int16_t iRegIndex;: ^8 C% B$ _+ s: R7 ]8 w& e
- " X5 @9 @* O: q6 @ p
- //查询是否在寄存器范围内
4 N, s6 T6 m& v- M0 |/ n: X - //为了避免警告,修改为有符号整数- R+ w1 M7 t4 @( O0 ?' n( o7 n T
- if( ( (int16_t)usAddress >= REG_INPUT_START ) \0 T' S3 V! O' J+ j ^( _" g
- && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
z2 y4 {. }5 B* E) r" { - {! z; Z) y8 P3 N# w/ o
- //获得操作偏移量,本次操作起始地址-输入寄存器的初始地址
+ f4 S& K7 o' l- Q' d - iRegIndex = ( int16_t )( usAddress - REG_INPUT_START );
! `! A. J K" @9 { - //逐个赋值
/ V# z3 [2 D. G5 C" L$ J - while( usNRegs > 0 )- E% O$ \3 b. @1 y/ M L- E* j
- {
m9 h& G7 H% j& F/ W, C) c - //赋值高字节0 @2 F5 s$ \& w7 }9 R
- *pucRegBuffer++ = ( uint8_t )( usRegInputBuf[iRegIndex] >> 8 );! x2 p( Z3 u7 j, Q6 c
- //赋值低字节
( i$ Y0 o2 T' n, B - *pucRegBuffer++ = ( uint8_t )( usRegInputBuf[iRegIndex] & 0xFF );+ O! G0 o0 ~- A* T! ?
- //偏移量增加) s5 f& i" r Z( u
- iRegIndex++;
+ |6 Y, z9 D# @4 u: f - //被操作寄存器数量递减1 t1 U; }8 [1 h$ v3 a
- usNRegs--;7 l1 r6 x3 m: z
- }
- c/ A8 f: R9 L y3 L - }- J1 L J- [2 p6 a/ u& W
- else; o7 {; J, B( O. G9 V5 F6 l+ G
- {" y" K+ _6 @4 O9 c' }
- //返回错误状态,无寄存器
/ S7 o% t9 O/ a! i; I( \& K& U8 k2 F - eStatus = MB_ENOREG;8 C# `! {1 t+ j5 N5 R
- }* r1 _' n- ~) E3 { l
4 J( M; B7 E$ F2 [% i( b- return eStatus;4 N1 [& j+ A; G0 b% a
- }
" e8 j9 l4 n+ L9 U6 q - & k1 {7 @$ \4 S$ U6 r* x
- /**: Z* x1 [4 J) R4 [+ O& H) z
- * @brief 保持寄存器处理函数,保持寄存器可读,可读可写$ } ^' ^# R/ k2 h
- * @param pucRegBuffer 读操作时--返回数据指针,写操作时--输入数据指针
% }: |" Z- ?6 |# D7 M: D - * usAddress 寄存器起始地址0 R" F N% Y. L" M6 q. J: O
- * usNRegs 寄存器长度
6 l4 S9 Q# K- h S/ k" R7 Z4 Z; M - * eMode 操作方式,读或者写/ H7 x0 ?. s6 ^
- * @retval eStatus 寄存器状态
: ~! ]: l& Z- g4 o$ C& e+ n, b - */
$ [; {( a' x h) o; U0 P g - eMBErrorCode
- e4 P" R& }/ z/ z& ~ - eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,1 t# s4 X! A* b3 @: W: K. ?% R
- eMBRegisterMode eMode )
4 X, I% ^% X" z+ H$ {% u4 \$ l - {
! C5 `7 }. v m. V5 a4 R4 g7 ^5 F - //错误状态
8 @0 c0 A$ \9 p8 B2 u - eMBErrorCode eStatus = MB_ENOERR;" y, s' u" ]. B6 U# ~ }0 g- S' y
- //偏移量
3 k0 C" L& F* p* I8 P& I! z - int16_t iRegIndex;2 B4 g+ A3 R' j' A a# E3 ]8 z
- & q% S' {4 X: |$ l5 {
- //判断寄存器是不是在范围内" R, }) V" C9 N# m/ t
- if( ( (int16_t)usAddress >= REG_HOLDING_START ) \7 @7 q; e# ^) k6 V( h& y
- && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
: U) c4 b( H8 `4 ^3 t& \3 z - {% B: ?9 [: c9 L% V: W
- //计算偏移量
, n3 ^& C4 |& ^ - iRegIndex = ( int16_t )( usAddress - REG_HOLDING_START );
% L3 c* q$ Y. B" W' v2 f
% H3 C1 q# v& ?- switch ( eMode )- r4 _- h; g" w6 b5 o. I
- {
) g& q% Y# ?1 V# a - //读处理函数4 K4 `- T6 a! w! S, p4 F. u
- case MB_REG_READ:1 v/ @ C$ a0 X7 M$ l: ^
- while( usNRegs > 0 ); z3 K; f9 E; {: A: P
- {
# O3 k; q2 L3 Q8 O+ _% P8 d - *pucRegBuffer++ = ( uint8_t )( usRegHoldingBuf[iRegIndex] >> 8 ); O% X1 e$ b! x, }1 `
- *pucRegBuffer++ = ( uint8_t )( usRegHoldingBuf[iRegIndex] & 0xFF ); e9 @( K% g5 U
- iRegIndex++;
3 @. H: y" E. a; k - usNRegs--;
& s! p) ^3 d4 h6 |) j. \" D - }5 f2 D& y( n) [. a
- break;
2 c3 B9 u7 k3 H! y j A - 2 `) U! e$ j7 _' f% W( i1 W! }
- //写处理函数' s8 t8 u1 k5 V- g$ n
- case MB_REG_WRITE:
" ]; R" i+ d" A; S' { - while( usNRegs > 0 )
1 P/ U0 @& P0 p1 d1 N. X3 w5 P - {
' D a; Y7 B5 T; ^ - usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;# y8 b2 ^1 }' r; s1 R) z
- usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
* g. v& C% `, ]/ K - iRegIndex++;- G+ J/ G# H$ j0 n' e
- usNRegs--;( O6 f4 x* z1 |, H
- }
5 k9 B+ }" q; i: k - break;
5 y H% C5 R! w0 y1 x$ f+ v - }
$ }6 S$ ?9 x' c4 L1 g - }
" G }" s! x1 X: I - else
# R) W, Q- i* A8 h7 [ - {
' r9 _, _4 O( L7 m+ f6 \+ N - //返回错误状态
/ p% J: R( k" {! f6 h, O - eStatus = MB_ENOREG;
* K) o7 U( H% Z5 A% a% W - }
* H* m: c0 h3 a6 e8 Q9 E - + V4 t- N$ j8 r9 h: I% B
- return eStatus;; C2 L) ` u8 i0 q
- }
5 }3 \& W! T/ P' }$ L7 X: j - 4 S' E! a9 `9 e3 N; g4 j. t; _6 d1 ^
/ d. L+ D E9 M p$ {4 z6 h, h0 F( P/ M
( F6 g! q# G/ |& j- & j) h0 e/ `' {& t' m6 p
- /**
- F2 r) E( s# `" N6 ^5 f) W - * @brief 线圈寄存器处理函数,线圈寄存器可读,可读可写
9 h3 Z+ P8 J* _ T. @; v) l) T - * @param pucRegBuffer 读操作---返回数据指针,写操作--返回数据指针- _ ]! H$ y) I6 j! q& e* |5 B* ?
- * usAddress 寄存器起始地址
# k# O, `. t; z0 T - * usNRegs 寄存器长度
& i$ O' v5 F' c - * eMode 操作方式,读或者写: M5 f" q8 ?* c8 Y
- * @retval eStatus 寄存器状态
4 w7 k" u1 W# M9 ?% G+ { - */
! [3 k4 M& b8 K7 g j# h - eMBErrorCode$ h7 L6 Y6 F. n9 S
- eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,8 f& f, v# L% J& Z- \* s
- eMBRegisterMode eMode )+ N9 h1 _' C0 D0 `% V8 L9 U; Q
- {+ W$ i+ S; W! D, u2 m' R! g
- //错误状态
% E* R, h2 q' Y" V/ m - eMBErrorCode eStatus = MB_ENOERR;
1 i' T/ Y: e& K3 q! z& U2 V! |7 F - //寄存器个数
f, }. F. i! W$ r3 k- Z; }3 u0 ~/ g+ `0 r8 Q - int16_t iNCoils = ( int16_t )usNCoils;
2 B& c8 o# O5 G( u* F2 L. O/ s - //寄存器偏移量
+ D+ G0 D- c2 V9 z - int16_t usBitOffset;
7 N/ e3 X; v" P; Y0 \8 i3 g( z8 G - % M+ S$ U8 @ N2 Z8 a
- //检查寄存器是否在指定范围内/ d: q$ b, @$ G/ {' [
- if( ( (int16_t)usAddress >= REG_COILS_START ) &&, I" H2 U/ ~/ O3 |
- ( usAddress + usNCoils <= REG_COILS_START + REG_COILS_SIZE ) )
3 |; {$ f4 O2 s: ^0 j - {
0 Z; D5 K. Q! U) F& J9 x4 v& P8 z - //计算寄存器偏移量( q- O# \1 q7 i; M1 l# H$ w% }8 W) r
- usBitOffset = ( int16_t )( usAddress - REG_COILS_START );+ C- r& S( i; H3 q3 D
- switch ( eMode )( A X: ]& j# v: H: j" S2 }# z
- {1 `( c4 a% _% Y3 i" R: a5 R( m
- //读操作! E7 \9 r; V) N/ H2 X7 l
- case MB_REG_READ:( a! G6 H% m0 g B6 b+ t
- while( iNCoils > 0 )$ J6 f( W1 @) P; o0 M
- {# h8 R( ^3 P9 X! Q. T
- *pucRegBuffer++ = xMBUtilGetBits( ucRegCoilsBuf, usBitOffset,% b! b. @% c) {; f$ M" N; e& u$ A: T! l
- ( uint8_t )( iNCoils > 8 ? 8 : iNCoils ) );
O O; `5 P% g - iNCoils -= 8;
' O1 q. x) h* `. C1 g/ e& a - usBitOffset += 8;
- @0 l* c. w' W - }$ j; t& f ]& L! }9 g6 T5 ]8 G' ]3 k
- break;5 x7 t/ P4 x+ O4 o: ]
- % f5 s7 |7 e$ P& R/ z
- //写操作
# L V' \4 i1 j - case MB_REG_WRITE:
: P; s- v; ~. h9 U+ r' x - while( iNCoils > 0 ); b" c. M5 Z+ I. C, v% j- U
- {
" r& k- V9 x: T+ o* T; Z - xMBUtilSetBits( ucRegCoilsBuf, usBitOffset,
1 U% H/ w/ f. |6 V - ( uint8_t )( iNCoils > 8 ? 8 : iNCoils ),
' t1 b1 |) V. M7 K+ F& c. Y - *pucRegBuffer++ );
- G/ K/ n# b4 E! ^- y6 s - iNCoils -= 8;
6 k9 B, Y1 e7 U/ x' ]) E - usBitOffset += 8;
6 S* c: c7 _" o8 i- i1 R1 W - }
/ ~+ e* }- A- \* P5 O& P) g- k0 {% w& u - break;
2 W3 q1 O% h1 z8 a/ v - }
* \8 ?/ r! w4 o' X6 R5 y - 3 h, m3 y' Z0 x7 \1 l
- }
I% K+ P s( c7 B5 |0 w - else$ B, j' t" e6 R0 P; j3 m
- {, T8 Q& F; b; o/ V
- eStatus = MB_ENOREG;1 C. ^3 a) a) m4 o7 d5 l* m
- }( y8 R2 s7 P, W; ?
- return eStatus; j+ x2 @. U/ ^
- }" p6 B$ P- B" _5 r* |! [- J7 S2 U! r) r. r
- / g) q& @; ]. C0 r
# O2 Z6 [- `+ L# ~# b/ b6 |! w- + B3 ~6 s# [2 m' I: \
- / H5 l! o& _4 p; F: m
- /** V y: I! {6 {: m* N
- * @brief 开关输入寄存器处理函数,开关输入寄存器,可读. A# I) b; _4 R2 \- K3 W
- * @param pucRegBuffer 读操作---返回数据指针,写操作--返回数据指针
! K# j( p& u5 t3 r - * usAddress 寄存器起始地址
2 k8 g* L. |) {% u - * usNRegs 寄存器长度
0 m$ g3 K, A; Z5 w0 z5 g - * eMode 操作方式,读或者写/ p# o; a y$ @2 V [# g
- * @retval eStatus 寄存器状态
( C( i$ B, F& n. B# \8 d0 ^! I% S - */
, ]% L- W) M/ S0 b$ [9 ~% ] - eMBErrorCode- g8 v* W3 T) z- `. q$ I/ N- C5 K/ |7 H% S
- eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
; X5 {8 w! M; j4 T - {8 m4 G9 r- ~0 n. U8 i' b8 H
- //错误状态
+ X6 `7 b' S, @2 e0 b A8 r# j; F - eMBErrorCode eStatus = MB_ENOERR;
, D- N/ b$ r. ? - //操作寄存器个数2 K& ~0 L. a/ Q7 t+ V
- int16_t iNDiscrete = ( int16_t )usNDiscrete;
' ^8 u4 o3 k9 P/ G1 C6 d" m" ? - //偏移量# m; k- j- q) |! P9 [6 a
- uint16_t usBitOffset;% X/ v. R; T( O
; z5 V, \, y$ |! U4 x- //判断寄存器时候再指定范围内 @! B4 Y9 |% E' o
- if( ( (int16_t)usAddress >= REG_DISCRETE_START ) &&
1 i/ {3 Y$ i8 k$ ]8 J8 T3 `: z - ( usAddress + usNDiscrete <= REG_DISCRETE_START + REG_DISCRETE_SIZE ) )
+ a3 n& B8 B \; [" v9 F: [ - {
( X' j/ w' T1 w, E/ E+ N- ~7 c+ f - //获得偏移量& H1 P3 M1 d7 j1 U- b- G: T
- usBitOffset = ( uint16_t )( usAddress - REG_DISCRETE_START );8 }; J* w% R8 b: J( l
- 0 |9 c0 ]* X1 s9 ?* P; g
- while( iNDiscrete > 0 ) C7 i/ V$ B6 f# [2 R8 c* [, ^
- {
# _% H; c9 i w- j, U - *pucRegBuffer++ = xMBUtilGetBits( ucRegDiscreteBuf, usBitOffset,
* X, W% L1 g: A - ( uint8_t)( iNDiscrete > 8 ? 8 : iNDiscrete ) );
$ k) D# A" s/ s2 F/ ~ - iNDiscrete -= 8;* [5 e( A. U* P3 u' g5 N* i" _) n
- usBitOffset += 8;1 ^9 ]' M0 l5 k3 X# g4 J; W
- }9 I1 ]4 {% l/ i T$ m& a" H. l/ p
: j' _! |$ M* I# K- }
* r' ^% ^$ f& r - else
! _- T0 c9 ^2 s7 l9 L& k1 j - {
! o* l) }: X5 T$ N( Z# S; s6 K7 H - eStatus = MB_ENOREG;" q. [1 |' t/ P, _
- }) q- O& x3 @5 T% u- j, r q7 C
- return eStatus;$ R8 a4 [/ C- C8 I8 }/ u: ~
- }
+ Q1 ?6 Z2 y& g# I2 w+ `) N( W - ' E* i4 t- L! I
- 5 y$ H& d8 L# d
复制代码 % K6 v, o! _; f3 M3 g9 O: | M
porttimer.c是和定时器相关的实现接口,计算好预分频与装载值之后,可以直接调用原子例程。
0 f; ]* e5 Z5 s1 c# O: r2 h( n- /*' q- U+ l) A- r1 S4 g p4 E
- * FreeModbus Libary: BARE Port
$ i; n2 |5 X& H% M) W W) j/ o - * Copyright (C) 2006 Christian Walter <wolti@sil.at>
( i; M; q7 _' o8 _' f - *" w2 D# [! B! C2 @
- * This library is free software; you can redistribute it and/or! t' h. g4 K o& d8 K" g/ r4 i
- * modify it under the terms of the GNU Lesser General Public; H1 D; @& i9 X7 s+ r9 s- r9 s
- * License as published by the Free Software Foundation; either
[- v# c$ R0 W: j% a& v4 | - * version 2.1 of the License, or (at your option) any later version., {% I5 T: O7 L [$ _
- *
% `" k: n( H8 O" C+ Y: z - * This library is distributed in the hope that it will be useful,7 ]6 @ j+ {# I3 e( [0 _
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
2 T* H6 g2 i2 C! {0 J - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
" A' _$ @/ j0 `- ^+ | - * Lesser General Public License for more details.
% w( A; e9 \4 j - *+ Z) j" |2 q2 z C6 I8 l
- * You should have received a copy of the GNU Lesser General Public2 n7 m& X( p2 ^' c; {' g
- * License along with this library; if not, write to the Free Software; U/ G) l% k- B6 u# y* p9 {7 q$ @8 L- s
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA1 h& U7 ?& D5 _. s) c$ z+ S9 B
- *
0 B; Q: u1 K8 b% I - * File: $Id$; Q) N0 h1 p1 f& w% V# e$ Z3 F! O# A
- */! K) a% _: C, p0 N
- 8 p1 h! S: C) S z
- #include "port.h"
R9 }0 e* Z% {. }5 T
1 T! y# u4 y- @# M6 |- /* ----------------------- Modbus includes ----------------------------------*/
/ g8 ]) g1 M' ^ v! `) T - #include "mb.h"
0 r a9 I) \. ]; J8 _! { - #include "mbport.h"
- H" M% V+ |7 {5 ^: C* L% M
" F' Z- }+ }0 `1 E6 I% u- #include "rs485.h") } l" K8 R# y$ L# }* ]: i
- % p# q* J* `5 i; P' W
- /* ----------------------- static functions ---------------------------------*/& F2 a+ \) I1 ~& d8 @) [' I
- static void prvvUARTTxReadyISR( void );, D0 I1 H3 }3 c
- static void prvvUARTRxISR( void );" L& K' t2 o0 j( C8 f
# l5 o" N4 L1 r7 W# ^2 i( n- /* ----------------------- Start implementation -----------------------------*/* P, E, g9 c& b8 ^: k5 O( _
- void
5 {" u- d6 |. F! ]: g$ Y - vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )( a6 p4 j6 p+ X5 j4 l9 b
- {
' T/ X, [. a8 e - /* If xRXEnable enable serial receive interrupts. If xTxENable enable
0 _% u" ~) ~& C - * transmitter empty interrupts.
( n2 g# ^# h. n, {' u - */: p: o v, B7 z3 b
- if(xRxEnable)
/ }9 u) D! b, ] - {
: p0 O* l, W$ u) v - //使能接收和接收中断
, m6 H, p' M; m: D/ {( P( ], m - USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);3 G3 X+ }2 L: m- T
- //MAX485操作 低电平为接收模式/ n8 V# b* Q' H* H0 U& Q& u1 }
- //GPIO_ResetBits(GPIOD,GPIO_Pin_8);
& \ N: J! C0 f8 h8 c - RS485_TX_EN=0;
! ^7 w: T2 ]9 [. y* ` - }/ X6 b& s8 X7 }" k3 \/ ~" a
- else
( x8 j% m/ h2 c. Q! I - {" R/ g( |1 `& [% E4 Y, m
- USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);% J# l% t% d% j* D' v
- //MAX485操作 高电平为发送模式2 q$ w) s5 s; v6 l P! v% s' S
- //GPIO_SetBits(GPIOD,GPIO_Pin_8);7 n2 e% N& E. p/ v; {+ V
- RS485_TX_EN=1;% a, z3 l9 ^% m6 L' ~2 H' ?
- }
$ I' r# E5 C1 q8 U% R- i - 5 p5 V1 a) ^! k5 C( f
- if(xTxEnable)7 ]1 @% `7 {7 z" J" B; M* r" r1 Y
- {
1 }4 x) R5 L6 F) d - //使能发送完成中断! Q# H2 H6 _5 G( b+ I4 @
- USART_ITConfig(USART2, USART_IT_TC, ENABLE);
& T# a1 M4 G* T& l3 p& m - }
9 I5 P( I0 ^' V+ H' y+ [5 { - else# D- x) d; u$ w7 z' t9 P! Q
- {# H; U$ A4 p0 d
- //禁止发送完成中断
# K' h* j# }- U$ R9 H9 Y) X2 q - USART_ITConfig(USART2, USART_IT_TC, DISABLE);1 |. `9 l! S3 G2 C
- }- L9 m$ p! i& ^7 Q4 Z
/ y; M9 o1 z* ]$ d9 k
, Y; l1 i3 y0 k" d1 }3 F3 R3 E/ C- }
5 y7 g1 k7 n4 C! F$ n; w
% ~/ q( C! U) y7 N5 o/ M$ t# `- BOOL: y6 C' e8 X$ d! m) g l8 y
- xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
- w7 z3 Z- T {' A' A( \) T0 |; a/ r - {
7 v0 j7 n# @0 O( z: A' c8 u - RS485_Init(9600); //初始化RS485
6 L$ z- G l: ?9 o9 L* s3 \* Z3 ` - return TRUE;6 z* O+ v1 q8 ^ V2 I3 \
- //return FALSE;
$ S3 U# D8 |" Y- t7 \5 K0 x - }
4 f: R. ?- G U" E9 G! ?2 e - 8 ^' i, D% e1 d+ x: L' J' q9 d
- BOOL
4 }& r) R6 o4 `- f3 K - xMBPortSerialPutByte( CHAR ucByte )
9 l: p( s8 q- j - {
! N! V" O! v) ]; K+ K - /* Put a byte in the UARTs transmit buffer. This function is called+ [, G/ p% y2 c, u
- * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
0 F, s! J, ~/ J - * called. */( I. Y# |' ~) Z5 V' e! o8 h( d* _
- //发送数据
" a& ?7 k& m1 K+ N& a! L* G - USART_SendData(USART2, ucByte);
' ?& n. f( S$ r# x4 ?/ n - return TRUE;
. @+ g$ Z( I: ?5 I - }) m0 i# {' e1 Z" b
0 F$ R, Z3 m, w: ?- # O: g1 t: Q. @) j( s! j; ^3 [
- BOOL! H6 |* s; l2 A) A+ ]$ W B; D6 U
- xMBPortSerialGetByte( CHAR * pucByte )
0 E7 ^/ ~% Y5 a2 M9 @. u# r2 [ - {
: I3 e1 u7 A4 d - /* Return the byte in the UARTs receive buffer. This function is called
. }" k# i9 h ` - * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.. {: ]9 n' `6 ]- r, j4 E
- */& r; v) E8 ?3 a& V/ D& H
- //接收数据
# U# g- R$ q4 m; c) @" w - *pucByte = USART_ReceiveData(USART2);
" x; y: M2 l% Z. i% U# y - return TRUE;
4 Q8 ?9 K7 B) M- t - - U8 O% a# A& Z. Z
- }( D r! k% z$ N& K8 P% G; A
- , \5 c% g" R* A+ i N, S* s. h
- /* Create an interrupt handler for the transmit buffer empty interrupt7 ?4 j+ Z8 n5 t
- * (or an equivalent) for your target processor. This function should then
6 H) g0 _0 D& h - * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
/ L9 U' U! X: l" O - * a new character can be sent. The protocol stack will then call, _, o( e5 ?4 ^7 t* _ U
- * xMBPortSerialPutByte( ) to send the character.
" o+ Y! ~. R. C% L) R) g, a; u - */
4 L/ y; M. v, `" K( | - static void prvvUARTTxReadyISR( void )# _5 M/ t8 _0 @
- {
9 p9 O* ^( }4 M* B. ]( Y - pxMBFrameCBTransmitterEmpty( );. a) l( \; l0 J' v5 `) A
- }
1 Y6 l4 u' R9 G n6 n5 e$ Y- E2 I
6 [& k2 M$ }6 P8 ]' ^9 \- /* Create an interrupt handler for the receive interrupt for your target3 `$ p* o0 ?+ ~- i' S
- * processor. This function should then call pxMBFrameCBByteReceived( ). The9 o- W9 }! p0 |: y! S1 X- x
- * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the$ B9 ~+ d9 Y% I$ }
- * character.
" O3 E# {% Z4 I; _) f9 u - */
4 A( K" `" }! l% _ - static void prvvUARTRxISR( void )6 d+ |' v: x" M& ~/ W% e9 {; S
- {: {; e V- w" \! p1 {
- pxMBFrameCBByteReceived( );3 `3 o5 _% e4 d; }
- }
- F6 N- h P7 {! b# d - : ~& i& G& @2 L' C0 q
- void USART2_IRQHandler(void); ?" l) e8 d U
- {1 M% d# }" }4 Z3 v, ]4 q% |/ z- Z0 U q7 Q
- if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
9 ^, ^: \# m& T; {* K3 w - {
0 m2 I% ~# H; _' ~$ b( e; P - prvvUARTRxISR();9 R3 a* \8 d2 [' q r* c0 K
- //清除中断标志位
) b- g6 U. W. Y: i" [1 p, ~ - USART_ClearITPendingBit(USART2, USART_IT_RXNE);) q) A) P& i$ Q* P& G& R
- }
5 M' M& J. s# z' A+ T: @1 w E4 U - 1 E: Y) G! K8 B% B8 t9 W& W
- //发生完成中断
6 O4 y+ T, d' ~; c! A) X. z - if(USART_GetITStatus(USART2, USART_IT_TC) == SET)
% H. @8 F- R" Q1 a& H8 k! q# V - {) J* b0 h2 I" l+ O: i- ~! b
- prvvUARTTxReadyISR();
4 @' h9 M8 @, N% H- c- x" T% v - //清除中断标志2 F& L v8 P2 c2 S( x( O5 ]2 I5 S
- USART_ClearITPendingBit(USART2, USART_IT_TC);
4 ]5 m; g- x1 L- n3 D - }
" G: {( H: T) G, x- N D5 ^ - 0 ~, v9 M% Q) a, X5 e9 K P/ h" @
- }, u X8 I" g, O
h3 u5 I3 [3 K
复制代码
' L- R5 p7 R" p gmodbus移植过程基本就完成了。. m# [1 X( B- k! p
————————————————
' W6 r' z/ l1 q }版权声明:小盼你最萌哒
/ p4 E/ E- _' K. [ f如有侵权请联系删除
3 e+ H! N, |: Y; [/ i! ?
, }1 [9 p: [, g8 |; X& G8 e, F$ P' q& C5 Q+ d, x
|