新聞中心

STM32 之 LED

作者: 時間:2016-12-03 來源:網(wǎng)絡(luò) 收藏
自己的USER文件組下有3個c文件,以后會按照這個程序結(jié)構(gòu)寫程序。

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

本文引用地址:http://butianyuan.cn/article/201612/325133.htm

(1)Main.c 主函數(shù)

(2)Init_External_Device.c 外設(shè)初始化函數(shù)

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

下面是源碼:

(1)Main

C語言:Codee#14359
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 實驗平臺 : ST 官方三合一套件
+ 硬件 : STM32F103C8T6
+ 開發(fā)平臺 : IAR For ARM 5.40
+ 仿真器 : J-Link
+ 日期 : 2010-10-14
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

#include "includes.h"

/*******************************************************************************
== Main 函數(shù) ==
*******************************************************************************/
intmain(void)
{
RCC_Configuration();//配置系統(tǒng)時鐘
NVIC_Configuration();//配置 NVIC 和 Vector Table

GPIO_Configuration();


//主循環(huán)
while(1)
{
delay();
//設(shè)置指定的數(shù)據(jù)端口位——LED1熄滅
GPIO_SetBits(GPIOB,GPIO_Pin_12);
delay();
//清除指定的數(shù)據(jù)端口位——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
*******************************************************************************/
voidRCC_Configuration(void)
{
ErrorStatusHSEStartUpStatus;

//將外設(shè) RCC寄存器重設(shè)為缺省值
RCC_DeInit();

//設(shè)置外部高速晶振(HSE)
RCC_HSEConfig(RCC_HSE_ON);

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

if(HSEStartUpStatus==SUCCESS)
{
//預(yù)取指緩存使能
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

//設(shè)置代碼延時值
//FLASH_Latency_2 2 延時周期
FLASH_SetLatency(FLASH_Latency_2);

//設(shè)置 AHB 時鐘(HCLK)
//RCC_SYSCLK_Div1 AHB 時鐘 = 系統(tǒng)時鐘
RCC_HCLKConfig(RCC_SYSCLK_Div1);

//設(shè)置高速 AHB 時鐘(PCLK2)
//RCC_HCLK_Div2 APB1 時鐘 = HCLK / 2
RCC_PCLK2Config(RCC_HCLK_Div2);

//設(shè)置低速 AHB 時鐘(PCLK1)
//RCC_HCLK_Div2 APB1 時鐘 = HCLK / 2
RCC_PCLK1Config(RCC_HCLK_Div2);

// PLLCLK = 8MHz * 9 = 72 MHz
//設(shè)置 PLL 時鐘源及倍頻系數(shù)
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

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

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


//設(shè)置系統(tǒng)時鐘(SYSCLK) 設(shè)置PLL為系統(tǒng)時鐘源
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

//等待PLL成功用作于系統(tǒng)時鐘的時鐘源
// 0x00:HSI 作為系統(tǒng)時鐘
// 0x04:HSE作為系統(tǒng)時鐘
// 0x08:PLL作為系統(tǒng)時鐘
while(RCC_GetSYSCLKSource()!=0x08)
{
}
}

//使能或者失能 APB2 外設(shè)時鐘
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
*******************************************************************************/
voidNVIC_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
*******************************************************************************/
voidGPIO_Configuration(void)
{
GPIO_InitTypeDefGPIO_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 : 延時函數(shù)
*******************************************************************************/

voiddelay()
{
inti;
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"

//==============================================================================
// ★★☆☆★★ 全局變量 ★★☆☆★★
//==============================================================================

//==============================================================================
// ★★☆☆★★ 調(diào)用函數(shù) ★★☆☆★★
//==============================================================================
//##### 時鐘部分 #############################################################
voidRCC_Configuration(void);//配置系統(tǒng)時鐘
voidNVIC_Configuration(void);//配置 NVIC 和 Vector Table

//##### I/O部分 ##############################################################
voidGPIO_Configuration(void);//配置使用的GPIO口

//##### 其他常用函數(shù) #########################################################
voiddelay(void);


#endif




關(guān)鍵詞: STM32LE

評論


技術(shù)專區(qū)

關(guān)閉