📄 system.c
字号:
#include "stm32f10x_lib.h"
#include "system.h"
ErrorStatus HSEStartUpStatus;
RCC_ClocksTypeDef RCC_ClockFreq;
//u16 CCR1_Val = 1;
u16 CCR1_Val = 1;
u16 CCR2_Val = 500;
u16 CCR3_Val = 250;
u16 CCR4_Val = 125;
//******************************************************************************
// Function Name : RCC_Configuration
// Description : Configures the different system clocks.
// Input : None
// Output : None
// Return : None
//******************************************************************************
void RCC_Configuration(void)
{
RCC_DeInit(); // RCC system reset(for debug purpose)
RCC_HSEConfig(RCC_HSE_ON); // Enable HSE
HSEStartUpStatus = RCC_WaitForHSEStartUp(); // Wait till HSE is ready
if(HSEStartUpStatus == SUCCESS) {
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); // Enable Prefetch Buffer
FLASH_SetLatency(FLASH_Latency_2); // Flash 2 wait state
RCC_HCLKConfig(RCC_SYSCLK_Div1); // HCLK = SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); // PCLK2 = HCLK
RCC_PCLK1Config(RCC_HCLK_Div4); // PCLK1 = HCLK/4
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); // PLLCLK = 8MHz * 9 = 72 MHz
RCC_PLLCmd(ENABLE); // Enable PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {} // Wait till PLL is ready
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Select PLL as system clock source
while(RCC_GetSYSCLKSource() != 0x08) {} // Wait till PLL is used as system clock source
}
//
RCC_GetClocksFreq(&RCC_ClockFreq); // This fills a RCC_ClocksTypeDef structure with the current frequencies.
// The RCC_ClockFreq members should be as following:
// RCC_ClockFreq.SYSCLK_Frequency = 72000000
// RCC_ClockFreq.HCLK_Frequency = 72000000
// RCC_ClockFreq.PCLK1_Frequency = 36000000
// RCC_ClockFreq.PCLK2_Frequency = 72000000
// RCC_ClockFreq.ADCCLK_Frequency = 12000000
RCC_ClockSecuritySystemCmd(ENABLE); // Enable Clock Security System(CSS): this will generate an NMI exception when HSE clock fails
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // TIM2 clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE); // GPIOA, C clock enable
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // GPIOC clock enable
}
//******************************************************************************
// Function Name : MCO_Out_Configuration
// Description : Output HSE clock on MCO pin
// Input : - RCC_MCO_SET: specifies the clock source to output.
// This parameter can be one of the following values:
// - RCC_MCO_NoClock: No clock selected
// - RCC_MCO_SYSCLK: System clock selected
// - RCC_MCO_HSI: HSI oscillator clock selected
// - RCC_MCO_HSE: HSE oscillator clock selected
// - RCC_MCO_PLLCLK_Div2: PLL clock divided by 2 selected
// Output : None
// Return : None
//*******************************************************************************
void MCO_Out_Configuration(unsigned char RCC_MCO_SET)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// RCC_MCOConfig(RCC_MCO_HSE);
RCC_MCOConfig(RCC_MCO_SET);
}
//*******************************************************************************
// Function Name : GPIO_Configuration
// Description : Configure the GPIOD Pins.
// Input : None
// Output : None
// Return : None
//*******************************************************************************
void GPIO_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_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure); // GPIOC Configuration: Pin6, 7, 8 and 9 in Output
}
//******************************************************************************
// Function Name : GPIO_Configuration
// Description : TIM2 Configuration: Output Compare Inactive Mode:
// TIM2CLK = 36 MHz, Prescaler = 35999, TIM2 counter clock = 1 KHz
// TIM2_CH1 delay = CCR1_Val/TIM2 counter clock = 1000 ms
// Input : None
// Output : None
// Return : None
//******************************************************************************
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_ARRPreloadConfig(TIM2, DISABLE);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); // Time base configuration
TIM_PrescalerConfig(TIM2, 35999, TIM_PSCReloadMode_Immediate); // 进行36000(35999 + 1)分频
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInit(TIM2, &TIM_OCInitStructure); // Output Compare Active Mode configuration: Channel1
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); // TIM IT enable
}
//******************************************************************************
// Function Name : NVIC_Configuration
// Description : Configure the nested vectored interrupt controller.
// Input : None
// Output : None
// Return : None
//*****************************************************************************
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); // Set the Vector Table base location at 0x20000000
#else // VECT_TAB_FLASH
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); // Set the Vector Table base location at 0x08000000
#endif
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; // Enable the TIM2 Interrupt
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -