- #include "stm32f10x.h"
- #include "stdio.h"
- #include "delay.h"
- #include "stm32f10x_usart.h"
- #include "stm32f10x_tim.h"
- RCC_ClocksTypeDef RCC_Clocks;
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void ADC_Configuration(void);
- int main(void)
- {
- int CCR1_Val = 0;
- float VolValue = 0.00;
- u32 ticks = 0;
- RCC_Configuration();
- RCC_GetClocksFreq(&RCC_Clocks);
- GPIO_Configuration();
- ADC_Configuration();
- // TIM_Configuration();
-
-
- while(1)
- {
- if(ticks ++ >= 2000000)
- {
- //float i=0;
- ticks = 0;
- VolValue = 3.3 * ADC_GetConversionValue(ADC1)/0X0FFF;
- if(VolValue >= 3.1 )
- {
- CCR1_Val = 60000;
- }
- if(VolValue >= 2.97 & VolValue < 3.1)
- {
- CCR1_Val = 54000;
- }
-
- if(VolValue >= 2.62 & VolValue < 2.97)
- {
- CCR1_Val = 47700;
- }
- if(VolValue >= 2.26 & VolValue < 2.62)
- {
- CCR1_Val = 41100;
- }
- if(VolValue >= 1.91 & VolValue < 2.26)
- {
- CCR1_Val = 34680;
- }
- if(VolValue >= 1.58 & VolValue < 1.91)
- {
- CCR1_Val = 28680;
- }
- if(VolValue >= 1.29 & VolValue < 1.58)
- {
- CCR1_Val = 23400;
- }
- if(VolValue >= 1.00 & VolValue < 1.29)
- {
- CCR1_Val = 18300;
- }
- if(VolValue >= 0.76 & VolValue < 1.00)
- {
- CCR1_Val = 13740;
- }
- if(VolValue >= 0.49 & VolValue < 0.76)
- {
- CCR1_Val = 8820;
- }
- if(VolValue >= 0.31 & VolValue < 0.49)
- {
- CCR1_Val = 5700;
- }
- if(VolValue >= 0.13 & VolValue < 0.31)
- {
- CCR1_Val = 2400;
- }
- if(VolValue >= 0.04 & VolValue < 0.13)
- {
- CCR1_Val = 660;
- }
- if(VolValue < 0.04)
- {
- CCR1_Val = 0;
- }
-
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
-
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_TimeBaseStructure.TIM_Period = 60000;
- TIM_TimeBaseStructure.TIM_Prescaler = 0;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
- TIM_OC1Init(TIM2,&TIM_OCInitStructure);
- TIM_OC1PreloadConfig(TIM2 , TIM_OCPreload_Disable);
- TIM_ARRPreloadConfig(TIM2, ENABLE);
- TIM_Cmd(TIM2,ENABLE);
-
- if(ticks ++ >= 2000000){};
- }
- }
-
- }
- void RCC_Configuration(void)
- {
-
-
- ErrorStatus HSEStartUpStatus;
- RCC_DeInit();
- RCC_HSEConfig(RCC_HSE_ON);
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- if(HSEStartUpStatus == SUCCESS)
- {
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- FLASH_SetLatency(FLASH_Latency_2);
-
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- RCC_PCLK2Config(RCC_HCLK_Div1);
- RCC_PCLK1Config(RCC_HCLK_Div2);
-
- 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)
- {
- }
- }
-
-
- }
- void GPIO_Configuration(void)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- void ADC_Configuration(void)
- {
- ADC_InitTypeDef ADC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
- RCC_ADCCLKConfig(RCC_PCLK2_Div4);
-
- ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
- ADC_InitStructure.ADC_ScanConvMode = ENABLE;
- ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
- ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
- ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
- ADC_InitStructure.ADC_NbrOfChannel = 1;
- ADC_Init(ADC1,&ADC_InitStructure);
-
- ADC_RegularChannelConfig(ADC1,ADC_Channel_8,1,ADC_SampleTime_55Cycles5);
- ADC_Cmd(ADC1, ENABLE);
- ADC_ResetCalibration(ADC1);
- while(ADC_GetResetCalibrationStatus(ADC1));
- ADC_StartCalibration(ADC1);
- while(ADC_GetCalibrationStatus(ADC1));
- ADC_SoftwareStartConvCmd(ADC1,ENABLE);
- }
復(fù)制代碼
|