[STM8] stm8 io位操作

[复制链接]
 楼主| gpjun2008 发表于 2013-10-29 15:39 | 显示全部楼层 |阅读模式
#define KEY2 PE3        比如这个语句可以吗,或者PE3应该怎么取。我这么直接预定义的话,调用KET2时候编译会报错。
grant_jx 发表于 2013-10-29 16:34 | 显示全部楼层
看你的编译器定义,与STM8S无关。

STM8S的汇编本身是有位指令操作的,你可以自定义,也可以用现成的。
在STM8S的库下面有个文件叫STM8S.H,里面已经帮你写好了。你可以直接调用
  1. /*============================== Handling bits ====================================*/
  2. /*-----------------------------------------------------------------------------
  3. Method : I
  4. Description : Handle the bit from the character variables.
  5. Comments :    The different parameters of commands are
  6.               - VAR : Name of the character variable where the bit is located.
  7.               - Place : Bit position in the variable (7 6 5 4 3 2 1 0)
  8.               - Value : Can be 0 (reset bit) or not 0 (set bit)
  9.               The "MskBit" command allows to select some bits in a source
  10.               variables and copy it in a destination var (return the value).
  11.               The "ValBit" command returns the value of a bit in a char
  12.               variable: the bit is reseted if it returns 0 else the bit is set.
  13.               This method generates not an optimised code yet.
  14. -----------------------------------------------------------------------------*/
  15. #define SetBit(VAR,Place)         ( (VAR) |= (u8)((u8)1<<(u8)(Place)) )
  16. #define ClrBit(VAR,Place)         ( (VAR) &= (u8)((u8)((u8)1<<(u8)(Place))^(u8)255) )

  17. #define ChgBit(VAR,Place)         ( (VAR) ^= (u8)((u8)1<<(u8)(Place)) )
  18. #define AffBit(VAR,Place,Value)   ((Value) ? \
  19.                                    ((VAR) |= ((u8)1<<(Place))) : \
  20.                                    ((VAR) &= (((u8)1<<(Place))^(u8)255)))
  21. #define MskBit(Dest,Msk,Src)      ( (Dest) = ((Msk) & (Src)) | ((~(Msk)) & (Dest)) )

  22. #define ValBit(VAR,Place)         ((u8)(VAR) & (u8)((u8)1<<(u8)(Place)))

  23. #define BYTE_0(n)                 ((u8)((n) & (u8)0xFF))        /*!< Returns the low byte of the 32-bit value */
  24. #define BYTE_1(n)                 ((u8)(BYTE_0((n) >> (u8)8)))  /*!< Returns the second byte of the 32-bit value */
  25. #define BYTE_2(n)                 ((u8)(BYTE_0((n) >> (u8)16))) /*!< Returns the third byte of the 32-bit value */
  26. #define BYTE_3(n)                 ((u8)(BYTE_0((n) >> (u8)24))) /*!< Returns the high byte of the 32-bit value */

  27. /*============================== Assert Macros ====================================*/
  28. #define IS_STATE_VALUE_OK(SensitivityValue) \
  29.   (((SensitivityValue) == ENABLE) || \
  30.    ((SensitivityValue) == DISABLE))

  31. /*-----------------------------------------------------------------------------
  32. Method : II
  33. Description : Handle directly the bit.
  34. Comments :    The idea is to handle directly with the bit name. For that, it is
  35.               necessary to have RAM area descriptions (example: HW register...)
  36.               and the following command line for each area.
  37.               This method generates the most optimized code.
  38. -----------------------------------------------------------------------------*/

  39. #define AREA 0x00     /* The area of bits begins at address 0x10. */

  40. #define BitClr(BIT)  ( *((unsigned char *) (AREA+(BIT)/8)) &= (~(1<<(7-(BIT)%8))) )
  41. #define BitSet(BIT)  ( *((unsigned char *) (AREA+(BIT)/8)) |= (1<<(7-(BIT)%8)) )
  42. #define BitVal(BIT)  ( *((unsigned char *) (AREA+(BIT)/8)) & (1<<(7-(BIT)%8)) )

  43. /* Exported functions ------------------------------------------------------- */

  44. #endif /* __STM8S_H */
复制代码
lee2k 发表于 2013-10-29 22:14 | 显示全部楼层
不行的,STM8的GPIO和其他的MCU有点不同,
GPIO的状态主要分输入和输出,由DDR寄存器控制,输出时由ODR寄存器控制输出状态,
输入时从IDR获得IO口状态,不能反过来。
还有具体的就得看文档了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

38

主题

267

帖子

3

粉丝
快速回复 在线客服 返回列表 返回顶部