-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
83 lines (67 loc) · 2.98 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "ADS1115.h"
#include "driver/i2c.h"
#include "esp_spi_flash.h"
#include "esp_system.h"
#include <esp_log.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include <stdio.h>
#define PIN_LED GPIO_NUM_19
#define PIN_SDA GPIO_NUM_22
#define PIN_SCL GPIO_NUM_23
#define I2C_MASTER_NUM I2C_NUM_0
#define I2C_MASTER_TIMEOUT_MS 1000
#define ADS1115_I2C_ADDRESS 0x48
void vReadAdc(void *pvParameters);
ADS1115 adc_ic_1;
SemaphoreHandle_t xBinarySemaphoreAdcReady;
void app_main(void)
{
printf("Hello world!\n");
/* Print chip information */
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
printf("This is ESP32 chip with %d CPU cores, WiFi%s%s, ", chip_info.cores,
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
printf("silicon revision %d, ", chip_info.revision);
printf("%dMB %s flash\n", spi_flash_get_chip_size() / (1024 * 1024),
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded"
: "external");
i2c_config_t i2c_conf = {.mode = I2C_MODE_MASTER,
.sda_io_num = PIN_SDA,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = PIN_SCL,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 100000};
i2c_param_config(I2C_MASTER_NUM, &i2c_conf);
i2c_driver_install(I2C_MASTER_NUM, i2c_conf.mode, 0, 0, 0);
xBinarySemaphoreAdcReady = xSemaphoreCreateBinary();
ADS1115_reset(&adc_ic_1);
vTaskDelay(pdMS_TO_TICKS(1000));
uint8_t status = ADS1115_init(&adc_ic_1, ADS1115_I2C_ADDRESS, I2C_MASTER_NUM);
ESP_LOGI("ADS1115", "Sensor Address %02x", adc_ic_1.i2c_addess);
xTaskCreate(&vReadAdc, "ADC IC Task", 4096, NULL, 1, NULL);
}
void vReadAdc(void *pvParameters)
{
ADS1115_set_mode(&adc_ic_1, ADS1115_MODE_CONTINUOUS);
ADS1115_set_gain(&adc_ic_1, ADS1115_PGA_2P048);
ADS1115_set_mux(&adc_ic_1, ADS1115_MUX_P1_NG);
// ADS1115_set_comparator_latch(&adc_ic_1, ADS1115_COMP_LAT_LATCHING);
// ADS1115_set_comparator_queue(&adc_ic_1, ADS1115_COMP_QUE_ASSERT1);
// ADS1115_set_ready_mode(&adc_ic_1);
// vTaskDelay(pdMS_TO_TICKS(1000));
// ADS1115_init(&adc_ic_1, ADS1115_I2C_ADDRESS, I2C_MASTER_NUM);
// ADS1115_trigger_conversion(&adc_ic_1);
vTaskDelay(pdMS_TO_TICKS(1000));
// ADS1115_init(&adc_ic_1, ADS1115_I2C_ADDRESS, I2C_MASTER_NUM);
for (;;)
{
// xSemaphoreTake(xBinarySemaphoreAdcReady, portMAX_DELAY);
ADS1115_get_conversion(&adc_ic_1);
ESP_LOGI("Read ADC", "Raw: %04x Equivalent: %0.0f mV", adc_ic_1.conversion[adc_ic_1.mux], ADS1115_get_millivolts(&adc_ic_1));
vTaskDelay(pdMS_TO_TICKS(1000));
}
}