esp-open-rtos/examples/mcp4725_test/main.c

72 lines
1.7 KiB
C
Raw Normal View History

/*
* Example of using MCP4725 DAC
*
* Part of esp-open-rtos
* Copyright (C) 2016 Ruslan V. Uss <unclerus@gmail.com>
* BSD Licensed as described in the file LICENSE
*/
#include <esp/uart.h>
#include <espressif/esp_common.h>
#include <i2c/i2c.h>
#include <mcp4725/mcp4725.h>
#include <stdio.h>
#include <FreeRTOS.h>
#include <task.h>
2017-09-01 09:29:32 +00:00
#define I2C_BUS 0
#define SCL_PIN 5
#define SDA_PIN 4
#define ADDR MCP4725A0_ADDR0
#define VDD 3.3
2017-09-01 09:29:32 +00:00
inline static void wait_for_eeprom(i2c_dev_t* dev)
{
2017-09-01 09:29:32 +00:00
while (mcp4725_eeprom_busy(dev))
{
printf("...DAC is busy, waiting...\n");
vTaskDelay(1);
}
}
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
2017-09-01 09:29:32 +00:00
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_100K);
i2c_dev_t dev = {
.addr = ADDR,
.bus = I2C_BUS,
};
// setup EEPROM values
2017-09-01 09:29:32 +00:00
if (mcp4725_get_power_mode(&dev, true) != MCP4725_PM_NORMAL)
{
printf("DAC was sleeping... Wake up Neo!\n");
2017-09-01 09:29:32 +00:00
mcp4725_set_power_mode(&dev, MCP4725_PM_NORMAL, true);
wait_for_eeprom(&dev);
}
printf("Set default DAC ouptut value to MAX...\n");
2017-09-01 09:29:32 +00:00
mcp4725_set_raw_output(&dev, MCP4725_MAX_VALUE, true);
wait_for_eeprom(&dev);
2017-09-01 09:29:32 +00:00
printf("And now default DAC output value is 0x%03x\n", mcp4725_get_raw_output(&dev, true));
printf("Now let's generate the sawtooth wave in slow manner\n");
float vout = 0;
while (true)
{
vout += 0.1;
if (vout > VDD) vout = 0;
printf("Vout: %.02f\n", vout);
2017-09-01 09:29:32 +00:00
mcp4725_set_voltage(&dev, VDD, vout, false);
// It will be very low freq wave
vTaskDelay(100 / portTICK_PERIOD_MS);
}
}