From 72def895431ad7a08e635b11f3da738e2b2c4618 Mon Sep 17 00:00:00 2001
From: wujiazhi <1147861305@qq.com>
Date: Thu, 13 Jun 2024 11:31:04 +0800
Subject: [PATCH] add lower model test

---
 HARDWARE/ADC/ADC.c |  405 ++++++++++++++++++++++++++-------------------------------
 1 files changed, 182 insertions(+), 223 deletions(-)

diff --git a/HARDWARE/ADC/ADC.c b/HARDWARE/ADC/ADC.c
index a22b48f..478098a 100644
--- a/HARDWARE/ADC/ADC.c
+++ b/HARDWARE/ADC/ADC.c
@@ -1,242 +1,201 @@
-
-/******************************************************************************
- * Copyright (C) 2014-2015 HangZhou SiZhu Co.,LTD.
- *
- *-----------------------------------------------------------------------------
- * File: 			    ADC.c
- * Description:		use arm inner adc to sample given voltage 
- * Author:			  Lishoujian (867693272@qq.com)
- * Date:			    Jan 9, 2015
- *****************************************************************************/
-
-/* ----------------------- Platform includes --------------------------------*/
 #include "adc.h"
-#include "delay.h"
-#include "fm25V02.h"
-#include "stm32f10x_dma.h"
-#include "calculate.h"
-#include "devicegpioinit.h"
+#include "gpio.h"
+#include "power_manage.h"
 
+// �ڲ��ۼ�  �¶�б��
+// float  const_TmpeK	= 4.5295;  //0X640 ADC�
+// float  const_30_top	= 30.0;
 
-volatile u16								ad_data[1000];
-/******************************************
- * func:    Adc_Init
- * desc:    config gpio;initialize arm inner adc1
- * input:   none
- * output:  none
- * return:  none
- *****************************************/
-void  Adc_set(u8 tem_flag)
-{ 	
-	ADC_InitTypeDef  	ADC_InitStructure; 
-	GPIO_InitTypeDef 	GPIO_InitStructure;
+// �ⲿ�ۼ�  �¶�б��
+float const_TmpeK_14BIT = 23.0247; // 14BIT ADC�
+float const_30_top = 30.0;
 
-	
-		/*GPIO��ʼ��*/
-
-	/*dma ad �ɼ�������ʼ��*/
-	/*dma ��ʼ�����ú���*/
-	RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);//enbale adc channel clock 
-	//RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);	//Enable DMA
-	RCC_ADCCLKConfig(RCC_PCLK2_Div6);
-	
-	
-	/*��λADC1*/
-	//ADC_DeInit(ADC1);
-
-	/*GPIO��ʼ��*/
-	GPIO_InitStructure.GPIO_Pin  = ADC_dianchi_PIN_NUM;
-	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
-	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-	GPIO_Init(ADC_dianchi_PIN_GROUP, &GPIO_InitStructure);	
-	
-	ADC_InitStructure.ADC_Mode                		=  ADC_Mode_Independent;	//ADC1 work mode:independent
-	ADC_InitStructure.ADC_ScanConvMode       		=  DISABLE;	//single channel mode
-	ADC_InitStructure.ADC_ContinuousConvMode  	=  DISABLE;	//once   conversion
-	ADC_InitStructure.ADC_ExternalTrigConv    		=  ADC_ExternalTrigConv_None;	//triggered by software
-	ADC_InitStructure.ADC_DataAlign           		=  ADC_DataAlign_Right;	//ADC data align right
-	ADC_InitStructure.ADC_NbrOfChannel        		=  13;
-	ADC_Init(ADC1, &ADC_InitStructure);//init adc1 register
-
-
-	if(tem_flag==1)
-	{
-		//�����¶ȴ�����
-    ADC_TempSensorVrefintCmd(ENABLE);
-	}
-
-
-  	/*����ADCʱ��*/
-	/*ָ��ͨ��������ת��ʱ��*/
-	//ADC_RegularChannelConfig(ADC1, data_p->ad_ch, 1, ADC_SampleTime_13Cycles5);
-
-	ADC_Cmd(ADC1, ENABLE);//enbale adc1
-	
-	ADC_ResetCalibration(ADC1);//reset & calibrate adc1
-	 
-	while(ADC_GetResetCalibrationStatus(ADC1));//waiting for ending
-	
-	ADC_StartCalibration(ADC1);//open calibrating adc1
- 
-	while(ADC_GetCalibrationStatus(ADC1));//waiting for ending
-
-}				  
-
-void  Adc_DeInit(u8 tem_flag)
+void ADC_LithIO_Init(void)
 {
-	
-	GPIO_InitTypeDef 	GPIO_InitStructure;
-//	ADC_InitTypeDef  	ADC_InitStructure; 
-	/*GPIO��ʼ��*/
-	RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 , DISABLE);//enbale adc channel clock 
-
-	if(tem_flag==1)
-	{
-		//�����¶ȴ�����
-    ADC_TempSensorVrefintCmd(DISABLE);
-	}
-	
-	
-	ADC_DeInit(ADC1);
-	ADC_Cmd(ADC1, DISABLE);
-	
-		
-	
-	GPIO_InitStructure.GPIO_Pin  = ADC_dianchi_PIN_NUM;
-	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
-	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
-	GPIO_Init(ADC_dianchi_PIN_GROUP, &GPIO_InitStructure);	
-	
+  CMU_PERCLK_SetableEx(PADCLK, ENABLE); // PADʱ�ӣ�GPIO��ʹ�ܺ���
+  //	AnalogIO(LIT_PWR_UNDER_PORT,LIT_PWR_UNDER_PIN);//ADC_5
+  //	GPIOx_ANEN_Setable(LIT_PWR_UNDER_PORT,LIT_PWR_UNDER_PIN,ENABLE);
+  AnalogIO(LIT_ADC_PORT, LIT_ADC_PIN); // ADC_IN4
+  GPIOx_ANEN_Setable(LIT_ADC_PORT, LIT_ADC_PIN, ENABLE);
 }
 
-
-u16 Get_val(u8 ch)
+void ADC_AlkaIO_Init(void)
 {
-		u16 DataValue; //
-		/* ADC1 regular channel14 configuration */ 
-		ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_55Cycles5);
-
-		/* Start ADC1 Software Conversion */ 
-		ADC_SoftwareStartConvCmd(ADC1, ENABLE);
-
-		/* Test if the ADC1 EOC flag is set or not */ 
-		while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
-
-		/*Returns the ADC1 Master data value of the last converted channel*/
-		DataValue = ADC_GetConversionValue(ADC1); 
-		return DataValue; 
-} 
-
-u16 Temp_GetADCData(void)
-{
-	/*�������ADת��*/
-	ADC_SoftwareStartConvCmd(ADC1, ENABLE);
-	
-	/*�ȴ�ADת�����*/
-	while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
-	
-	/*��ȡ�ͷ���ADת��ֵ*/
-	return (ADC_GetConversionValue(ADC1));
+  CMU_PERCLK_SetableEx(PADCLK, ENABLE); // PADʱ�ӣ�GPIO��ʹ�ܺ���
+  AnalogIO(ALK_ADC_PORT, ALK_ADC_PIN);  // ADC_IN8
+  GPIOx_ANEN_Setable(ALK_ADC_PORT, ALK_ADC_PIN, ENABLE);
 }
 
-
-
-#define		DATA_START				2
-#define		DATA_GET_LENGTH		20
-/******************************************
- * func:    Get_vol_Average
- * desc:    
- *   - sample total 100 data,list data from low to hign,get middle 64 data
- *   - calculation sample mean of 64 middle data
- * input:   
- *   ch       - select sample channel of adc1
- *   vref_adc - Voltage reference of adc1 multiply by 1000. eg:3.3v ->33000
- * output:  none
- * return:  calculation sample mean of 64 middle data,multiply by 1000. eg:1.0v -> 1000
- *****************************************/
-uint16 Get_vol_Average(u8 channel,u8 tem_flag)  //channelͨ����   tem_flag��0��ʾ�ⲿ��ȡ  1��ʾ�ڲ�ͨ����ȡ�¶���
+void ADC_IN5_Init(void)
 {
-	u32  	i,  t;
-//	uint16	temp;
-	uint32 	temp_vol;
-	uint16 	ad_get_address_p[DATA_GET_LENGTH + 1];
-	uint32	temp_vol_small;
-	uint32	temp_vol_big;
-	
-	temp_vol = 0;
-	Adc_set(tem_flag);
-	//ADC_DMA_Transmit();
+  CDIF_CR_INTF_EN_Setable(ENABLE); // ���Դ��ӿ�ʹ��
+  VRTC_Init_RCMF_Trim();
+  VRTC_RCMFCR_EN_Setable(ENABLE);
+  VRTC_ADCCR_CKS_Set(VRTC_ADCCR_CKS_RCMF_2);    // ADC����ʱ��ѡ��
+  VRTC_ADCCR_CKE_Setable(ENABLE);               // ADC����ʱ��ʹ��
+  ADC_CFGR_BUFSEL_Set(ADC_CFGR_BUFSEL_ADC_IN5); // ADC����ͨ��ѡ��
 
-	for(i = 0; i < DATA_GET_LENGTH; i ++)
-	{
-		ad_get_address_p[i] = Get_val(channel);
-		temp_vol += ad_get_address_p[i];
-	}
-	Adc_DeInit(tem_flag);
-	
-	
-	temp_vol_small 	= ad_get_address_p[DATA_START];
-	temp_vol_big		= ad_get_address_p[DATA_START];
-	for(i = (DATA_START+1); i < (DATA_GET_LENGTH -1); i ++)
-	{
-		if(temp_vol_small > ad_get_address_p[i])
-			temp_vol_small = ad_get_address_p[i];
-		
-		if(temp_vol_big < ad_get_address_p[i])
-			temp_vol_big = ad_get_address_p[i];
-	}
-	
-	
-	temp_vol = 0;
-	for(t = DATA_START; t < (DATA_GET_LENGTH - 1); t ++)
-	{
-		temp_vol += ad_get_address_p[t];
-	}
-	temp_vol = temp_vol - temp_vol_small - temp_vol_big;
-	
-  temp_vol  = temp_vol /(DATA_GET_LENGTH - DATA_START - 3); 
-	
-	return (uint16)temp_vol;
+  ADC_CFGR_BUFEN_Setable(ENABLE);                      // ADC����ͨ��bufferʹ��/��ֹ
+  ADC_CR_MODE_Set(ADC_CR_MODE_EXTERNAL);               // ADC����ģʽѡ���ⲿ�ۼ���
+  ADC_CR_RSTCTRL_EN_Setable(ENABLE);                   // ����������ⲿ��λ
+  ADC_CFGR_ACC_PERIOD_Set(ADC_CFGR_ACC_PERIOD_14BITS); // �ⲿ�ۼ����ۼ���������
+  ADC_CR_HPEN_Set(ADC_CR_HPEN_1MHZ);
+
+  ADC_TRIM_Write(0X7FF); // adcƵ��1M ʱ ����ʱ��4ms
+  //  ADC_TRIM_Write(0X3FF);									//adcƵ��1M ʱ ����ʱ��2ms
+  //	ADC_TRIM_Write(0X1FF);									//adcƵ��1M ʱ ����ʱ��1ms
+
+  ADC_CR_ACC_IE_Setable(DISABLE); // �ⲿ�ۼ�ģʽ�жϽ�ֹ
+  ADC_CR_EN_Setable(DISABLE);     // ADC�ر�
 }
 
