【先楫HPM6750测评】测试I2C与ADS1115通信
[复制链接]
HPM6750有4个I2C,HPM6750EVKMINI排针引出了2个I2C,模块侧有上拉电阻,所以使用板子上的I2C0,但比较遗憾的是HPM6750的I2C最高速度只支持1000k,不知道以后会不会支持3.4M。
ADS1115是TI家的兼容 I 2C 的 16 位高精度低功耗模数转换器 (ADC),I2C的最高速度是3.4M,本来想测一下3.4M的I2C,后来查看HPM6750的手册看不支持3.4M。
找了个ADS1115模块,连接杜邦线开干
主要程序如下:
1.初始化I2C,速度为1000K
init_i2c_pins(TEST_I2C);
i2c_config_t configi;
uint32_t freq;
configi.i2c_mode = i2c_mode_fast_plus ;//i2c_mode_normal
configi.is_10bit_addressing = false;
freq = clock_get_frequency(TEST_I2C_CLOCK_NAME);
stat = i2c_init_master(TEST_I2C, freq, &configi);
if (stat != status_success) {
return stat;
}
2.定义I2C地址,因为我接线将ADS1115的ADDR直接接地,所以I2C地址为0x48
#define ADS1115_WRITE_ADDRESS (0x90>>1)//0x90
#define ADS1115_READ_ADDRESS (0x90>>1)//0x91
3.配置寄存器
void ads1115_config_register(uint8_t pointADD,uint8_t configH,uint8_t configL)
{
uint8_t reg_data[3]={pointADD,configH,configL};
if (status_success != i2c_master_write(TEST_I2C, ADS1115_WRITE_ADDRESS, reg_data, 3)) {
printf("IIC write failed");
printf("ads1115 Config Register error!!!\r\n");
while (1) {
}
}
}
4.读数据
int16_t ads1115_read_data(void)
{
int16_t data;
uint8_t rx_data[2]={0};
if (status_success != i2c_master_write(TEST_I2C, ADS1115_WRITE_ADDRESS, 0x00, 1)) {
printf("IIC write failed");
printf("ads1115 convert Register error!!!\r\n");
while (1) {
}
}
if (status_success != i2c_master_read(TEST_I2C, ADS1115_READ_ADDRESS, rx_data, 2)) {
printf("IIC read failed\n");
printf("ads1115 read data error!!!\r\n");
while (1) {
}
}
data=rx_data[0]<<8 | rx_data[1];
return data;
}
5.获取电压值
double ads1115_get_voltage_val(uint8_t pointADD,uint8_t configH,uint8_t configL)
{
double val;
int16_t ad_val;
ads1115_config_register(pointADD,configH,configL);
delay_ms(10);//等待10ms
ad_val=ads1115_read_data();
if((ad_val==0x7FFF)|(ad_val==0X8000))//是否超量程了
{
ad_val=0;
printf("over PGA\r\n");
}
switch((0x0E&configH)>>1)//量程对应的分辨率
{
case(0x00):
val=(double)ad_val*187.5/1000000.0;//
break;
case(0x01):
val=(double)ad_val*125/1000000.0;
break;
case(0x02):
val=(double)ad_val*62.5/1000000.0;
break;
case(0x03):
val=(double)ad_val*31.25/1000000.0;
break;
case(0x04):
val=(double)ad_val*15.625/1000000.0;
break;
case(0x05):
val=(double)ad_val*7.8125/1000000.0;
break;
}
return val;
}
6.下载程序分别测试0V和3.3V
总结:1.HPM6750的I2C最高速率目前是1000K,2.HPM6750的库中I2C是给7bit地址,读写函数自动移位和增加读写位,所以只需要定义I2C从机实际地址就可以。
|