ADC 模拟量
#include "bsp_adc.h"
#include "blb.h"
/**
* @brief ADC GPIO 初始化
* @param 无
* @retval 无
*/
static void ADCx_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 打开 ADC IO端口时钟
ADC_GPIO_APBxClock_FUN ( ADC_Solar_CLK|
ADC_LaLi_CLK|
ADC_FS_CLK|
ADC_FW_Motor_CLK|
ADC_FY_Motor_CLK, ENABLE );
// 配置Solar引脚模式
GPIO_InitStructure.GPIO_Pin = ADC_Solar_PIN1|
ADC_Solar_PIN2|
ADC_Solar_PIN3|
ADC_Solar_PIN4|
ADC_Solar_PIN5|
ADC_Solar_PIN6|
ADC_Solar_PIN7|
ADC_Solar_PIN8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
// 初始化 ADC IO
GPIO_Init(ADC_Solar_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ADC_LaLi_PIN1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_LaLi_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ADC_FS_PIN1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_FS_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ADC_FW_Motor_PIN1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_FW_Motor_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = ADC_FY_Motor_PIN1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADC_FY_Motor_PORT, &GPIO_InitStructure);
}
/**
* @brief 配置ADC工作模式
* @param 无
* @retval 无
*/
static void ADCx_Mode_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
// 打开DMA时钟
RCC_AHBPeriphClockCmd(ADC_DMA_CLK, ENABLE);
// 打开ADC时钟
ADC_APBxClock_FUN ( ADC_CLK, ENABLE );
// 复位DMA控制器
DMA_DeInit(ADC_DMA_CHANNEL);
// 配置 DMA 初始化结构体
// 外设基址为:ADC 数据寄存器地址
DMA_InitStructure.DMA_PeripheralBaseAddr = ( u32 ) ( & ( ADC_x->DR ) );
// 存储器地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_ConvertedValue;
// 数据源来自外设
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
// 缓冲区大小,应该等于数据目的地的大小
DMA_InitStructure.DMA_BufferSize = NOFCHANEL;
// 外设寄存器只有一个,地址不用递增
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
// 存储器地址递增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
// 外设数据大小为半字,即两个字节
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
// 内存数据大小也为半字,跟外设数据大小相同
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
// 循环传输模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
// DMA 传输通道优先级为高,当使用一个DMA通道时,优先级设置不影响
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
// 禁止存储器到存储器模式,因为是从外设到存储器
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
// 初始化DMA
DMA_Init(ADC_DMA_CHANNEL, &DMA_InitStructure);
// 使能 DMA 通道
DMA_Cmd(ADC_DMA_CHANNEL , ENABLE);
// ADC 模式配置
// 只使用一个ADC,属于单模式
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
// 扫描模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE ;
// 连续转换模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
// 不用外部触发转换,软件开启即可
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
// 转换结果右对齐
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
// 转换通道个数
ADC_InitStructure.ADC_NbrOfChannel = NOFCHANEL;
// 初始化ADC
ADC_Init(ADC_x, &ADC_InitStructure);
// 配置ADC时钟N狿CLK2的8分频,即9MHz
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
// 配置ADC 通道的转换顺序和采样时间
ADC_RegularChannelConfig(ADC_x, ADC_Solar_CHANNEL1, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_Solar_CHANNEL2, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_Solar_CHANNEL3, 3, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_Solar_CHANNEL4, 4, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_Solar_CHANNEL5, 5, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_Solar_CHANNEL6, 6, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_Solar_CHANNEL7, 7, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_Solar_CHANNEL8, 8, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_LaLi_CHANNEL1, 9, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_FS_CHANNEL1, 10, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_FW_Motor_CHANNEL1, 11, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC_x, ADC_FY_Motor_CHANNEL1, 12, ADC_SampleTime_55Cycles5);
// 使能ADC DMA 请求
ADC_DMACmd(ADC_x, ENABLE);
// 开启ADC ,并开始转换
ADC_Cmd(ADC_x, ENABLE);
// 初始化ADC 校准寄存器
ADC_ResetCalibration(ADC_x);
// 等待校准寄存器初始化完成
while(ADC_GetResetCalibrationStatus(ADC_x));
// ADC开始校准
ADC_StartCalibration(ADC_x);
// 等待校准完成
while(ADC_GetCalibrationStatus(ADC_x));
// 由于没有采用外部触发,所以使用软件触发ADC转换
ADC_SoftwareStartConvCmd(ADC_x, ENABLE);
}
/**
* @brief ADC初始化
* @param 无
* @retval 无
*/
void ADCx_Init(void)
{
ADCx_GPIO_Config();
ADCx_Mode_Config();
}
#define filter_len 5
unsigned int filter0(unsigned int data)
{
static unsigned int buffer[filter_len] = {0};
static unsigned int* buffer_p = (int*)buffer + (filter_len - 1);
static unsigned int sum = (filter_len >> 1);
sum -= *buffer_p;
*buffer_p-- = data;
sum += data;
if (buffer_p < buffer)
{
buffer_p += filter_len;
}
return sum / filter_len;
}
unsigned int filter1(unsigned int data)
{
static unsigned int buffer[filter_len] = {0};
static unsigned int* buffer_p = (int*)buffer + (filter_len - 1);
static unsigned int sum = (filter_len >> 1);
sum -= *buffer_p;
*buffer_p-- = data;
sum += data;
if (buffer_p < buffer)
{
buffer_p += filter_len;
}
return sum / filter_len;
}
unsigned int filter2(unsigned int data)
{
static unsigned int buffer[filter_len] = {0};
static unsigned int* buffer_p = (int*)buffer + (filter_len - 1);
static unsigned int sum = (filter_len >> 1);
sum -= *buffer_p;
*buffer_p-- = data;
sum += data;
if (buffer_p < buffer)
{
buffer_p += filter_len;
}
return sum / filter_len;
}
unsigned int filter3(unsigned int data)
{
static unsigned int buffer[filter_len] = {0};
static unsigned int* buffer_p = (int*)buffer + (filter_len - 1);
static unsigned int sum = (filter_len >> 1);
sum -= *buffer_p;
*buffer_p-- = data;
sum += data;
if (buffer_p < buffer)
{
buffer_p += filter_len;
}
return sum / filter_len;
}
unsigned int filter4(unsigned int data)
{
static unsigned int buffer[filter_len] = {0};
static unsigned int* buffer_p = (int*)buffer + (filter_len - 1);
static unsigned int sum = (filter_len >> 1);
sum -= *buffer_p;
*buffer_p-- = data;
sum += data;
if (buffer_p < buffer)
{
buffer_p += filter_len;
}
return sum / filter_len;
}
unsigned int filter5(unsigned int data)
{
static unsigned int buffer[filter_len] = {0};
static unsigned int* buffer_p = (int*)buffer + (filter_len - 1);
static unsigned int sum = (filter_len >> 1);
sum -= *buffer_p;
*buffer_p-- = data;
sum += data;
if (buffer_p < buffer)
{
buffer_p += filter_len;
}
return sum / filter_len;
}
unsigned int filter6(unsigned int data)
{
static unsigned int buffer[filter_len] = {0};
static unsigned int* buffer_p = (int*)buffer + (filter_len - 1);
static unsigned int sum = (filter_len >> 1);
sum -= *buffer_p;
*buffer_p-- = data;
sum += data;
if (buffer_p < buffer)
{
buffer_p += filter_len;
}
return sum / filter_len;
}
unsigned int filter7(unsigned int data)
{
static unsigned int buffer[filter_len] = {0};
static unsigned int* buffer_p = (int*)buffer + (filter_len - 1);
static unsigned int sum = (filter_len >> 1);
sum -= *buffer_p;
*buffer_p-- = data;
sum += data;
if (buffer_p < buffer)
{
buffer_p += filter_len;
}
return sum / filter_len;
}
void Get_filter_adc(void)
{
if(T100mS_FLAG)
{
ADC_filter[0]=filter0(ADC_ConvertedValue[0]);
ADC_filter[1]=filter1(ADC_ConvertedValue[1]);
ADC_filter[2]=filter2(ADC_ConvertedValue[2]);
ADC_filter[3]=filter3(ADC_ConvertedValue[3]);
ADC_filter[4]=filter4(ADC_ConvertedValue[4]);
ADC_filter[5]=filter5(ADC_ConvertedValue[5]);
ADC_filter[6]=filter6(ADC_ConvertedValue[6]);
ADC_filter[7]=filter7(ADC_ConvertedValue[7]);
//将模拟量赋值给上、下、东、西四个方向的光电传感器,顶层
T_Sensor_Shang=ADC_filter[1];
T_Sensor_Xia=ADC_filter[0];
T_Sensor_Dong=ADC_filter[2];
T_Sensor_Xi=ADC_filter[3];
//将模拟量赋值给上、下、东、西四个方向的光电传感器,底层
B_Sensor_Shang=ADC_filter[4];
B_Sensor_Xia=ADC_filter[5];
B_Sensor_Dong=ADC_filter[7];
B_Sensor_Xi=ADC_filter[6];
FW_Sensor_chazhi=B_Sensor_Dong-B_Sensor_Xi;
FY_Sensor_chazhi=B_Sensor_Shang-B_Sensor_Xia;
}
}
//获取最大值,判断阴天晴天
//阴天,返回0
//晴天,返回1
unsigned char Get_Day_YQ(void)
{
if((T_Sensor_Shang+T_Sensor_Xia+T_Sensor_Dong+T_Sensor_Xi)>Sensor_YQ)
{
return 1;
}
else
{
return 0;
}
}
/*********************************************END OF FILE**********************/
|