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

📄 main.c

📁 stm32初级例程
💻 C
字号:
/*----------------------------------------------------------------------------
QQ: 958664258
21IC用户名:banhushui
交流平台:http://blog.21ic.com/user1/5817/index.html
淘宝店铺:http://shop58559908.taobao.com
旺旺:半壶水电子
编译器版本:MDK4.12
 *---------------------------------------------------------------------------*/

/* Includes ------------------------------------------------------------------*/
#define SYS_GLOBALS
#include "include.h"


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

#define  LED_OFF 	GPIO_WriteBit(GPIOC, GPIO_Pin_0, Bit_SET)
#define  LED_ON 	GPIO_WriteBit(GPIOC, GPIO_Pin_0, Bit_RESET)

/* Private functions ---------------------------------------------------------*/

/*----------------------------------------------------------------------------
  delay
  insert a delay time.
  软件延时,非精确延时
 *----------------------------------------------------------------------------*/
void delay(unsigned int nCount)
{
   for (; nCount != 0; nCount--)
      ;
}

/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
   debug();
#endif

   int i;

   /* System Clocks Configuration */
   RCC_Configuration();//配置系统时钟   

   GPIO_Configuration();//配置GPIO

   /* NVIC configuration */
   NVIC_Configuration();//配置中断

   /* Check if the system has resumed from IWDG reset -------------------------*/
   //检查看门狗复位标志
   if (RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)   {
      /* IWDGRST flag set */
      /* Turn on led connected to PC.08 */
      //点亮LED
//      GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_RESET);
	  LED_ON;
      
      /* Clear reset flags */
      //清除复位标志
      RCC_ClearFlag();
   }
   else
   {
      /* IWDGRST flag is not set */
      /* Turn off led connected to PC.08 */
      //熄灭LED
      //GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_SET);
	  LED_OFF;
   }

   /* IWDG timeout equal to 280 ms (the timeout may varies due to LSI frequency
      dispersion) -------------------------------------------------------------*/
   /* Enable write access to IWDG_PR and IWDG_RLR registers */
   //允许IWDG被修改
   IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

   /* IWDG counter clock: 40KHz(LSI) / 32 = 1.25 KHz */
   //设置IWDG时钟
   IWDG_SetPrescaler(IWDG_Prescaler_32);

   /* Set counter reload value to 349 */
   //设置定时器重载值
   IWDG_SetReload(349);

   /* Reload IWDG counter */
   //重载IWDG计数器
   IWDG_ReloadCounter();

   /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
   //使能IWDG
   IWDG_Enable();
   
   //循环10次重复喂狗
   //10次之后不喂狗,看门狗将复位
   for (i = 0; i < 10; i++)
   {
      delay(1000000);  // 短延时
      IWDG_ReloadCounter();  // reload the watchdog 重装载看门狗定时器

      //GPIO_WriteBit(GPIOC, GPIO_Pin_9, Bit_RESET);
	  LED_ON;

      delay(1000000);     // 短延时
      IWDG_ReloadCounter(); //reload the watchdog 重装载看门狗定时器
      //LED off LED灭
      //GPIO_WriteBit(GPIOC, GPIO_Pin_9, Bit_SET);
	  LED_OFF;
   }
   //LED on  LED亮
   //GPIO_WriteBit(GPIOC, GPIO_Pin_9, Bit_RESET);
   LED_ON;
   
   //不喂狗,等待看门狗复位
   //执行到这里,不喂狗,那么系统复位,如果系统不复位,LED不再变化,
   //可以看到LED停止闪烁一段时间又开始闪烁说明看门狗复位了
   while (1)
   {
      // Loop forever
   } // end while
}


/*******************************************************************************
* 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)
    {
    }
  }
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  
  /* Enable GPIO_LED clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);//使能GPIO时钟
  //LED_init-------------------------------------------------------
  ////配置管脚
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//最大输出速度为50MHz
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;////通用推挽输出
  GPIO_Init(GPIOC, &GPIO_InitStructure);			 //设置GPIO为输出
  
}

/*******************************************************************************
* 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 + -