新聞中心

STM32 之 SysTick

作者: 時(shí)間:2016-12-03 來(lái)源:網(wǎng)絡(luò) 收藏
感覺(jué)定時(shí)1秒還是有點(diǎn)不準(zhǔn),僅為目測(cè),下次用示波器去測(cè)量下。

包含文件:

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

(1)Main

C語(yǔ)言:Codee#14620
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 實(shí)驗(yàn)平臺(tái) : ST 官方三合一套件
+ 硬件 : STM32F103C8T6
+ 開(kāi)發(fā)平臺(tái) : IAR For ARM 5.40
+ 仿真器 : J-Link
+ 日期 : 2010-10-26
+ 頻率 :HSE = 8MHz ,主頻 = 72MHz
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

#include "includes.h"

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

GPIO_Configuration();


LED1_HIGH;LED2_HIGH;LED3_HIGH;LED4_HIGH;// 初始化讓燈全滅

//主循環(huán)
while(1)
{
LED1_LOW;
Delay_Ms(50);
LED1_HIGH;
Delay_Ms(50);
}
}

(2)Init_External_Device.c

C語(yǔ)言:Codee#14621
#include "includes.h"

/*******************************************************************************
== 全局變量 ==
*******************************************************************************/
vu32TimingDelay;// 精確延時(shí)在SysTick中斷里用的計(jì)數(shù)變量

/*******************************************************************************
* 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è)置代碼延時(shí)值
//FLASH_Latency_2 2 延時(shí)周期
FLASH_SetLatency(FLASH_Latency_2);

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

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

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

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

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

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


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

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

//RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE);


//使能或者失能 APB2 外設(shè)時(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 : SysTick_Config SysTick設(shè)置
* Description : Configures SysTick
* Input : None
* Output : None
* Return : None
*******************************************************************************/
voidSysTick_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 : 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_LED_PORTB;
GPIO_InitTypeDefGPIO_InitStructure_KEY_PORTA;
GPIO_InitTypeDefGPIO_InitStructure_KEY_PORTB;
GPIO_InitTypeDefGPIO_InitStructure_KEY_PORTC;

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

//==== KEY =======================================================
GPIO_InitStructure_KEY_PORTA.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure_KEY_PORTA.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure_KEY_PORTA.GPIO_Mode=GPIO_Mode_IPU;//上拉輸入
GPIO_Init(GPIOA,&GPIO_InitStructure_KEY_PORTA);

GPIO_InitStructure_KEY_PORTB.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure_KEY_PORTB.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure_KEY_PORTB.GPIO_Mode=GPIO_Mode_IPU;//上拉輸入
GPIO_Init(GPIOB,&GPIO_InitStructure_KEY_PORTB);

GPIO_InitStructure_KEY_PORTC.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure_KEY_PORTC.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure_KEY_PORTC.GPIO_Mode=GPIO_Mode_IPU;//上拉輸入
GPIO_Init(GPIOC,&GPIO_InitStructure_KEY_PORTC);

}

/*******************************************************************************
* Function Name : 精確延時(shí)函數(shù)
*******************************************************************************/
voidDelay_Ms(u32nTime)
{
/* 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);
}

(3)includes.h

C語(yǔ)言:Codee#14622
#ifndef INCLUDES
#define INCLUDES 1

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

//==============================================================================
// ★★☆☆★★ 全局變量 ★★☆☆★★
//==============================================================================
externvu32TimingDelay;// 精確延時(shí)在SysTick中斷里用的計(jì)數(shù)變量

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

//##### 系統(tǒng)脈搏部分 #########################################################
voidSysTick_Config(void);//配置系統(tǒng)脈搏定時(shí)器

//##### 中斷部分 #############################################################
voidNVIC_Configuration(void);//配置 NVIC 和 Vector Table

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

//##### 其他常用函數(shù) #########################################################
voidDelay_Ms(u32nTime);//精確毫秒延時(shí)

//==============================================================================
// ★★☆☆★★ IO口定義 ★★☆☆★★
//==============================================================================
#define LED1_HIGH ( GPIO_SetBits(GPIOB, GPIO_Pin_12) )
#define LED2_HIGH ( GPIO_SetBits(GPIOB, GPIO_Pin_13) )
#define LED3_HIGH ( GPIO_SetBits(GPIOB, GPIO_Pin_14) )
#define LED4_HIGH ( GPIO_SetBits(GPIOB, GPIO_Pin_15) )

#define LED1_LOW ( GPIO_ResetBits(GPIOB, GPIO_Pin_12) )
#define LED2_LOW ( GPIO_ResetBits(GPIOB, GPIO_Pin_13) )
#define LED3_LOW ( GPIO_ResetBits(GPIOB, GPIO_Pin_14) )
#define LED4_LOW ( GPIO_ResetBits(GPIOB, GPIO_Pin_15) )

#define KEY_UP ( GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_14) )
#define KEY_DOWN ( GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) )
#define KEY_LEFT ( GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_15) )
#define KEY_RIGHT ( GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) )
#define KEY_SELECT ( GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7) )


#endif

(4)stm32f10x_it.c

C語(yǔ)言:Codee#14624
/* Includes ------------------------------------------------------------------*/
#include "includes.h"
//#include "stm32f10x_it.h"

// ... ...

/*******************************************************************************
* Function Name : SysTickHandler
* Description : This function handles SysTick Handler.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
//Line :136
voidSysTickHandler(void)
{
if(TimingDelay!=0x00)
{
TimingDelay--;
}
}

// ... ...



關(guān)鍵詞: STM32SysTic

評(píng)論


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

關(guān)閉