首页 > 其他 > 详细

《我的嵌入式开发》---- IIC 通信

时间:2019-02-12 11:43:00      阅读:155      评论:0      收藏:0      [点我收藏+]

IIC 通用文件,文件是在NRF51xx 芯片基础,keil 平台开发测试通过,后期可以修改。

//Header files ...
#include "nrf_gpio.h"

typedef unsigned              char uint8;
typedef unsigned              int uint32;
//typedef   signed             char uint8;

#define I2C_FALSE            0     //returns 
#define I2C_TRUE             1     

#define I2C_DELAY_TM         1     //slot time (1us)

#define SDA                 15     //data
#define SCL                 16     //clock

// Output 0/1
#define SDA_OUT_0                            nrf_gpio_pin_write(SDA,0) 
#define SDA_OUT_1                            nrf_gpio_pin_write(SDA,1) 
#define SCL_OUT_0                            nrf_gpio_pin_write(SCL,0) 
#define SCL_OUT_1                            nrf_gpio_pin_write(SCL,1) 

//    Choose output/input 
#define SET_SDA_IN                        ( I2C_SDA_Dir (1) ) 
#define SET_SDA_OUT                       ( I2C_SDA_Dir (0) )
#define SET_SCL_IN                        ( I2C_SCL_Dir (1) )
#define SET_SCL_OUT                       ( I2C_SCL_Dir (0) )

// Reads lines
#define READ_SDA_IN                          nrf_gpio_pin_read(SDA)  
#define READ_SCL_IN                          nrf_gpio_pin_read(SCL) 

// 
#define NOP()                                 __nop()
#define I2C_Delay(x)                          nrf_delay_us(x) 

// Function declaration
void Delay1us(void);
void Delay5us(void);
void Delay10us(void);
void nrf_delay_ms(uint32 volatile number_of_ms);
void nrf_delay_us(uint32 volatile number_of_us);
void I2c_Stop(void);
void I2c_NoAck(void);
void I2c_Reset (void);
uint8 I2c_Start(void);
uint8 I2C_RcvByte(void);
void I2C_SendByte(uint8 c);
void I2C_SDA_Dir (uint8 dir);
void I2C_SCL_Dir (uint8 dir);


/**@brief Delay 5us
 */
void Delay1us(void)
{
    NOP();
        NOP();
        NOP();
        NOP();
}

/**@brief Delay 5us
 */
void Delay5us(void)
{
    Delay1us();
    Delay1us();
    Delay1us();
    Delay1us();
    Delay1us();
}

/**@brief Delay 10us
 */
void Delay10us(void)
{
    Delay5us();
    Delay5us();
}

/**@brief Delay(ms)  
 *
 * @param[in] number_of_ms 
 */
void nrf_delay_ms(uint32 volatile number_of_ms)
{
    number_of_ms = number_of_ms * 1000;
    while(number_of_ms != 0){
        number_of_ms--;
        Delay1us();
    }
}

/**@brief Delay(us)  
 *
 * @param[in] number_of_us 
 */
void nrf_delay_us(uint32 volatile number_of_us)
{
         while(number_of_us != 0){
        number_of_us--;
                Delay1us();
    }
}


/**@brief The data line outputs 0/1.
 *
 * @param[in] dir 
 */
void I2C_SDA_Dir (uint8 dir)
{
    if ( dir == 0 ) {
        nrf_gpio_cfg_output(SDA);
    }
    else {
        nrf_gpio_cfg_input(SDA,NRF_GPIO_PIN_PULLUP);
    }       
}

/**@brief The clock line outputs 0/1.
 *
 * @param[in] number_of_us 
 */
void I2C_SCL_Dir (uint8 dir)
{
       if ( dir == 0 ) {
           nrf_gpio_cfg_output(SCL);
    }
    else {
        nrf_gpio_cfg_input(SCL,NRF_GPIO_PIN_PULLUP);
    }
}    

/**@brief IIC communication to prepare.
 *
 * @param[in] i2c_master 
 */
uint8 I2c_Start(void)
{
      I2c_Reset();    
 
      SET_SCL_OUT;
      SET_SDA_OUT;  
      I2C_Delay(I2C_DELAY_TM);
    
      SDA_OUT_1;
      SCL_OUT_1;
      I2C_Delay(I2C_DELAY_TM);    
      if( !READ_SDA_IN ) {
            return I2C_FALSE; //BUSY
        }
      SDA_OUT_0;
      I2C_Delay(I2C_DELAY_TM);
      if( READ_SDA_IN ) {
            return I2C_FALSE; //ERROR
        }
      SDA_OUT_0;
      I2C_Delay(I2C_DELAY_TM);
      return I2C_TRUE;
}

/**@brief IIC stop the communication .
 *
 * @param[in] i2c_master 
 */
void I2c_Stop(void)
{
        SCL_OUT_0;
      I2C_Delay(I2C_DELAY_TM);
      SDA_OUT_0;
      I2C_Delay(I2C_DELAY_TM);
      SCL_OUT_1;
      I2C_Delay(I2C_DELAY_TM);
      SDA_OUT_1;
      I2C_Delay(I2C_DELAY_TM);
}
/**@brief reset  
 */
void I2c_Reset (void)
{
    if( READ_SDA_IN == 0  
        //|| READ_SCL_IN == 0 
        ) 
  { SET_SCL_OUT; SET_SDA_OUT; I2C_Delay(I2C_DELAY_TM); SDA_OUT_1; SCL_OUT_1; I2C_Delay(I2C_DELAY_TM); I2C_SendByte(
0xFF); I2c_NoAck(); I2c_Stop(); } } /**@brief Send the data ‘c‘ out,either as data or as an address. * * @param[in] C */ void I2C_SendByte(uint8 c) { uint8 i; i = 8; while(i--) { SCL_OUT_0; I2C_Delay(I2C_DELAY_TM); if( c&0x80) { SDA_OUT_1; } else { SDA_OUT_0; } c<<=1; I2C_Delay(I2C_DELAY_TM); SCL_OUT_1; I2C_Delay(I2C_DELAY_TM); } SCL_OUT_0; } /**@brief Reads a bytes of data and returns it. * * @param[out] retc */ uint8 I2C_RcvByte(void) { uint8 i = 8; uint8 retc = 0; SET_SDA_IN; SDA_OUT_1; while(i--) { retc<<=1; SCL_OUT_0; I2C_Delay(I2C_DELAY_TM); SCL_OUT_1; I2C_Delay(I2C_DELAY_TM); if( READ_SDA_IN ) { retc |= 0x01; } } SCL_OUT_0; SET_SDA_OUT; return retc; } /**@brief ACK response */ uint8 I2c_RecvACK(void) { SCL_OUT_0; I2C_Delay(I2C_DELAY_TM); SDA_OUT_1; SET_SDA_IN; I2C_Delay(I2C_DELAY_TM); SCL_OUT_1; I2C_Delay(I2C_DELAY_TM); if( READ_SDA_IN ) { SCL_OUT_0; SET_SDA_OUT; return I2C_FALSE; } else { SCL_OUT_0; SET_SDA_OUT; return I2C_TRUE; } } /**@brief Don‘t reply */ void I2c_NoAck(void) { SCL_OUT_0; I2C_Delay(I2C_DELAY_TM); SDA_OUT_1; I2C_Delay(I2C_DELAY_TM); SCL_OUT_1; I2C_Delay(I2C_DELAY_TM); SCL_OUT_0; I2C_Delay(I2C_DELAY_TM); }

 

《我的嵌入式开发》---- IIC 通信

原文:https://www.cnblogs.com/LVNG2018/p/10364328.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!