Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7464
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2015-07-02 17:21
文章分类

全部博文(1)

文章存档

2016年(1)

我的朋友
最近访客

分类: C/C++

2016-04-21 09:30:12

    以下代码是使用GPIO模拟IIC通讯的代码,时序由MCU产生,在CC2541上运行通过。

点击(此处)折叠或打开

  1. /*
  2.  * file iic.h
  3.  *
  4.  */
  5. #include "hal_types.h"

  6. #ifndef __IIC_H__
  7. #define __IIC_H__


  8. #if 1 /* exchange SDA SCL */

  9. #define SCL_SET() (I2CIO |= (1<<1))
  10. #define SDA_SET() (I2CIO |= (1<<0))
  11. #define SCL_CLR() (I2CIO &= ~(1<<1))
  12. #define SDA_CLR() (I2CIO &= ~(1<<0))

  13. #define SET_SDA_IN() (I2CWC &= ~(1<<0))
  14. #define SET_SDA_OUT() (I2CWC |= 1)
  15. #define GET_SDA() (I2CIO & 0x1)

  16. #else

  17. #define SCL_SET() (I2CIO |= (1<<0))
  18. #define SDA_SET() (I2CIO |= (1<<1))
  19. #define SCL_CLR() (I2CIO &= ~(1<<0))
  20. #define SDA_CLR() (I2CIO &= ~(1<<1))

  21. #define SET_SDA_IN() (I2CWC &= ~(1<<1))
  22. #define SET_SDA_OUT() (I2CWC |= 1<<1)
  23. #define GET_SDA() ((I2CIO>>1) & 0x1)

  24. #endif


  25. #define DEEP_OPTIMIZATION (0)

  26. extern void iic_init(void);
  27. extern void iic_release(void);
  28. extern void iic_start(void);
  29. extern void iic_stop(void);
  30. extern uint8 iic_send_byte(uint8 data);
  31. extern uint8 iic_recv_byte_with_ack(void);
  32. extern uint8 iic_recv_byte_with_nak(void);

  33. #endif