-// /**
-//   * @brief  ��ȡ������˹�˲��������
-//   * @param  ��
-//   * @retval ADC����
-//   */
-double Temp_GetBFiltedData(ad_data_s * data_p)
+void ADC_IN10_Init(void)
 {
-	u16 i,t;		
-	volatile double average;
-  volatile	u32 sum=0;		
-	double temp_adc_data[20];
-	/*-------------------------���ݲɼ�-------------------------*/
-	//AD����SMPL_NUM��
 
-	Adc_set(0);
-//	ADC_DMA_Transmit();
-	
-		for(t = 0; t < 20; t ++)
-	{
-		temp_adc_data[t]= data_p->ad_get_address_p[t];
-		sum += temp_adc_data[t];
-	}
-	
-	average=(double)sum/20;	
-	sum=0;
+  CDIF_CR_INTF_EN_Setable(ENABLE); // ���Դ��ӿ�ʹ��
+  VRTC_Init_RCMF_Trim();
+  VRTC_RCMFCR_EN_Setable(ENABLE);
+  VRTC_ADCCR_CKS_Set(VRTC_ADCCR_CKS_RCMF_2);     // ADC����ʱ��ѡ��
+  VRTC_ADCCR_CKE_Setable(ENABLE);                // ADC����ʱ��ʹ��
+  ADC_CFGR_BUFSEL_Set(ADC_CFGR_BUFSEL_ADC_IN10); // ADC����ͨ��ѡ��
 
-	/*------------------------������˹�˲�-----------------------*/	
-	filter_set_initial(temp_adc_data[1],temp_adc_data[0],average,average);
-	
-	//��ȡ�˲�������ݲ����
-	for(i=2;i<20;i++)
-	{
-		temp_adc_data[i]=filter_get_output(temp_adc_data[i]);
-		sum+= temp_adc_data[i];
-	}
-	
-	//�����˲������ݵ�ƽ��ֵ
-	average=(double)sum/(20-2);	
+  ADC_CFGR_BUFEN_Setable(ENABLE);                      // ADC����ͨ��bufferʹ��/��ֹ
+  ADC_CR_MODE_Set(ADC_CR_MODE_EXTERNAL);               // ADC����ģʽѡ���ⲿ�ۼ���
+  ADC_CR_RSTCTRL_EN_Setable(ENABLE);                   // ����������ⲿ��λ
+  ADC_CFGR_ACC_PERIOD_Set(ADC_CFGR_ACC_PERIOD_14BITS); // �ⲿ�ۼ����ۼ���������
+  ADC_CR_HPEN_Set(ADC_CR_HPEN_1MHZ);
 
-	return average;
+  ADC_TRIM_Write(0X7FF); // adcƵ��1M ʱ ����ʱ��4ms
+  //  ADC_TRIM_Write(0X3FF);									//adcƵ��1M ʱ ����ʱ��2ms
+  //	ADC_TRIM_Write(0X1FF);									//adcƵ��1M ʱ ����ʱ��1ms
+
+  ADC_CR_ACC_IE_Setable(DISABLE); // �ⲿ�ۼ�ģʽ�жϽ�ֹ
+  ADC_CR_EN_Setable(DISABLE);     // ADC�ر�
+}
+
+void ADC_IN8_Init(void)
+{
+
+  CDIF_CR_INTF_EN_Setable(ENABLE); // ���Դ��ӿ�ʹ��
+  VRTC_Init_RCMF_Trim();
+  VRTC_RCMFCR_EN_Setable(ENABLE);
+  VRTC_ADCCR_CKS_Set(VRTC_ADCCR_CKS_RCMF_2);    // ADC����ʱ��ѡ��
+  VRTC_ADCCR_CKE_Setable(ENABLE);               // ADC����ʱ��ʹ��
+  ADC_CFGR_BUFSEL_Set(ADC_CFGR_BUFSEL_ADC_IN8); // ADC����ͨ��ѡ��
+
+  ADC_CFGR_BUFEN_Setable(ENABLE);                      // ADC����ͨ��bufferʹ��/��ֹ
+  ADC_CR_MODE_Set(ADC_CR_MODE_EXTERNAL);               // ADC����ģʽѡ���ⲿ�ۼ���
+  ADC_CR_RSTCTRL_EN_Setable(ENABLE);                   // ����������ⲿ��λ
+  ADC_CFGR_ACC_PERIOD_Set(ADC_CFGR_ACC_PERIOD_14BITS); // �ⲿ�ۼ����ۼ���������
+  ADC_CR_HPEN_Set(ADC_CR_HPEN_1MHZ);
+
+  ADC_TRIM_Write(0X7FF); // adcƵ��1M ʱ ����ʱ��4ms
+  //    ADC_TRIM_Write(0X3FF);									//adcƵ��1M ʱ ����ʱ��2ms
+  //	ADC_TRIM_Write(0X1FF);									//adcƵ��1M ʱ ����ʱ��1ms
+
+  ADC_CR_ACC_IE_Setable(DISABLE); // �ⲿ�ۼ�ģʽ�жϽ�ֹ
+  ADC_CR_EN_Setable(DISABLE);     // ADC�ر�
+}
+
+void ADC_Temp_Init(void)
+{
+  CDIF_CR_INTF_EN_Setable(ENABLE); // ���Դ��ӿ�ʹ��
+  VRTC_Init_RCMF_Trim();
+  VRTC_RCMFCR_EN_Setable(ENABLE);
+  VRTC_ADCCR_CKS_Set(VRTC_ADCCR_CKS_RCMF_2); // ADC����ʱ��ѡ��
+  VRTC_ADCCR_CKE_Setable(ENABLE);            // ADC����ʱ��ʹ��
+  ADC_CFGR_BUFSEL_Set(ADC_CFGR_BUFSEL_TS);   // ADC����ͨ��ѡ��
+
+  ADC_CFGR_BUFEN_Setable(ENABLE); // ADC����ͨ��bufferʹ��/��ֹ
+
+  ADC_CR_MODE_Set(ADC_CR_MODE_EXTERNAL); // ADC����ģʽѡ���ⲿ�ۼ���
+
+  ADC_CR_RSTCTRL_EN_Setable(ENABLE); // ����������ⲿ��λ  �ڲ�ģʽdisable  �ⲿģʽenable
+
+  ADC_TRIM_Write(0x7FF); // �ⲿ�ۼ�   ����д0x7FF
+
+  ADC_CFGR_ACC_PERIOD_Set(ADC_CFGR_ACC_PERIOD_14BITS); // �ⲿ�ۼ����ۼ��������� �ڲ��ۼӿ�ע��
+
+  ADC_CR_ACC_IE_Setable(DISABLE); // �ⲿ�ۼ�ģʽ�жϽ�ֹ
+
+  ADC_CR_EN_Setable(DISABLE); // ADC�ر�
+}
+
+uint32_t adc_vol_cal(uint32_t adc_data)
+{
+  int32_t Volt = 0;
+  Volt = (adc_data * const_adc_Slope / 1000.0) + const_adc_Offset / 100.0;
+
+  if (Volt < 0) // VoltΪ��ֵ
+  {
+    Volt = 0;
+  }
+  return Volt;
+}
+
+float adc_tem_cal(uint32_t adc_data)
+{
+  float T = 0;
+  // �ڲ��ۼӼ���
+  //	if(adc_data >const_T_30 )
+  //	{
+  //		T=(adc_data-const_T_30)/const_TmpeK+const_30_top;
+  //	}
+  //	else
+  //	{
+  //		T=const_30_top-(const_T_30-adc_data)/const_TmpeK;
+  //	}
+
+  // �ⲿ�ۼƼ���
+  if (adc_data > const_T_30_14BIT)
+  {
+    T = (adc_data - const_T_30_14BIT) / const_TmpeK_14BIT + const_30_top - 0.5;
+  }
+  else
+  {
+    T = const_30_top - (const_T_30_14BIT - adc_data) / const_TmpeK_14BIT - 0.5;
+  }
+  return T;
+}
+
+uint08 adc_wait_finish(void)
+{
+  uint32 timeout = 0;
+  do
+  {
+    if (SET == ADC_ISR_ACC_IF_Chk())
+      return 0; // �ⲿ�ۼ�
+  } while (timeout++ < 0xFFFFFFFFU);
+  return 1; // ��ʱ
+}
+
+uint32_t Get_AdcValue(void)
+{
+  volatile int32_t fVlotage = 0;
+  uint32 fTempADC = 0;
+  ADC_CR_EN_Setable(ENABLE); // ADC����
+
+  ADC_ISR_ACC_IF_Clr();       // �ⲿ�ۼ�����жϱ�־
+  if (0 == adc_wait_finish()) // �ȴ�ת�����
+    fTempADC = ADC_DR_Read(); // ��ȡADֵ
+  else
+    return 0;
+  fVlotage = adc_vol_cal(fTempADC); // ADֵת��Ϊ��ѹ,��Դ��ѹΪ5V
+
+  return fVlotage;
+}
+
+float Get_AdcTempValue(void)
+{
+  float Temperature = 0;
+  uint32 fTempADC = 0;
+  ADC_CR_EN_Setable(ENABLE); // ADC����
+
+  ADC_ISR_ACC_IF_Clr();       // �ⲿ�ۼ�����жϱ�־
+  if (0 == adc_wait_finish()) // �ȴ�ת�����
+    fTempADC = ADC_DR_Read(); // ��ȡADֵ
+  else
+    return 0;
+  Temperature = adc_tem_cal(fTempADC);
+  return Temperature;
 }

--
Gitblit v1.9.3