ASR6601 硬件IIC配置

1.硬件IIC初始化

//硬件IIC相关函数
void IIC_HardInit(void)
{
  i2c_config_t config;
  
  // enable the clk
  rcc_enable_peripheral_clk(RCC_PERIPHERAL_I2C0, true);
  rcc_enable_peripheral_clk(RCC_PERIPHERAL_GPIOA, true);

  // set iomux
  gpio_set_iomux(GPIOA, GPIO_PIN_14, 3);
  gpio_set_iomux(GPIOA, GPIO_PIN_15, 3);

  // init
  i2c_config_init(&config);
  config.settings.master.speed = I2C_CR_BUS_MODE_FAST;
  i2c_init(I2C0, &config);
  i2c_cmd(I2C0, true);
}

2.写寄存器

//硬件IIC往寄存器里写数据
void KX126_WriteReg_Hardware(u8 reg,u8 dat)
{

  //起始信号+从机地址+写
  i2c_master_send_start(I2C0, KX126_ADDR, I2C_WRITE);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
  
  //写寄存器
  i2c_send_data(I2C0, reg);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
  
  //写数据
  i2c_send_data(I2C0, dat);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
  
  //停止信号
  i2c_master_send_stop(I2C0);
}

3.读寄存器

//硬件IIC从寄存器里读数据
u8 KX126_ReadReg_Hardware(u8 reg)
{
  u8 dat=0;
  
  //起始信号+从机地址+写
  i2c_master_send_start(I2C0, KX126_ADDR, I2C_WRITE);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
    
  //写寄存器
  i2c_send_data(I2C0, reg);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
  
  //起始信号+从机地址+读
  i2c_master_send_start(I2C0, KX126_ADDR, I2C_READ);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
    
  //读数据
  i2c_set_receive_mode(I2C0, I2C_NAK);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_RECV_FULL) != SET);
  
  dat= i2c_receive_data(I2C0);
  i2c_clear_flag_status(I2C0, I2C_FLAG_RECV_FULL);
  
  //停止信号
  i2c_master_send_stop(I2C0);
  
  return dat;
}

4.连续写寄存器

//硬件IIC往寄存器写入连续数据
void KX126_WriteRegBuf_Hardware(u8 reg,u8 *buf,u16 len)
{
  u8 i;
  //起始信号+从机地址+写
  i2c_master_send_start(I2C0, KX126_ADDR, I2C_WRITE);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
  
  //写寄存器
  i2c_send_data(I2C0, reg);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
  
  //发送数据
  for(i=0;i<len;i++)
  {
    //写数据
    i2c_send_data(I2C0, buf[i]);
    i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
    while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
  }
  //IIC_Stop();
 i2c_master_send_stop(I2C0);
}

5.连续读寄存器

//硬件IIC从寄存器里读取连续数据
void KX126_ReadRegBuf_Hardware(u8 reg,u8 *buf,u16 len)
{
  //起始信号+从机地址+写
  i2c_master_send_start(I2C0, KX126_ADDR, I2C_WRITE);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
  
  //写寄存器
  i2c_send_data(I2C0, reg);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
  
  //起始信号+从机地址+读
  i2c_master_send_start(I2C0, KX126_ADDR, I2C_READ);
  i2c_clear_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY);
  while (i2c_get_flag_status(I2C0, I2C_FLAG_TRANS_EMPTY) != SET);
  //读取数据
  while(len)
  {
    if(len==1)
    {
      i2c_set_receive_mode(I2C0, I2C_NAK);
      while (i2c_get_flag_status(I2C0, I2C_FLAG_RECV_FULL) != SET);//有数据进来时退出
      
      *buf= i2c_receive_data(I2C0);
      i2c_clear_flag_status(I2C0, I2C_FLAG_RECV_FULL);
    }
    else
    {
      i2c_set_receive_mode(I2C0, I2C_ACK);
      while (i2c_get_flag_status(I2C0, I2C_FLAG_RECV_FULL) != SET);
      
      *buf= i2c_receive_data(I2C0);
      i2c_clear_flag_status(I2C0, I2C_FLAG_RECV_FULL);
    }        
    len--;
    buf++;
  }
   //IIC_Stop();
  i2c_master_send_stop(I2C0);
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值