STM32 启动配置

STM32 之 LED
2010-10-14 13:17

自己的USER文件组下有3个c文件,以后会按照这个程序结构写程序。

代码参考于 “ OPELC思蛻蒙 http://bbs.opelc.org/viewthread.php?tid=6441&extra=page%3D1 ”

(1)Main.c 主函数

(2)Init_External_Device.c   外设初始化函数

(3)includes.h 自己的c文件用的包含头文件

下面是源码:

(1)Main

C语言Codee#14359
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 实验平台 : ST 官方三合一套件 
+ 硬件     : STM32F103C8T6
+ 开发平台 : IAR For ARM 5.40
+ 仿真器   : J-Link
+ 日期     : 2010-10-14
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

#include "includes.h"

/*******************************************************************************
                             == Main 函数 == 
*******************************************************************************/
int  main( void)
{
RCC_Configuration();     //配置系统时钟 
NVIC_Configuration();    //配置 NVIC 和 Vector Table 

GPIO_Configuration(); 


//主循环 
while ( 1)
{
       delay();
       //设置指定的数据端口位——LED1熄灭
       GPIO_SetBits( GPIOB ,  GPIO_Pin_12);
       delay();
       //清除指定的数据端口位——LED1亮
       GPIO_ResetBits( GPIOB ,  GPIO_Pin_12);
       delay();
       GPIO_SetBits( GPIOB ,  GPIO_Pin_13);
       delay();
       GPIO_ResetBits( GPIOB ,  GPIO_Pin_13);
       delay();
       GPIO_SetBits( GPIOB ,  GPIO_Pin_14);
       delay();
       GPIO_ResetBits( GPIOB ,  GPIO_Pin_14);
       delay();
       GPIO_SetBits( GPIOB ,  GPIO_Pin_15);
       delay();
       GPIO_ResetBits( GPIOB ,  GPIO_Pin_15);
      
}
}

(2)Init_External_Device.c

C语言Codee#14361
#include "includes.h"

/*******************************************************************************
* 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_Div2);

     //设置低速 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 : 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
}

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

GPIO_InitStructure . GPIO_Pin  =  GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14 |  GPIO_Pin_15 ;
GPIO_InitStructure . GPIO_Speed  =  GPIO_Speed_50MHz;
GPIO_InitStructure . GPIO_Mode  =  GPIO_Mode_Out_PP; //推挽
GPIO_Init( GPIOB ,  & GPIO_InitStructure); 

}

/*******************************************************************************
* Function Name : 延时函数
*******************************************************************************/

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

(3)includes.h

C语言Codee#14362

#ifndef INCLUDES
#define INCLUDES 1

//==============================================================================
//             ★★☆☆★★      包含文件         ★★☆☆★★                      
//==============================================================================
#include "stm32f10x_lib.h"
#include "stm32f10x_type.h"
#include "stm32f10x_it.h"

//==============================================================================
//             ★★☆☆★★      全局变量       ★★☆☆★★                      
//==============================================================================

//==============================================================================
//             ★★☆☆★★      调用函数         ★★☆☆★★                      
//==============================================================================
//##### 时钟部分 #############################################################
    void RCC_Configuration(void);       //配置系统时钟 
    void NVIC_Configuration(void);      //配置 NVIC 和 Vector Table 
    
//##### I/O部分 ##############################################################
    void GPIO_Configuration(void);      //配置使用的GPIO口
    
//##### 其他常用函数 #########################################################
    void delay(void);


#endif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值