📄 adc.c
字号:
#include "stm32f10x_lib.h"
#include "LCD_12864.h"
#define ADC1_DR_Address ((u32)0x4001244C)
unsigned char K1=6,K2=1;
extern u16 vl1[5],vl2[5];
extern u16 vo1[5],vo2[5];
extern u8 ad_ch_pb,adc_flag,text;
//--------------------------------------
u8 page=0,warning=0;
u8 bat_c_show=0; //电池电量显示
u8 adc1[4], adc2[4]; //bat_c电池容量指示
u16 v1=0,v2=0,v3=0,I1=0,v4=0,v5=0,v6=0,v7=0,v8=0;
//===========================================
vu16 ADCConvertedValue[2];
//ErrorStatus HSEStartUpStatus;
void adc_Configuration(void){
// TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* DMA1 channel1 configuration ----------------------------------------------*/
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADCConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 2;
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_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
// TIM4 configuration ------------------------------------------------------
TIM_DeInit(TIM4);
// Time Base configuration
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 2879;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
// Channel1 Configuration in PWM mode
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1000;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC4Init(TIM4,&TIM_OCInitStructure);
/* ADC1 configuration ------------------------------------------------------*/
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 = 2;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);
ADC_TempSensorVrefintCmd(ENABLE);
ADC_ExternalTrigConvCmd(ADC1, ENABLE);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
/**************************************************************/
// Enable JEOC interupt
ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
/**************************************************************/
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
// TIM1 counter enable
TIM_Cmd(TIM4,ENABLE);
// TIM1 main Output Enable
TIM_CtrlPWMOutputs(TIM4,ENABLE);
}
//温度测试函数
float temptran(u16 tempv)
{
float tempr ;
float f , temp0 ,temp1 ;
tempr=(float)(2561-tempv)/(((float)tempv)/20000.0) ;
f = tempr; //这里f的值是最终读到的热敏电阻阻值
if(( f>=692 ) && ( f<= 47975) ) //温度范围-25~85
{
if(( 37088<f) && ( f<=47975) )
{ temp0 = -25; temp1 =(47975 - f)*5/(47975-37088); }
else if(( 28903<f) && ( f<=37088) )
{ temp0 = -20; temp1 =(37088 - f)*5/(37088-28903); }
else if(( 22695<f) && ( f<=28903) )
{ temp0 = -15; temp1 =(28903 - f)*5/(28903-22695); }
else if(( 17948<f) && ( f<=22695) )
{ temp0 = -10; temp1 =(22695 - f)*5/(22695-17948); }
else if(( 14286<f) && ( f<=17948) )
{ temp0 = -5; temp1 =(17948 - f)*5/(17948-14286); }
else if(( 11435<f) && ( f<=14286) )
{ temp0 = 0; temp1 =(14286 - f)*5/(14286-11435); }
else if(( 9213<f) && ( f<=11435) )
{ temp0 = 5; temp1 =(11435 - f)*5/(11435-9213); }
else if(( 7471<f) && ( f<=9213) )
{ temp0 = 10; temp1 =(9213 - f)*5/(9213-7471); }
else if(( 6094<f) && ( f<=7471) )
{ temp0 = 15; temp1 =(7471 - f)*5/(7471-6094); }
else if(( 5000<f) && ( f<=6094) )
{ temp0 = 20; temp1 =(6094 - f)*5/(6094-5000); }
else if(( 4127<f) && ( f<=5000) )
{ temp0 = 25; temp1 =(5000 - f)*5/(5000-4127); }
else if(( 3427<f) && ( f<=4127) )
{ temp0 = 30; temp1 =(4127 - f)*5/(4127-3427); }
else if(( 2863<f) && ( f<=3427) )
{ temp0 = 35; temp1 =(3427 - f)*5/(3427-2863); }
else if(( 2406<f) && ( f<=2863) )
{ temp0 = 40; temp1 =(2863 - f)*5/(2863-2406); }
else if(( 2032<f) && ( f<=2406) )
{ temp0 = 45; temp1 =(2406 - f)*5/(2406-2032); }
else if(( 1723<f) && ( f<=2032) )
{ temp0 = 50; temp1 =(2032 - f)*5/(2032-1723); }
else if(( 1467<f) && ( f<=1723) )
{ temp0 = 55; temp1 =(1723 - f)*5/(1723-1467); }
else if(( 1255<f) && ( f<=1467) )
{ temp0 = 60; temp1 =(1467 - f)*5/(1467-1255); }
else if(( 1077<f) && ( f<=1255) )
{ temp0 = 65; temp1 =(1255 - f)*5/(1255-1077); }
else if(( 926<f) && ( f<=1077) )
{ temp0 = 70; temp1 =(1077 - f)*5/(1077-926); }
else if(( 799<f) && ( f<=926) )
{ temp0 = 75; temp1 =(926 - f)*5/(926-799); }
}
else
{
temp0 = 0; temp1 = 0;
}
return (float)(temp0+temp1) ; //*10 单位为0.1
}
void vol_show(void)
{
switch(page)
{
case 0: {
lcd_show_char(0,0,"零电流纹波逆变器");
//========
lcd_show_char(0,1,"输出电流:");
if(v2/1000==0) adc2[0]=' ';
else adc2[0]=v2/1000 + '0';
v2 %= 1000;
adc2[1]=v2/100 + '0';
adc2[2]='.';
v2 %= 100;
adc2[3]=v2/10 + '0';
//adc2[4]='V';
//adc2[5]=' ';
lcd_show_char(5,1,adc2);
lcd_show_char(7,1,"安");
lcd_show_char(0,2,"输出电压:");
if(v1/1000==0) {adc1[0]=' ';}
else{adc1[0]=v1/1000 + '0';}
v1 %= 1000;
adc1[1]=v1/100 + '0';
adc1[2]='.';
v1 %= 100;
adc1[3]=v1/10 + '0';
//adc1[4]='V';
//adc1[5]=' ';
lcd_show_char(5,2,adc1);
lcd_show_char(7,2,"伏");
//========
lcd_show_char(0,3," ");
break;}
case 1:{
break;}
case 2:{
break;}
}
}
void adc_deal(void) {
unsigned long v1_sum=0,v2_sum=0;
u8 i=0;
if(adc_flag) {
for(i=0;i<5;i++){
if(ad_ch_pb){
v1_sum+=vl1[i];
v2_sum+=vl2[i];
}
else{
v1_sum+=vo1[i];
v2_sum+=vo2[i];
}
}
v1=v1_sum/5;
v2=v2_sum/5;
v1=(v1/10)*K1;
v2=(v2/10)*K2;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -