//Author: "Tears and Sweat"
//Data: 2011-November
//Function: Accumulate programming ideas and routines
The configuration functions included in this page are:
void SysTick_Configuration(void);
void GPIO_Configuration(void); //Configure the port pin function
void EXTI_Configuration(void); //Configure the external interrupt line
void NVIC_Configuration(void); //Configure the interrupt priority and internal interrupt vector
void RCC_Configuration(void); //Configure the clock of the system and its peripherals
void IWDG_Configuration(void); //Watchdog configuration structure
void TIM_Configuration(void)
void ADC_configuration();
void DMA_Configuration(void);
void DAC_Configuration(void);
//Structure variables that need to be configured at the same time
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
*******//Cortex system timer (SysTick) ******************************
******************************************************
**********************************************************
void SysTick_Configuration(void)
{
//Select the clock source of the system timer (SysTick)
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
SysTick_SetReload(250000);
SysTick_CounterCmd(SysTick_Counter_Enable);
SysTick_ITConfig(ENABLE);
}
*********///GPIO configuration program****************************** *************
*************************************** ***************************
*********************** ******************************************
void GPIO_Configuration(void) void RCC_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz
; _Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
//Interrupt line initialization
void EXTI_Configuration(void)
{
EXTI_InitStructure.EXTI_Line = EXTI_Line9;
.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure .EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
///Nested interrupt vector NVIC****** ****** **********************
/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
//Enable and configure interrupt vector for EXTI_Line9
//Enable and configure interrupt vector for EXTI_Line0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQChannel;
NVIC_Init(&NVIC_InitStructure);
//Enable and configure interrupt vector for EXTI_Line9
//Enable and configure interrupt vector for EXTI_Line13
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQChannel;
NVIC_Init(&NVIC_InitStructure);
}
*************************************************** ******************
************** System clock configuration RCC_Configuration****************** *********
void RCC_Configuration(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1) ;
RCC_PCLK1Config( RCC_HCLK_Div2);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
*/
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08)
{
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB 2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
}[page]
*****************//Initialize independent watchdog ************************************
*********************************************************************
********************************************************************
****************************************************************************
void IWDG_Configuration(void)
{
//Enable or disable registers IWDG_PR and IWDG_RLR
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
//Select the operating frequency
IWDG_SetPrescaler(IWDG_Prescaler_32);
//Set the reload value
IWDG_SetReload(230);
//Reload the value of IWDG counter according to the value of reloader-----------This action is equivalent to "feeding the dog"
IWDG_ReloadCounter();
//Enable IWDG
IWDG_Enable();
}
/ *******************
/
TIM_DeInit(TIM1); //Reset peripheral TIMx registers to default values
//2. Basic settings
TIM1_TimeBaseStructure.TIM_Prescaler = 0x0; //Pre-division, this value + 1 is the divisor of the division to get the counting period;
TIM1_TimeBaseStructure.TIM_Period = 0xFFFF; //Counting period (that is, when the count value reaches the "count value", it takes exactly one counting period)
TIM1_TimeBaseStructure.TIM_ClockDivision = 0x0; //Clock factor
TIM1_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Counter mode
//TIM_CounterMode_Dowm counts down
//TIM_CounterMode_CenterAligned1 Center alignment 1
// TIM_CounterMode_CenterAligned2 Center alignment 2
// TIM_CounterMode_CenterAligned3 Center alignment 3
TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseStructure);
// 3. Output channel setting
TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
//TIM_OCMode_Timing output comparison time mode (output pin freeze is invalid)
//TIM_OCMode_Active output comparison active mode (set the output pin to a valid level when matching, and force the output to be high when the count value is the same as the compare/capture register value)
TIM_OCMode_Inactive; Output comparison inactive mode (set the output pin to an invalid level when matching, and force the output to be low when the count value is the same as the compare/capture register value)
//TIM_OCMode_Toggle output comparison trigger mode (flip. When the count value is the same as the compare/capture register value, flip the level of the output pin)
//When TIM_OCMode_PWM1 counts up, when TIMx_CNT < TIMx_CCR*, the output level is valid, otherwise it is invalid,
//When counting down, when TIMx_CNT > When TIMx_CCR*, the output level is invalid, otherwise it is valid
//TIM_OCMode_PWM2; opposite to PWM1 mode
TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM1_OCInitStructure.TIM_Pulse
= CCR1_Val;
TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM1_OCInitStructure. TIM_OCNPolarity = TIM_OCNPolarity_Low; TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM1_OCInitStructure.TIM_OCNIdleState
= TIM_OCIdleState_Reset; TIM_OC1Init(TIM1,&TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM1,&TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM1,&TIM1_OCInitStructure);
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; Break
=
TIM_Break_Enable;
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStructure);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
////////////AD configuration function /////////////////////////////
//Clock configuration:
// ADCCLK = PCLK2/4
//Application configuration
void ADC_configuration()
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
//ADC_ScanConvMode specifies whether the analog-to-digital conversion works in scan mode (multi-channel) or single (single channel) mode. You can set this
//as ENABLE or DISABLE.
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;
//ADC_ContinuousConvMode specifies whether the analog-to-digital conversion works in continuous or single mode. You can set this parameter to ENABLE or
//DISABLE.
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
//ADC_ExternalTrigConv description
//ADC_ExternalTrigConv_T1_CC1 selects capture compare 1 of timer 1 as external trigger for conversion
//ADC_ExternalTrigConv_T1_CC2 selects capture compare 2 of timer 1 as external trigger for conversion
//ADC_ExternalTrigConv_T1_CC3 selects capture compare 3 of timer 1 as external trigger for conversion
//ADC_ExternalTrigConv_T2_CC2 selects capture compare 2 of timer 2 as external trigger for conversion
//ADC_ExternalTrigConv_T3_TRGO selects TRGO of timer 3 as external trigger for conversion
//ADC_ExternalTrigConv_T4_CC4 selects capture compare 4 of timer 4 as external trigger for conversion
//ADC_ExternalTrigConv_Ext_IT11 selects external interrupt line 11 Event as external trigger for conversion
//ADC_ExternalTrigConv_None Conversion is performed by software instead of external trigger
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
//ADC_DataAlign description:
//ADC_DataAlign_Right ADC data is aligned to the right
//ADC_DataAlign_Left ADC data is aligned to the left
ADC_InitStructure.ADC_NbrOfChannel = 1; // Specifies the number of ADC channels for regular conversion in sequence. The value range of this number is 1 to 16.
//ADC_NbreOfChannel
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);
//(ADC1 or ADC2,ADC_Channel_X,Rank,ADC_SampleTime);
//Regular group sampling order. The value range is 1 to 16.
// ADC_SampleTime: //ADC_SampleTime_1Cycles5 sampling time is 1.5 cycles
//ADC_SampleTime_7Cycles5 sampling time is 7.5 cycles
//ADC_SampleTime_13Cycles5 sampling time is 13.5 cycles
//ADC_SampleTime_28Cycles5 sampling time is 28.5 cycles
//ADC_SampleTime_41Cycles5 sampling time is 41.5 cycles
//ADC_SampleTime_55Cycles5 sampling time is 55.5 cycles
//ADC_SampleTime_71Cycles5 sampling time is 71.5 cycles
//ADC_SampleTime_239Cycles5 sampling time is 239.5 cycles
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus (ADC1)); //Wait for the ADC calibration register to be set
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
AD_value=ADC_GetConversionValue(ADC1); //Return the most recent conversion result of the ADCx rule group
}
void DMA_Configuration(void)
{
DMA_DeInit(DMA_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //Define DMA peripheral base
addressDMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //Define DMA memory base addressDMA_InitStructure.DMA_DIR
= DMA_DIR_PeripheralSRC; //Peripheral as the source of data transmission (peripheral to memory) DMA_DIR_PeripheralDST——memory to peripheralDMA_InitStructure.DMA_BufferSize
= 3; //Continuously convert 3 AD channel valuesDMA_InitStructure.DMA_PeripheralInc
= DMA_PeripheralInc_Disable; //Peripheral register address remains
unchangedDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //Memory register address increments
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //Peripheral data width 32bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; //Memory data width 32bit
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA mode is circular
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA priority
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA is not set for memory to memory transfer
DMA_Init(DMA_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA_Channel1, ENABLE);
}
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
void DAC_Configuration(void)
{
DAC_InitTypeDef DAC_InitStructure;
DAC_InitStructure.DAC_Trigger=DAC_Trigger_Software; //DAC trigger mode is software control
DAC_InitStructure.DAC_WaveGeneration=DAC_WaveGeneration_None; //Do not generate waveform from DAC port
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bits8_0; //8-bit DA waveform generation mode
DAC_InitStructure.DAC_OutputBuffer=DAC_OutputBuffer_Enable; //Enable DAC output buffer
//Initialize DAC channel 1 with the above parameters
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE); //Enable DAC channel 1
DAC_Init(DAC_Channel_2, &DAC_InitStructure); //Initialize DAC channel 1 with the above parameters
DAC_Cmd(DAC_Channel_2, ENABLE); //Enable DAC channel 2
DA usage:
DAC_SetChannel2Data(DAC_Align_12b_L, DAC_data); //Set DAC channel 1 to 12 bits and left-aligned data mode
DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE); //Enable software trigger mode of DAC channel 1 and convert once
DAC_SetChannel1Data(DAC_Align_12b_L, DAC_data); //Set DAC channel 1 to 12 bits and left-aligned data mode
DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE); // Enable software trigger mode for DAC channel 1 and convert once
}