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

📄 main.c

📁 有关STM32精确延时的程序
💻 C
字号:
/****************************************Copyright (c)**************************************************
**                       思 蜕 盟 豆 皮 开 发 小 组
**                             stmfans 论坛
**
**                   QQ 群: 65081316  StmFans思蜕盟 1组
**                   QQ 群: 68584951  StmFans思蜕盟 2组
**                      http://www.stmfans.com/bbs/
**
** This program was produced by the
** IAR Embedded Workbench 4.0 Kickstart 442
** Copyright 2008-2009 stmfans 
** Chip type           : STM32F103VB
** Program type        : Application
** Clock frequency     : 8.000000 MHz
** Memory model        : 
** External SRAM size  : 
** Data Stack size     : 
**--------------文件信息--------------------------------------------------------------------------------
**文   件   名: Main.c
**创   建   人: 陈海
**最后修改日期: 2008年10月23日
**描        述: 豆皮开发板教程
**              
**--------------历史版本信息----------------------------------------------------------------------------
** 创建人: 陈海
** 版  本: v0.01
** 日 期: 2008年10月23日
** 描 述: 原始版本
**
**--------------当前版本信息----------------------------------------------------------------------------
** 创建人: 陈海
** 版  本: v0.01
** 日 期: 2008年10月23日
** 描 述: 当前版本
**
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "main.h"

static vu32 TimingDelay;

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

  //配置系统时钟
  RCC_Configuration();

  //配置  NVIC 和 Vector Table 
  NVIC_Configuration();

  //改变指定管脚的映射  GPIO_Remap_SWJ_Disable SWJ 完全失能(JTAG+SW-DP)
  //GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
  
  //改变指定管脚的映射  GPIO_Remap_SWJ_JTAGDisable  JTAG-DP 失能 + SW-DP使能
  //GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
  
  //配置LED使用的GPIO口
  LED_GPIO_Configuration();
  
  //配置KEY使用的GPIO口
  //KEY_GPIO_Configuration();

  //配置buzzer蜂鸣器使用的端口
  //BUZZER_GPIO_Configuration();
  
  SysTick_Config();

  //打开 LED2 其他的关闭显示
  GPIO_SetBits(GPIOC, GPIO_Pin_10);
  GPIO_SetBits(GPIOC, GPIO_Pin_12);
  GPIO_ResetBits(GPIOC, GPIO_Pin_11); 
  
  //主循环 
  while (1)
  {
    // 每隔1S钟就点亮一次LED
    Delay_Ms(500);
    GPIO_SetBits(GPIOC, GPIO_Pin_12);
    Delay_Ms(500);
    GPIO_ResetBits(GPIOC, GPIO_Pin_12); 

     //循环扫描按键 按键按下时 对应的LED亮
    //LED_For_Key_Shine( KEY_GPIO_Scanning() );
  }
}

/*******************************************************************************
* Function Name  : SysTick_Config
* Description    : Configures SysTick
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
//SysTick设置
void SysTick_Config(void)
{
    /* Disable SysTick Counter */
    SysTick_CounterCmd(SysTick_Counter_Disable);
  
    /* Disable the SysTick Interrupt */
    SysTick_ITConfig(DISABLE);
  
    /* Configure HCLK clock as SysTick clock source */
    SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);

    /* SysTick interrupt each 1000 Hz with HCLK equal to 72MHz */
    SysTick_SetReload(9000);

    /* Enable the SysTick Interrupt */
    SysTick_ITConfig(ENABLE);

}

/*******************************************************************************
* Function Name  : Delay
* Description    : Inserts a delay time.
* Input          : nTime: specifies the delay time length, in milliseconds.
* Output         : None
* Return         : None
*******************************************************************************/
void Delay_Ms(u32 nTime)
{
  /* Enable the SysTick Counter */
  SysTick_CounterCmd(SysTick_Counter_Enable);
  
  TimingDelay = nTime;

  while(TimingDelay != 0);

  /* Disable SysTick Counter */
  SysTick_CounterCmd(SysTick_Counter_Disable);
  /* Clear SysTick Counter */
  SysTick_CounterCmd(SysTick_Counter_Clear);
}

/*******************************************************************************
* Function Name  : TimingDelayMs_Decrement
* Description    : Decrements the TimingDelay variable.
* Input          : None
* Output         : TimingDelay
* Return         : None
*******************************************************************************/
void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  { 
    TimingDelay--;
  }
}

