⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 init_config.c

📁 STM32 ID加密算法
💻 C
字号:
#include"init_config.h"


/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures System Clocks
*******************************************************************************/
void RCC_Configuration(void)
{
	/*枚举变量,定义高速时钟的启动状态*/
	ErrorStatus HSEStartUpStatus;
	
	/*将RCC寄存器重设为默认值*/
	RCC_DeInit();

	/*使能外部高速晶振HSE*/
	RCC_HSEConfig(RCC_HSE_ON);
	
	/*等待外部晶振稳定,超时则退出*/
	HSEStartUpStatus=RCC_WaitForHSEStartUp();

   	/*判断HSE是否就绪*/
	/*SUCCESS:HSE稳定且就绪,ERROR:HSE未就绪*/
	if(HSEStartUpStatus==SUCCESS) //若外部时钟就绪
	{
		/*设置高速总线时钟(AHB)=系统时钟,HCLK=SYSCLK*/
		RCC_HCLKConfig(RCC_SYSCLK_Div1);//将系统时钟进行1分频后,作为AHB总线时钟			
	   	
		/*设置低速外部总线1的时钟=高速总线时钟的2分频,PCLK1=HCLK/2*/
	  	RCC_PCLK1Config(RCC_HCLK_Div2);

		/*设置低速外部总线2的时钟=高速总线时钟的1分频,PCLK2=HCLK*/
		RCC_PCLK2Config(RCC_HCLK_Div1);

		/*设置FLASH储存器延时时钟周期数,2是针对高频时钟的
		  FLASH_Latency_0:0延时周期,FLASH_Latency_1:1延时周期
		  FLASH_Latency_2:2延时周期*/
		FLASH_SetLatency(FLASH_Latency_2);

		/*使能FLASH预取指令缓冲区,这两句跟RCC没有直接关系*/
		FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

		/*利用锁相环将HSE外部8MHz晶振9倍频到72MHz*/
		RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

		/*使能PLL*/
		RCC_PLLCmd(ENABLE);

	  	/*等待PLL输出稳定*/
		/*RCC_FLAG_HSIRDY: HSI晶振就绪,RCC_FLAG_HSERDY: HSE晶振就绪
		  RCC_FLAG_PLLRDY: PLL就绪,    RCC_FLAG_LSERDY: LSE晶振就绪
		  RCC_FLAG_LSIRDY: LSE晶振就绪,RCC_FLAG_PINRST: 引脚复位
		  RCC_FLAG_PORRST: POR/PDR复位,RCC_FLAG_SFTRST: 软件复位
		  RCC_FLAG_IWDGRST: IWDG复位,  RCC_FLAG_WWDGRST: WWDG复位
		  RCC_FLAG_LPWRRST: 低功耗复位  */
		while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET){}

		/*设置系统时钟来自PLL输出*/
		/*RCC_SYSCLKSource_HSI: 选择HSI作为系统时钟
		  RCC_SYSCLKSource_HSE: 选择HSE作为系统时钟
		  RCC_SYSCLKSource_PLLCLK: 选择PLL作为系统时钟*/
		RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

		/*等待PLL时钟作为系统时钟*/
		/*系统时钟标志位:0x00:HSI作为系统时钟
						  0x04:HSE作为系统时钟
						  0x08:PLL作为系统时钟*/
		while(RCC_GetSYSCLKSource()!=0x08){}
	 }
       
	/*使能GPIOA-GPIOE和AFIO的时钟*/
   	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO,ENABLE);

	/*使能串口USART1的时钟*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

	/*使能定时器TIM1时钟*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

	/*使能定时器TIM2的时钟*/
	RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

	/*使能ADC1的时钟*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures GPIO Modes
*******************************************************************************/
void GPIO_Configuration()
{
	/*定义GPIO初始化结构体*/
	GPIO_InitTypeDef GPIO_InitStructure;


	/*配置USART1的引脚*/
   	/*配置USART1 TX,使PA.09为复用推挽输出*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	/*配置USART1 RX,使PA.10为浮空输入*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA,&GPIO_InitStructure);


	/*配置LED端口*/
	/*配置PB.08 PB.09为LED的推挽输出控制*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	/*配置PC.12 PC.13为LED的推挽输出控制*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStructure);


	/*配置按键PC.08-PC.11为浮空输入*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStructure);


	/*配置电机端口*/
   	/*配置电机控制端口PD.07 PD.08 PD.09 PD.10为推挽输出*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOD,&GPIO_InitStructure);

	/*配置红外接口,或USART2接口*/
	/*配置PA.02为复用推挽输出*/		
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	/*配置PA.03为浮空输入*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
   	
	/*配置AD端口PB.0*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
	GPIO_Init(GPIOB,&GPIO_InitStructure);

   /* Configure infrared IO*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
  	
	
	/*配置中断输入PE.04 PE.05*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5;;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
	GPIO_Init(GPIOE,&GPIO_InitStructure);


	/*配置LCD1206端口*/
	/*配置数据接口PC0-PC7为推挽输出*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3
								|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	/*配置PD.04-PD.06为1206推挽输出控制端*/
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOD,&GPIO_InitStructure);
}

/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures NVIC Modes for T1
*******************************************************************************/ 
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure,NVIC_InitStruct;
  EXTI_InitTypeDef EXTI_InitStructure;
