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