void ADSample(void) { u8 i,tttemp; ADC1_DeInit(); ADC1_ConversionConfig(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_3, ADC1_ALIGN_RIGHT); ADC1_SchmittTriggerConfig(ADC1_SCHMITTTRIG_CHANNEL4,ENABLE); ADC1_ITConfig(ADC1_IT_EOCIE,DISABLE); ADC1_Cmd(ENABLE); ADC1->CSR &= (u8)(~0x80); ADC1_StartConversion(); while((ADC1->CSR & 0x80)!=0x80) IWDG_ReloadCounter(); // µÈ´ýת»»½áÊø Conversion_Value += ADC1_GetConversionValue(); ADC1->CSR &= (u8)(~0x80); u8ADChannelCnt++; if(u8ADChannelCnt>=SAMPLE_TIMES) { u8ADChannelCnt = 0; tttemp = (u8)((Conversion_Value>>2)/SAMPLE_TIMES); if((tttemp<20) || (tttemp>200) ) u8Temp = OFF_TMP_SET; else { for(i=0;T3580Tab[i]<tttemp;i++); u8Temp = (u8)(i+(40*2)); } Conversion_Value = 0; }
//-----------------------------------------------------------------// // Setup ADC // ADC_CLK = 4 MHz // ADC single conversion time is ADC_StartDelay + // (29 to 36 ADC clock cycles depending on ADC_DelayGo) // ADC clock = 4MHz / 128 = 31.250kHz (T = 32uS) //-----------------------------------------------------------------// void HW_ADCInit(void) { ADC_InitTypeDef sADC; ADCx_InitTypeDef sADCx; // ADC Configuration // Reset all ADC settings ADC_DeInit(); ADC_StructInit(&sADC); sADC.ADC_SynchronousMode = ADC_SyncMode_Independent; sADC.ADC_StartDelay = 10; sADC.ADC_TempSensor = ADC_TEMP_SENSOR_Enable; sADC.ADC_TempSensorAmplifier = ADC_TEMP_SENSOR_AMPLIFIER_Enable; sADC.ADC_TempSensorConversion = ADC_TEMP_SENSOR_CONVERSION_Enable; sADC.ADC_IntVRefConversion = ADC_VREF_CONVERSION_Enable; sADC.ADC_IntVRefTrimming = 1; ADC_Init (&sADC); // ADC1 Configuration ADCx_StructInit (&sADCx); sADCx.ADC_ClockSource = ADC_CLOCK_SOURCE_ADC; sADCx.ADC_SamplingMode = ADC_SAMPLING_MODE_SINGLE_CONV; sADCx.ADC_ChannelSwitching = ADC_CH_SWITCHING_Disable; sADCx.ADC_ChannelNumber = ADC_CH_TEMP_SENSOR; sADCx.ADC_Channels = 0; sADCx.ADC_LevelControl = ADC_LEVEL_CONTROL_Disable; sADCx.ADC_LowLevel = 0; sADCx.ADC_HighLevel = 0; sADCx.ADC_VRefSource = ADC_VREF_SOURCE_EXTERNAL; sADCx.ADC_IntVRefSource = ADC_INT_VREF_SOURCE_EXACT; sADCx.ADC_Prescaler = ADC_CLK_div_128; sADCx.ADC_DelayGo = 0; // CHECKME ADC1_Init (&sADCx); ADC2_Init (&sADCx); // Disable ADC interupts ADC1_ITConfig((ADCx_IT_END_OF_CONVERSION | ADCx_IT_OUT_OF_RANGE), DISABLE); ADC2_ITConfig((ADCx_IT_END_OF_CONVERSION | ADCx_IT_OUT_OF_RANGE), DISABLE); // ADC1 enable ADC1_Cmd (ENABLE); ADC2_Cmd (ENABLE); //-------------------// /* ADC1_SetChannel(ADC_CH_TEMP_SENSOR); ADC1_Start(); while( ADC_GetFlagStatus(ADC1_FLAG_END_OF_CONVERSION)==RESET ); temp_adc = ADC1_GetResult(); ADC1_Start(); while( ADC_GetFlagStatus(ADC1_FLAG_END_OF_CONVERSION)==RESET ); temp_adc = ADC1_GetResult(); temp_adc = temp_adc; */ }
/** * @brief Deinitializes the ADC. * @retval : None */ void pot_DeInit(void){ /* De-Init ADC peripheral*/ ADC1_DeInit(); /* Enable EOC interrupt */ ADC1_ITConfig(ADC1_IT_EOCIE,DISABLE); }
/** * @brief Main program. * @param None * @retval None */ void main ( void ) { ClockConfigure(); #if defined (_USE_DEBUG_UART_) DebugUARTInit(); #endif /* #if defined (_USE_DEBUG_UART_) */ /* ADC Configuration */ /* Reset all ADC settings */ ADC_DeInit(); DEBUG_PRINTF("ADC Init..."); // Inti clock ADC RST_CLK_ADCclkSelection(RST_CLK_ADCclkCPU_C1); RST_CLK_ADCclkPrescaler(RST_CLK_ADCclkDIV2); // Enable clock ADC RST_CLK_ADCclkEnable(ENABLE); RST_CLK_PCLKcmd(RST_CLK_PCLK_ADC, ENABLE); ADC_StructInit(&sADC); sADC.ADC_StartDelay = 0; sADC.ADC_TempSensor = ADC_TEMP_SENSOR_Enable; sADC.ADC_TempSensorAmplifier = ADC_TEMP_SENSOR_AMPLIFIER_Enable; sADC.ADC_TempSensorConversion = ADC_TEMP_SENSOR_CONVERSION_Enable; sADC.ADC_IntVRefConversion = ADC_VREF_CONVERSION_Disable; sADC.ADC_IntVRefTrimming = 1; ADC_Init(&sADC); /* ADC1 Configuration */ ADCx_StructInit(&sADCx); sADCx.ADC_ClockSource = ADC_CLOCK_SOURCE_CPU; sADCx.ADC_SamplingMode = ADC_SAMPLING_MODE_CICLIC_CONV; sADCx.ADC_ChannelSwitching = ADC_CH_SWITCHING_Disable; sADCx.ADC_ChannelNumber = ADC_CH_TEMP_SENSOR; sADCx.ADC_Channels = 0; sADCx.ADC_LevelControl = ADC_LEVEL_CONTROL_Disable; sADCx.ADC_LowLevel = 0; sADCx.ADC_HighLevel = 0; sADCx.ADC_VRefSource = ADC_VREF_SOURCE_INTERNAL; sADCx.ADC_IntVRefSource = ADC_INT_VREF_SOURCE_INEXACT; sADCx.ADC_Prescaler = ADC_CLK_div_512; sADCx.ADC_DelayGo = 7; ADC1_Init(&sADCx); /* Enable ADC1 EOCIF and AWOIFEN interupts */ ADC1_ITConfig(ADCx_IT_END_OF_CONVERSION, ENABLE); /* Enable ADC IRQ */ NVIC_EnableIRQ(ADC_IRQn); DEBUG_PRINTF("Ok\r\n"); /* ADC1 enable */ ADC1_Cmd(ENABLE); while (1); }
/******************************************************************************* * Function Name : ADC_IRQHandler * Description : This function handles ADC global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void ADC_IRQHandler(void) { /* Disable ADC1 end of conversion interrupt */ ADC1_ITConfig(ADC1_FLAG_END_OF_CONVERSION, DISABLE); if (ADC1_GetFlagStatus(ADC1_IT_END_OF_CONVERSION) == SET) { /* Read value of the ADC1_RUSULT register and clear (automatically) * ADC1_IT_END_OF_CONVERSION flag */ ADC1_Value = ADC1_GetResult(); } if (ADC1_GetFlagStatus(ADCx_FLAG_OVERWRITE) == SET) { ADC1_ClearOverwriteFlag(); } }
/** * @brief Initializes the ADC. * ADC conversion mode: continious, EOC interrupt enabled * @retval : None */ void pot_init(void){ /* Init GPIO for ADC7 */ GPIO_Init(GPIOB, GPIO_PIN_7, GPIO_MODE_IN_FL_NO_IT); /* De-Init ADC peripheral*/ ADC1_DeInit(); /* Init ADC1 peripheral */ ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, ADC1_CHANNEL_7, ADC1_PRESSEL_FCPU_D18, \ ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL7,\ DISABLE); /* Enable EOC interrupt */ ADC1_ITConfig(ADC1_IT_EOCIE,ENABLE); /* Enable general interrupts */ enableInterrupts(); /*Start Conversion */ ADC1_StartConversion(); }
/* Configure ADC1 Continuous Conversion with End Of Conversion interrupt enabled . */ void InitADC() { /* De-Init ADC1 peripheral */ ADC1_DeInit(); /* Enable EOC interrupt */ ADC1_ITConfig(ADC1_IT_EOCIE, ENABLE); /* Enable conversion data buffering */ ADC1_DataBufferCmd(ENABLE); /* Enable scan mode conversion */ ADC1_ScanModeCmd(ENABLE); /* Init ADC1 peripheral */ /* ADC1 Channel 3 */ ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, REF_2V5_ANALOG, ADC1_PRESSEL_FCPU_D4, \ ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL3,\ DISABLE); /* ADC1 Channel 4 */ ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, L9110_U3_DRAW, ADC1_PRESSEL_FCPU_D4, \ ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL4,\ DISABLE); /* ADC1 Channel 5 */ ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, L9110_U4_DRAW, ADC1_PRESSEL_FCPU_D4, \ ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL5,\ DISABLE); /* ADC1 Channel 6 */ ADC1_Init(ADC1_CONVERSIONMODE_CONTINUOUS, L9110_U5_DRAW, ADC1_PRESSEL_FCPU_D4, \ ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL6,\ DISABLE); ADC1_Cmd(ENABLE); }
void _Device_Stop_ADC_Conversion(){ ADC1_Cmd(DISABLE); //disable ADC,//Close conversion ADC1_ITConfig(ADC1_IT_EOCIE, DISABLE); //Disable EOC interrupt }
void adc_enable_Data_Buffer_and_EOC_interrupt(void){ ADC1_DataBufferCmd(ENABLE); //Enable the ADC1 data store into the Data Buffer registers ADC1_ITConfig(ADC1_IT_EOCIE, ENABLE); //Enable EOC interrupt }
void main(void) #endif { RST_CLK_DeInit(); RST_CLK_CPU_PLLconfig (RST_CLK_CPU_PLLsrcHSIdiv2,0); /* Enable peripheral clocks --------------------------------------------------*/ RST_CLK_PCLKcmd((RST_CLK_PCLK_RST_CLK | RST_CLK_PCLK_DMA | RST_CLK_PCLK_ADC),ENABLE); RST_CLK_PCLKcmd((RST_CLK_PCLK_SSP1 | RST_CLK_PCLK_SSP2),ENABLE); /* Disable all interrupt */ NVIC->ICPR[0] = 0xFFFFFFFF; NVIC->ICER[0] = 0xFFFFFFFF; /* DMA Configuration */ /* Reset all settings */ DMA_DeInit(); DMA_StructInit(&DMA_InitStr); /* Set Primary Control Data */ DMA_PriCtrlStr.DMA_SourceBaseAddr = (uint32_t)(&(MDR_ADC->ADC1_RESULT)); DMA_PriCtrlStr.DMA_DestBaseAddr = (uint32_t)ADCConvertedValue; DMA_PriCtrlStr.DMA_SourceIncSize = DMA_SourceIncNo; DMA_PriCtrlStr.DMA_DestIncSize = DMA_DestIncHalfword; DMA_PriCtrlStr.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_PriCtrlStr.DMA_Mode = DMA_Mode_PingPong; DMA_PriCtrlStr.DMA_CycleSize = 10; DMA_PriCtrlStr.DMA_NumContinuous = DMA_Transfers_1; DMA_PriCtrlStr.DMA_SourceProtCtrl = DMA_SourcePrivileged; DMA_PriCtrlStr.DMA_DestProtCtrl = DMA_DestPrivileged; /* Set Alternate Control Data */ DMA_AltCtrlStr.DMA_SourceBaseAddr = (uint32_t)(&(MDR_ADC->ADC1_RESULT)); DMA_AltCtrlStr.DMA_DestBaseAddr = (uint32_t)ADCConvertedValue; DMA_AltCtrlStr.DMA_SourceIncSize = DMA_SourceIncNo; DMA_AltCtrlStr.DMA_DestIncSize = DMA_DestIncHalfword; DMA_AltCtrlStr.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_AltCtrlStr.DMA_Mode = DMA_Mode_PingPong; DMA_AltCtrlStr.DMA_CycleSize = 10; DMA_AltCtrlStr.DMA_NumContinuous = DMA_Transfers_1; DMA_AltCtrlStr.DMA_SourceProtCtrl = DMA_SourcePrivileged; DMA_AltCtrlStr.DMA_DestProtCtrl = DMA_DestPrivileged; /* Set Channel Structure */ DMA_InitStr.DMA_PriCtrlData = &DMA_PriCtrlStr; DMA_InitStr.DMA_AltCtrlData = &DMA_AltCtrlStr; DMA_InitStr.DMA_Priority = DMA_Priority_Default; DMA_InitStr.DMA_UseBurst = DMA_BurstClear; DMA_InitStr.DMA_SelectDataStructure = DMA_CTRL_DATA_PRIMARY; /* Init DMA channel ADC1 */ DMA_Init(DMA_Channel_ADC1, &DMA_InitStr); /* Enable dma_req or dma_sreq to generate DMA request */ MDR_DMA->CHNL_REQ_MASK_CLR = (1<<DMA_Channel_ADC1); MDR_DMA->CHNL_USEBURST_CLR = (1<<DMA_Channel_ADC1); /* Enable DMA channel ADC1 */ DMA_Cmd(DMA_Channel_ADC1, ENABLE); /* ADC Configuration */ /* Reset all ADC settings */ ADC_DeInit(); ADC_StructInit(&sADC); sADC.ADC_SynchronousMode = ADC_SyncMode_Independent; sADC.ADC_StartDelay = 0; sADC.ADC_TempSensor = ADC_TEMP_SENSOR_Enable; sADC.ADC_TempSensorAmplifier = ADC_TEMP_SENSOR_AMPLIFIER_Enable; sADC.ADC_TempSensorConversion = ADC_TEMP_SENSOR_CONVERSION_Enable; sADC.ADC_IntVRefConversion = ADC_VREF_CONVERSION_Disable; sADC.ADC_IntVRefTrimming = 1; ADC_Init (&sADC); /* ADC1 Configuration */ ADCx_StructInit (&sADCx); sADCx.ADC_ClockSource = ADC_CLOCK_SOURCE_CPU; sADCx.ADC_SamplingMode = ADC_SAMPLING_MODE_CICLIC_CONV; sADCx.ADC_ChannelSwitching = ADC_CH_SWITCHING_Disable; sADCx.ADC_ChannelNumber = ADC_CH_TEMP_SENSOR; sADCx.ADC_Channels = 0; sADCx.ADC_LevelControl = ADC_LEVEL_CONTROL_Disable; sADCx.ADC_LowLevel = 0; sADCx.ADC_HighLevel = 0; sADCx.ADC_VRefSource = ADC_VREF_SOURCE_INTERNAL; sADCx.ADC_IntVRefSource = ADC_INT_VREF_SOURCE_INEXACT; sADCx.ADC_Prescaler = ADC_CLK_div_512; sADCx.ADC_DelayGo = 7; ADC1_Init (&sADCx); /* Enable ADC1 EOCIF and AWOIFEN interupts */ ADC1_ITConfig((ADCx_IT_END_OF_CONVERSION | ADCx_IT_OUT_OF_RANGE), DISABLE); /* ADC1 enable */ ADC1_Cmd (ENABLE); /* Enable DMA IRQ */ NVIC_EnableIRQ(DMA_IRQn); while(1); }
void main(void) #endif { ClockConfigure(); #if defined (_USE_DEBUG_UART_) DebugUARTInit(); #endif /* #if defined (_USE_DEBUG_UART_) */ /* Enable peripheral clocks ----------------------------------------------*/ RST_CLK_PCLKcmd((RST_CLK_PCLK_RST_CLK | RST_CLK_PCLK_ADC | RST_CLK_PCLK_PORTD | RST_CLK_PCLK_PORTB), ENABLE); /* Reset PORTD settings */ PORT_DeInit(MDR_PORTD ); /* Configure ADC pin: ADC2 */ /* Configure PORTD pin 9 */ PORT_InitStructure.PORT_Pin = PORT_Pin_9; PORT_InitStructure.PORT_OE = PORT_OE_IN; PORT_InitStructure.PORT_MODE = PORT_MODE_ANALOG; PORT_Init(MDR_PORTD, &PORT_InitStructure); /* Configure PORTB pins 0, 1 for output to switch LED1,2 on/off */ /* Configure PORTB pins 0, 1 */ PORT_InitStructure.PORT_FUNC = PORT_FUNC_PORT; PORT_InitStructure.PORT_Pin = PORT_Pin_0 | PORT_Pin_1; PORT_InitStructure.PORT_OE = PORT_OE_OUT; PORT_InitStructure.PORT_MODE = PORT_MODE_DIGITAL; PORT_InitStructure.PORT_SPEED = PORT_SPEED_MAXFAST; PORT_Init(MDR_PORTB, &PORT_InitStructure); /* ADC Configuration */ /* Reset all ADC settings */ ADC_DeInit(); DEBUG_PRINTF("ADC Init..."); // Inti clock ADC RST_CLK_ADCclkSelection(RST_CLK_ADCclkCPU_C1); RST_CLK_ADCclkPrescaler(RST_CLK_ADCclkDIV2); // Enable clock ADC RST_CLK_ADCclkEnable(ENABLE); RST_CLK_PCLKcmd(RST_CLK_PCLK_ADC, ENABLE); ADC_StructInit(&sADC); sADC.ADC_StartDelay = 0; sADC.ADC_TempSensor = ADC_TEMP_SENSOR_Enable; sADC.ADC_TempSensorAmplifier = ADC_TEMP_SENSOR_AMPLIFIER_Enable; sADC.ADC_TempSensorConversion = ADC_TEMP_SENSOR_CONVERSION_Disable; sADC.ADC_IntVRefConversion = ADC_VREF_CONVERSION_Disable; sADC.ADC_IntVRefTrimming = 0; sADC.ADC_IntVRefAmplifier = ADC_INT_VREF_AMPLIFIER_Enable; ADC_Init(&sADC); ADCx_StructInit(&sADCx); sADCx.ADC_ClockSource = ADC_CLOCK_SOURCE_CPU; sADCx.ADC_SamplingMode = ADC_SAMPLING_MODE_CICLIC_CONV; sADCx.ADC_ChannelSwitching = ADC_CH_SWITCHING_Disable; sADCx.ADC_ChannelNumber = ADC_CH_ADC2; sADCx.ADC_Channels = 0; sADCx.ADC_LevelControl = ADC_LEVEL_CONTROL_Enable; sADCx.ADC_LowLevel = L_Level; sADCx.ADC_HighLevel = H_Level; sADCx.ADC_VRefSource = ADC_VREF_SOURCE_INTERNAL; sADCx.ADC_IntVRefSource = ADC_INT_VREF_SOURCE_INEXACT; sADCx.ADC_Prescaler = ADC_CLK_div_16; sADCx.ADC_DelayGo = 0xF; ADC1_Init(&sADCx); /* Enable ADC1 EOCIF and AWOIFEN interupts */ ADC1_ITConfig((ADCx_IT_END_OF_CONVERSION | ADCx_IT_OUT_OF_RANGE), ENABLE); NVIC_EnableIRQ(ADC_IRQn); /* ADC1 enable */ ADC1_Cmd(ENABLE); while (1); }