|
2黑币
/***********adc.c***********/
#include "adc.h"
#include "SysTick.h"
volatile u16 ADC_ConvertedValue;
void ADC1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
ADC_InitTypeDef ADC_InitStruct;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AIN
GPIO_Init(GPIOA,&GPIO_InitStruct);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1->DR;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
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(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div8)
ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;
ADC_InitStruct.ADC_NbrOfChannel=1;
ADC_InitStruct.ADC_ContinuousConvMode=ENABLE
ADC_InitStruct.ADC_ScanConvMode=DISABLE
ADC_InitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None
ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;
ADC_Init(ADC1,&ADC_InitStruct);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1,ENABLE);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_55Cycles5);
ADC_ResetCalibration(ADC1)
while(ADC_GetResetCalibrationStatus(ADC1))
ADC_StartCalibration(ADC1)
while(ADC_GetCalibrationStatus(ADC1))
ADC_SoftwareStartConvCmd(ADC1,ENABLE)
}
/*********************main.c*****************/
#include "system.h"
#include "SysTick.h"
#include "led.h"
#include "usart.h"
#include "adc.h"
extern __IO uint16_t ADC_ConvertedValue;
float ADC_ConvertedValueLocal;
int main(void)
{
u8 i=0;
SysTick_Init(72);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
LED_Init();
USART1_Init(9600);
ADC1_Init();
while(1)
{
i++;
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/4096*3.3
printf("êä3öADÖμ£o%d\r\n", ADC_ConvertedValue);
printf("êä3öμçÑ1%.2f V \r\n",ADC_ConvertedValueLocal);
if(i%20==0)
{
led1=!led1;
}
delay_ms(10);
}
}
|
|