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

📄 init.c

📁 STM32F107串口,GPIO 测试程序
💻 C
字号:
#include "stm32f10x.h"
#include "init.h"
//串口1初始化
void USART1_Configuration( uint32_t baud)
{
	USART_InitTypeDef USART_InitStructure;		   //定义结构体变量
	USART_InitStructure.USART_BaudRate            = baud  ;	// 波特率
	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_HardwareFlowControl = USART_HardwareFlowControl_None;	 //无流控
	USART_InitStructure.USART_Mode                = USART_Mode_Rx | USART_Mode_Tx;	//收发使能

	USART_Init(USART1, &USART_InitStructure);	//参数初始化
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//串口1接收中断使能
	USART_Cmd(USART1, ENABLE); //使能USART外设    
/*	DIR485_L;  */
}

//系统中断管理
void NVIC_Configuration(void)
{ 	 NVIC_InitTypeDef NVIC_InitStructure; 
  	/* Configure the NVIC Preemption Priority Bits */  
	//选择使用优先级分组
  	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

	#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
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  	NVIC_Init(&NVIC_InitStructure);
}

void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	/* Configure IO connected to LD1, LD2, LD3 and LD4 leds *********************/	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_Init(GPIOD, &GPIO_InitStructure);

   	/* Configure USART1 Tx (PA.09) as alternate function push-pull */
  	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);
    
  	/* Configure USART1 Rx (PA.10) as input floating */
  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  	GPIO_Init(GPIOA, &GPIO_InitStructure);
	/*按键输入*/
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入模式
  	GPIO_Init(GPIOC, &GPIO_InitStructure);

	
}


//配置系统时钟,使能各外设时钟  
/*
系统的时钟可以有3个来源:内部时钟HSI,外部时钟HSE,或者PLL(锁相环模块)的输出。它们由RCC_CFGR寄存器中的SW来选择。
SW(1:0):系统时钟切换,由软件置'1'或清'0'来选择系统时钟源。在从停止或待机模式中返回时或直接或间接作为系统时钟
的HSE出现故障时,由硬件强制选择HSI作为系统时钟(如果时钟安全系统已经启动) 
		00:HSI作为系统时钟; 
		01:HSE作为系统时钟; 
		10:PLL输出作为系统时钟; 
		11:不可用PLL的输出直接送到USB模块,经过适当的分频后得到48M的频率供USB模块使用。
系统时钟的一路被直接送到I2S模块;另一路经过AHB分频后送出,送往各个系统,其中直接送往SDI,FMSC,AHB总线;8分频后作为系
统定时器时钟;经过APB1分频分别控制PLK1、定时器TIM2~TIM7;经过APB2分频分别控制PLK2、定时器TIM1~TIM8、再经分频控制ADC;
*/

void RCC_Configuration(void)
{
	SystemInit();	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA 
                           |RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
                           |RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE
						   |RCC_APB2Periph_ADC1  | RCC_APB2Periph_AFIO 
                           |RCC_APB2Periph_SPI1, ENABLE );
  // RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL ,ENABLE );
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 
                           |RCC_APB1Periph_USART3|RCC_APB1Periph_TIM2	                           
                           , ENABLE );
	 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
}

//配置所有外设
void Init_All_Periph(void)
{
	RCC_Configuration();	
//nitDis();
//	GLCD_Test();
	GPIO_Configuration();
	NVIC_Configuration();
	USART1_Configuration(9600);
//	USART1Write((u8*)"    GoldBull  ADC_example ",sizeof("    GoldBull  GPIO_example "));
}

⌨️ 快捷键说明

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