STM32 SysTick

Publisher:知识阁楼Latest update time:2016-10-06 Source: eefocusKeywords:STM32  SysTick Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
I feel that the 1 second timing is still a little inaccurate. It is just a visual measurement. I will use an oscilloscope to measure it next time.

Included Files:

  STM32 SysTick - wanghengzhi@126 - Code Beans

(1)Main

C language:  Code#14620
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Experimental platform: ST official three-in-one kit 
+ Hardware: STM32F103C8T6
+ Development platform: IAR For ARM 5.40
+ Emulator: J-Link
+ Date: 2010-10-26
+ Frequency: HSE = 8MHz, main frequency = 72MHz
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

#include "includes.h"

/*******************************************************************************
                             == Main function== 
***************************************************************************/
int main(void)

RCC_Configuration(); //Configure system clock 
NVIC_Configuration(); //Configure NVIC and Vector Table 
SysTick_Config(); //Configure SysTick's precise delay

GPIO_Configuration(); 


LED1_HIGH ; LED2_HIGH ; LED3_HIGH ; LED4_HIGH ; //Initialize and turn off all lights

//Main loop 
while (1)

    LED1_LOW ; 
    Delay_Ms(50);
    LED1_HIGH ; 
    Delay_Ms(50);
}
}

(2)Init_External_Device.c

C language:  Code#14621
#include "includes.h"

/*******************************************************************************
                             == Global variables == 
**************************************************************************/
vu32 TimingDelay; // Precise delay counting variable used in SysTick interrupt

/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;

// Reset the peripheral RCC registers to the default values
​​RCC_DeInit();

// Set external high-speed crystal oscillator (HSE)
RCC_HSEConfig(RCC_HSE_ON);

// Wait for HSE to start 
HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)
{
    //Enable prefetch buffer
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

     //Set code delay value
    //FLASH_Latency_2 2 delay period
    FLASH_SetLatency(FLASH_Latency_2);

    //Set AHB clock (HCLK)
    //RCC_SYSCLK_Div1 AHB clock = system clock 
    RCC_HCLKConfig(RCC_SYSCLK_Div1);

     //Set high speed AHB clock (PCLK2)
    //RCC_HCLK_Div2 APB1 clock = HCLK / 2 
    RCC_PCLK2Config(RCC_HCLK_Div2);

    //Set low speed AHB clock (PCLK1)
    //RCC_HCLK_Div2 APB1 clock = HCLK / 2 
    RCC_PCLK1Config(RCC_HCLK_Div2);

    //PLLCLK = 8MHz * 9 = 72 MHz 
    //Set PLL clock source and frequency multiplierRCC_PLLConfig
    (RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    //Enable or disable PLL
    RCC_PLLCmd(ENABLE);

    //Wait for the specified RCC flag to be set successfullyWait for PLL initialization to be successfulwhile
    (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }


    //Set system clock (SYSCLK) Set PLL as system clock sourceRCC_SYSCLKConfig
    (RCC_SYSCLKSource_PLLCLK);

    //Wait for PLL to be successfully used as the clock source for system clock
    // 0x00: HSI as system clock 
    // 0x04: HSE as system clock 
    // 0x08: PLL as system clockwhile 
    (RCC_GetSYSCLKSource() != 0x08)
    {
    }
}

//RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE);


//Enable or disable APB2 peripheral clock
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 language:  Code#14622
#ifndef INCLUDES
#define INCLUDES 1

//================================================================================
// ★★☆☆★★ Include files ★★☆☆★★                      
//================================================================================
#include "stm32f10x_lib.h"
#include "stm32f10x_type.h"
#include "stm32f10x_it.h"

//=====================================================================================
// ★★☆☆★★ Global variables ★★☆☆★★                      
//=
    ... The counting variable used for precise delay in SysTick interrupt

//====================================================================================
// ★★☆☆★★ Calling function ★★☆☆★★                      
//=
... Clock part#####################################################################
    void RCC_Configuration(void); //Configure system clock 

//###### System pulse part#####################################################################
    void SysTick_Config(void); //Configure system pulse timer

//###### Interrupt part####################################################################
    void NVIC_Configuration(void); //Configure NVIC and Vector Table 
    
//###### I/O part###################################################################
    void GPIO_Configuration(void); //Configure the GPIO port to be used
    
//###### Other common functions########################################################################
    void Delay_Ms(u32 nTime); //Precise millisecond delay
    
//=================================================================================
// ★★☆☆★★ IO port definition ★★☆☆★★                      
//===============================================================================
    #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 language:  Code#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--;
}
}

// ... ...

Keywords:STM32  SysTick Reference address:STM32 SysTick

Previous article:STM32 UART1(1)
Next article:STM32 KEY

Recommended ReadingLatest update time:2024-11-25 13:30

STM32 interrupt setting and interrupt priority setting
Recently, I was working on a smart lock project. Today, I encountered a problem about interrupts. Therefore, I came back to learn about interrupt configuration. As the saying goes, sharpening the knife does not delay the chopping of wood. What is the problem? I used a touch keyboard TTP229 in the project. As a result,
[Microcontroller]
STM32 interrupt setting and interrupt priority setting
STM32-USB virtual serial port-learning notes
USB It is used to standardize the connection and communication between computers and external devices. It is an interface technology used in the PC field. USB interface supports plug-and-play and hot-swap functions of devices There are already multiple versions of USB1.0/1.1/2.0/3.0. Currently, USB1.1 and USB2.0 ar
[Microcontroller]
STM32 A method to call the Bootloader in the system memory from the user code
Preface As we all know, any STM32 contains a system memory, which stores the internal boot code Bootloader. Different STM32 models support different communication ports for upgrading code, and you need to refer to the application note AN2606. However, there is a problem that cannot be avoided, that is, how to enter th
[Microcontroller]
STM32 A method to call the Bootloader in the system memory from the user code
STM32 HSE LSE crystal oscillator official recommendation
I plan to choose HSE: HC-49S-8 MHz chip            LSE : DMX-26S 32768HZ patch    
[Microcontroller]
STM32 HSE LSE crystal oscillator official recommendation
Experiment on sending and receiving data using the STM32 serial port
0 Target 1 Introduction to STM32 Serial Port 2 Hardware Design 3 Software Design 4 Download verification 0. Goal Use serial port 1 to continuously print information to the computer, receive data from the serial port at the same time, and send the sent data directly back to the computer.   1. Introduction to
[Microcontroller]
Experiment on sending and receiving data using the STM32 serial port
Systick Tick Timer - Delay Function
1. References       "STM32F1 Development Guide - Library Function Version" - Section 5.1 Introduction to the delay folder        "Cortex-M3 Authoritative Guide - Chinese" - Chapter 8, the last section: Systick Timer 2. Systick Timer      Systick timer is a simple timer. Both CM3 and CM4 core chips have Systick t
[Microcontroller]
Systick Tick Timer - Delay Function
The difference between STM32 startup file selection
To put it bluntly, the following are the differences. There are selective differences when choosing the startup file! startup_stm32f10x_cl.s Interconnected STM32F105xx, STM32F107xx startup_stm32f10x_hd.s Large capacity STM32F101xx, STM32F102xx, STM32F103xx startup_stm32f10x_hd_vl.s Large capacity STM32F100xx startup
[Microcontroller]
STM32 learning 003_STM32F10X clock configuration
1. In STM32, there are 5 clock sources: HSI, HSE, LSI, LSE, PLL. ①HSI is a high-speed internal clock, RC oscillator, with a frequency of 8MHz; ②HSE--High-speed external clock, can be connected to quartz or ceramic resonator, or external clock source, frequency range 4MHz--15MHz. ③LSI--Low speed internal clock, RC osci
[Microcontroller]
STM32 learning 003_STM32F10X clock configuration
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号