📄 systm_init.c
字号:
#include "stm32f10x_lib.h"
#include "stm32f10x_it.h"
#include "PWM_adjust.h"
#include "uart.h"
#include "adc.h"
#include "key.h"
/************************************************************************/
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM1_TimeBaseStructure;
TIM_OCInitTypeDef TIM1_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
ErrorStatus HSEStartUpStatus;
//DMA_InitTypeDef DMA_InitStructure;
/*******************************************************************************
* 函数名 : SysTick定义
* 描述 : 定义SysTick,使之每100uS产生一次中断。
* 输入 : 无
* 输出 : 无
* 返回 : 无
*******************************************************************************/
void SysTick_Configuration(void)
{
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);// 选择 AHB 时钟(HCLK) 作为 SysTick 时钟源
NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 1, 0); // 设定 SysTick Priority 为 3
SysTick_SetReload(72000);// SysTick interrupt each 100uS with HCLK equal to 72MHz
SysTick_ITConfig(ENABLE);// Enable the SysTick Interrupt Disable //ENABLE
SysTick_CounterCmd(SysTick_Counter_Enable); //Enable
}
/*******************************************************************************
* 函数名 : RCC定义
* 描述 : 定义不同的系统时钟
* 输入 : 无
* 输出 : 无
* 返回 : 无
*******************************************************************************/
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)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1); // HCLK = SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //PCLK2 = HCLK
RCC_PCLK1Config(RCC_HCLK_Div2); // PCLK1 = HCLK/2
RCC_ADCCLKConfig(RCC_PCLK2_Div4); // ADCCLK = PCLK2/4
FLASH_SetLatency(FLASH_Latency_2); // Flash 2 wait state
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);// Enable Prefetch Buffer
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_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC1 |
RCC_APB2Periph_ADC2 |RCC_APB2Periph_USART1|RCC_APB2Periph_TIM1 |RCC_APB2Periph_AFIO, ENABLE);// Enable GPIOA and AFIO clocks
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
}
/*******************************************************************************
* 函数名 : NVIC定义
* 描述 : 定义中断向量表
* 输入 : 无
* 输出 : 无
* 返回 : 无
*******************************************************************************/
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_PriorityGroupConfig(NVIC_PriorityGroup_2);// Configure one bit for preemption priority
//USART1
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/**************************** Enable ADC IRQChannel ******************************/
NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQChannel ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* 函数名 : GPIO定义
* 描述 : 定义不同的GPIO端口
* 输入 : 无
* 输出 : 无
* 返回 : 无
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PA.0,1 (ADC Channel) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTx_Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTx_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PA.8口设置为TIM1的OC1输出口
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);
//PB.13口设置为TIM1_CH1N输出口
//------------------------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//---------------------------------------------------
/*GPIOA Configuration: TIM3 channel 1 as alternate function push-pull */
/* GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);*/
/* Configure PB8/12/14/15 as Output push-pull */
//--------------------------PB1..PB13-------------------------
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_8|GPIO_Pin_12|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure); //LCD
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//----------------------------------------------------------------
/*GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6; //PB4 FOR WORK;PB5 FOR STOP;PB6 FOR WARNING
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure); //LED
*/
}
void Sys_Configuration(void)
{
RCC_Configuration(); //定义系统时钟
SysTick_Configuration();//定义SysTick
NVIC_Configuration();//定义中断向量表
GPIO_Configuration();//定义GPIO
PWM_DMA_Configuration();
TIM_Configuration();
USART_Configuration();
adc_Configuration();
key_GPIO_init();
//TEXTI_Configuration();//定义外部中断端口
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -