728x90
First, explain that the delay function of the first atomic brother is no problem. Atomic brother's delay function is used in STM32F103, depending on the type of development board. Here are the delay functions of STM32F103 and STM32F030.
(1) Used in STM32F030:
#include "stm32f0xx.h"
#include "delay.h"
//////////////////////////////////////////////////////////////////////////////////
//This program is for learning use only and cannot be used for any other purpose without permission from the author
//Mini STM32 development board
//Use SysTick's normal counting mode to manage the delay
//Including delay_us, delay_ms
//Punctual atom @ALIENTEK
//Technical Forum: www.openedv.com
//Date of modification: 2010/5/27
//Version: V1.2
//Copyright, piracy will be investigated.
//Copyright(C) punctual atom 2009-2019
//All rights reserved
//********************************************************************************
//V1.2 modification instructions
//Fixed the error that the call in the interrupt occurs in an infinite loop
//To prevent inaccurate delay, use do while structure!
//////////////////////////////////////////////////////////////////////////////////
static u8 fac_us=0;//us delay multiplier
static u16 fac_ms=0;//ms delay multiplier
//Initialize delay function
//The clock of SYSTICK is fixed at 1/8 of the HCLK clock
//SYSCLK: system clock
void delay_init(u8 SYSCLK)
{
SysTick->CTRL&=0xfffffffb;//bit2 clear, select external clock HCLK/8
fac_us=SYSCLK/8;
fac_ms=(u16)fac_us*1000;
}
//Delay nms
//Note the range of nms
//SysTick->LOAD is a 24-bit register, so the maximum delay is:
//nms<=0xffffff*8*1000/SYSCLK
//SYSCLK unit is Hz, nms unit is ms
//For 72M, nms<=1864
void delay_ms(u16 nms)
{
u32 temp;
SysTick->LOAD=(u32)nms*fac_ms;//Time loading (SysTick->LOAD is 24bit)
SysTick->VAL =0x00; //Clear counter
SysTick->CTRL=0x01; //Start countdown
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16)));//Wait time to arrive
SysTick->CTRL=0x00; //Turn off the counter
SysTick->VAL =0X00; //Clear counter
}
//Delay nus
//nus is the number of us to be delayed.
void delay_us(u32 nus)
{
u32 temp;
SysTick->LOAD=nus*fac_us; //Time to load
SysTick->VAL=0x00; //Clear counter
SysTick->CTRL=0x01; //Start countdown
do
{
temp=SysTick->CTRL;
}
while(temp&0x01&&!(temp&(1<<16)));//Wait time to arrive
SysTick->CTRL=0x00; //Turn off the counter
SysTick->VAL =0X00; //Clear counter
}
(2) Used in STM32F103:
'홍익인간 프로젝트 > C' 카테고리의 다른 글
'break' statement when using curly braces in switch-caseAsked (0) | 2021.03.04 |
---|---|
C program to convert Hexadecimal to Decimal (0) | 2020.12.16 |
gettimeofday (31) | 2020.12.01 |