系列文章:
【从0到1搭建LoRa物联网】1、LoRa物联网的架构
【从0到1搭建LoRa物联网】2、终端设备开发方式
【从0到1搭建LoRa物联网】3、国产LoRa终端ASR6505软硬件
【从0到1搭建LoRa物联网】4、国产LoRa终端ASR6505普通GPIO
【从0到1搭建LoRa物联网】5、国产LoRa终端ASR6505驱动DHT11
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通用代码:
//初始化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));//等待ackI2C_SendData(I2C1, (uint8_t)(reg));while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//等待ackI2C_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));//等待ackwhile(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));//等待ackI2C_SendData(I2C1, (uint8_t)Reg);while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTING));//等待ackI2C_SendData(I2C1, (uint8_t)Data);while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));//等待ackI2C_AcknowledgeConfig(I2C1,DISABLE);I2C_GenerateSTOP(I2C1,ENABLE); }
关于BM1383AGLV设备相关代码:
//检测设备是否存在void BM1383AGLVCheck(void){uint16_t chip_id;I2C_ReadRegNByte(BM1383AGLV_ID1,2,(uint8_t*)(&chip_id));printf("chip_id=0x%x\r\n",chip_id);if(chip_id!=0xe032){printf("Device is not exist!\r\n"); }else{printf("Device is exist\r\n!"); }}//BM1383AGL设备初始化void BM1383AGLVInit(void){I2C_WriteRegByte(BM1383AGLV_PWR_DOWN,ACTIVE);//设置芯片上电 DelayMs(5);//延时必须大于2msI2C_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 hPa\r\n",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 ℃\r\n",temperture); }
主函数相关代码:
int main( void ){BoardInitMcu( );BoardInitPeriph( );printf("ASR6505 I2C demo test!\r\n");I2C_PeriphInit();BM1383AGLVCheck();BM1383AGLVInit();while(1){BM1383AGLVRead();printf("---------------------------------------\r\n\r\n");Delay(5);}}
6、实验现象
欢迎关注微信公众号【物联网思考】,回复关键字“ASR6505”获取资料,《从0到1搭建LoRa物联网》系列的所有软硬件资料将会分享在公众号。