G-CAMS-DATU/applications/ADC/adc.c

214 lines
4.8 KiB
C
Raw Normal View History

2024-05-13 08:08:47 +00:00
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-05-11 LJ the first version
*/
#include <adc.h>
rt_adc_device_t adc1_dev;
rt_adc_device_t adc2_dev;
rt_adc_device_t adc3_dev;
ADC_Data ADC;
/**
* ADC初始化函数
* @return
*/
rt_uint8_t adc_init()
{
//查找设备
adc1_dev = (rt_adc_device_t) rt_device_find("adc1");
if (adc1_dev == RT_NULL)
{
rt_kprintf("adc sample run failed! can't find %s device!\n", "adc1");
return RT_ERROR;
}
adc2_dev = (rt_adc_device_t) rt_device_find("adc2");
if (adc2_dev == RT_NULL)
{
rt_kprintf("adc sample run failed! can't find %s device!\n", "adc3");
return RT_ERROR;
}
adc3_dev = (rt_adc_device_t) rt_device_find("adc3");
if (adc3_dev == RT_NULL)
{
rt_kprintf("adc sample run failed! can't find %s device!\n", "adc3");
return RT_ERROR;
}
//使能设备
rt_adc_enable(adc1_dev, ADC1_CHANNEL_0);
rt_adc_enable(adc2_dev, ADC2_CHANNEL_4);
rt_adc_enable(adc3_dev, ADC3_CHANNEL_10);
rt_adc_enable(adc3_dev, ADC3_CHANNEL_11);
return RT_EOK;
}
/**
* ADC读取数据
*/
void adc_read()
{
/**
*
* 0-10v vout = vin*2/6.8 + 0.2v
* = vout - 0.2* 6.8 / 2
*
* 4-20ma vin = i * 150
* = vout - 0.2* 6.8 / 2 / 150
*/
ADC.ADC1_0_value = rt_adc_read(adc1_dev, ADC1_CHANNEL_0); //读取采样值
ADC.ADC1_0_vol = ADC.ADC1_0_value * REFER_VOLTAGE / CONVERT_BITS; //转换为对应电压值
// h_vol = (((float)vol - 0.2) * 6.8 ) / 2 ;
ADC.ADC1_0_h_vol = (rt_uint16_t) (((float) ADC.ADC1_0_vol / 100 - 0.2) * 340);
if (ADC.ADC1_mode == 1) //1:0-10V 2:4-20ma
{
}
else if (ADC.ADC1_mode == 2)
{
ADC.ADC1_0_Current = (rt_uint16_t) (((float) ADC.ADC1_0_vol / 100 - 0.2) / 150 * 340);
}
if (ADC.log)
{
rt_kprintf("ADC1_CHANNEL_0 value is :%d voltage is :%d.%02d \n", ADC.ADC1_0_value, ADC.ADC1_0_vol / 100, ADC.ADC1_0_vol % 100);
rt_kprintf("ADC1_CHANNEL_0 0-10V voltage is :%d.%02d , Current is :%d.%02d\n", ADC.ADC1_0_h_vol / 100, ADC.ADC1_0_h_vol % 100,
ADC.ADC1_0_Current / 100, ADC.ADC1_0_Current % 100);
}
ADC.ADC2_4_value = rt_adc_read(adc2_dev, ADC2_CHANNEL_4);
ADC.ADC2_4_vol = ADC.ADC2_4_value * REFER_VOLTAGE / CONVERT_BITS;
ADC.ADC2_4_h_vol = (rt_uint16_t) (((float) ADC.ADC2_4_vol / 100 - 0.2) * 340);
if (ADC.ADC2_mode == 1)
{
}
else if (ADC.ADC2_mode == 2)
{
ADC.ADC2_4_Current = (rt_uint16_t) (((float) ADC.ADC2_4_vol / 100 - 0.2) / 150 * 340);
}
if (ADC.log)
{
rt_kprintf("ADC2_CHANNEL_4 value is :%d voltage is :%d.%02d \n", ADC.ADC2_4_value, ADC.ADC2_4_vol / 100, ADC.ADC2_4_vol % 100);
rt_kprintf("ADC2_CHANNEL_4 0-10V voltage is :%d.%02d , Current is :%d.%02d\n", ADC.ADC2_4_h_vol / 100, ADC.ADC2_4_h_vol % 100,
ADC.ADC2_4_Current / 100, ADC.ADC2_4_Current % 100);
}
ADC.ADC3_10_value = rt_adc_read(adc3_dev, ADC3_CHANNEL_10);
ADC.ADC3_10_vol = ADC.ADC3_10_value * REFER_VOLTAGE / CONVERT_BITS;
if (ADC.log)
{
rt_kprintf("ADC3_CHANNEL_10 value is :%d voltage is :%d.%02d \n", ADC.ADC3_10_value, ADC.ADC3_10_vol / 100, ADC.ADC3_10_vol % 100);
}
ADC.ADC3_11_value = rt_adc_read(adc3_dev, ADC3_CHANNEL_11);
ADC.ADC3_11_vol = ADC.ADC3_11_value * REFER_VOLTAGE / CONVERT_BITS;
if (ADC.log)
{
rt_kprintf("ADC3_CHANNEL_11 value is :%d voltage is :%d.%02d \n", ADC.ADC3_11_value, ADC.ADC3_11_vol / 100, ADC.ADC3_11_vol % 100);
}
}
/**
* 3V3的值ADC
* @return
*/
rt_uint16_t get_ADC3_10_vol()
{
return ADC.ADC3_10_vol;
}
rt_uint16_t get_ADC3_11_vol()
{
return ADC.ADC3_11_vol;
}
/**
* 0-10vADC值
* @return
*/
rt_uint16_t get_ADC1_0_h_vol()
{
return ADC.ADC1_0_h_vol;
}
/**
* 4-10ma电流值
* @return
*/
rt_uint16_t get_ADC1_0_Current()
{
return ADC.ADC1_0_Current;
}
/**
* 0-10vADC值
* @return
*/
rt_uint16_t get_ADC2_4_h_vol()
{
return ADC.ADC2_4_h_vol;
}
/**
* 4-10ma电流值
* @return
*/
rt_uint16_t get_ADC2_4_Current()
{
return ADC.ADC2_4_Current;
}
void set_ADC_log(rt_uint8_t log)
{
ADC.log = log;
}
/**
* ADC1的模式1:0-10V 2:4-20ma
* @param mode
*/
void set_ADC1_mode(rt_uint8_t mode)
{
ADC.ADC1_mode = mode;
}
/**
* ADC2的模式1:0-10V 2:4-20ma
* @param mode
*/
void set_ADC2_mode(rt_uint8_t mode)
{
ADC.ADC2_mode = mode;
}
/**
* ADC1的模式1:0-10V 2:4-20ma
* @param mode
*/
rt_uint8_t get_ADC1_mode()
{
return ADC.ADC1_mode;
}
/**
* ADC2的模式1:0-10V 2:4-20ma
* @param mode
*/
rt_uint8_t get_ADC2_mode()
{
return ADC.ADC2_mode;
}