新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > stm32f10x的AD配置

        stm32f10x的AD配置

        作者: 時間:2016-11-26 來源:網絡 收藏
        #include "stm32f10x_lib.h"

        #include"stdio.h"


        #define ADC1_DR_Address((u32)0x4001244C)
        int AD_value;
        static unsigned long ticks;
        unsigned char Clock1s;

        本文引用地址:http://www.104case.com/article/201611/321615.htm



        ADC_InitTypeDef ADC_InitStructure;
        DMA_InitTypeDef DMA_InitStructure;
        vu16 ADC_ConvertedValue;
        ErrorStatus HSEStartUpStatus;


        void RCC_Configuration(void);
        void GPIO_Configuration(void);

        void USART_Configuration1(void);
        void SetupClock (void);
        int fputc(int ch, FILE *f);

        int main(void)
        {


        RCC_Configuration();



        GPIO_Configuration();


        USART_Configuration1();

        DMA_DeInit(DMA1_Channel1);
        DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
        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);


        ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
        ADC_InitStructure.ADC_ScanConvMode = ENABLE;
        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_14, 1, ADC_SampleTime_55Cycles5);


        ADC_DMACmd(ADC1, ENABLE);


        ADC_Cmd(ADC1, ENABLE);


        ADC_ResetCalibration(ADC1);

        while(ADC_GetResetCalibrationStatus(ADC1));


        ADC_StartCalibration(ADC1);

        while(ADC_GetCalibrationStatus(ADC1));


        ADC_SoftwareStartConvCmd(ADC1, ENABLE);

        while(1)
        {
        AD_value=ADC_GetConversionValue(ADC1);
        if (ticks++ >= 900000)
        {
        ticks=0;
        Clock1s=1;
        }
        }
        }


        void RCC_Configuration(void)
        {

        RCC_DeInit();

        RCC_HSEConfig(RCC_HSE_ON);

        HSEStartUpStatus = RCC_WaitForHSEStartUp();
        if(HSEStartUpStatus == SUCCESS)
        {

        RCC_HCLKConfig(RCC_SYSCLK_Div1);

        RCC_PCLK2Config(RCC_HCLK_Div1);

        RCC_PCLK1Config(RCC_HCLK_Div2);

        RCC_ADCCLKConfig(RCC_PCLK2_Div4);

        FLASH_SetLatency(FLASH_Latency_2);

        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

        RCC_PLLCmd(ENABLE);

        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
        {
        }

        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        while(RCC_GetSYSCLKSource() != 0x08)
        {
        }
        }



        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);


        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);


        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
        }


        void GPIO_Configuration(void)
        {
        GPIO_InitTypeDef GPIO_InitStructure;


        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
        GPIO_Init(GPIOC, &GPIO_InitStructure);


        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);


        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        }


        void USART_Configuration1(void)
        {
        USART_InitTypeDef USART_InitStructure;
        USART_ClockInitTypeDefUSART_ClockInitStructure;


        USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
        USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
        USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
        USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

        USART_ClockInit(USART1, &USART_ClockInitStructure);

        USART_InitStructure.USART_BaudRate = 115200;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_Parity = USART_Parity_No ;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;


        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

        USART_Init(USART1, &USART_InitStructure);

        USART_Cmd(USART1, ENABLE);
        }

        int fputc(int ch, FILE *f)
        {
        #ifdef DBG_ITM

        if (DEMCR & TRCENA) {
        while (ITM_Port32(0) == 0);
        ITM_Port8(0) = ch;
        }
        #else

        USART_SendData(USART1, (unsigned char) ch);
        while (!(USART1->SR & USART_FLAG_TXE));
        #endif
        return (ch);
        }



        關鍵詞: stm32f10xAD配

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 南通市| 台山市| 江油市| 定边县| 南城县| 买车| 黑龙江省| 罗田县| 峨眉山市| 西丰县| 桓台县| 新余市| 绥阳县| 陕西省| 金坛市| 晋州市| 镇宁| 毕节市| 民县| 云霄县| 大安市| 昔阳县| 鸡泽县| 平果县| 嘉兴市| 肃北| 江油市| 本溪市| 金昌市| 开原市| 桐梓县| 鄂尔多斯市| 滁州市| 龙山县| 石渠县| 洛隆县| 申扎县| 仪陇县| 泸溪县| 平顶山市| 石棉县|