点击(此处)折叠或打开

  1. /*
  2.  * file iic.c
  3.  *
  4.  */
  5. #include "ioCC2541.h"
  6. #include "hal_mcu.h"
  7. #include "common_iic.h"
  8.        
  9. #define iic_nop() {\
  10.                         asm("NOP");\
  11.                     }
  12.    
  13. /*
  14.  * Prototype : void iic_init(void)
  15.  * Description : IIC init
  16.  */
  17. void iic_init(void)
  18. {
  19.   I2CCFG &= ~(1 << 6);
  20.   
  21.   SCL_SET();
  22.   SDA_SET();
  23.   
  24.   I2CWC |= (1 << 7 | 0x3<<0);
  25. }

  26. /* release all iic pin to gpio */
  27. void iic_release(void)
  28. {
  29.   SCL_CLR(); SET_SDA_IN();
  30.         
  31.   /* SWITCH TO IIC , AND DISABLE THE SDA ,SCL OUTPUT */
  32.   I2CWC &= ~(0x1<<7);I2CWC |= (0x3<<0);
  33. }


  34. /*
  35.  * Prototype : void iic_start(void)
  36.  * Description : IIC init
  37.  */
  38. void inline iic_start(void)
  39. {
  40. #if DEEP_OPTIMIZATION == 1
  41.     halIntState_t x;
  42.     
  43.     HAL_ENTER_CRITICAL_SECTION(x);
  44. #endif
  45.     
  46.     SET_SDA_OUT();
  47.     SDA_SET();
  48.     iic_nop();
  49.     SCL_SET();
  50.     iic_nop();
  51.     SDA_CLR();
  52.     iic_nop();
  53.     SCL_CLR();
  54.  
  55. #if DEEP_OPTIMIZATION == 1
  56.     HAL_EXIT_CRITICAL_SECTION(x);
  57. #endif
  58. }

  59. /*
  60.  * Prototype :void iic_stop(void)
  61.  * Description : IIC init
  62.  */
  63. void inline iic_stop(void)
  64. {
  65. #if DEEP_OPTIMIZATION == 1
  66.     halIntState_t x;
  67.     
  68.     HAL_ENTER_CRITICAL_SECTION(x);
  69. #endif
  70.     
  71.     SET_SDA_OUT();
  72.     SDA_CLR();
  73.     SCL_SET();
  74.     iic_nop();
  75.     SDA_SET();
  76.     iic_nop();
  77.     
  78. #if DEEP_OPTIMIZATION == 1
  79.     HAL_EXIT_CRITICAL_SECTION(x);
  80. #endif
  81. }

  82. /* Prototype : uchar iic_send_byte(uchar sendDAT)
  83.  *(Calls :
  84.  *Parameters : uchar sendDAT---data to be send
  85.  *Return Value : CY--slave ack (1---noack,0---ack)
  86.  *Description : Send one byte to I2C
  87.  */
  88. uint8 iic_send_byte(uint8 data)
  89. {
  90.     uint8 i,revack = 1;

  91. #if DEEP_OPTIMIZATION == 1
  92.     halIntState_t x;
  93.     
  94.     HAL_ENTER_CRITICAL_SECTION(x);
  95. #endif
  96.     
  97.     SCL_CLR();
  98.     SET_SDA_OUT();
  99.     
  100.     for ( i = 0 ; i < 8; i++){
  101.          SCL_CLR();
  102.          iic_nop();

  103.          if (data & 0x80){ // write data
  104.                SDA_SET();
  105.          }else {
  106.                SDA_CLR();
  107.          }

  108.        data <<= 1;
  109.        SCL_SET();
  110.        iic_nop();
  111.     }
  112.  
  113.     SCL_CLR();
  114.     SDA_SET(); // release bus
  115.     iic_nop();
  116.     SET_SDA_IN();
  117.     SCL_SET();
  118.     revack = GET_SDA();
  119.         
  120.     SET_SDA_OUT();
  121.     SCL_CLR();
  122.     
  123. #if DEEP_OPTIMIZATION == 1
  124.     HAL_EXIT_CRITICAL_SECTION(x);
  125. #endif
  126.     return revack;
  127. }

  128. /* Prototype : uint8 iic_recv_byte(void)
  129.  * Description : recv one byte from I2C
  130.  */
  131. uint8 iic_recv_byte_with_ack(void)
  132. {
  133.     uint8 value, i;
  134.     
  135. #if DEEP_OPTIMIZATION == 1
  136.     halIntState_t x;
  137.     
  138.     HAL_ENTER_CRITICAL_SECTION(x);
  139. #endif
  140.     
  141.     value = 0;
  142.     
  143.     SCL_CLR();
  144.     SET_SDA_IN();
  145.     for(i = 0; i < 8; i++){
  146.       
  147.         value <<= 1;
  148.         SCL_SET();
  149.         iic_nop();
  150.         
  151.         if(GET_SDA())
  152.             value |= 1;
  153.          
  154.         SCL_CLR();
  155.         iic_nop();
  156.         
  157.     }
  158.     
  159.     // send a ACK
  160.     SET_SDA_OUT();
  161.     SDA_CLR();
  162.     iic_nop();
  163.     SCL_SET();
  164.     iic_nop();
  165.     iic_nop();
  166.     iic_nop();
  167.     iic_nop();
  168.     SCL_CLR();
  169.     iic_nop();
  170.     SDA_SET();
  171.     iic_nop();

  172. #if DEEP_OPTIMIZATION == 1
  173.     HAL_EXIT_CRITICAL_SECTION(x);
  174. #endif
  175.     
  176.     return value;
  177. }

  178. /* Prototype : uint8 iic_recv_byte_with_nak(void)
  179.  * Description : recv one byte from I2C
  180.  *
  181.  * NOTE : IIC Master must send a 'NAK' to slave when last byte data was read,otherwise the
  182.  * slave do not recognize the termination condition.
  183.  */
  184. uint8 iic_recv_byte_with_nak(void)
  185. {
  186.     uint8 value, i;
  187.     
  188. #if DEEP_OPTIMIZATION == 1
  189.     halIntState_t x;

  190.     HAL_ENTER_CRITICAL_SECTION(x);
  191. #endif
  192.     
  193.     value = 0;
  194.     
  195.     SCL_CLR();
  196.     SET_SDA_IN();
  197.     for ( i = 0; i < 8; i++){
  198.       
  199.         value <<= 1;
  200.         SCL_SET();
  201.         iic_nop();
  202.         
  203.         if(GET_SDA())
  204.             value |= 1;
  205.          
  206.         SCL_CLR();
  207.         iic_nop();
  208.     }
  209.     
  210.     // send a NAK
  211.     SET_SDA_OUT();
  212.     SDA_SET();
  213.     iic_nop();
  214.     SCL_SET();
  215.     iic_nop();
  216.     iic_nop();
  217.     iic_nop();
  218.     iic_nop();
  219.     SCL_CLR();
  220.     iic_nop();
  221. #if DEEP_OPTIMIZATION == 1
  222.     HAL_EXIT_CRITICAL_SECTION(x);
  223. #endif
  224.     
  225.     return value;
  226. }



阅读(1763) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~