1、DAC
void Dac_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitType;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE );
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) ;
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) ;
DAC_InitType.DAC_Trigger=DAC_Trigger_None;
DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;
DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;
DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ;
DAC_Init(DAC_Channel_1,&DAC_InitType);
DAC_InitType.DAC_Trigger=DAC_Trigger_None;
DAC_InitType.DAC_WaveGeneration=DAC_WaveGeneration_None;
DAC_InitType.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bit0;
DAC_InitType.DAC_OutputBuffer=DAC_OutputBuffer_Disable ;
DAC_Init(DAC_Channel_2,&DAC_InitType);
DAC_Cmd(DAC_Channel_1, ENABLE);
DAC_Cmd(DAC_Channel_2, ENABLE);
DAC_SetChannel1Data(DAC_Align_12b_R, 0);
DAC_SetChannel2Data(DAC_Align_12b_R, 0);
}
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 );
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_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_Cmd ( ADC1, ENABLE );
ADC_ResetCalibration ( ADC1 );
while ( ADC_GetResetCalibrationStatus ( ADC1 ) );
ADC_StartCalibration ( ADC1 );
while ( ADC_GetCalibrationStatus ( ADC1 ) );
}
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_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 ( DMA_CHx, &DMA_InitStructure );
}
4、获取通道电压值
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数据校验
uint16_t Do_Crc(uint8_t *ptr, uint32_t len)
{
uint16_t i;
uint16_t crc = 0xFFFF;
while(len--)
{
crc ^= *ptr++;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
crc = (crc >> 1) ^ 0xA001;
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(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 --;
}
}
}