#ifndef __ADC_H__ #define __ADC_H__ #include <rtthread.h> void rt_hw_adc_init(void); #endif
#include <rtthread.h> #include <stm32f10x.h> void rt_hw_adc_init(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AIN; GPIO_Init(GPIOC,&GPIO_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式 ADC_InitStructure.ADC_ScanConvMode =DISABLE; //连续多通道模式 ADC_InitStructure.ADC_ContinuousConvMode =ENABLE; //连续转换 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换不受外界决定 ADC_InitStructure.ADC_DataAlign =ADC_DataAlign_Right; //右对齐 ADC_InitStructure.ADC_NbrOfChannel =1; //扫描通道数 ADC_Init(ADC1,&ADC_InitStructure); ADC_RegularChannelConfig(ADC1,ADC_Channel_10, 1,ADC_SampleTime_13Cycles5); ADC_Cmd (ADC1,ENABLE); //使能或者失能指定的ADC ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); rt_kprintf("ACD init.....OK\n\r"); } #ifdef RT_USING_FINSH #include <finsh.h> void adc(void) { rt_int32_t adc; rt_kprintf("hello here is a ADC test\n\r"); ADC_SoftwareStartConvCmd(ADC1,ENABLE);//使能或者失能指定的ADC的软件转换启动功能 while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)==RESET); //检查制定ADC标志位置1与否 ADC_FLAG_EOC 转换结束标志位 adc = ADC_GetConversionValue(ADC1); rt_kprintf("ADC: 0X%xV\n\r", adc); rt_kprintf("ADC: 0D%dV\n\r", adc); rt_kprintf("ADC: 0X%xV\n\r", adc*3230); rt_kprintf("ADC: 0D%dV\n\r", adc*3230); rt_kprintf("ADC: 0X%xV\n\r", adc*3230/4095); rt_kprintf("ADC: 0D%dV\n\r", adc*3230/4095); rt_thread_delay( RT_TICK_PER_SECOND/2 ); } FINSH_FUNCTION_EXPORT(adc, start adc function test.) #endif