ADS1115(测量ADC)的使用教程(IIC) 您所在的位置:网站首页 ADS使用手册中文 ADS1115(测量ADC)的使用教程(IIC)

ADS1115(测量ADC)的使用教程(IIC)

2023-09-07 02:21| 来源: 网络整理| 查看: 265

ADS1115可以测量ADC,能够测量单端对地电压和差分对输入的电压,测量范围是0-6V。

1、main.c:

#include "led.h" #include "delay.h" #include "sys.h" #include "usart.h" #include "lcd.h" #include "key.h" #include "ads1115.h" float aa; int main(void) { float t1; u16 t,result; u8 key; u16 i=0; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2 delay_init(); uart_init(9600); //串口初始化为9600 LED_Init(); //LCD_Init(); KEY_Init(); //IIC初始化 ADS1115_Init(); while(1) { key=KEY_Scan(0); if(key==WKUP_PRES) { result=lvbo(0xeb,0x82); //A0 A1为差分输入测试端 低八位+高八位1111 1011,1000 0010 if(result >= 0x8000 && result = 0xffff) result = 0; t1=4.096*2*result/65535; //转换成电压 printf("量程为4.096V,A0-A1之间电压 = %f V\r\n",t1);//打印 if(result == 0x7fff || result == 0x8000) { printf("已超量程!\r\n\r\n"); } else { printf("读取正常!\r\n\r\n"); } } if(key==KEY0_PRES) { result=lvbo(0xe3,0xb2); //A2 A3为差分输入测试端 低八位+高八位 1111 0011,1011 0010 if(result >= 0x8000) result = 0xffff - result; //差值为负取绝对值,使得A2 A3正反接都行 t1=4.096*2*result/65535; //转换成电压 printf("量程为4.096V,A2-A3之间电压 = %f V\r\n",t1); //打印 if(result == 0x7fff || result == 0x8000) //超过最大值或者低于最小值 { printf("已超量程!\r\n\r\n"); } else { printf("读取正常!\r\n\r\n"); } } if(key == KEY1_PRES) { result=lvbo(0xe3,0xb4); //A2 A3为差分输入测试端 低八位+高八位 1111 0011,1011 0100 if((result >= 0x8000) && (result = 0xffff) result = 0; t1=2.048*2*result/65535; //转换成电压 printf("量程为2.048V,A0-A1之间电压 = %f V\r\n",t1);//打印 if(result == 0x7fff || result == 0x8000) { printf("已超量程!\r\n\r\n"); } else { printf("读取正常!\r\n\r\n"); } } i++; delay_ms(10); if(i==20) { LED0=!LED0;//提示系统正在运行 i=0; } } }

2、ADS1115.c

#include "sys.h" #include "stm32f10x_i2c.h" #include "ads1115.h" static void ADS1115_delay(u16 D) { while(--D); } void delay_nms(u16 ms) { u16 i; u32 M = 0;//720W for(i = 0;i < ms; i++) for(M=12000;M > 0;M--); } void delay_nus(u16 us) { u16 i; u16 M = 0;//720W for(i = 0;i < us; i++) for(M=72;M > 0;M--); } /PA8 SDAPA9 SCL/// void ADS1115_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC ,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;//A SCL SDA GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); SDA_A1; SCL_A1; delay_nms(5); } //I2C总线启动 void I2CStart_A(void) { SDA_A1; ADS1115_delay(5); SCL_A1; ADS1115_delay(5); SDA_A0; ADS1115_delay(5);//MIN 160ns SCL_A0; ADS1115_delay(5); } //I2C停止总线 void I2CStop_A(void) { SDA_A0; ADS1115_delay(5); SCL_A1; ADS1115_delay(5); SDA_A1; ADS1115_delay(5);//MIN 160ns } //I2C 写一字节 void I2CWriteByte_A(u8 DATA) { u8 i; SCL_A0; for(i = 0;i < 8; i++) { if(DATA&0x80) { SDA_A1; } else { SDA_A0; } SCL_A1;//按照手册不需延时 ADS1115_delay(5); SCL_A0; ADS1115_delay(5); DATA = DATA


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有