void adcInit( void ) { // ADC Clock Div. RCC_ADCCLKConfig( RCC_PCLK2_Div8 ); // Turn ADC and appropriate GPIO's clocks on. RCC_APB2PeriphClockCmd( ADC_CLK | GPIO_ADC_CLK, ENABLE ); // Setup GPIO in type. GPIO_InitTypeDef init; init.GPIO_Pin = ADC_PIN; init.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init( GPIO_ADC, &init ); // Setp ADC. ADC_DeInit( ADC1 ); ADC_InitTypeDef adcInit; ADC_StructInit( &adcInit ); adcInit.ADC_Mode = ADC_Mode_Independent; adcInit.ADC_ContinuousConvMode = DISABLE; adcInit.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; adcInit.ADC_DataAlign = ADC_DataAlign_Right; adcInit.ADC_NbrOfChannel = 1; // Now do the setup ADC_Init( ADC1, &adcInit ); // Enable ADC1 ADC_Cmd( ADC1, ENABLE ); // Enable ADC1 reset calibaration register ADC_ResetCalibration( ADC1 ); // Check the end of ADC1 reset calibration register while( ADC_GetResetCalibrationStatus( ADC1 ) ) ; // Start ADC1 calibaration ADC_StartCalibration( ADC1 ); // Check the end of ADC1 calibration while( ADC_GetCalibrationStatus( ADC1 ) ) ; }
void RCC_Config(void) { RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); //Включим внешний генератор while (SUCCESS != RCC_WaitForHSEStartUp()); //Подождем запуска генератора RCC_PLLCmd(DISABLE); RCC_PLLConfig(RCC_PLLSource_PREDIV1,RCC_PLLMul_12); //Вход ПЛЛ внешний генератор умножим на 12 RCC_PLLCmd(ENABLE); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Системное тактирование от ПЛЛ RCC_HCLKConfig(RCC_SYSCLK_Div1); //Тактирование AHB с делением 1 RCC_PCLKConfig(RCC_HCLK_Div8); //Тактирование переферии с делением 8 RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div2); //Тактирование АЦП RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); //Тактируем шину I2C от системного тактирования RCC_USARTCLKConfig(RCC_USART1CLK_PCLK); //Тактируем UART от шини переферии RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA + RCC_AHBPeriph_GPIOB,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 + RCC_APB2Periph_USART1 + RCC_APB2Periph_TIM1 + RCC_APB2Periph_TIM15, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); #ifdef MCO_ENABLE GPIOA_Struct_init.GPIO_Pin = GPIO_Pin_8; GPIOA_Struct_init.GPIO_Mode = GPIO_Mode_AF; GPIOA_Struct_init.GPIO_Speed = GPIO_Speed_Level_2; GPIOA_Struct_init.GPIO_OType = GPIO_OType_PP; GPIO_Init(GPIOA,&GPIOA_Struct_init); RCC_MCOConfig(RCC_MCOSource_SYSCLK); while (1); #else RCC_MCOConfig(RCC_MCOSource_NoClock); #endif /*MCO_ENABLE */ };
void SysInitRCC( void ) { // System clock is set by the SystemInit-function, called by the startup-file before main is called. // So, when correctly configured in system_stm32f10x.c, the system runs at HSE 8 MHz, using PLL to create 72 MHz SystemClock // ADCCLK = PCLK2/<divider> // PCLK2 runs @72 MHz, ADCCLK can be max. 14 MHz RCC_ADCCLKConfig(RCC_PCLK2_Div6); /* Enable AHB periphs */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); /* Enable APB1 periphs */ RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE ); /* Enable APB2 periphs */ RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD, ENABLE ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE ); // for remapping purposes }
/** * @brief Initialize an ADC peripheral. * * Initializes the RCC clock line for the given peripheral. Resets * ADC device registers. * * @param dev ADC peripheral to initialize */ void adc_init(const adc_dev *dev) { /* Enable The HSI */ RCC_HSICmd(ENABLE); /* Check that HSI oscillator is ready */ while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); dev->clkcmd(dev->clk, ENABLE); ADC_DeInit(dev->ADCx); RCC_ADCCLKConfig(RCC_PCLK2_Div6); /* ADCx Init ****************************************************************/ ADC_InitTypeDef ADC_InitStructure; ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(dev->ADCx, &ADC_InitStructure); }
/* * ===================================================================================== * * Filename: thermcouple.c * * Description: * * Version: 1.0 * Created: 2013/3/23 20:10:37 * Revision: none * Compiler: gcc * * Author: RY (good lucky), [email protected] * Organization: * * ===================================================================================== */ #include "thermcouple.h" uint16_t oldTemparture; void thermcoupleIOinit(void)/*{{{*/ { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //GPIOB时钟 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //口线翻转速度为50MHz GPIO_InitStructure.GPIO_Pin = ThermcoupleSck_Pin; GPIO_Init(ThermcoupleSck_Port, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = ThermcoupleCs_Pin; GPIO_Init(ThermcoupleCs_Port, &GPIO_InitStructure); // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = ThermcoupleDo_Pin; GPIO_Init(ThermcoupleDo_Port, &GPIO_InitStructure); oldTemparture=25; }/*}}}*/ uint16_t thermcoupleReadTemp(void)/*{{{*/ { uint16_t data=0; uint8_t i,result; bool badTemperature=false; bool haveData=false; GPIO_ResetBits(ThermcoupleSck_Port,ThermcoupleSck_Pin); GPIO_ResetBits(ThermcoupleCs_Port,ThermcoupleCs_Pin); delay_us(20); for(i=0;i<16;i++) { GPIO_SetBits(ThermcoupleSck_Port,ThermcoupleSck_Pin); delay_us(1); if(i>=1 && i<11) { data=data<<1; if(GPIO_ReadInputDataBit(ThermcoupleDo_Port,ThermcoupleDo_Pin)) { data=data|0x01; haveData=true; } } if(i==13) { if(GPIO_ReadInputDataBit(ThermcoupleDo_Port,ThermcoupleDo_Pin)) badTemperature=true; } delay_us(1); GPIO_ResetBits(ThermcoupleSck_Port,ThermcoupleSck_Pin); delay_us(2); } GPIO_SetBits(ThermcoupleCs_Port,ThermcoupleCs_Pin); delay_us(20); GPIO_ResetBits(ThermcoupleSck_Port,ThermcoupleSck_Pin); if(badTemperature) { data=oldTemparture; } else { } return data; }/*}}}*/ void thermistorIOinit(void)/*{{{*/ { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //GPIOC时钟 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //口线翻转速度为50MHz GPIO_InitStructure.GPIO_Pin = HeaterBoardTherm_Pin; GPIO_Init(HeaterBoardTherm_Port,&GPIO_InitStructure); RCC_ADCCLKConfig(RCC_CFGR_ADCPRE_DIV6);//ADC时钟PLCK2的6分频 12M。ADC最大时钟不能超过14M! RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//工作在独立模式 ADC_InitStructure.ADC_ScanConvMode = ENABLE; //使能扫描 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//ADC转换工作在连续模式 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//有软件控制转换 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//转换数据右对齐 ADC_InitStructure.ADC_NbrOfChannel = 1;//转换通道1个 ADC_Init(ADC1, &ADC_InitStructure); //初始化ADC ADC_RegularChannelConfig(ADC1, ADC_Channel_11,1, ADC_SampleTime_239Cycles5);//选择ADC1 通道11(因为是ADC_11),次序1,采样239.5周期 ADC_Cmd(ADC1, ENABLE);//使能ADC1 ADC_ResetCalibration(ADC1); //重置ADC1校准寄存器 while(ADC_GetResetCalibrationStatus(ADC1));//等待ADC1校准重置完成 ADC_StartCalibration(ADC1);//开始ADC1校准 while(ADC_GetCalibrationStatus(ADC1));//等待ADC1校准完成 ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能ADC1软件开始转换 }/*}}}*/
void ADC_Configuration(void) { ADC_InitTypeDef ADC_InitStructure; /* PCLK2 is the APB2 clock */ /* ADCCLK = PCLK2/6 = 72/6 = 12MHz*/ RCC_ADCCLKConfig(RCC_PCLK2_Div6); /* Enable ADC1 clock so that we can talk to it */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Put everything back to power-on defaults */ ADC_DeInit(ADC1); /* ADC1 Configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; /* ADC1 and ADC2 operate independently */ ADC_InitStructure.ADC_ScanConvMode = DISABLE; /* Disable the scan conversion so we do one at a time */ ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; /* Don't do contimuous conversions - do them on demand */ ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; /* Start conversin by software, not an external trigger */ ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; /* Conversions are 12 bit - put them in the lower 12 bits of the result */ ADC_InitStructure.ADC_NbrOfChannel = 1; /* Say how many channels would be used by the sequencer */ ADC_Init(ADC1, &ADC_InitStructure); /* Now do the setup */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 */ /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); }
void BSP_Init (void) { BSP_IntInit(); RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); RCC_WaitForHSEStartUp(); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); RCC_ADCCLKConfig(RCC_PCLK2_Div6); 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) { ; } BSP_ADC_Init(); /* Initialize the I/Os for the ADC controls. */ BSP_LED_Init(); /* Initialize the I/Os for the LED controls. */ BSP_PB_Init(); /* Initialize the I/Os for the PB control. */ BSP_Joystick_Init(); /* Initialize the I/Os for the Joystick control. */ STM3210E_LCD_Init(); LCD_Clear(0xFFFF); }
static void adc1_mode_config( void) { DMA_InitTypeDef dma_init_structure; ADC_InitTypeDef adc_init_structure; /* 复位 ADC1,将与DMA有关的寄存器设我初始值; */ DMA_DeInit( DMA1_Channel1); /*定义DMA外设基地址, 这里的ADC1_DR_Address 是用户自己定义的,即为存放转换结果的寄存器; ,他的作用就是告诉DMA取数就到ADC1_DR_Address 这里来取。;*/ dma_init_structure.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS; /*定义内存基地址,即告诉DMA要将从AD中取来的数放到ADC_ConvertedValue中 ;*/ dma_init_structure.DMA_MemoryBaseAddr = ( uint32_t)&s_adc1_converted_value; /*定义AD外设作为数据传输的来源,即告诉DMA是将AD中的数据取出放到内存中,不能反过来;*/ dma_init_structure.DMA_DIR = DMA_DIR_PeripheralSRC; /*指定DMA通道的DMA缓存的大小,即告诉DMA开辟几个内存空间,由于我们只取通道10的AD数据所以只需开辟一个内存空间;*/ dma_init_structure.DMA_BufferSize = 1; //dma_init_structure.DMA_BufferSize = 2; /*设定寄存器地址固定,即告诉DMA,只从固定的一个地方取数;*/ dma_init_structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; /*设定内存地址固定,即每次DMA,,只将数搬到固定的内存中;*/ dma_init_structure.DMA_MemoryInc = DMA_MemoryInc_Disable; /*设定内存地址递加,即每次DMA都是将该外设寄存器中的值传到两个内存空间中;*/ //DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; /*设定外设数据宽度,即告诉DMA要取的数的大小,半字(16位);*/ dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; /*设定内存的的宽度;*/ dma_init_structure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; /*设定DMA工作再循环缓存模式,即告诉DMA要不停的搬运,不能偷懒;*/ dma_init_structure.DMA_Mode = DMA_Mode_Circular; /*设定DMA选定的通道软件优先级;*/ dma_init_structure.DMA_Priority = DMA_Priority_High; dma_init_structure.DMA_M2M = DMA_M2M_Disable; DMA_Init( DMA1_Channel1, &dma_init_structure); DMA_Cmd( DMA1_Channel1, ENABLE); /* ADC1 configuration */ /*设置ADC工作在独立模式;*/ adc_init_structure.ADC_Mode = ADC_Mode_Independent; /*规定AD转换工作在扫描模式,即对多个通道采样;*/ //ADC_InitStructure.ADC_ScanConvMode = ENABLE /*规定AD转换工作在单次模式,即对一个通道采样;*/ adc_init_structure.ADC_ScanConvMode = DISABLE; /*设定AD转化在连续模式;*/ adc_init_structure.ADC_ContinuousConvMode = ENABLE; /*不使用外部促发转换;*/ adc_init_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; /*采集的数据在寄存器中以右对齐的方式存放;*/ adc_init_structure.ADC_DataAlign = ADC_DataAlign_Right; /*设定要转换的AD通道数目;*/ adc_init_structure.ADC_NbrOfChannel = 1; //adc_init_structure.ADC_NbrOfChannel = 2; ADC_Init( ADC1, &adc_init_structure); /*配置ADC时钟,为PCLK2的8分频,即9MHz;*/ RCC_ADCCLKConfig( RCC_PCLK2_Div8); /*配置ADC1的通道11为55.5个采样周期; */ ADC_RegularChannelConfig( ADC1,ADC_Channel_10,1,ADC_SampleTime_55Cycles5); //ADC_RegularChannelConfig( ADC1,ADC_Channel_11,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转换; */ ADC_SoftwareStartConvCmd( ADC1, ENABLE); }
/** * @brief Configures the different system clocks. * @param None * @retval None */ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* ADCCLK = PCLK2/4 */ RCC_ADCCLKConfig(RCC_PCLK2_Div4); #ifndef STM32F10X_CL /* PLLCLK = 8MHz * 7 = 56 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7); #else /* Configure PLLs *********************************************************/ /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */ RCC_PREDIV2Config(RCC_PREDIV2_Div5); RCC_PLL2Config(RCC_PLL2Mul_8); /* Enable PLL2 */ RCC_PLL2Cmd(ENABLE); /* Wait till PLL2 is ready */ while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET) {} /* PLL configuration: PLLCLK = (PLL2 / 5) * 7 = 56 MHz */ RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5); RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_7); #endif /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } /* Enable peripheral clocks --------------------------------------------------*/ /* Enable ADC1 and GPIO_LED clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); }
/** * @brief Configures the different system clocks. * @param None * @retval None */ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* ADCCLK = PCLK2/4 */ RCC_ADCCLKConfig(RCC_PCLK2_Div4); #ifndef STM32F10X_CL /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); #else /* Configure PLLs *********************************************************/ /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */ RCC_PREDIV2Config(RCC_PREDIV2_Div5); RCC_PLL2Config(RCC_PLL2Mul_8); /* Enable PLL2 */ RCC_PLL2Cmd(ENABLE); /* Wait till PLL2 is ready */ while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET) {} /* PLL configuration: PLLCLK = (PLL2 / 5) * 9 = 72 MHz */ RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5); RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9); /* PPL3 configuration: PLL3CLK = (HSE / 5) * 11 = PLL3_VCO = 110 MHz */ RCC_PLL3Config(RCC_PLL3Mul_11); /* Enable PLL3 */ RCC_PLL3Cmd(ENABLE); /* Wait till PLL3 is ready */ while (RCC_GetFlagStatus(RCC_FLAG_PLL3RDY) == RESET) {} /* Configure I2S clock source: On Connectivity-Line Devices, the I2S can be clocked by PLL3 VCO instead of SYS_CLK in order to guarantee higher precision */ RCC_I2S3CLKConfig(RCC_I2S3CLKSource_PLL3_VCO); RCC_I2S2CLKConfig(RCC_I2S2CLKSource_PLL3_VCO); #endif /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } /* Enable peripheral clocks --------------------------------------------------*/ /* GPIOA, GPIOB and AFIO clocks enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); #ifdef USE_STM3210C_EVAL /* GPIOC Clock enable (for the SPI3 remapped pins) */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE); #endif /* USE_STM3210C_EVAL */ /* SPI2 and SPI3 clocks enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2 | RCC_APB1Periph_SPI3, ENABLE); }
void adcInit(drv_adc_config_t *init) { #if defined(CJMCU) || defined(CC3D) UNUSED(init); #endif uint8_t i; uint8_t configuredAdcChannels = 0; memset(&adcConfig, 0, sizeof(adcConfig)); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; #ifdef ADC1_GPIO if (init->channelMask & ADC_CHANNEL1_ENABLE) { GPIO_InitStructure.GPIO_Pin = ADC1_GPIO_PIN; GPIO_Init(ADC1_GPIO, &GPIO_InitStructure); adcConfig[ADC_CHANNEL_1].adcChannel = ADC1_CHANNEL; adcConfig[ADC_CHANNEL_1].dmaIndex = configuredAdcChannels++; adcConfig[ADC_CHANNEL_1].enabled = true; adcConfig[ADC_CHANNEL_1].sampleTime = ADC_SampleTime_239Cycles5; } #endif #ifdef ADC2_GPIO if (init->channelMask & ADC_CHANNEL2_ENABLE) { GPIO_InitStructure.GPIO_Pin = ADC2_GPIO_PIN; GPIO_Init(ADC2_GPIO, &GPIO_InitStructure); adcConfig[ADC_CHANNEL_2].adcChannel = ADC2_CHANNEL; adcConfig[ADC_CHANNEL_2].dmaIndex = configuredAdcChannels++; adcConfig[ADC_CHANNEL_2].enabled = true; adcConfig[ADC_CHANNEL_2].sampleTime = ADC_SampleTime_239Cycles5; } #endif #ifdef ADC3_GPIO if (init->channelMask & ADC_CHANNEL3_ENABLE) { GPIO_InitStructure.GPIO_Pin = ADC3_GPIO_PIN; GPIO_Init(ADC3_GPIO, &GPIO_InitStructure); adcConfig[ADC_CHANNEL_3].adcChannel = ADC3_CHANNEL; adcConfig[ADC_CHANNEL_3].dmaIndex = configuredAdcChannels++; adcConfig[ADC_CHANNEL_3].enabled = true; adcConfig[ADC_CHANNEL_3].sampleTime = ADC_SampleTime_239Cycles5; } #endif #ifdef ADC4_GPIO if (init->channelMask & ADC_CHANNEL4_ENABLE) { GPIO_InitStructure.GPIO_Pin = ADC4_GPIO_PIN; GPIO_Init(ADC4_GPIO, &GPIO_InitStructure); adcConfig[ADC_CHANNEL_4].adcChannel = ADC4_CHANNEL; adcConfig[ADC_CHANNEL_4].dmaIndex = configuredAdcChannels++; adcConfig[ADC_CHANNEL_4].enabled = true; adcConfig[ADC_CHANNEL_4].sampleTime = ADC_SampleTime_239Cycles5; } #endif RCC_ADCCLKConfig(RCC_PCLK2_Div8); // 9MHz from 72MHz APB2 clock(HSE), 8MHz from 64MHz (HSI) RCC_AHBPeriphClockCmd(ADC_AHB_PERIPHERAL, ENABLE); RCC_APB2PeriphClockCmd(ADC_ABP2_PERIPHERAL, ENABLE); // FIXME ADC driver assumes all the GPIO was already placed in 'AIN' mode DMA_DeInit(ADC_DMA_CHANNEL); DMA_InitTypeDef DMA_InitStructure; DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC_INSTANCE->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = configuredAdcChannels; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = configuredAdcChannels > 1 ? DMA_MemoryInc_Enable : 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(ADC_DMA_CHANNEL, &DMA_InitStructure); DMA_Cmd(ADC_DMA_CHANNEL, ENABLE); ADC_InitTypeDef ADC_InitStructure; ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = configuredAdcChannels > 1 ? ENABLE : DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = configuredAdcChannels; ADC_Init(ADC_INSTANCE, &ADC_InitStructure); uint8_t rank = 1; for (i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcConfig[i].enabled) { continue; } ADC_RegularChannelConfig(ADC_INSTANCE, adcConfig[i].adcChannel, rank++, adcConfig[i].sampleTime); } ADC_DMACmd(ADC_INSTANCE, ENABLE); ADC_Cmd(ADC_INSTANCE, ENABLE); ADC_ResetCalibration(ADC_INSTANCE); while(ADC_GetResetCalibrationStatus(ADC_INSTANCE)); ADC_StartCalibration(ADC_INSTANCE); while(ADC_GetCalibrationStatus(ADC_INSTANCE)); ADC_SoftwareStartConvCmd(ADC_INSTANCE, ENABLE); }
void BSP_Init (void) { USART_InitTypeDef USART_InitStructure; /* USARTx configuration ------------------------------------------------------*/ /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 9600; 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; RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); RCC_WaitForHSEStartUp(); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); RCC_ADCCLKConfig(RCC_PCLK2_Div6); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 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) { ; } /* SCU_AHBPeriphClockConfig(__VIC,ENABLE); //VIC_DeInit(); VIC_Config(UART1_ITLine, VIC_IRQ, 15); VIC_ITCmd(UART1_ITLine, ENABLE); */ BSP_ADC_Init(); /* Initialize the I/Os for the ADC controls. */ BSP_LED_Init(); /* Initialize the I/Os for the LED controls. */ BSP_PB_Init(); /* Initialize the I/Os for the PB control. */ // BSP_Joystick_Init(); /* Initialize the I/Os for the Joystick control. */ GPIO_Configuration(); USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1, ENABLE); UART2_int(); }
void SampleChannel_Init() { NVIC_InitTypeDef NVIC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA| RCC_APB2Periph_AFIO, ENABLE); GPIO_InitStructure.GPIO_Pin = ADC_CHANNEL10_PIN | ADC_CHANNEL11_PIN | ADC_CHANNEL12_PIN | ADC_CHANNEL14_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //端口模式为模拟输入方式 GPIO_Init(ADC_PORT, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); TIM_DeInit(TIM8); TIM_TimeBaseStructure.TIM_Period = 1000000 / 1000 - 1; TIM_TimeBaseStructure.TIM_Prescaler = 71; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); TIM_SelectOutputTrigger(TIM8, TIM_TRGOSource_Update); RCC_ADCCLKConfig(RCC_PCLK2_Div6); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_ADC1_ETRGREG, ENABLE); ADC_DeInit(ADC1); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_Ext_IT11_TIM8_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 4; ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_71Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 2, ADC_SampleTime_71Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 3, ADC_SampleTime_71Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 4, ADC_SampleTime_71Cycles5); ADC_DiscModeChannelCountConfig(ADC1, 1); ADC_DiscModeCmd(ADC1, ENABLE); ADC_DMACmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); ADC_ResetCalibration(ADC1); while (ADC_GetResetCalibrationStatus(ADC1)) ; ADC_StartCalibration(ADC1); while (ADC_GetCalibrationStatus(ADC1)) ; ADC_ExternalTrigConvCmd(ADC1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (u32) (&ADCBuff); DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = ADCNUM; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 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_Medium; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA_ClearFlag(DMA1_IT_TC1 | DMA1_IT_HT1); DMA_ITConfig(DMA1_Channel1, DMA_IT_TC | DMA_IT_HT, ENABLE); DMA_Cmd(DMA1_Channel1, ENABLE); }
/* * @brief Initialize the ADC peripheral. */ void ADC_DMA_Init() { //Using "Dual Slow Interleaved ADC Mode" to achieve higher input impedance ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; // ADCCLK = PCLK2/6 = 72/6 = 12MHz RCC_ADCCLKConfig(RCC_PCLK2_Div6); // Enable DMA1 clock RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // Enable ADC1 and ADC2 clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE); // DMA1 channel1 configuration DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADC_DualConvertedValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = ADC_DMA_BUFFERSIZE; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); // ADC1 configuration ADC_InitStructure.ADC_Mode = ADC_Mode_SlowInterl; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); // ADC2 configuration ADC_InitStructure.ADC_Mode = ADC_Mode_SlowInterl; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC2, &ADC_InitStructure); // Enable ADC1 ADC_Cmd(ADC1, ENABLE); // Enable ADC1 reset calibration register ADC_ResetCalibration(ADC1); // Check the end of ADC1 reset calibration register while(ADC_GetResetCalibrationStatus(ADC1)); // Start ADC1 calibration ADC_StartCalibration(ADC1); // Check the end of ADC1 calibration while(ADC_GetCalibrationStatus(ADC1)); // Enable ADC2 ADC_Cmd(ADC2, ENABLE); // Enable ADC2 reset calibration register ADC_ResetCalibration(ADC2); // Check the end of ADC2 reset calibration register while(ADC_GetResetCalibrationStatus(ADC2)); // Start ADC2 calibration ADC_StartCalibration(ADC2); // Check the end of ADC2 calibration while(ADC_GetCalibrationStatus(ADC2)); }
/* 函数名:ADC1_Mode_Config * 描述 :配置ADC1的工作模式为MDA模式 * 输入 : 无 * 输出 :无 * 调用 :内部调用 */ static void ADC1_Mode_Config(void) { DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; /* DMA channel1 configuration */ DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; // 外设基地址 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; // AD转换值所存放的内存基地址 (就是给个地址) DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // 外设作为数据传输的来源 DMA_InitStructure.DMA_BufferSize = 1; // 定义指定DMA通道 DMA缓存的大小 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // 外设地址寄存器不变 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; // 内存地址寄存器不变 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; // 数据宽度为16位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // 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); /* Enable DMA channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); //ENABLE她 /* ADC1 configuration */ 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; //Right ADC_InitStructure.ADC_NbrOfChannel = 1; //仅一个通道转换 ADC_Init(ADC1, &ADC_InitStructure); /*配置ADC时钟,为PCLK2的8分频,即9Hz*/ RCC_ADCCLKConfig(RCC_PCLK2_Div8); /* ADC1 regular channel16 configuration */ //设置采样通道IN16, 设置采样时间 ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_239Cycles5); //使能温度传感器和内部参考电压 ADC_TempSensorVrefintCmd(ENABLE); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); }
/******************************************************************************************************** * 函 数 名 : RCC_Configuration() * 描 述 : 系统、外围设备时钟初始化 * 输入参数 : None. * 输出参数 : None. * 返 回 : None. ********************************************************************************************************/ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ ErrorStatus HSEStartUpStatus; RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); RCC_LSEConfig(RCC_LSE_OFF); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/4 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* PLLCLK = 8MHz * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* ADCCLK = PCLK2/4 */ RCC_ADCCLKConfig(RCC_PCLK2_Div8); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } /* GPIO clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |RCC_APB2Periph_GPIOF|RCC_APB2Periph_GPIOG, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); /* Enable AFIO clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* TIM3 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* TIM4 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); /* TIM5 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); /* TIM6 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); /* TIM7 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE); /* USART1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); /* USART2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); /* USART3 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); /* Enable I2C1 clocks */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* Enable ADC2 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); }
//---------------------------------------- static void Task_Led2(void* p_arg)//负责采集电压 { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; INT16U shu[4]={0}; INT16U *s; vu16 ADC_ConvertedValue; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE); /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); /* DMA channel1 configuration ----------------------------------------------*/ 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 = 2; 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); /* Enable DMA channel1 */ //DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式 CR1 ADC_InitStructure.ADC_ScanConvMode = ENABLE; //扫描模式使能 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续转换模式使能 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //启动转换的外部事件--无 CR2 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //转换后的数据右对齐 ADC_InitStructure.ADC_NbrOfChannel = 1; //转换的通道数为1 ADC_Init(ADC1, &ADC_InitStructure); /* ADCCLK = PCLK2/4 */ RCC_ADCCLKConfig(RCC_PCLK2_Div6); /* ADC1 regular channel14 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5); //通道10采样时间 //ADC_DMACmd(ADC1, ENABLE); //允许ADC1进行DMA传送 /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); //使能ADC1 /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); //允许ADC1复位校准寄存器 /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); //检测校准寄存器是否复位完成 /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); //启动ADC1 校准 /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); //检测校准是否完成 /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); //软件触发启动ADC1转换 (void) p_arg; while (1) { //while(0 == ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)); if(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)) { shu[2]=shu[1]; shu[1]=shu[0]; shu[0]=ADC_GetConversionValue(ADC1);//获得ADC1的转换数据 } //shu[3]=0xff; //USART_OUT(USART1,"ADC_%d!\r\n",shu[0]); s=&shu[0]; //取出shu中的地址付给指针s OSMboxPost(SeMbox,s); OSTimeDlyHMSM(0, 0, 0, 500); } }
void adcInit(const adcConfig_t *config) { uint8_t configuredAdcChannels = 0; memset(&adcOperatingConfig, 0, sizeof(adcOperatingConfig)); if (config->vbat.enabled) { adcOperatingConfig[ADC_BATTERY].tag = config->vbat.ioTag; } if (config->rssi.enabled) { adcOperatingConfig[ADC_RSSI].tag = config->rssi.ioTag; //RSSI_ADC_CHANNEL; } if (config->external1.enabled) { adcOperatingConfig[ADC_EXTERNAL1].tag = config->external1.ioTag; //EXTERNAL1_ADC_CHANNEL; } if (config->current.enabled) { adcOperatingConfig[ADC_CURRENT].tag = config->current.ioTag; //CURRENT_METER_ADC_CHANNEL; } ADCDevice device = adcDeviceByInstance(ADC_INSTANCE); if (device == ADCINVALID) return; const adcDevice_t adc = adcHardware[device]; bool adcActive = false; for (int i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcOperatingConfig[i].tag) continue; adcActive = true; IOInit(IOGetByTag(adcOperatingConfig[i].tag), OWNER_ADC_BATT + i, 0); IOConfigGPIO(IOGetByTag(adcOperatingConfig[i].tag), IO_CONFIG(GPIO_Mode_AIN, 0)); adcOperatingConfig[i].adcChannel = adcChannelByTag(adcOperatingConfig[i].tag); adcOperatingConfig[i].dmaIndex = configuredAdcChannels++; adcOperatingConfig[i].sampleTime = ADC_SampleTime_239Cycles5; adcOperatingConfig[i].enabled = true; } if (!adcActive) { return; } RCC_ADCCLKConfig(RCC_PCLK2_Div8); // 9MHz from 72MHz APB2 clock(HSE), 8MHz from 64MHz (HSI) RCC_ClockCmd(adc.rccADC, ENABLE); dmaInit(dmaGetIdentifier(adc.DMAy_Channelx), OWNER_ADC, 0); DMA_DeInit(adc.DMAy_Channelx); DMA_InitTypeDef DMA_InitStructure; DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&adc.ADCx->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = configuredAdcChannels; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = configuredAdcChannels > 1 ? DMA_MemoryInc_Enable : 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(adc.DMAy_Channelx, &DMA_InitStructure); DMA_Cmd(adc.DMAy_Channelx, ENABLE); ADC_InitTypeDef ADC_InitStructure; ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = configuredAdcChannels > 1 ? ENABLE : DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = configuredAdcChannels; ADC_Init(adc.ADCx, &ADC_InitStructure); uint8_t rank = 1; for (int i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcOperatingConfig[i].enabled) { continue; } ADC_RegularChannelConfig(adc.ADCx, adcOperatingConfig[i].adcChannel, rank++, adcOperatingConfig[i].sampleTime); } ADC_DMACmd(adc.ADCx, ENABLE); ADC_Cmd(adc.ADCx, ENABLE); ADC_ResetCalibration(adc.ADCx); while (ADC_GetResetCalibrationStatus(adc.ADCx)); ADC_StartCalibration(adc.ADCx); while (ADC_GetCalibrationStatus(adc.ADCx)); ADC_SoftwareStartConvCmd(adc.ADCx, ENABLE); }
void adcInit(){ memset(measLongTermBuffer, 0, sizeof(measLongTermBuffer)); memset(measLongTermMean, 0, sizeof(measLongTermMean)); measMeanPos = 0; /* ADCCLK = PCLK2/4 */ RCC_ADCCLKConfig(RCC_PCLK2_Div4); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef gpioInit; gpioInit.GPIO_Pin = sharpPins[0] | sharpPins[1] | sharpPins[2] | sharpPins[3]; gpioInit.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(SHARP_PORT, &gpioInit); gpioInit.GPIO_Pin = measPins[0] | measPins[1] | measPins[2]; GPIO_Init(MEAS_PORT, &gpioInit); NVIC_InitTypeDef nvicInit; nvicInit.NVIC_IRQChannel = ADC1_2_IRQn; nvicInit.NVIC_IRQChannelPreemptionPriority = 0; nvicInit.NVIC_IRQChannelSubPriority = 0; nvicInit.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvicInit); DMA_InitTypeDef dmaInit; dmaInit.DMA_PeripheralBaseAddr = (uint32_t)&(ADC1->DR); dmaInit.DMA_MemoryBaseAddr = (uint32_t)adcBuffer; dmaInit.DMA_DIR = DMA_DIR_PeripheralSRC; dmaInit.DMA_BufferSize = ADC_BUFFER; dmaInit.DMA_PeripheralInc = DMA_PeripheralInc_Disable; dmaInit.DMA_MemoryInc = DMA_MemoryInc_Enable; dmaInit.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; dmaInit.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; dmaInit.DMA_Mode = DMA_Mode_Circular; dmaInit.DMA_Priority = DMA_Priority_Low; dmaInit.DMA_M2M = DMA_M2M_Disable; DMA_Init(ADC_DMA_CHANNEL, &dmaInit); DMA_Cmd(ADC_DMA_CHANNEL, ENABLE); ADC_InitTypeDef adcInit; adcInit.ADC_Mode = ADC_Mode_Independent; adcInit.ADC_ScanConvMode = ENABLE; adcInit.ADC_ContinuousConvMode = DISABLE; adcInit.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; adcInit.ADC_DataAlign = ADC_DataAlign_Right; adcInit.ADC_NbrOfChannel = 7; ADC_Init(ADC1, &adcInit); ADC_RegularChannelConfig(ADC1, sharpChannels[0], sharpRanks[0], ADC_SampleTime_28Cycles5); ADC_RegularChannelConfig(ADC1, sharpChannels[1], sharpRanks[1], ADC_SampleTime_28Cycles5); ADC_RegularChannelConfig(ADC1, sharpChannels[2], sharpRanks[2], ADC_SampleTime_28Cycles5); ADC_RegularChannelConfig(ADC1, sharpChannels[3], sharpRanks[3], ADC_SampleTime_28Cycles5); ADC_RegularChannelConfig(ADC1, measChannels[0], measRanks[0], ADC_SampleTime_28Cycles5); ADC_RegularChannelConfig(ADC1, measChannels[1], measRanks[1], ADC_SampleTime_28Cycles5); ADC_RegularChannelConfig(ADC1, measChannels[2], measRanks[2], ADC_SampleTime_28Cycles5); ADC_DMACmd(ADC1, ENABLE); ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE); ADC_Cmd(ADC1, ENABLE); ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ //ADC_SoftwareStartConvCmd(ADC1, ENABLE); }
void analogin_init(analogin_t *obj, PinName pin) { ADC_TypeDef *adc; ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; // Get the peripheral name from the pin and assign it to the object obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC); if (obj->adc == (ADCName)NC) { error("ADC pin mapping failed"); } // Configure GPIO pinmap_pinout(pin, PinMap_ADC); // Save pin number for the read function obj->pin = pin; // The ADC initialization is done once if (adc_inited == 0) { adc_inited = 1; // Get ADC registers structure address adc = (ADC_TypeDef *)(obj->adc); // Enable ADC clock RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE); // Calibration ADC_VoltageRegulatorCmd(adc, ENABLE); wait_us(10); ADC_SelectCalibrationMode(adc, ADC_CalibrationMode_Single); ADC_StartCalibration(adc); while (ADC_GetCalibrationStatus(adc) != RESET) {} // Configure ADC ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_OneShot; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(adc, &ADC_CommonInitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = 1; ADC_Init(adc, &ADC_InitStructure); // Enable ADC ADC_Cmd(adc, ENABLE); while (!ADC_GetFlagStatus(adc, ADC_FLAG_RDY)) {} } }
/* 函数名:ADC1_Mode_Config * 描述 :配置ADC1的工作模式为DMA模式 * 输入 : 无 * 输出 :无 * 调用 :内部调用 */ static void ADC1_Mode_Config(void) { DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* DMA channel1 configuration */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //ADC地址 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;//内存地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 96; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址固定 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址可变 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); /* ADC1 configuration */ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立ADC模式 ADC_InitStructure.ADC_ScanConvMode = ENABLE ; //扫描模式,扫描模式用于多通道采集 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //开启连续转换模式,即不停地进行ADC转换 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //不使用外部触发转换 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //采集数据右对齐 ADC_InitStructure.ADC_NbrOfChannel = 3; //要转换的通道数目3 ADC_Init(ADC1, &ADC_InitStructure); /*配置ADC时钟,为PCLK2的8分频,即9MHz*/ RCC_ADCCLKConfig(RCC_PCLK2_Div8); /*配置ADC1的通道2.1.0为239.5个采样周期,序列为3,2,1,转换两路需要53.2us */ ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /*复位校准寄存器 */ ADC_ResetCalibration(ADC1); /*等待校准寄存器复位完成 */ while(ADC_GetResetCalibrationStatus(ADC1)); /* ADC校准 */ ADC_StartCalibration(ADC1); /* 等待校准完成*/ while(ADC_GetCalibrationStatus(ADC1)); /* Enable DMA channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /*Enable interruput of DMA1_Channel1 */ DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); /* 由于没有采用外部触发,所以使用软件触发ADC转换 */ NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // Enable the DMA Interrupt }
void adcInit(const adcConfig_t *config) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; uint8_t adcChannelCount = 0; memset(&adcOperatingConfig, 0, sizeof(adcOperatingConfig)); if (config->vbat.enabled) { adcOperatingConfig[ADC_BATTERY].tag = config->vbat.ioTag; } if (config->rssi.enabled) { adcOperatingConfig[ADC_RSSI].tag = config->rssi.ioTag; //RSSI_ADC_CHANNEL; } if (config->external1.enabled) { adcOperatingConfig[ADC_EXTERNAL1].tag = config->external1.ioTag; //EXTERNAL1_ADC_CHANNEL; } if (config->current.enabled) { adcOperatingConfig[ADC_CURRENT].tag = config->current.ioTag; //CURRENT_METER_ADC_CHANNEL; } ADCDevice device = adcDeviceByInstance(ADC_INSTANCE); if (device == ADCINVALID) return; #ifdef ADC24_DMA_REMAP SYSCFG_DMAChannelRemapConfig(SYSCFG_DMARemap_ADC2ADC4, ENABLE); #endif adcDevice_t adc = adcHardware[device]; bool adcActive = false; for (int i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcVerifyPin(adcOperatingConfig[i].tag, device)) { continue; } adcActive = true; IOInit(IOGetByTag(adcOperatingConfig[i].tag), OWNER_ADC_BATT + i, 0); IOConfigGPIO(IOGetByTag(adcOperatingConfig[i].tag), IO_CONFIG(GPIO_Mode_AN, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL)); adcOperatingConfig[i].adcChannel = adcChannelByTag(adcOperatingConfig[i].tag); adcOperatingConfig[i].dmaIndex = adcChannelCount++; adcOperatingConfig[i].sampleTime = ADC_SampleTime_601Cycles5; adcOperatingConfig[i].enabled = true; } if (!adcActive) { return; } if ((device == ADCDEV_1) || (device == ADCDEV_2)) { // enable clock for ADC1+2 RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div256); // 72 MHz divided by 256 = 281.25 kHz } else { // enable clock for ADC3+4 RCC_ADCCLKConfig(RCC_ADC34PLLCLK_Div256); // 72 MHz divided by 256 = 281.25 kHz } RCC_ClockCmd(adc.rccADC, ENABLE); dmaInit(dmaGetIdentifier(adc.DMAy_Channelx), OWNER_ADC, 0); DMA_DeInit(adc.DMAy_Channelx); DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&adc.ADCx->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = adcChannelCount; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = adcChannelCount > 1 ? DMA_MemoryInc_Enable : 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(adc.DMAy_Channelx, &DMA_InitStructure); DMA_Cmd(adc.DMAy_Channelx, ENABLE); // calibrate ADC_VoltageRegulatorCmd(adc.ADCx, ENABLE); delay(10); ADC_SelectCalibrationMode(adc.ADCx, ADC_CalibrationMode_Single); ADC_StartCalibration(adc.ADCx); while (ADC_GetCalibrationStatus(adc.ADCx) != RESET); ADC_VoltageRegulatorCmd(adc.ADCx, DISABLE); ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonStructInit(&ADC_CommonInitStructure); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_SynClkModeDiv4; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(adc.ADCx, &ADC_CommonInitStructure); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = adcChannelCount; ADC_Init(adc.ADCx, &ADC_InitStructure); uint8_t rank = 1; for (int i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcOperatingConfig[i].enabled) { continue; } ADC_RegularChannelConfig(adc.ADCx, adcOperatingConfig[i].adcChannel, rank++, adcOperatingConfig[i].sampleTime); } ADC_Cmd(adc.ADCx, ENABLE); while (!ADC_GetFlagStatus(adc.ADCx, ADC_FLAG_RDY)); ADC_DMAConfig(adc.ADCx, ADC_DMAMode_Circular); ADC_DMACmd(adc.ADCx, ENABLE); ADC_StartConversion(adc.ADCx); }
void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_ADCCLKConfig(RCC_PCLK2_Div8); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 24; TIM_TimeBaseStructure.TIM_Prescaler = 224; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* TIM1 channel1 configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0x7F; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(TIM1, &TIM_OCInitStructure); /* DMA1 channel1 configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 5*256; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 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); /* ADC1 configuration ------------------------------------------------------*/ 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 = 5; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel11 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 3, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 4, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 5, ADC_SampleTime_55Cycles5); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); }
//Clock configuration static void prvClockInit(void) { ErrorStatus HSEStartUpStatus; RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if (HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* ADCCLK = PCLK2/6 = 72 / 6 = 12 MHz*/ RCC_ADCCLKConfig(RCC_PCLK2_Div6); /* PLLCLK = 16MHz/2 * 9 = 72 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08); } else { GPIO_InitTypeDef GPIO_InitStructure; //Cannot start the main oscillator: red/green LED of death... GPIO_InitStructure.GPIO_Pin = LED_GPIO_RED | LED_GPIO_GREEN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_ResetBits(GPIOB, LED_RED); GPIO_ResetBits(GPIOB, LED_GREEN); //Cannot start xtal oscillator! while(1); } }
void ADC_Configuration(void) { ADC1_Convertion_buff=malloc(ADC_BUFF_SIZE); //64 samples * 2 for interleaving, * 2bytes/sample==256 ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; /* PCLK2 is the APB2 clock */ /* ADCCLK = PCLK2/6 = 72/6 = 12MHz*/ RCC_ADCCLKConfig(RCC_PCLK2_Div6); /* Enable ADC1,2 clock so that we can talk to them */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE); /*Enable the DMA1 clk*/ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Put everything back to power-on defaults */ ADC_DeInit(ADC1); ADC_DeInit(ADC2); /* ADC2 Configuration ------------------------------------------------------*/ /* ADC1 and ADC2 operate independently */ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; /* Enable the scan conversion so we do three at a time */ ADC_InitStructure.ADC_ScanConvMode = ENABLE; /* Don't do contimuous conversions - do them on demand */ ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; /* Start conversin by software, not an external trigger */ ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; /* Conversions are 12 bit - put them in the lower 12 bits of the result */ ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; /* Say how many channels would be used by the sequencer */ ADC_InitStructure.ADC_NbrOfChannel = 1; /* Now do the setup */ ADC_Init(ADC2, &ADC_InitStructure); /* ADC2 injected channel configuration */ #if BOARD<3 ADC_InjectedSequencerLengthConfig(ADC2, 2);//two conversions #else ADC_InjectedSequencerLengthConfig(ADC2, 3);//three conversions on the version 3 pcb - thermistor on the sensor #endif ADC_InjectedChannelConfig(ADC2, PRESSURE_ADC_CHAN, 1, ADC_SampleTime_239Cycles5); //ADC_InjectedChannelConfig(ADC2, 16, 3, ADC_SampleTime_239Cycles5);//on die temperature sensor - only on adc1 :-( ADC_InjectedChannelConfig(ADC2, BATTERY_ADC_CHAN, 2, ADC_SampleTime_239Cycles5); #if BOARD>=3 ADC_InjectedChannelConfig(ADC2, THERMISTOR_ADC_CHAN, 3, ADC_SampleTime_239Cycles5); #endif ADC_ExternalTrigInjectedConvConfig(ADC2, ADC_ExternalTrigInjecConv_None);//set sw injected channels /* Set the analogue watchdog on the battery voltage conversion*/ ADC_AnalogWatchdogCmd(ADC2,ADC_AnalogWatchdog_SingleInjecEnable); ADC_AnalogWatchdogThresholdsConfig(ADC2,0x0FFF,(uint16_t)((float)SAMPLING_FACTOR*MINIMUM_VOLTAGE));//watchdog fires on low voltage ADC_AnalogWatchdogSingleChannelConfig(ADC2, BATTERY_ADC_CHAN);//set the watchdog to the battery voltage channel ADC_ITConfig(ADC2, ADC_IT_AWD, ENABLE);//enable the analogue watchdog interrupt /* Enable the die temperature sensing and vref internal inputs to adc1*/ //ADC_TempSensorVrefintCmd(ENABLE); /* Enable ADC2 */ ADC_Cmd(ADC2, ENABLE); /* Enable ADC2 reset calibaration register */ ADC_ResetCalibration(ADC2); /* Check the end of ADC2 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC2)); /* Start ADC2 calibaration */ ADC_StartCalibration(ADC2); /* Check the end of ADC2 calibration */ while(ADC_GetCalibrationStatus(ADC2)); readADC2(BATTERY_ADC_CHAN);//Have to flush this for some reason /* ADC2 is now set up - move the ADC1 using DMA*/ /* DMA1 channel1(ADC1) configuration -------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC1_Convertion_buff; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = ADC_BUFF_SIZE/2;//2bytes/sample DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 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_ITConfig(DMA1_Channel1, DMA_IT_TC | DMA_IT_HT, ENABLE);//interrupt on complete and half complete DMA_ClearFlag(DMA1_FLAG_TC1|DMA1_FLAG_HT1); //make sure flags are clear /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ 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); /* ADC1 regular channel configuration */ ADC_RegularChannelConfig(ADC1, CRT_PPG_ADC_CHAN, 1, ADC_SampleTime_1Cycles5);/*239Cycles5);*/ /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Calibrate the ADC1*/ ADC_ResetCalibration(ADC1); while (ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while (ADC_GetCalibrationStatus(ADC1)); ADC_SoftwareStartConvCmd(ADC1, ENABLE); /* Enable the NVIC interrupt */ DMA_ISR_Config(); }
void ADCwithDMA_Config(void){ ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; //DMA RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* PCLK2 is the APB2 clock */ /* ADCCLK = PCLK2/6 = 72/6 = 12MHz*/ RCC_ADCCLKConfig(RCC_PCLK2_Div6); /* Enable ADC1 clock so that we can talk to it */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* Put everything back to power-on defaults */ ADC_DeInit(ADC1); /* DMA1 channel1 configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&(ADC.raw[0]); // Data direction: Peripheral is source DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; // Data Buffer Size DMA_InitStructure.DMA_BufferSize = 9; // Do not increment Peripheral Address Register DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; // Do increment destination memory pointer DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; // Transport data repeatedly DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; // Memory to Memory Mode Disabled DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; // Enable DMA Channel DMA_Init(DMA1_Channel1, &DMA_InitStructure); #ifdef ADC_DMA_TransferCompleteInterrupt /* Enable DMA1 Channel Transfer Complete interrupt */ DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); #endif //ADC_DMA_TransferCompleteInterrupt /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ 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; // number of ADC channels that will be converted using the sequencer ADC_InitStructure.ADC_NbrOfChannel = 9; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channels configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_71Cycles5); //AIN0 ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_71Cycles5); //AIN1 ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_71Cycles5); //MOTORCURRENT ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 4, ADC_SampleTime_71Cycles5); //THERM0 ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 5, ADC_SampleTime_71Cycles5); //THERM1 ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 6, ADC_SampleTime_71Cycles5); //PVCC ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 7, ADC_SampleTime_71Cycles5); //24V ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 8, ADC_SampleTime_71Cycles5); //12V ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 9, ADC_SampleTime_71Cycles5); //5V /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); ADC.uVoltsPerUnit = 7975; ADC.unitsOffset = 0; ADC.logicZeroMax_mV = 6000; ADC.logicOneMin_mV = 18000; }
void adcInit(void) { ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; ADC_CommonStructInit(&ADC_CommonInitStructure); ADC_StructInit(&ADC_InitStructure); DMA_StructInit(&DMA_InitStructure); GPIO_StructInit(&GPIO_InitStructure); /////////////////////////////////// RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div256); // 72 MHz divided by 256 = 281.25 kHz RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE); /////////////////////////////////// DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adc1ConvertedValues; //DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 2; //DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 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); /////////////////////////////////// GPIO_InitStructure.GPIO_Pin = VBATT_PIN | DIFF_PRESSURE_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /////////////////////////////////// ADC_VoltageRegulatorCmd(ADC1, ENABLE); delay(10); ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1) != RESET ); /////////////////////////////////// //ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode; //ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular; //ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(ADC1, &ADC_CommonInitStructure); /////////////////////////////////// ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable; //ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; //ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; //ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC_InitStructure.ADC_OverrunMode = DISABLE; //ADC_InitStructure.ADC_AutoInjMode = DISABLE; ADC_InitStructure.ADC_NbrOfRegChannel = 2; ADC_Init(ADC1, &ADC_InitStructure); /////////////////////////////////// ADC_RegularChannelConfig(ADC1, VBATT_CHANNEL, 1, ADC_SampleTime_181Cycles5); ADC_RegularChannelConfig(ADC1, DIFF_PRESSURE_CHANNEL, 2, ADC_SampleTime_181Cycles5); ADC_Cmd(ADC1, ENABLE); while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY)); ADC_DMAConfig(ADC1, ADC_DMAMode_Circular); ADC_DMACmd(ADC1, ENABLE); ADC_StartConversion(ADC1); }
static void ADCSample_DMATransmit_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; /* Enable ADC1 and GPIOC clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 |RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE); /* Configure PC. as analog input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOC, &GPIO_InitStructure); // GPIO_ResetBits(GPIOC , GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOB, &GPIO_InitStructure); // GPIO_ResetBits(GPIOB , GPIO_Pin_0|GPIO_Pin_1); #if 1 /* Enable DMA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* DMA channel1 configuration */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //ADC地址 DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_Value_Raw;//内存地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = ADC_RANK_NUM; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址递增 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传输完成中断 */ DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); /* Enable DMA channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); #endif /* ADC1 configuration */ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立ADC模式 ADC_InitStructure.ADC_ScanConvMode = ENABLE ; //扫描模式用于多通道采集 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //开启连续转换模式,即不停地进行ADC转换 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //不使用外部触发转换 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //采集数据右对齐 ADC_InitStructure.ADC_NbrOfChannel = ADC_RANK_NUM; //要转换的通道数目 ADC_Init(ADC1, &ADC_InitStructure); /*配置ADC1的通道为55. 5个采样周期,序列为 */ #if 1 RCC_ADCCLKConfig(RCC_PCLK2_Div8); ADC_RegularChannelConfig(ADC1, ADC_Channel_10, RANK_NO_1, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_11, RANK_NO_2, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_12, RANK_NO_3, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_13, RANK_NO_4, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_14, RANK_NO_5, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_15, RANK_NO_6, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_8, RANK_NO_7, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_9, RANK_NO_8, ADC_SampleTime_55Cycles5); #endif /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /*复位校准寄存器 */ ADC_ResetCalibration(ADC1); /*等待校准寄存器复位完成 */ while(ADC_GetResetCalibrationStatus(ADC1)); /* ADC校准 */ ADC_StartCalibration(ADC1); /* 等待校准完成*/ while(ADC_GetCalibrationStatus(ADC1)); /* 由于没有采用外部触发,所以使用软件触发ADC转换 */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); /* Enable DMA channel1 */ // DMA_Cmd(DMA1_Channel1, ENABLE); }
/* 函数名:ADC1_Mode_Config * 描述 :配置ADC1的工作模式为MDA模式 * 输入 : 无 * 输出 :无 * 调用 :内部调用 */ static void ADC1_Mode_Config(void) { DMA_InitTypeDef DMA_InitStructure; ADC_InitTypeDef ADC_InitStructure; /* Enable DMA clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Enable ADC1 clock and GPIOC clock already enabled in GPIO.c */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* DMA channel1 configuration */ 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 = 11; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; 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); /* Enable DMA channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration */ ADC_DeInit(ADC1); //??? ADC1 ???????????? 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 = 11; ADC_Init(ADC1, &ADC_InitStructure); /********************************/ RCC_ADCCLKConfig(RCC_PCLK2_Div6); //72M/6=12,ADC????????14M /* ADC1 regular channel configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 1, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 2, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 3, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 4, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 5, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 6, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 7, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 8, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 9, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 10, ADC_SampleTime_55Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 11, ADC_SampleTime_55Cycles5); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); }
int ybs_drv_init(void) { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; //mos ctrl, PB0, high off, low on RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); //adc, PA1, ADC12_IN1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_ADCCLKConfig(RCC_PCLK2_Div8); /*72Mhz/8 = 9Mhz*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); ADC_DeInit(ADC1); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_71Cycles5); //9Mhz/(71.5 + 12.5) = 107.1Khz ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while (ADC_GetCalibrationStatus(ADC1)); ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConvCmd(ADC1, ENABLE); //TIM4 TIMEBASE RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_TimeBaseStructure.TIM_Period = YBS_US - 1; TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; //Fapb1 = TIM_clk = 72Mhz, Tick = 1us TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //TIM_ARRPreloadConfig(TIM4, ENABLE); TIM_Cmd(TIM4, DISABLE); TIM_ClearFlag(TIM4, TIM_FLAG_Update); TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //IRQ OF TIM4 NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //DAC static const struct ad5663_cfg_s ad5663_cfg = { .spi = &spi1, .gpio_cs = SPI_1_NSS, .gpio_ldac = SPI_CS_PA2, .gpio_clr = SPI_CS_PA3, }; dev_register("ad5663", &ad5663_cfg); ybs_fd_dac = dev_open("dac0", 0); dev_ioctl(ybs_fd_dac, DAC_SET_CH, 1); //for fast ch1 write purpose return 0; }