/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
  ErrorStatus HSEStartUpStatus;
  
  //将外设 RCC寄存器重设为缺省值
  RCC_DeInit();

  //设置外部高速晶振(HSE)
  RCC_HSEConfig(RCC_HSE_ON);

  //等待 HSE 起振 
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if(HSEStartUpStatus == SUCCESS)
  {
    //预取指缓存使能
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

     //设置代码延时值
    //FLASH_Latency_2  2 延时周期
    FLASH_SetLatency(FLASH_Latency_2);

    //设置 AHB 时钟(HCLK)
    //RCC_SYSCLK_Div1  AHB 时钟 =  系统时钟 
    RCC_HCLKConfig(RCC_SYSCLK_Div1);

     //设置高速 AHB 时钟(PCLK2)
    //RCC_HCLK_Div2  APB1 时钟  = HCLK / 2 
    RCC_PCLK2Config(RCC_HCLK_Div1);

    //设置低速 AHB 时钟(PCLK1)
    //RCC_HCLK_Div2  APB1 时钟  = HCLK / 2 
    RCC_PCLK1Config(RCC_HCLK_Div2);

    // PLLCLK = 8MHz * 9 = 72 MHz 
    //设置 PLL 时钟源及倍频系数
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    //使能或者失能 PLL
    RCC_PLLCmd(ENABLE);

    //等待指定的 RCC 标志位设置成功 等待PLL初始化成功
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }


    //设置系统时钟(SYSCLK) 设置PLL为系统时钟源
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    //等待PLL成功用作于系统时钟的时钟源
    //  0x00:HSI 作为系统时钟 
    //  0x04:HSE作为系统时钟 
    //  0x08:PLL作为系统时钟  
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }

  //使能或者失能 APB2 外设时钟
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
  
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LED_GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_4 | GPIO_Pin_3 | GPIO_Pin_2 ;
  //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  //GPIO_Init(GPIOD, &GPIO_InitStructure); 

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ;
  //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  //GPIO_Init(GPIOA, &GPIO_InitStructure); 
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void KEY_GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOC, &GPIO_InitStructure); 
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}

/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
u8 KEY_GPIO_Scanning(void)
{
	u8 scan_bit;  //单个按键扫描变量
        u8 scan_sum;  //四个按键总的情况变量 scan_sum低四位的每一位对应一个按键

	scan_bit = 0;
        scan_sum = 0;

        //扫描按键
	scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8);
        //如果按键按下 则延迟 再扫描 然后根据判断处理
        if( 0x01 == scan_bit )
        {
          delay();
          scan_bit = 0;
          scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8);
          if( 0x01 == scan_bit )
            scan_sum |= GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8);
          scan_bit = 0;
        }
        
        scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7);
        if( 0x01 == scan_bit )
        {
          delay();
          scan_bit = 0;
          scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7);
          if( 0x01 == scan_bit )
            scan_sum |= GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)<<1;
          scan_bit = 0;
        }
        
        scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6);
        if( 0x01 == scan_bit )
        {
          delay();
          scan_bit = 0;
          scan_bit = GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6);
          if( 0x01 == scan_bit )
            scan_sum |= GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_6)<<2;
          scan_bit = 0;
        }
        
        scan_bit = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15);
        if( 0x01 == scan_bit )
        {
          delay();
          scan_bit = 0;
          scan_bit = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15);
          if( 0x01 == scan_bit )
            scan_sum |= GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15)<<3;
          scan_bit = 0;
        }
        
	return scan_sum;
}

/*******************************************************************************
* Function Name  : BUZZER_GPIO_Configuration
* Description    : Configures the BUZZER GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void BUZZER_GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

  //蜂鸣器使用的GPIOB 的 Pin9 端口
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  //蜂鸣器端口设置为 推挽输出
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
}


/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void LED_For_Key_Shine(u8 scan_sum)
{
    //使用一个8位变量 表示当前是否有按键按下
    u8 key_count = 4 ;
    
    if( scan_sum & 0x01 )
    {
      GPIO_SetBits(GPIOD, GPIO_Pin_2);
      key_count--;
    }
    else
    {
      GPIO_ResetBits(GPIOD, GPIO_Pin_2);
      key_count++;
    }
  
    if( scan_sum & 0x02 )
    {
      GPIO_SetBits(GPIOD, GPIO_Pin_3);
      key_count--;
    }
    else
    {
      GPIO_ResetBits(GPIOD, GPIO_Pin_3);
      key_count++;
    }
  
    if( scan_sum & 0x04 )
    {
      GPIO_SetBits(GPIOD, GPIO_Pin_4);
      key_count--;
    }
    else
    {
      GPIO_ResetBits(GPIOD, GPIO_Pin_4);
      key_count++;
    }
    
    if( scan_sum & 0x08 )
    {
      GPIO_SetBits(GPIOD, GPIO_Pin_5);
      key_count--;
    }
    else
    {
      GPIO_ResetBits(GPIOD, GPIO_Pin_5);  
      key_count++;
    }

    //如果 key_count为0 则表示当前没有按键被按下 蜂鸣器不响
    if( key_count == 0 )
      GPIO_ResetBits(GPIOB, GPIO_Pin_9);
    else
      GPIO_SetBits(GPIOB, GPIO_Pin_9);

}


/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures NVIC and Vector Table base location.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#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
}


void delay()
{
  int i;
  for (i=0; i<0xffff; i++)
    ;
}

#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 + -