感觉定时1秒还是有点不准,仅为目测,下次用示波器去测量下。
包含文件:
(1)Main
C语言: Codee#14620
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 实验平台 : ST 官方三合一套件
+ 硬件 : STM32F103C8T6
+ 开发平台 : IAR For ARM 5.40
+ 仿真器 : J-Link
+ 日期 : 2010-10-26
+ 频率 :HSE = 8MHz ,主频 = 72MHz
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include "includes.h"
/*******************************************************************************
== Main 函数 ==
*******************************************************************************/
int main(void)
{
RCC_Configuration(); //配置系统时钟
NVIC_Configuration(); //配置 NVIC 和 Vector Table
SysTick_Config(); //配置SysTick的精确延时
GPIO_Configuration();
LED1_HIGH ; LED2_HIGH ; LED3_HIGH ; LED4_HIGH ; // 初始化让灯全灭
//主循环
while (1)
{
LED1_LOW ;
Delay_Ms(50);
LED1_HIGH ;
Delay_Ms(50);
}
}
(2)Init_External_Device.c
C语言: Codee#14621
#include "includes.h"
/*******************************************************************************
== 全局变量 ==
*******************************************************************************/
vu32 TimingDelay; // 精确延时在SysTick中断里用的计数变量
/*******************************************************************************
* 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)
{
}
}
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE);
//使能或者失能 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 : SysTick_Config SysTick设置
* Description : Configures SysTick
* Input : None
* Output : None
* Return : None
*******************************************************************************/
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 : 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_LED_PORTB;
GPIO_InitTypeDef GPIO_InitStructure_KEY_PORTA;
GPIO_InitTypeDef GPIO_InitStructure_KEY_PORTB;
GPIO_InitTypeDef GPIO_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 : 精确延时函数
*******************************************************************************/
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);
}
(3)includes.h
C语言: Codee#14622
#ifndef INCLUDES
#define INCLUDES 1
//==============================================================================
// ★★☆☆★★ 包含文件 ★★☆☆★★
//==============================================================================
#include "stm32f10x_lib.h"
#include "stm32f10x_type.h"
#include "stm32f10x_it.h"
//==============================================================================
// ★★☆☆★★ 全局变量 ★★☆☆★★
//==============================================================================
extern vu32 TimingDelay; // 精确延时在SysTick中断里用的计数变量
//==============================================================================
// ★★☆☆★★ 调用函数 ★★☆☆★★
//==============================================================================
//##### 时钟部分 #############################################################
void RCC_Configuration(void); //配置系统时钟
//##### 系统脉搏部分 #########################################################
void SysTick_Config(void); //配置系统脉搏定时器
//##### 中断部分 #############################################################
void NVIC_Configuration(void); //配置 NVIC 和 Vector Table
//##### I/O部分 ##############################################################
void GPIO_Configuration(void); //配置使用的GPIO口
//##### 其他常用函数 #########################################################
void Delay_Ms(u32 nTime); //精确毫秒延时
//==============================================================================
// ★★☆☆★★ 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语言: 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
void SysTickHandler(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
// ... ...
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』