标签:cpu content alignment scale version return stat tco extern
//不说那么多了,直接上程序 void ADC1_DeInit(void) { ADC1->CSR = ADC1_CSR_RESET_VALUE; ADC1->CR1 = ADC1_CR1_RESET_VALUE; ADC1->CR2 = ADC1_CR2_RESET_VALUE; ADC1->CR3 = ADC1_CR3_RESET_VALUE; ADC1->TDRH = ADC1_TDRH_RESET_VALUE; ADC1->TDRL = ADC1_TDRL_RESET_VALUE; ADC1->HTRH = ADC1_HTRH_RESET_VALUE; ADC1->HTRL = ADC1_HTRL_RESET_VALUE; ADC1->LTRH = ADC1_LTRH_RESET_VALUE; ADC1->LTRL = ADC1_LTRL_RESET_VALUE; ADC1->AWCRH = ADC1_AWCRH_RESET_VALUE; ADC1->AWCRL = ADC1_AWCRL_RESET_VALUE; } void ADC1_Init(ADC1_ConvMode_TypeDef ADC1_ConversionMode, ADC1_Channel_TypeDef ADC1_Channel, ADC1_PresSel_TypeDef ADC1_PrescalerSelection, ADC1_ExtTrig_TypeDef ADC1_ExtTrigger, FunctionalState ADC1_ExtTriggerState, ADC1_Align_TypeDef ADC1_Align, ADC1_SchmittTrigg_TypeDef ADC1_SchmittTriggerChannel, FunctionalState ADC1_SchmittTriggerState) { /* Check the parameters */ assert_param(IS_ADC1_CONVERSIONMODE_OK(ADC1_ConversionMode));//单次还 是连续转换 assert_param(IS_ADC1_CHANNEL_OK(ADC1_Channel)); assert_param(IS_ADC1_PRESSEL_OK(ADC1_PrescalerSelection)); assert_param(IS_ADC1_EXTTRIG_OK(ADC1_ExtTrigger)); assert_param(IS_FUNCTIONALSTATE_OK(((ADC1_ExtTriggerState)))); assert_param(IS_ADC1_ALIGN_OK(ADC1_Align)); assert_param(IS_ADC1_SCHMITTTRIG_OK(ADC1_SchmittTriggerChannel)); assert_param(IS_FUNCTIONALSTATE_OK(ADC1_SchmittTriggerState)); /*-----------------CR1 & CSR configuration --------------------*/ /* Configure the conversion mode and the channel to convert respectively according to ADC1_ConversionMode & ADC1_Channel values & ADC1_Align values */ ADC1_ConversionConfig(ADC1_ConversionMode, ADC1_Channel, ADC1_Align); /* Select the prescaler division factor according to ADC1_PrescalerSelection values */ ADC1_PrescalerConfig(ADC1_PrescalerSelection); /*-----------------CR2 configuration --------------------*/ /* Configure the external trigger state and event respectively according to NewState, ADC1_ExtTrigger */ ADC1_ExternalTriggerConfig(ADC1_ExtTrigger, ADC1_ExtTriggerState); /*------------------TDR configuration ---------------------------*/ /* Configure the schmitt trigger channel and state respectively according to ADC1_SchmittTriggerChannel & ADC1_SchmittTriggerNewState values */ ADC1_SchmittTriggerConfig(ADC1_SchmittTriggerChannel, ADC1_SchmittTriggerState); /* Enable the ADC1 peripheral */ ADC1->CR1 |= ADC1_CR1_ADON; } void ADC1_Cmd(FunctionalState NewState) { /* Check the parameters */ assert_param(IS_FUNCTIONALSTATE_OK(NewState)); if (NewState != DISABLE) { ADC1->CR1 |= ADC1_CR1_ADON; } else /* NewState == DISABLE */ { ADC1->CR1 &= (u8)(~ADC1_CR1_ADON); } } void ADC1_StartConversion(void) { ADC1->CR1 |= ADC1_CR1_ADON; } FlagStatus ADC1_GetFlagStatus(ADC1_Flag_TypeDef Flag) { u8 flagstatus = 0; u8 temp = 0; /* Check the parameters */ assert_param(IS_ADC1_FLAG_OK(Flag)); if ((Flag & 0x0F) == 0x01) { /* Get OVR flag status */ flagstatus = (u8)(ADC1->CR3 & ADC1_CR3_OVR); } else if ((Flag & 0xF0) == 0x10) { /* Get analog watchdog channel status */ temp = (u8)(Flag & 0x0F); if (temp < 8) { flagstatus = (u8)(ADC1->AWSRL & (1 << temp)); } else { flagstatus = (u8)(ADC1->AWSRH & (1 << (temp - 8))); } } else /* Get EOC | AWD flag status */ { flagstatus = (u8)(ADC1->CSR & Flag); } return ((FlagStatus)flagstatus); } u16 ADC1_GetConversionValue(void) { u16 temph = 0; u8 templ = 0; if (ADC1->CR2 & ADC1_CR2_ALIGN) /* Right alignment */ { /* Read LSB first */ templ = ADC1->DRL; /* Then read MSB */ temph = ADC1->DRH; temph = (u16)(templ | (u16)(temph << (u8)8)); } else /* Left alignment */ { /* Read MSB firts*/ temph = ADC1->DRH; /* Then read LSB */ templ = ADC1->DRL; temph = (u16)((u16)(templ << (u8)6) | (u16)(temph << (u8)8)); } return ((u16)temph); } //下面程序是用AN3即PB3进行AD转换,并将AD值放在 u16 ADVal //初始化时调用: GPIO_DeInit(GPIOB); GPIO_Init(GPIOB, GPIO_PIN_3, GPIO_MODE_IN_FL_NO_IT);//设置IO为悬浮输入 ADC1_DeInit(); ADC1_Init(ADC1_CONVERSIONMODE_SINGLE,ADC1_CHANNEL_3, ADC1_PRESSEL_FCPU_D6,ADC1_EXTTRIG_TIM,DISABLE, ADC1_ALIGN_RIGHT,ADC1_SCHMITTTRIG_CHANNEL3,DISABLE); /*单次转换*/ /*PB3/AN3*/ /*6分频*/ /*内部定时器1 TRG事件*/ /*禁止外部触发转换*/ /*数据右对齐 (低8字节在ADC_DRL寄存器,其余高字节 位在ADC_DRH寄存器)读顺序 应先读低位,再读高位字节*/ ADC1_Cmd(ENABLE);//同意进行AD转换 //进行AD取值时调用: ADC1_StartConversion();//開始AD转换 while(0==ADC1_GetFlagStatus(ADC1_FLAG_EOC));//等待转换完毕 ADC1_ClearFlag(ADC1_FLAG_EOC); //清除AD完毕标志位 ADVal = ADC1_GetConversionValue();//读AD值
标签:cpu content alignment scale version return stat tco extern
原文地址:http://www.cnblogs.com/clnchanpin/p/6740145.html