#ifdef  VECT_TAB_RAM  
  // Set the Vector Table base location at 0x20000000 
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 
#else  // VECT_TAB_FLASH  
  // Set the Vector Table base location at 0x08000000 
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif

  // Configure the NVIC Preemption Priority Bits[配置优先级组]   
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  
  // Enable the TIM1 gloabal Interrupt [允许TIM1全局中断]
  NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  // Enable the TIM2 gloabal Interrupt [允许TIM2全局中断]
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  // Enable the RTC Interrupt 
  NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  // Configure INT IO  PC9 enable exti9_5
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource9);  
  EXTI_InitStructure.EXTI_Line=EXTI_Line9;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  NVIC_InitStruct.NVIC_IRQChannel = EXTI9_5_IRQn;
  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority =0;
  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStruct.NVIC_IRQChannelCmd =ENABLE;
  NVIC_Init(&NVIC_InitStruct);

  // Configure INT IO  PE4 enable exti4
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4);  
  EXTI_InitStructure.EXTI_Line=EXTI_Line4;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);
  
  NVIC_InitStruct.NVIC_IRQChannel = EXTI4_IRQn;
  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority =0;
  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStruct.NVIC_IRQChannelCmd =ENABLE;
  NVIC_Init(&NVIC_InitStruct);

  // Configure INT IO  PE5 enable exti9_5
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource5);  
  EXTI_InitStructure.EXTI_Line=EXTI_Line5;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  NVIC_InitStruct.NVIC_IRQChannel = EXTI9_5_IRQn;
  NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority =0;
  NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStruct.NVIC_IRQChannelCmd =ENABLE;
  NVIC_Init(&NVIC_InitStruct);
} 
  
/*******************************************************************************
* Function Name  : USART_Configuration
* Description    : Configures the USART1.
*******************************************************************************/
void USART_Configuration(void)
{
	USART_InitTypeDef USART_InitStructure;
	USART_ClockInitTypeDef USART_ClockInitStructure;

	/* 配置USART1-------------------------------------------------------------------*/
  	/* USART1 configured as follow:
        - BaudRate = 115200 baud 波特率为115200 
        - Word Length = 8 Bits	 数据宽度8位
        - One Stop Bit			 一位停止位
        - No parity				 无校验位
        - Hardware flow control disabled (RTS and CTS signals)无硬件流控制
        - Receive and transmit enabled		使能接收发送传输
        - USART Clock disabled				禁止串口时钟
        - USART CPOL: Clock is active low   时钟低电平,指定了下个SCLK引脚上时钟输出的极性
        - USART CPHA: Data is captured on the middle 时钟第二个边沿进行数据捕获,指定SCLK引脚时钟输出的相位
        - USART LastBit: The clock pulse of the last data bit is not output to 
                         the SCLK pin  最后一位数据的时钟不从SCLK输出
	*/
	USART_InitStructure.USART_BaudRate=115200;					//波特率115200
	USART_InitStructure.USART_WordLength=USART_WordLength_8b; 	//8位传输
	USART_InitStructure.USART_StopBits=USART_StopBits_1;	   	//1个停止位
	USART_InitStructure.USART_Parity=USART_Parity_No;			//无校验位
	USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;	//Rx,Tx模式
	USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//禁止硬件流控制,禁止RTS,CTS信号
	USART_Init(USART1,&USART_InitStructure);

	/*USART1时钟配置*/
	USART_ClockInitStructure.USART_Clock=USART_Clock_Disable;	  //禁止串口时钟
	USART_ClockInitStructure.USART_CPOL=USART_CPOL_Low;
	USART_ClockInitStructure.USART_CPHA=USART_CPHA_2Edge;
	USART_ClockInitStructure.USART_LastBit=USART_LastBit_Disable;
	USART_ClockInit(USART1,&USART_ClockInitStructure);

//	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//使能USART中断
	//USART_ITConfig(USART1,USART_IT_TXE,ENABLE);//开启发送中断设置,此处最好不用

	/*清除USART1发送完成标志*/
	USART_ClearFlag(USART1,USART_FLAG_TC);//一个一个字节发送时使用USART_FLAG_TXE,连续发送时用USART_FLAG_TC
	/*使能USART1*/
	USART_Cmd(USART1,ENABLE);
}

/*******************************************************************************
* Function Name  : fputc
* Description    : Retargets the C library printf function to the USART.
*******************************************************************************/
int fputc(int ch, FILE *f)
{
	/*重载fputc函数,例:写一个字符到USART1*/
	USART_SendData(USART1,(u8)ch);			//向USART1发送一个字符ch
	/*等待USART1发送完毕*/
	while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);  
  
 	return ch;
}

/*******************************************************************************
* Function Name  : BSP_Init
* Description    : Configures RCC,GPIO,NVIC
*******************************************************************************/
//void BSP_Init()
//{
  /* Configure the system clocks */
 // RCC_Configuration();
    
  /* GPIO Configuration */
//  GPIO_Configuration();

  /* NVIC Configuration */
 // NVIC_Configuration();
//}

/*******************************************************************************
* Function Name  : delay_nus
* Description    : delay n us
*******************************************************************************/
void delay_nus(u32 n)  //延时n us: n>=6,最小延时单位6us
{ 
  u8 j;
  while(n--)              // 外部晶振:8M;PLL:9;8M*9=72MHz
  {
    j=8;				  // 微调参数,保证延时的精度
	while(j--);
  }
}

/*******************************************************************************
* Function Name  : delay_nms
* Description    : delay n ms
*******************************************************************************/
void delay_nms(u32 n)  //延时n ms
{
  while(n--)		   // 外部晶振:8M;PLL:9;8M*9=72MHz
    delay_nus(1100);   // 1ms延时补偿
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -