STM32 Quick Learning 6——SysTick Timing 1s Control LED

Publisher:未来感知Latest update time:2016-09-26 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Set to use external 8M crystal

Setting Pin Function

Set systick to 1s interrupt

Using systick interruption, you can get 1s time

/// ...

The systick of stm32 is set by a few programs. After using the systick_config() function, its loaded value is your parameter, and the interrupt is automatically turned on, and the interrupt is set to the lowest priority, and its clock is set to HCLK, that is, the system clock 72mhz, and the count register is reset to start counting. The clock can also be set to eight times the frequency of HCKL by using SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8) immediately following systick_config(), and changing the priority uses the NVIC_SetPriority(SysTick_IRQn,...) function, and the time base unit is set using the following formula, Reload Value = SysTick Counter Clock (Hz) x Desired Time base (s) Reload Value is the parameter passed in. But the parameter cannot exceed 0xFFFFFF.
Systick's clock includes internal clock and external clock. For stm32, its internal clock FCLK is the AHB clock, 72MHz, and the external clock STCLK is the clock after 8 division, 9MHz. Therefore, when using the systick_config() function, its default clock is 72MHz.

When using systick delay, you can also read and write its registers directly without enabling interrupts.

SysTick_Config(uint32_t  ticks): Set the system tick clock and enable interrupts

         The description of the STM32 core is different from that of the CM3 core. There are two clock sources: AHB/8 and AHB. In this function, HCLK is selected.

(SysTick_CTRL_CLKSOURCE_Msk), so the timing time = ticks  HCLK. When the timing is 10ms and the HCLK is 24MHz, ticks  10000  24  240000.

         If you need to select HCLK/8, you can modify this function directly, or follow this function with SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource) in misc  to set it

////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ////////////////////////////////////////////////////// /////////////////////////////

The 3.4 library is used here, note that it is SystemCoreClock, the 3.0 versions use the word SystemFrequency, the methods of other 2.x versions are different.

STM32 Quick Learning 6——SysTick Timing 1s Control LED - Bkey - Bkey's Blog

 

 

Note: The global variable TimingDelay must be defined as volatile type, and the delay time will not change with the system clock frequency.

[Reprint] STM32 Quick Learning 6——SysTick Timing 1s Control LED - ╄→Wind, Blowing Away - ╄→Wind, Blowing Away

  

Main file

#include "stm32f10x.h"

 

void RCC_Configuration(void);

void GPIO_Configuration(void);

void SysTick_Configuration(void);

void Delay(volatile uint32_t nTime);

 

static volatile uint32_t TimingDelay;

 

 

int main(void)

{

  RCC_Configuration();

  GPIO_Configuration();

  SysTick_Configuration();

 

  while(1)

  {

    GPIO_SetBits(GPIOA, GPIO_Pin_0); 

    Delay(1000);

    GPIO_SetBits(GPIOA, GPIO_Pin_1);

    Delay(1000);

    GPIO_ResetBits(GPIOA, GPIO_Pin_0);

Delay(1000);

    GPIO_ResetBits(GPIOA, GPIO_Pin_1);

Delay(1000);

  }

}

 

void RCC_Configuration(void) /*Use external 8M*/

{    

  ErrorStatus HSEStartUpStatus;

 

  /* RCC system reset(for debug purpose) */

  RCC_DeInit();

 

  /* Enable HSE */

  RCC_HSEConfig(RCC_HSE_ON);

 

  /* Wait till HSE is ready */

  HSEStartUpStatus = RCC_WaitForHSEStartUp();

 

  if(HSESTartUpStatus == SUCCESS)

  {

    /* Enable Prefetch Buffer Prefetch buffer enable */

    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

 

    /* Flash 2 wait state, FLASH memory delay clock cycles*/

    FLASH_SetLatency(FLASH_Latency_2);

 

    /* HCLK = SYSCLK */

    RCC_HCLKConfig(RCC_SYSCLK_Div1); 

  

    /* PCLK2 = HCLK */

    RCC_PCLK2Config(RCC_HCLK_Div1); 

 

    /* PCLK1 = HCLK/2 */

    RCC_PCLK1Config(RCC_HCLK_Div2);

 

    /* PLLCLK = 8MHz * 9 = 72 MHz */

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

 

    /* Enable PLL */ 

    RCC_PLLCmd(ENABLE);

 

    /* Wait till PLL is ready */

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)

    {

    }

 

    /* Select PLL as system clock source */

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

 

    /* Wait till PLL is used as system clock source */

    while(RCC_GetSYSCLKSource() != 0x08)

    {

    }

  }

 

  /* Enable GPIOC clock */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

}

 

 

void GPIO_Configuration(void)

{

  GPIO_InitTypeDef GPIO_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;

  GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

}

 

void SysTick_Configuration()

{

  if (SysTick_Config(SystemCoreClock / 1000))

  { 

    /* Capture error */ 

    while (1);

  }

}

 

void Delay(volatile uint32_t nTime)

  TimingDelay = nTime;

 

  while(TimingDelay != 0);

}

 

void TimingDelay_Decrement(void)

{

  if (TimingDelay != 0x00)

  { 

    TimingDelay--;

  }

}

 

 

 

 

Stm32f10x_it.c added

 

void TimingDelay_Decrement(void);

 

void SysTick_Handler(void)

{

  TimingDelay_Decrement();

}

Keywords:stm32 Reference address:STM32 Quick Learning 6——SysTick Timing 1s Control LED

Previous article:STM32 power-off detection + Flash access
Next article:stm32 quick learning 2-light up the LED

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号