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

📄 main.c

📁 stm32初级例程
💻 C
字号:
/*
 * FileName:        main.c
 * Description:     例程主文件    
 */

/**************************************************************
** 	精英STM32开发板
**  版本:V1.0
**  功能:按下按键,对应的LED点亮 
***************************************************************/

#include "include.h"

#define LED2_ON GPIO_ResetBits(GPIOC, GPIO_Pin_2)
#define LED2_OFF GPIO_SetBits(GPIOC, GPIO_Pin_2)

#define LED1_ON GPIO_ResetBits(GPIOC, GPIO_Pin_1)
#define LED1_OFF GPIO_SetBits(GPIOC, GPIO_Pin_1)

#define LED0_ON GPIO_ResetBits(GPIOC, GPIO_Pin_0)
#define LED0_OFF GPIO_SetBits(GPIOC, GPIO_Pin_0)

#define KEY1_ON GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2)==1
#define KEY1_OFF GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2)!=1

#define KEY2_ON GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3)==1
#define KEY2_OFF GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3)!=1

#define KEY3_ON GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4)==1
#define KEY3_OFF GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4)!=1


/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);


int main()
{

	/* System Clocks Configuration */
	RCC_Configuration();//配置系统时钟   
	
	GPIO_Configuration();//配置GPIO
	
	/* NVIC configuration */
	NVIC_Configuration();//配置中断

	LED0_OFF;
	LED1_OFF;
	LED2_OFF;

//按下按键,对应的LED点亮
	while(1)
	{
		if(KEY1_ON)
		{
			LED0_ON;
			while(KEY1_ON);
			LED0_OFF;
		}
		else if(KEY2_ON)
		{
			LED1_ON;
			while(KEY2_ON);
			LED1_OFF;			
		}
		else if(KEY3_ON)
		{
			LED2_ON;
			while(KEY3_ON);
			LED2_OFF;			
		}		
	}				
}



/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{ErrorStatus HSEStartUpStatus;
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);//使能外部时钟 

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待外部时钟就绪

  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer 启用预取缓冲器 */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* Flash 2 wait state FLASH设置2个等待周期*/
    FLASH_SetLatency(FLASH_Latency_2);
 
    /* HCLK = SYSCLK 设置系统设置*/
    RCC_HCLKConfig(RCC_SYSCLK_Div1); 
  
    /* PCLK2 = HCLK PCLK2时钟=主时钟*/
    RCC_PCLK2Config(RCC_HCLK_Div1); 

    /* PCLK1 = HCLK/2 PCLK1时钟为主时钟1/2*/
    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* PLLCLK = 8MHz * 9 = 72 MHz 设置时钟为72M*/
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

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

    /* Wait till PLL is ready 等待PLL工作稳定*/
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }

    /* Select PLL as system clock source 选择PLL做为系统时钟源*/
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source 准备就绪,开始干活*/
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOE, ENABLE );
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	/* Configure IO connected to LD0, LD1, LD2 leds *********************/	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_Init(GPIOC, &GPIO_InitStructure);

	/* Configure IO connected to KEY1, KEY2, KEY3  *********************/	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4;
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  	GPIO_Init(GPIOE, &GPIO_InitStructure);
  
}

/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures Vector Table base location.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
//#ifdef  VECT_TAB_RAM
#if defined (VECT_TAB_RAM)
  /* Set the Vector Table base location at 0x20000000 */ 
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //设置中断向量在RAM中
//#elif defined(VECT_TAB_FLASH_IAP)
//  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */ 
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //设置中断向量在FLASH中  
#endif 
}




#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

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