STM8S AD转换(一)

2014-11-23 21:25:52 · 作者: · 浏览: 19
//不说那么多了,直接上程序
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) /* R