你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

如何在裸机stm32移植FreeModbus

[复制链接]
攻城狮Melo 发布时间:2023-3-14 16:54
官方下载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
  1. #include "mb.h"
    ; _. H( \4 F* `$ C
  2. #include "mbport.h"2 ]8 L/ B- m( m; g) v2 a( F
  3. #include "mbutils.h"
    2 C* E0 k: L/ c& e( q
  4. //输入寄存器4 v( I2 p' ~, l/ f% x  |0 ~! v
  5. #define REG_INPUT_START 10013 G5 M- F& _0 U, ~! ?; X: U
  6. #define REG_INPUT_NREGS 2
    ! X$ S1 _; F4 X* S& p

  7. ! ?9 n9 k1 Q; o2 x) t5 X7 M5 u
  8. static USHORT   usRegInputStart = REG_INPUT_START;3 x% }/ H& y9 V7 E* R( A
  9. static USHORT   usRegInputBuf[REG_INPUT_NREGS];
    ( D) p# ?) ?2 m6 K2 W3 J" i1 w
  10. 5 D6 k6 [* k/ Z, r6 c
  11. //保持寄存器0 f  l. P. h* U( @1 [7 [9 ]
  12. #define REG_HOLDING_START 2001
    8 Z2 d# x: \3 t% x0 V7 i( j5 y
  13. #define REG_HOLDING_NREGS 19 D% c) Y0 Z1 g5 p

  14. : p; K) k, a2 I% F8 G
  15. static USHORT   usRegHoldingStart = REG_HOLDING_START;
    + O5 b6 i* v* P. ?$ O
  16. static USHORT   usRegHoldingBuf[REG_HOLDING_NREGS];* T) R$ S4 S$ r; o7 S

  17. - t0 T9 X7 w  }
  18. //输出线圈点7 p6 n4 U/ b* d4 _& E" \4 Q
  19. #define REG_COILS_START 3001
    ; S% ~  P2 O- i6 [
  20. #define REG_COILS_SIZE 4' U& n( X; ^+ }6 a& H) ~7 Z- [. z
  21. 4 Q( y" }. m: I, A  f+ S
  22. static USHORT   usRegCoilsStart = REG_COILS_START;
    6 ~8 {! A/ D1 K- s
  23. static UCHAR           ucRegCoilsBuf[REG_COILS_SIZE/8+(REG_COILS_SIZE%8?1:0)];
    , B$ b' p& d& [" z' v9 H" [& w

  24. 6 a, U9 b! |7 M) q. l% |  m
  25. //离散输入点! B9 l6 ^9 f$ P) Z0 [
  26. #define REG_DISCRETE_START 4001+ y  E, _7 o) `, ]8 ]% W( Y1 }
  27. #define REG_DISCRETE_SIZE 8: u- x1 J; v( p" c
  28. 9 S9 ], R$ Y/ B9 X. x
  29. static USHORT   usRegDiscreteStart = REG_DISCRETE_START;
    : \" ], B$ J2 E7 T: K  }
  30. static UCHAR           ucRegDiscreteBuf[REG_DISCRETE_SIZE/8+(REG_DISCRETE_SIZE%8?1:0)];# W1 X! m7 `+ b9 ?+ W# ]$ U
  31. 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
  1. eStatus = eMBInit( MB_RTU, addr, 0, 9600, MB_PAR_EVEN );, y8 x8 ]% G) y) p! U

  2. ( P% ?. m) k4 r+ [% b
  3.     /* Enable the Modbus Protocol Stack. */
    6 p+ U& _$ r6 i' C/ |. d  e
  4.     eStatus = eMBEnable(  );5 I3 S2 h; f2 v3 |* O

  5. & _1 e# c# s: @* c! D
  6.     for( ;; )
    - \5 \# G2 z% b8 i$ O, B! R
  7.     {
    6 P2 I; E6 Y0 @3 h! t1 N
  8.         ( void )eMBPoll(  );- M& c  n+ R8 b
  9.         usRegInputBuf[0]=Get_Adc_Average(8,10);
    ; H$ l' F5 K# O& e$ F7 A
  10.         usRegInputBuf[1]=Get_Adc_Average(9,10);: d6 k4 O" k! u3 _0 v1 Z  P* W1 ^
  11.                         9 r4 W' c& v  o1 ^  h. ~! d/ W* _: e+ E
  12.         ucRegDiscreteBuf[0]=(GPIOB->IDR&0xFFFFFF00)>>8;1 F3 m9 S/ n+ r7 g3 R$ v
  13.         GPIOB->ODR=GPIOB->ODR&0xFFFFFF0F;
    1 d" v* _' }) v. j
  14.         GPIOB->ODR=GPIOB->ODR|ucRegCoilsBuf[0]<<4;
    % B* N% l4 n8 f( g% `
  15.     }- Z. k2 G; _1 p3 J4 n
复制代码
& C" H7 j2 L* z; h) n
支持的4种读写function扔在主函数后面就行了。
3 P( m0 G6 V4 X7 \' ^
  1. /*** s+ p0 T- S1 ]9 H! A
  2. * @brief 输入寄存器处理函数,输入寄存器可读,但不可写。
    : k. |, w9 j4 M1 Q) u4 S
  3. * @param pucRegBuffer 返回数据指针
    3 b9 Q- j' R% C8 ]  k
  4. * usAddress 寄存器起始地址
    + Y2 B2 O8 l. g  Q- L" x
  5. * usNRegs 寄存器长度  c8 n1 _0 v2 _6 S# U
  6. * @retval eStatus 寄存器状态
    4 v/ f- Q" O1 v
  7. */3 O( O5 x' q; n
  8. eMBErrorCode
    - b( Q' v% X! I, ~3 ^' B/ Z& _
  9. eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )# y6 F( F, v4 |$ j; l. |) }5 a4 d$ x
  10. {/ X( ]9 E$ f9 k" G# }$ K
  11.     eMBErrorCode eStatus = MB_ENOERR;
    # }" H3 h1 X2 p2 l* r: o$ z
  12.     int16_t iRegIndex;: ^8 C% B$ _+ s: R7 ]8 w& e
  13. " X5 @9 @* O: q6 @  p
  14.     //查询是否在寄存器范围内
    4 N, s6 T6 m& v- M0 |/ n: X
  15.     //为了避免警告,修改为有符号整数- R+ w1 M7 t4 @( O0 ?' n( o7 n  T
  16.     if( ( (int16_t)usAddress >= REG_INPUT_START ) \0 T' S3 V! O' J+ j  ^( _" g
  17.             && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
      z2 y4 {. }5 B* E) r" {
  18.     {! z; Z) y8 P3 N# w/ o
  19.         //获得操作偏移量,本次操作起始地址-输入寄存器的初始地址
    + f4 S& K7 o' l- Q' d
  20.         iRegIndex = ( int16_t )( usAddress - REG_INPUT_START );
    ! `! A. J  K" @9 {
  21.         //逐个赋值
    / V# z3 [2 D. G5 C" L$ J
  22.         while( usNRegs > 0 )- E% O$ \3 b. @1 y/ M  L- E* j
  23.         {
      m9 h& G7 H% j& F/ W, C) c
  24.             //赋值高字节0 @2 F5 s$ \& w7 }9 R
  25.             *pucRegBuffer++ = ( uint8_t )( usRegInputBuf[iRegIndex] >> 8 );! x2 p( Z3 u7 j, Q6 c
  26.             //赋值低字节
    ( i$ Y0 o2 T' n, B
  27.             *pucRegBuffer++ = ( uint8_t )( usRegInputBuf[iRegIndex] & 0xFF );+ O! G0 o0 ~- A* T! ?
  28.             //偏移量增加) s5 f& i" r  Z( u
  29.             iRegIndex++;
    + |6 Y, z9 D# @4 u: f
  30.             //被操作寄存器数量递减1 t1 U; }8 [1 h$ v3 a
  31.             usNRegs--;7 l1 r6 x3 m: z
  32.         }
    - c/ A8 f: R9 L  y3 L
  33.     }- J1 L  J- [2 p6 a/ u& W
  34.     else; o7 {; J, B( O. G9 V5 F6 l+ G
  35.     {" y" K+ _6 @4 O9 c' }
  36.         //返回错误状态,无寄存器
    / S7 o% t9 O/ a! i; I( \& K& U8 k2 F
  37.         eStatus = MB_ENOREG;8 C# `! {1 t+ j5 N5 R
  38.     }* r1 _' n- ~) E3 {  l

  39. 4 J( M; B7 E$ F2 [% i( b
  40.     return eStatus;4 N1 [& j+ A; G0 b% a
  41. }
    " e8 j9 l4 n+ L9 U6 q
  42. & k1 {7 @$ \4 S$ U6 r* x
  43. /**: Z* x1 [4 J) R4 [+ O& H) z
  44. * @brief 保持寄存器处理函数,保持寄存器可读,可读可写$ }  ^' ^# R/ k2 h
  45. * @param pucRegBuffer 读操作时--返回数据指针,写操作时--输入数据指针
    % }: |" Z- ?6 |# D7 M: D
  46. * usAddress 寄存器起始地址0 R" F  N% Y. L" M6 q. J: O
  47. * usNRegs 寄存器长度
    6 l4 S9 Q# K- h  S/ k" R7 Z4 Z; M
  48. * eMode 操作方式,读或者写/ H7 x0 ?. s6 ^
  49. * @retval eStatus 寄存器状态
    : ~! ]: l& Z- g4 o$ C& e+ n, b
  50. */
    $ [; {( a' x  h) o; U0 P  g
  51. eMBErrorCode
    - e4 P" R& }/ z/ z& ~
  52. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,1 t# s4 X! A* b3 @: W: K. ?% R
  53.                  eMBRegisterMode eMode )
    4 X, I% ^% X" z+ H$ {% u4 \$ l
  54. {
    ! C5 `7 }. v  m. V5 a4 R4 g7 ^5 F
  55.     //错误状态
    8 @0 c0 A$ \9 p8 B2 u
  56.     eMBErrorCode eStatus = MB_ENOERR;" y, s' u" ]. B6 U# ~  }0 g- S' y
  57.     //偏移量
    3 k0 C" L& F* p* I8 P& I! z
  58.     int16_t iRegIndex;2 B4 g+ A3 R' j' A  a# E3 ]8 z
  59. & q% S' {4 X: |$ l5 {
  60.     //判断寄存器是不是在范围内" R, }) V" C9 N# m/ t
  61.     if( ( (int16_t)usAddress >= REG_HOLDING_START ) \7 @7 q; e# ^) k6 V( h& y
  62.             && ( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
    : U) c4 b( H8 `4 ^3 t& \3 z
  63.     {% B: ?9 [: c9 L% V: W
  64.         //计算偏移量
    , n3 ^& C4 |& ^
  65.         iRegIndex = ( int16_t )( usAddress - REG_HOLDING_START );
    % L3 c* q$ Y. B" W' v2 f

  66. % H3 C1 q# v& ?
  67.         switch ( eMode )- r4 _- h; g" w6 b5 o. I
  68.         {
    ) g& q% Y# ?1 V# a
  69.         //读处理函数4 K4 `- T6 a! w! S, p4 F. u
  70.         case MB_REG_READ:1 v/ @  C$ a0 X7 M$ l: ^
  71.             while( usNRegs > 0 ); z3 K; f9 E; {: A: P
  72.             {
    # O3 k; q2 L3 Q8 O+ _% P8 d
  73.                 *pucRegBuffer++ = ( uint8_t )( usRegHoldingBuf[iRegIndex] >> 8 );  O% X1 e$ b! x, }1 `
  74.                 *pucRegBuffer++ = ( uint8_t )( usRegHoldingBuf[iRegIndex] & 0xFF );  e9 @( K% g5 U
  75.                 iRegIndex++;
    3 @. H: y" E. a; k
  76.                 usNRegs--;
    & s! p) ^3 d4 h6 |) j. \" D
  77.             }5 f2 D& y( n) [. a
  78.             break;
    2 c3 B9 u7 k3 H! y  j  A
  79. 2 `) U! e$ j7 _' f% W( i1 W! }
  80.         //写处理函数' s8 t8 u1 k5 V- g$ n
  81.         case MB_REG_WRITE:
    " ]; R" i+ d" A; S' {
  82.             while( usNRegs > 0 )
    1 P/ U0 @& P0 p1 d1 N. X3 w5 P
  83.             {
    ' D  a; Y7 B5 T; ^
  84.                 usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;# y8 b2 ^1 }' r; s1 R) z
  85.                 usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
    * g. v& C% `, ]/ K
  86.                 iRegIndex++;- G+ J/ G# H$ j0 n' e
  87.                 usNRegs--;( O6 f4 x* z1 |, H
  88.             }
    5 k9 B+ }" q; i: k
  89.             break;
    5 y  H% C5 R! w0 y1 x$ f+ v
  90.         }
    $ }6 S$ ?9 x' c4 L1 g
  91.     }
    " G  }" s! x1 X: I
  92.     else
    # R) W, Q- i* A8 h7 [
  93.     {
    ' r9 _, _4 O( L7 m+ f6 \+ N
  94.         //返回错误状态
    / p% J: R( k" {! f6 h, O
  95.         eStatus = MB_ENOREG;
    * K) o7 U( H% Z5 A% a% W
  96.     }
    * H* m: c0 h3 a6 e8 Q9 E
  97. + V4 t- N$ j8 r9 h: I% B
  98.     return eStatus;; C2 L) `  u8 i0 q
  99. }
    5 }3 \& W! T/ P' }$ L7 X: j
  100. 4 S' E! a9 `9 e3 N; g4 j. t; _6 d1 ^

  101. / d. L+ D  E9 M  p$ {4 z6 h, h0 F( P/ M

  102. ( F6 g! q# G/ |& j
  103. & j) h0 e/ `' {& t' m6 p
  104. /**
    - F2 r) E( s# `" N6 ^5 f) W
  105. * @brief 线圈寄存器处理函数,线圈寄存器可读,可读可写
    9 h3 Z+ P8 J* _  T. @; v) l) T
  106. * @param pucRegBuffer 读操作---返回数据指针,写操作--返回数据指针- _  ]! H$ y) I6 j! q& e* |5 B* ?
  107. * usAddress 寄存器起始地址
    # k# O, `. t; z0 T
  108. * usNRegs 寄存器长度
    & i$ O' v5 F' c
  109. * eMode 操作方式,读或者写: M5 f" q8 ?* c8 Y
  110. * @retval eStatus 寄存器状态
    4 w7 k" u1 W# M9 ?% G+ {
  111. */
    ! [3 k4 M& b8 K7 g  j# h
  112. eMBErrorCode$ h7 L6 Y6 F. n9 S
  113. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,8 f& f, v# L% J& Z- \* s
  114.                eMBRegisterMode eMode )+ N9 h1 _' C0 D0 `% V8 L9 U; Q
  115. {+ W$ i+ S; W! D, u2 m' R! g
  116.     //错误状态
    % E* R, h2 q' Y" V/ m
  117.     eMBErrorCode eStatus = MB_ENOERR;
    1 i' T/ Y: e& K3 q! z& U2 V! |7 F
  118.     //寄存器个数
      f, }. F. i! W$ r3 k- Z; }3 u0 ~/ g+ `0 r8 Q
  119.     int16_t iNCoils = ( int16_t )usNCoils;
    2 B& c8 o# O5 G( u* F2 L. O/ s
  120.     //寄存器偏移量
    + D+ G0 D- c2 V9 z
  121.     int16_t usBitOffset;
    7 N/ e3 X; v" P; Y0 \8 i3 g( z8 G
  122. % M+ S$ U8 @  N2 Z8 a
  123.     //检查寄存器是否在指定范围内/ d: q$ b, @$ G/ {' [
  124.     if( ( (int16_t)usAddress >= REG_COILS_START ) &&, I" H2 U/ ~/ O3 |
  125.             ( usAddress + usNCoils <= REG_COILS_START + REG_COILS_SIZE ) )
    3 |; {$ f4 O2 s: ^0 j
  126.     {
    0 Z; D5 K. Q! U) F& J9 x4 v& P8 z
  127.         //计算寄存器偏移量( q- O# \1 q7 i; M1 l# H$ w% }8 W) r
  128.         usBitOffset = ( int16_t )( usAddress - REG_COILS_START );+ C- r& S( i; H3 q3 D
  129.         switch ( eMode )( A  X: ]& j# v: H: j" S2 }# z
  130.         {1 `( c4 a% _% Y3 i" R: a5 R( m
  131.         //读操作! E7 \9 r; V) N/ H2 X7 l
  132.         case MB_REG_READ:( a! G6 H% m0 g  B6 b+ t
  133.             while( iNCoils > 0 )$ J6 f( W1 @) P; o0 M
  134.             {# h8 R( ^3 P9 X! Q. T
  135.                 *pucRegBuffer++ = xMBUtilGetBits( ucRegCoilsBuf, usBitOffset,% b! b. @% c) {; f$ M" N; e& u$ A: T! l
  136.                                                   ( uint8_t )( iNCoils > 8 ? 8 : iNCoils ) );
      O  O; `5 P% g
  137.                 iNCoils -= 8;
    ' O1 q. x) h* `. C1 g/ e& a
  138.                 usBitOffset += 8;
    - @0 l* c. w' W
  139.             }$ j; t& f  ]& L! }9 g6 T5 ]8 G' ]3 k
  140.             break;5 x7 t/ P4 x+ O4 o: ]
  141. % f5 s7 |7 e$ P& R/ z
  142.         //写操作
    # L  V' \4 i1 j
  143.         case MB_REG_WRITE:
    : P; s- v; ~. h9 U+ r' x
  144.             while( iNCoils > 0 ); b" c. M5 Z+ I. C, v% j- U
  145.             {
    " r& k- V9 x: T+ o* T; Z
  146.                 xMBUtilSetBits( ucRegCoilsBuf, usBitOffset,
    1 U% H/ w/ f. |6 V
  147.                                 ( uint8_t )( iNCoils > 8 ? 8 : iNCoils ),
    ' t1 b1 |) V. M7 K+ F& c. Y
  148.                                 *pucRegBuffer++ );
    - G/ K/ n# b4 E! ^- y6 s
  149.                 iNCoils -= 8;
    6 k9 B, Y1 e7 U/ x' ]) E
  150.                 usBitOffset += 8;
    6 S* c: c7 _" o8 i- i1 R1 W
  151.             }
    / ~+ e* }- A- \* P5 O& P) g- k0 {% w& u
  152.             break;
    2 W3 q1 O% h1 z8 a/ v
  153.         }
    * \8 ?/ r! w4 o' X6 R5 y
  154. 3 h, m3 y' Z0 x7 \1 l
  155.     }
      I% K+ P  s( c7 B5 |0 w
  156.     else$ B, j' t" e6 R0 P; j3 m
  157.     {, T8 Q& F; b; o/ V
  158.         eStatus = MB_ENOREG;1 C. ^3 a) a) m4 o7 d5 l* m
  159.     }( y8 R2 s7 P, W; ?
  160.     return eStatus;  j+ x2 @. U/ ^
  161. }" p6 B$ P- B" _5 r* |! [- J7 S2 U! r) r. r
  162. / g) q& @; ]. C0 r

  163. # O2 Z6 [- `+ L# ~# b/ b6 |! w
  164. + B3 ~6 s# [2 m' I: \
  165. / H5 l! o& _4 p; F: m
  166. /**  V  y: I! {6 {: m* N
  167. * @brief 开关输入寄存器处理函数,开关输入寄存器,可读. A# I) b; _4 R2 \- K3 W
  168. * @param pucRegBuffer 读操作---返回数据指针,写操作--返回数据指针
    ! K# j( p& u5 t3 r
  169. * usAddress 寄存器起始地址
    2 k8 g* L. |) {% u
  170. * usNRegs 寄存器长度
    0 m$ g3 K, A; Z5 w0 z5 g
  171. * eMode 操作方式,读或者写/ p# o; a  y$ @2 V  [# g
  172. * @retval eStatus 寄存器状态
    ( C( i$ B, F& n. B# \8 d0 ^! I% S
  173. */
    , ]% L- W) M/ S0 b$ [9 ~% ]
  174. eMBErrorCode- g8 v* W3 T) z- `. q$ I/ N- C5 K/ |7 H% S
  175. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
    ; X5 {8 w! M; j4 T
  176. {8 m4 G9 r- ~0 n. U8 i' b8 H
  177.     //错误状态
    + X6 `7 b' S, @2 e0 b  A8 r# j; F
  178.     eMBErrorCode eStatus = MB_ENOERR;
    , D- N/ b$ r. ?
  179.     //操作寄存器个数2 K& ~0 L. a/ Q7 t+ V
  180.     int16_t iNDiscrete = ( int16_t )usNDiscrete;
    ' ^8 u4 o3 k9 P/ G1 C6 d" m" ?
  181.     //偏移量# m; k- j- q) |! P9 [6 a
  182.     uint16_t usBitOffset;% X/ v. R; T( O

  183. ; z5 V, \, y$ |! U4 x
  184.     //判断寄存器时候再指定范围内  @! B4 Y9 |% E' o
  185.     if( ( (int16_t)usAddress >= REG_DISCRETE_START ) &&
    1 i/ {3 Y$ i8 k$ ]8 J8 T3 `: z
  186.             ( usAddress + usNDiscrete <= REG_DISCRETE_START + REG_DISCRETE_SIZE ) )
    + a3 n& B8 B  \; [" v9 F: [
  187.     {
    ( X' j/ w' T1 w, E/ E+ N- ~7 c+ f
  188.         //获得偏移量& H1 P3 M1 d7 j1 U- b- G: T
  189.         usBitOffset = ( uint16_t )( usAddress - REG_DISCRETE_START );8 }; J* w% R8 b: J( l
  190. 0 |9 c0 ]* X1 s9 ?* P; g
  191.         while( iNDiscrete > 0 )  C7 i/ V$ B6 f# [2 R8 c* [, ^
  192.         {
    # _% H; c9 i  w- j, U
  193.             *pucRegBuffer++ = xMBUtilGetBits( ucRegDiscreteBuf, usBitOffset,
    * X, W% L1 g: A
  194.                                               ( uint8_t)( iNDiscrete > 8 ? 8 : iNDiscrete ) );
    $ k) D# A" s/ s2 F/ ~
  195.             iNDiscrete -= 8;* [5 e( A. U* P3 u' g5 N* i" _) n
  196.             usBitOffset += 8;1 ^9 ]' M0 l5 k3 X# g4 J; W
  197.         }9 I1 ]4 {% l/ i  T$ m& a" H. l/ p

  198. : j' _! |$ M* I# K
  199.     }
    * r' ^% ^$ f& r
  200.     else
    ! _- T0 c9 ^2 s7 l9 L& k1 j
  201.     {
    ! o* l) }: X5 T$ N( Z# S; s6 K7 H
  202.         eStatus = MB_ENOREG;" q. [1 |' t/ P, _
  203.     }) q- O& x3 @5 T% u- j, r  q7 C
  204.     return eStatus;$ R8 a4 [/ C- C8 I8 }/ u: ~
  205. }
    + Q1 ?6 Z2 y& g# I2 w+ `) N( W
  206. ' E* i4 t- L! I
  207. 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
  1. /*' q- U+ l) A- r1 S4 g  p4 E
  2. * FreeModbus Libary: BARE Port
    $ i; n2 |5 X& H% M) W  W) j/ o
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
    ( i; M; q7 _' o8 _' f
  4. *" w2 D# [! B! C2 @
  5. * This library is free software; you can redistribute it and/or! t' h. g4 K  o& d8 K" g/ r4 i
  6. * modify it under the terms of the GNU Lesser General Public; H1 D; @& i9 X7 s+ r9 s- r9 s
  7. * License as published by the Free Software Foundation; either
      [- v# c$ R0 W: j% a& v4 |
  8. * version 2.1 of the License, or (at your option) any later version., {% I5 T: O7 L  [$ _
  9. *
    % `" k: n( H8 O" C+ Y: z
  10. * This library is distributed in the hope that it will be useful,7 ]6 @  j+ {# I3 e( [0 _
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2 T* H6 g2 i2 C! {0 J
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    " A' _$ @/ j0 `- ^+ |
  13. * Lesser General Public License for more details.
    % w( A; e9 \4 j
  14. *+ Z) j" |2 q2 z  C6 I8 l
  15. * You should have received a copy of the GNU Lesser General Public2 n7 m& X( p2 ^' c; {' g
  16. * 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
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA1 h& U7 ?& D5 _. s) c$ z+ S9 B
  18. *
    0 B; Q: u1 K8 b% I
  19. * File: $Id$; Q) N0 h1 p1 f& w% V# e$ Z3 F! O# A
  20. */! K) a% _: C, p0 N
  21. 8 p1 h! S: C) S  z
  22. #include "port.h"
      R9 }0 e* Z% {. }5 T

  23. 1 T! y# u4 y- @# M6 |
  24. /* ----------------------- Modbus includes ----------------------------------*/
    / g8 ]) g1 M' ^  v! `) T
  25. #include "mb.h"
    0 r  a9 I) \. ]; J8 _! {
  26. #include "mbport.h"
    - H" M% V+ |7 {5 ^: C* L% M

  27. " F' Z- }+ }0 `1 E6 I% u
  28. #include "rs485.h") }  l" K8 R# y$ L# }* ]: i
  29. % p# q* J* `5 i; P' W
  30. /* ----------------------- static functions ---------------------------------*/& F2 a+ \) I1 ~& d8 @) [' I
  31. static void prvvUARTTxReadyISR( void );, D0 I1 H3 }3 c
  32. static void prvvUARTRxISR( void );" L& K' t2 o0 j( C8 f

  33. # l5 o" N4 L1 r7 W# ^2 i( n
  34. /* ----------------------- Start implementation -----------------------------*/* P, E, g9 c& b8 ^: k5 O( _
  35. void
    5 {" u- d6 |. F! ]: g$ Y
  36. vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )( a6 p4 j6 p+ X5 j4 l9 b
  37. {
    ' T/ X, [. a8 e
  38.     /* If xRXEnable enable serial receive interrupts. If xTxENable enable
    0 _% u" ~) ~& C
  39.      * transmitter empty interrupts.
    ( n2 g# ^# h. n, {' u
  40.      */: p: o  v, B7 z3 b
  41.     if(xRxEnable)
    / }9 u) D! b, ]
  42.     {
    : p0 O* l, W$ u) v
  43.         //使能接收和接收中断
    , m6 H, p' M; m: D/ {( P( ], m
  44.         USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);3 G3 X+ }2 L: m- T
  45.         //MAX485操作 低电平为接收模式/ n8 V# b* Q' H* H0 U& Q& u1 }
  46.         //GPIO_ResetBits(GPIOD,GPIO_Pin_8);
    & \  N: J! C0 f8 h8 c
  47.         RS485_TX_EN=0;
    ! ^7 w: T2 ]9 [. y* `
  48.     }/ X6 b& s8 X7 }" k3 \/ ~" a
  49.     else
    ( x8 j% m/ h2 c. Q! I
  50.     {" R/ g( |1 `& [% E4 Y, m
  51.         USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);% J# l% t% d% j* D' v
  52.         //MAX485操作 高电平为发送模式2 q$ w) s5 s; v6 l  P! v% s' S
  53.         //GPIO_SetBits(GPIOD,GPIO_Pin_8);7 n2 e% N& E. p/ v; {+ V
  54.         RS485_TX_EN=1;% a, z3 l9 ^% m6 L' ~2 H' ?
  55.     }
    $ I' r# E5 C1 q8 U% R- i
  56. 5 p5 V1 a) ^! k5 C( f
  57.     if(xTxEnable)7 ]1 @% `7 {7 z" J" B; M* r" r1 Y
  58.     {
    1 }4 x) R5 L6 F) d
  59.         //使能发送完成中断! Q# H2 H6 _5 G( b+ I4 @
  60.         USART_ITConfig(USART2, USART_IT_TC, ENABLE);
    & T# a1 M4 G* T& l3 p& m
  61.     }
    9 I5 P( I0 ^' V+ H' y+ [5 {
  62.     else# D- x) d; u$ w7 z' t9 P! Q
  63.     {# H; U$ A4 p0 d
  64.         //禁止发送完成中断
    # K' h* j# }- U$ R9 H9 Y) X2 q
  65.         USART_ITConfig(USART2, USART_IT_TC, DISABLE);1 |. `9 l! S3 G2 C
  66.     }- L9 m$ p! i& ^7 Q4 Z

  67. / y; M9 o1 z* ]$ d9 k

  68. , Y; l1 i3 y0 k" d1 }3 F3 R3 E/ C
  69. }
    5 y7 g1 k7 n4 C! F$ n; w

  70. % ~/ q( C! U) y7 N5 o/ M$ t# `
  71. BOOL: y6 C' e8 X$ d! m) g  l8 y
  72. xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
    - w7 z3 Z- T  {' A' A( \) T0 |; a/ r
  73. {
    7 v0 j7 n# @0 O( z: A' c8 u
  74.     RS485_Init(9600);        //初始化RS485
    6 L$ z- G  l: ?9 o9 L* s3 \* Z3 `
  75.     return TRUE;6 z* O+ v1 q8 ^  V2 I3 \
  76.     //return FALSE;
    $ S3 U# D8 |" Y- t7 \5 K0 x
  77. }
    4 f: R. ?- G  U" E9 G! ?2 e
  78. 8 ^' i, D% e1 d+ x: L' J' q9 d
  79. BOOL
    4 }& r) R6 o4 `- f3 K
  80. xMBPortSerialPutByte( CHAR ucByte )
    9 l: p( s8 q- j
  81. {
    ! N! V" O! v) ]; K+ K
  82.     /* Put a byte in the UARTs transmit buffer. This function is called+ [, G/ p% y2 c, u
  83.      * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
    0 F, s! J, ~/ J
  84.      * called. */( I. Y# |' ~) Z5 V' e! o8 h( d* _
  85.     //发送数据
    " a& ?7 k& m1 K+ N& a! L* G
  86.     USART_SendData(USART2, ucByte);
    ' ?& n. f( S$ r# x4 ?/ n
  87.     return TRUE;
    . @+ g$ Z( I: ?5 I
  88. }) m0 i# {' e1 Z" b

  89. 0 F$ R, Z3 m, w: ?
  90. # O: g1 t: Q. @) j( s! j; ^3 [
  91. BOOL! H6 |* s; l2 A) A+ ]$ W  B; D6 U
  92. xMBPortSerialGetByte( CHAR * pucByte )
    0 E7 ^/ ~% Y5 a2 M9 @. u# r2 [
  93. {
    : I3 e1 u7 A4 d
  94.     /* Return the byte in the UARTs receive buffer. This function is called
    . }" k# i9 h  `
  95.      * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.. {: ]9 n' `6 ]- r, j4 E
  96.      */& r; v) E8 ?3 a& V/ D& H
  97.     //接收数据
    # U# g- R$ q4 m; c) @" w
  98.     *pucByte = USART_ReceiveData(USART2);
    " x; y: M2 l% Z. i% U# y
  99.     return TRUE;
    4 Q8 ?9 K7 B) M- t
  100. - U8 O% a# A& Z. Z
  101. }( D  r! k% z$ N& K8 P% G; A
  102. , \5 c% g" R* A+ i  N, S* s. h
  103. /* Create an interrupt handler for the transmit buffer empty interrupt7 ?4 j+ Z8 n5 t
  104. * (or an equivalent) for your target processor. This function should then
    6 H) g0 _0 D& h
  105. * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
    / L9 U' U! X: l" O
  106. * a new character can be sent. The protocol stack will then call, _, o( e5 ?4 ^7 t* _  U
  107. * xMBPortSerialPutByte( ) to send the character.
    " o+ Y! ~. R. C% L) R) g, a; u
  108. */
    4 L/ y; M. v, `" K( |
  109. static void prvvUARTTxReadyISR( void )# _5 M/ t8 _0 @
  110. {
    9 p9 O* ^( }4 M* B. ]( Y
  111.     pxMBFrameCBTransmitterEmpty(  );. a) l( \; l0 J' v5 `) A
  112. }
    1 Y6 l4 u' R9 G  n6 n5 e$ Y- E2 I

  113. 6 [& k2 M$ }6 P8 ]' ^9 \
  114. /* Create an interrupt handler for the receive interrupt for your target3 `$ p* o0 ?+ ~- i' S
  115. * processor. This function should then call pxMBFrameCBByteReceived( ). The9 o- W9 }! p0 |: y! S1 X- x
  116. * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the$ B9 ~+ d9 Y% I$ }
  117. * character.
    " O3 E# {% Z4 I; _) f9 u
  118. */
    4 A( K" `" }! l% _
  119. static void prvvUARTRxISR( void )6 d+ |' v: x" M& ~/ W% e9 {; S
  120. {: {; e  V- w" \! p1 {
  121.     pxMBFrameCBByteReceived(  );3 `3 o5 _% e4 d; }
  122. }
    - F6 N- h  P7 {! b# d
  123. : ~& i& G& @2 L' C0 q
  124. void USART2_IRQHandler(void); ?" l) e8 d  U
  125. {1 M% d# }" }4 Z3 v, ]4 q% |/ z- Z0 U  q7 Q
  126.     if(USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
    9 ^, ^: \# m& T; {* K3 w
  127.     {
    0 m2 I% ~# H; _' ~$ b( e; P
  128.         prvvUARTRxISR();9 R3 a* \8 d2 [' q  r* c0 K
  129.         //清除中断标志位
    ) b- g6 U. W. Y: i" [1 p, ~
  130.         USART_ClearITPendingBit(USART2, USART_IT_RXNE);) q) A) P& i$ Q* P& G& R
  131.     }
    5 M' M& J. s# z' A+ T: @1 w  E4 U
  132. 1 E: Y) G! K8 B% B8 t9 W& W
  133.     //发生完成中断
    6 O4 y+ T, d' ~; c! A) X. z
  134.     if(USART_GetITStatus(USART2, USART_IT_TC) == SET)
    % H. @8 F- R" Q1 a& H8 k! q# V
  135.     {) J* b0 h2 I" l+ O: i- ~! b
  136.         prvvUARTTxReadyISR();
    4 @' h9 M8 @, N% H- c- x" T% v
  137.         //清除中断标志2 F& L  v8 P2 c2 S( x( O5 ]2 I5 S
  138.         USART_ClearITPendingBit(USART2, USART_IT_TC);
    4 ]5 m; g- x1 L- n3 D
  139.     }
    " G: {( H: T) G, x- N  D5 ^
  140. 0 ~, v9 M% Q) a, X5 e9 K  P/ h" @
  141. }, u  X8 I" g, O

  142.   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
赞 收藏 评论0 发布时间:2023-3-14 16:54

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版