打印
[STM32F0]

STM32F0 adc采集

[复制链接]
1139|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ofsummer|  楼主 | 2016-4-24 10:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
代码修改自库函数例程ADC_LowPower和IO_Toggle,下面是代码:
#define BSRR_VAL        0x0300

ADC_InitTypeDef          ADC_InitStructure;
GPIO_InitTypeDef         GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef        TIM_OCInitStructure;

void delay_ms(int i)
{
  int s;
  while(i--)
{
  s=8000;
  while(s--);
}
}

int main(void)
{
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* TIM3 Configuration*/
  TIM_DeInit(TIM3);
  
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  
  TIM_OCStructInit(&TIM_OCInitStructure);
   
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 0xFF;
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  
  /* TIM3 TRGO selection */
  TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update);

  ADC_DeInit(ADC1);
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising;   
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward;
  ADC_Init(ADC1, &ADC_InitStructure);
  ADC_ChannelConfig(ADC1, ADC_Channel_11 , ADC_SampleTime_28_5Cycles);   
  ADC_GetCalibrationFactor(ADC1);
  ADC_WaitModeCmd(ADC1, ENABLE);
  ADC_AutoPowerOffCmd(ADC1, ENABLE);
  ADC_Cmd(ADC1, ENABLE);     
  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN));

TIM_Cmd(TIM3, ENABLE);

  ADC_StartOfConversion(ADC1);

  while (1)
  {
    /* Get ADC1 converted data */
    ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
   
    /* Compute the voltage */
    ADC1ConvertedVoltage = (ADC1ConvertedValue * 3300)/0xFFF;
    /* Set PC8 and PC9 */
    GPIOC->BSRR = BSRR_VAL;
    delay_ms((int)(ADC1ConvertedVoltage/5));
    /* Reset PC8 and PC9 */
    GPIOC->BRR = BSRR_VAL;
    delay_ms((int)(ADC1ConvertedVoltage/5));
  }
}



沙发
orangebanana| | 2016-4-24 20:40 | 只看该作者
这个用的是dma方式吧,没见到adc采集的那块

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

71

帖子

1

粉丝