*******************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
TIM2_Configuration();
usart_Configuration();
ADC_Configuration();
while(1)
{
}
}
//.........................................................................................................
//ÖжÏÖ´Ðк¯Êý
void TIM2_IRQHandler(void)
{
vu16 capture2 = 0;
if(TIM_GetITStatus(TIM2,TIM_IT_CC2) != RESET)
{
TIM_ClearITPendingBit(TIM2,TIM_IT_CC2);
GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOE,GPIO_Pin_8)));
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
do
{
id = ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC);
}while(!id);
AD_value_float = ADC_GetConversionValue(ADC1); //·µ»Ø×î½üÒ»´ÎADC³£¹æת»»½á¹û
AD_value_float = AD_value_float/4096*13200;
printf("%.2fV\r\n",AD_value_float);
capture2 = TIM_GetCapture2(TIM2);
TIM_SetCompare2(TIM2,capture2 + CCR2_Val);
}
}
/*******************************************************************************
* Function Name : ADC_Configuration
* Description : Configure the ADC.
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
//¶Ë¿ÚÅäÖÃ
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PE.07 (LED 1), as analog output -------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Configure PA.06 (ADC Channel6), PA.07 (ADC Channel7) as analog input -------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//...................................................................................................
//ʱÖÓÅäÖÃ
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;//½«ÍⲿʱÖÓÉèÖÃΪö¾Ù±äÁ¿
RCC_DeInit();//¸´Î»RCCÍⲿÉ豸¼Ä´æÆ÷µ½Ä¬ÈÏÖµ
RCC_HSEConfig(RCC_HSE_ON);//´ò¿ªÍⲿ¾§Õñ
HSEStartUpStatus = RCC_WaitForHSEStartUp();//¼ì²âÍⲿ¾§ÕñÊÇ•ñ¾ÍÐ÷
if(HSEStartUpStatus == SUCCESS)//Èô¾ÍÐ÷
{
RCC_HSICmd(ENABLE);
RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);/* PCLK1 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_ADCCLKConfig(RCC_PCLK2_Div8);//ÅäÖÃADCʱÖÓ(72/4)9M
//ÅäÖÃPLLʱÖÓ
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)== RESET)//¼ì²âPLLÊÇ•ñ¾ÍÐ÷£¬RESET:¾ÍÐ÷
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//ÅäÖÃÍⲿʱÖÓ×÷ΪϵͳʱÖÓ
while(RCC_GetSYSCLKSource()!=0x08);//¼ì²âÍⲿʱÖÓÊÇ•ñ×÷ΪPLLµÄÊäÈëʱÖÓ
}
/*¸ø¸÷Ä£¿é¿ªÆôʱÖÓ*/
RCC_ADCCLKConfig(RCC_PCLK2_Div8);//(72/8)M
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//Æô¶¯DMA1ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);//Æô¶¯ADC1ʱÖÓ
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //Æô¶¯TIM2ʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE ,ENABLE);
}
//¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£¡£
//ÖжÏÅäÖÃ
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;//³õʼ»¯ÖжϕÖ×麯Êý
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
//ʹÄܶ¨Ê±TIM2ÖжÏ
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //ÅäÖÃÓÅÏȼ¶×é
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //ÉèÖÃTIM2ͨµÀÊäÈëÖжÏ
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /* Configure the NVIC Preemption Priority Bits[ÅäÖÃÓÅÏȼ¶×é] */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //ÉèÖÃÏìÓ¦ÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /* Enable the TIM2 gloabal Interrupt [ÔÊÐíTIM2È«¾ÖÖжÏ]*/
NVIC_Init(&NVIC_InitStructure);//³õʼ»¯
}
//...............................................................................................................
//¶¨Ê±Æ÷ÅäÖÃ
void TIM2_Configuration(void)
{
//³õʼ»¯½á¹¹Ìå
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM2);//TIM2
TIM_TimeBaseStructure.TIM_Period = 19;
TIM_TimeBaseStructure.TIM_Prescaler = 719;
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ʱÖÓ•Ö¸î0
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊý
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//ÉèÖÃÔ¤•ÖƵֵÇÒÁ¢¼´×°Èë
TIM_PrescalerConfig(TIM2,719,TIM_PSCReloadMode_Immediate);//µ¥´Î¼ÆÊýʱ¼äT=(7199+1)/72MHZ=100us£¬T=(3599+1)/72MHZ=50us
//ÉèÖÃOC1ͨµÀ£¬¹¤×÷ģʽΪ¼ÆÊýÆ÷ģʽ£¬
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; //TIMÊä³ö±È½Ïʱ¼äģʽ
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //ʹÄÜÊä³ö±È½Ï״̬
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Êä³ö±È½Ï¼«ÐÔ¸ß
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM2,&TIM_OCInitStructure);
/*½ûֹԤװÔؼĴæÆ÷£¬ÊÇÒòΪ¶¨Ê±Æ÷×î´ó¼ÆÊýÖÜÆÚΪ66635£¬½ûÖ¹ÖØ×°ÔØ£¬¾ÍʹµÃ¼ÆÊýÖµÂú²úÉúÒç³öÖжϺó£¬
¼ÆÊýÖµ»Ø¹éÖÁÁãÖØÐÂÏòÉϼÆÊ££¬¶ø²»ÊǼÌÐøÉÏÒ»´ÎµÄÖµ¼ÌÐø¼ÆÊý*/
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Disable);
//ʹÄÜTIM2ÖжÏ
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE); //ʹÄÜTIM2µÄ¸üÐÂ
//Æô¶¯TIM2¼ÆÊý
TIM_Cmd(TIM2, ENABLE);
}
//....................................................................................................
//ADCÅäÖÃ
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
/* DMA channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_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_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* 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 = 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);
/* ADC1 regular channel6 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_55Cycles5);
/* Enable ADC1 DMA */
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);
}
|