2-STM32103的DAC与ADC初始化配置

1、DAC

void Dac_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	DAC_InitTypeDef DAC_InitType;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );	  //使能PORTA通道时钟
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE );	  //使能DAC通道时钟 

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;				 // 端口配置
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; 		 //模拟输入
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_SetBits(GPIOA,GPIO_Pin_4)	;//PA.4 输出高
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;				 // 端口配置
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; 		 //模拟输入
 	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_SetBits(GPIOA,GPIO_Pin_5)	;//PA.4 输出高
					
	DAC_InitType.DAC_Trigger=DAC_Trigger_None;	//不使用触发功能 TEN1=0
	DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;//不使用波形发生
	DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;//屏蔽、幅值设置
	DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ;	//DAC1输出缓存关闭 BOFF1=1
  DAC_Init(DAC_Channel_1,&DAC_InitType);	 //初始化DAC通道1
	
	DAC_InitType.DAC_Trigger=DAC_Trigger_None;	//不使用触发功能 TEN1=0
	DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;//不使用波形发生
	DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;//屏蔽、幅值设置
	DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ;	//DAC1输出缓存关闭 BOFF1=1
  DAC_Init(DAC_Channel_2,&DAC_InitType);	 //初始化DAC通道2

	DAC_Cmd(DAC_Channel_1, ENABLE);  //使能DAC1
  DAC_Cmd(DAC_Channel_2, ENABLE);  //使能DAC1
	
  DAC_SetChannel1Data(DAC_Align_12b_R, 0);  //12位右对齐数据格式设置DAC值
	DAC_SetChannel2Data(DAC_Align_12b_R, 0);  //12位右对齐数据格式设置DAC值

}

2、ADC

void Adc_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
  ADC_InitTypeDef  ADC_InitStructure;  
	
  RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA  | RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO, ENABLE );
  RCC_ADCCLKConfig ( RCC_PCLK2_Div6 );		// 72M / 6 = 12M  ADC最大不能超过14M
    
    // ADC1 --- ADC8
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init ( GPIOA, &GPIO_InitStructure );
	
  ADC_DeInit ( ADC1 );
   
  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 = ADC_CHANNEL_NUM;								//ADC通道数
  ADC_Init ( ADC1, &ADC_InitStructure );
    
    //规则通道配置
  ADC_RegularChannelConfig ( ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 );
  ADC_RegularChannelConfig ( ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5 );
  ADC_RegularChannelConfig ( ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5 );
  ADC_RegularChannelConfig ( ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5 );
    
  ADC_DMACmd ( ADC1, ENABLE );					//使能ADC DMA传输
  ADC_Cmd ( ADC1, ENABLE );						//使能ADC1
	
  ADC_ResetCalibration ( ADC1 );					//使能复位校准
  while ( ADC_GetResetCalibrationStatus ( ADC1 ) );	//等待复位校准结束
	
  ADC_StartCalibration ( ADC1 );					//开启AD校准
  while ( ADC_GetCalibrationStatus ( ADC1 ) );	//等待校准结束
//  ADC_SoftwareStartConvCmd ( ADC1, ENABLE );	//启动转换
}

3、DMA配置

void My_DMA_Config ( DMA_Channel_TypeDef *DMA_CHx, u32 cpar, u32 cmar, u16 cndtr )
{
	DMA_InitTypeDef DMA_InitStructure;
  RCC_AHBPeriphClockCmd ( RCC_AHBPeriph_DMA1, ENABLE );
	
  DMA_DeInit ( DMA_CHx );
	
  DMA_InitStructure.DMA_PeripheralBaseAddr = cpar;			//外设基地址
  DMA_InitStructure.DMA_MemoryBaseAddr = cmar;				//内存基地址
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;			//数据传输从外设到内存
  DMA_InitStructure.DMA_BufferSize = cndtr;					//DMA缓存
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;	//外设地址不变
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;				//内存地址递增
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设数据16位
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;			//内存地址1位
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;				//循环工作模式
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;			//高优先级
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;				//没有设置内存到内存传输
	
  DMA_Init ( DMA_CHx, &DMA_InitStructure );
}

4、获取通道电压值

/*获取ADC的电压值***************/
void ADC_Convert_val()
{
	u8 i, j;
	memset((u8*)ADC1_Value_SUM,0x00,sizeof(ADC1_Value_SUM));
	for(i=0;i<ADC_CHANNEL_NUM;i++)
	{
		for(j=0;j<ADC_C_VAL_TIMES;j++)
		{
			ADC1_Value_SUM[i] += ADC1_Value[j][i];
		}
	}
	for(i=0;i<ADC_CHANNEL_NUM;i++)
	{
		ADC1_Value_reg[i]=ADC1_Value_SUM[i]/ADC_C_VAL_TIMES;
		ADC1_Value_Vol[i] = ADC1_Value_reg[i] * 3.3 / 4096;
	}
}

5、CRC数据校验

/*CRC校验函数************/
uint16_t Do_Crc(uint8_t *ptr, uint32_t len)
{
	uint16_t i;
	uint16_t crc = 0xFFFF;  //crc16位寄存器初始值

	while(len--)
	{
		crc ^= *ptr++;
		for (i = 0; i < 8; ++i)
		{
			if (crc & 1)
			crc = (crc >> 1) ^ 0xA001; //多项式 POLY(0x8005)的高低位交换值,这是由于其模型的一些参数决定的
			else
			crc = (crc >> 1);
		}
	}
	return crc;
}

6、类PID函数

u8 overFlow_min_ba=0;
u8 overFlow_max_ba=0;
u16 temp_Difference=0;
void Adjust_PID(struct para_ld_def_ *Module, u16 dacMaxValue, u16 adjustThreshold)
{
	if(Module->get_c_ma<=(Module->set_c_ma-adjustThreshold))
	{
		overFlow_min_ba++;
		if(overFlow_max_ba>=1)overFlow_max_ba--;
		else overFlow_max_ba = 0;
	}
	else if(Module->get_c_ma>(Module->set_c_ma+adjustThreshold))
	{
		overFlow_max_ba++;
		if(overFlow_min_ba>=1)overFlow_min_ba--;
		else overFlow_min_ba = 0;
	}
	if(overFlow_min_ba >= 50 )
	{
		overFlow_min_ba=0;
		temp_Difference = Module->set_c_ma - Module->get_c_ma;
		if(temp_Difference>1000)
		{
			Module->set_c_ad += 5;
			if(Module->set_c_ad>dacMaxValue) Module->set_c_ad = dacMaxValue;
			}else if(temp_Difference>600){
			Module->set_c_ad += 4;
			if(Module->set_c_ad>dacMaxValue) Module->set_c_ad = dacMaxValue;
			}else if(temp_Difference>300){
			Module->set_c_ad += 3;
			if(Module->set_c_ad>dacMaxValue) Module->set_c_ad = dacMaxValue;
			}else if(temp_Difference>100){
			Module->set_c_ad += 2;
			if(Module->set_c_ad>dacMaxValue) Module->set_c_ad = dacMaxValue;
			}else{
			Module->set_c_ad ++;
			if(Module->set_c_ad>dacMaxValue) Module->set_c_ad = dacMaxValue;
		}
		
	}
	//else if(pumpModule->adcOpticalPower>(pumpModule->targetOpticalPower+adjustThreshold))
	else if(overFlow_max_ba>=50)
	{
		overFlow_max_ba=0;
		temp_Difference = Module->get_c_ma - Module->set_c_ma ;
		if(temp_Difference>1000)
		{
			if(Module->set_c_ad>5)Module->set_c_ad -= 5;
			else if (Module->set_c_ad>=1)Module->set_c_ad --;
		}
		else if(temp_Difference>600)
		{
			if(Module->set_c_ad>4)Module->set_c_ad -= 4;
			else if (Module->set_c_ad>=1)Module->set_c_ad --;
		}
		else if(temp_Difference>300)
		{
			if(Module->set_c_ad>3)Module->set_c_ad -= 3;
			else if (Module->set_c_ad>=1)Module->set_c_ad --;
		}
		else if(temp_Difference>100)
		{
			if(Module->set_c_ad>2)Module->set_c_ad -= 2;
			else if (Module->set_c_ad>=1) Module->set_c_ad --;
		}
		else
		{
			if(Module->set_c_ad>=1)Module->set_c_ad --;
		}
	}
}

你可能感兴趣的:(嵌入式软件,stm32,单片机,嵌入式硬件)