【stm32f407】SysTick to achieve delay

Publisher:LIGANG888Latest update time:2019-02-12 Source: eefocusKeywords:stm32f407  SysTic  Delay Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. SysTick Introduction:


The processing of the CM4 core is the same as that of the CM3. It contains a SysTick timer. SysTick is a 24-bit countdown timer. When it reaches 0, it will automatically reload the initial value of the timer from the RELOAD register. As long as the enable bit in the SysTick control and status register is not cleared, it will never stop. We use the internal SysTick of STM32 to achieve delay, which does not occupy interrupts or system timers.


Usually SysTick can be implemented by interruption, which will be added later, but currently it is only implemented by polling


2. Register Introduction


SysTick has 4 registers


The corresponding code is in core_cm4.h


typedefstruct

{

  __IO uint32_t CTRL;                    /*!< Offset: 0x000(R/W)  SysTick Control and StatusRegister */

  __IO uint32_t LOAD;                    /*!< Offset: 0x004(R/W)  SysTick Reload Value Register       */

  __IO uint32_t VAL;                     /*!< Offset: 0x008(R/W)  SysTick Current ValueRegister      */

  __I uint32_t CALIB;                  /*!< Offset: 0x00C (R/ ) SysTick Calibration Register       */

} SysTick_Type; 

1) CTR register as shown in the figure:



Bit 0: ENABLE, Systick enable bit (0: turn off Systick function; 1: turn on Systick function)


Bit 1: TICKINT, Systick interrupt enable bit (0: disable Systick interrupt; 1: enable Systick interrupt)


Bit 2: CLKSOURCE, Systick clock source selection (0: use HCLK/8 as Systick clock; 1: use HCLK as Systick clock)


Bit 16: COUNTFLAG, Systick count comparison flag, if SysTick has counted to 0 after the last read of this register, this bit is 1. If this bit is read, it will be automatically cleared.


2) The LOAD register is as shown in the figure:



Systick is a decrementing timer. When the timer decrements to 0, the value in the reload register will be reloaded and continue to decrement. STK_LOAD reload register is a 24-bit register with a maximum count of 0xFFFFFF.


3) VAL register as shown in the figure:



This is also a 24-bit register. When read, it returns the current countdown value. Writing it clears it to zero and also clears the COUNTFLAG flag in the SysTick control and status register.


4) CALIB register as shown in the figure



Generally not used


3. Source code

delay.h



#ifndef _DELAY_H_H_H

#define _DELAY_H_H_H

#include "stm32f4xx.h"

 

void delay_init(u8 SYSCLK);

void delay_ms(u16 nms);

void delay_us(u32 nus);

#endif


delay.c



#include "delay.h"

 

static u8  fac_us=0;    

static u16 fac_ms=0;

void delay_init(u8 SYSCLK)

{

  SysTick->CTRL&=~(1<<2);

  fac_us=SYSCLK/8;

  fac_ms=((u32)SYSCLK*1000)/8;

}

void delay_xms(u16 nms)

{     

  u32 temp;    

  SysTick->LOAD=(u32)nms*fac_ms;

  SysTick->VAL =0x00;

  SysTick->CTRL=0x01 ;

  do

  {

    temp=SysTick->CTRL;

  }while((temp&0x01)&&!(temp&(1<<16)));

  SysTick->CTRL=0x00;

  SysTick->VAL =0X00;       

 

void delay_ms(u16 nms)

{

  u8 repeat=nms/540;

  u16 remain=nms%540;

  while(repeat)

  {

    delay_xms(540);

    repeat--;

  }

  if(remain)delay_xms(remain);

  

}

void delay_us(u32 nus)

{

  u32 temp;      

  SysTick->LOAD=nus*fac_us;   

  SysTick->VAL=0x00;

  SysTick->CTRL=0x01 ;  

  do

  {

    temp=SysTick->CTRL;

  }while((temp&0x01)&&!(temp&(1<<16)));

  SysTick->CTRL=0x00;

  SysTick->VAL =0X00;  

}


Keywords:stm32f407  SysTic  Delay Reference address:【stm32f407】SysTick to achieve delay

Previous article:【stm32f407】IO pin multiplexing and mapping
Next article:【stm32f407】Clock tree and SystemInit analysis

Recommended ReadingLatest update time:2024-11-16 17:42

STM32F407 drives MT9T001 CMOS image sensor module
1. Introduction to MT9T001 Image Sensor MT9T001 is a CMOS image sensor produced by MICRON, with a resolution of QXGA and an effective pixel array of 2048H*1536V. It can realize on-chip windowing, row and column skipping pixels, and snapshot functions, and the internal register programming adopts I2C bus.   MT9T001
[Microcontroller]
STM32F407 drives MT9T001 CMOS image sensor module
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号