LoRa终端设备ASR6505之I2C通信

2020-09-27 09:50:15 浏览数 (1)

前言:I2C接口是比较常见的接口,尤其是各种传感器,本篇聊聊ASR6505的I2C使用。

1、ASR6505的I2C接口

只有1路I2C,与STM8是相对应的。两个引脚PC0(I2C_SDA)和PC1(I2C_SCL)是开漏输出的,开发板中的H4针脚,需要外接上拉电阻。开发板上已经接了4.7K的上拉电阻。本例程以一个I2C设备(BM1383AGLV是一个压力和温度二合一的传感器)读写的展开。

2、接线

ASR6505 BM1383AGLV

VCC VCC

GND GND

PC0 SDA

PC1 SCL

3、STM8L关于I2C的操作

使用STM8L标准库函数,硬件I2C。主要还是起始信号、结束信号、ACK、NACK等的操作。

4、实验设计

间隔5s读取BM1383AGLV的数据,计算出当前压力和温度,并通过串口打印。

5、程序实现

关于I2C通用代码:

代码语言:javascript复制
//初始化I2C外设
void I2C_PeriphInit(void)
{
  CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE);

  I2C_DeInit(I2C1);
  I2C_Init(I2C1, 100000, BM1383AGLV_I2C_ADDR, I2C_Mode_I2C, I2C_DutyCycle_2,I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);
  I2C_Cmd(I2C1, ENABLE);
}
//从特定的寄存器读取数据
void I2C_ReadRegNByte(uint8_t reg,uint8_t num,uint8_t *datbuf)
{
  uint8_t totalread=0;

  totalread=num;

  while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));

  I2C_GenerateSTART(I2C1, ENABLE);//起始信号
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));/*EV5,主模式*/

  I2C_Send7bitAddress(I2C1, BM1383AGLV_I2C_ADDR, I2C_Direction_Transmitter);//写地址
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//等待ack

  I2C_SendData(I2C1, (uint8_t)(reg));
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//等待ack

  I2C_GenerateSTART(I2C1,ENABLE);    
  while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));

  I2C_Send7bitAddress(I2C1, BM1383AGLV_I2C_ADDR, I2C_Direction_Receiver);//读地址
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));//等待ack

  while(num)  
  {
    if(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED))      //测试EV6 
    {      
      datbuf[totalread-num]=I2C_ReceiveData(I2C1);      //取一个字节

      if(num == 1)       //读到最后一个字节
      {
        I2C_AcknowledgeConfig(I2C1,DISABLE);   //不需要应答
        I2C_GenerateSTOP(I2C1,ENABLE);         //发结束位
      }
      else 
      {
        I2C_AcknowledgeConfig(I2C1,ENABLE);    //不是最后一个字节向从设备发送应答信号
      }
      num--;
    }   
  }
}
//写数据
void I2C_WriteRegByte(uint8_t Reg,uint8_t Data)
{    
  while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));

  I2C_GenerateSTART(I2C1, ENABLE);//起始信号
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));// EV5,主模式

  I2C_Send7bitAddress(I2C1, BM1383AGLV_I2C_ADDR, I2C_Direction_Transmitter);//写地址
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//等待ack

  I2C_SendData(I2C1, (uint8_t)Reg);
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING));//等待ack

  I2C_SendData(I2C1, (uint8_t)Data);
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//等待ack

  I2C_AcknowledgeConfig(I2C1,DISABLE);
  I2C_GenerateSTOP(I2C1,ENABLE); 
}

关于BM1383AGLV设备相关代码:

代码语言:javascript复制
//检测设备是否存在
void BM1383AGLVCheck(void)
{
  uint16_t chip_id;

  I2C_ReadRegNByte(BM1383AGLV_ID1,2,(uint8_t*)(&chip_id));

  printf("chip_id=0x%xrn",chip_id);

  if(chip_id!=0xe032)
  {
    printf("Device is not exist!rn"); 
  }
  else
  {
    printf("Device is existrn!"); 
  }
}
//BM1383AGL设备初始化
void BM1383AGLVInit(void)
{
  I2C_WriteRegByte(BM1383AGLV_PWR_DOWN,ACTIVE);          //设置芯片上电  
  DelayMs(5);//延时必须大于2ms
  I2C_WriteRegByte(BM1383AGLV_RESET,MEASUREMENT_ACTIVE);    //启动测量功能
  I2C_WriteRegByte(BM1383AGLV_MODE_CONTROL,MODE_CONTINUOUS|DEFAULT_1|DRDY_PIN_DISABLE|AVE_NUM_8);    //持续测量模式、每次测量8次、不使能DRDY引脚
}
//BM1383AGL读取数据
void BM1383AGLVRead(void)
{
  uint32_t pressure_count=0;
  uint8_t pressure_m , pressure_l,pressure_d;
  float pressure;

  uint32_t temperture_count=0;
  uint8_t temperture_m , temperture_l;
  float temperture;
  uint8_t  status;

  do
  {
    I2C_ReadRegNByte(BM1383AGLV_STATUS,1,&status);
  }
  while((status&DATA_OUTPUT_AVAILABLE)!=DATA_OUTPUT_AVAILABLE);//等待当前测量完成

  I2C_ReadRegNByte(BM1383AGLV_PRESSURE_MSB,1,&pressure_m);
  I2C_ReadRegNByte(BM1383AGLV_PRESSURE_LSB,1,&pressure_l);
  I2C_ReadRegNByte(BM1383AGLV_PRESSURE_DEC,1,&pressure_d);

  pressure_d=pressure_d>>2;//高6位有效

  pressure_count =(pressure_m*16384ul);
  pressure_count =(pressure_l<<6);
  pressure_count =(pressure_d);

  pressure=pressure_count/2048.0;

  printf("Pressure:%.2f hParn",pressure);

  I2C_ReadRegNByte(BM1383AGLV_TEMPERATURE_MSB,1,&temperture_m);
  I2C_ReadRegNByte(BM1383AGLV_TEMPERATURE_LSB,1,&temperture_l);

  temperture_count=temperture_m<<8;
  temperture_count =temperture_l;

  temperture=temperture_count/32.0;

  printf("Temperture:%f ℃rn",temperture); 
}

主函数相关代码:

代码语言:javascript复制
int main( void )
{   
    BoardInitMcu( );
    BoardInitPeriph( );

    printf("ASR6505 I2C demo test!rn");

    I2C_PeriphInit();
    BM1383AGLVCheck();
    BM1383AGLVInit();
    while(1)
    {
      BM1383AGLVRead();
      printf("---------------------------------------rnrn");
      Delay(5);
    }
}

6、实验现象

na

0 人点赞