当前位置:网站首页>III Chip startup and clock system
III Chip startup and clock system
2022-07-02 11:08:00 【Heavy vehicle water】
Chip startup and clock system
1. Chip start
First stm32 According to the starting method ( Reference manual 2.4 section ) Load the startup code from the startup location into memory , Then start executing the startup code , Generally, the official startup code is enough ---------- xxx.s

Start the work of the code :
Initialize stack space , Define exception vector table call SystemInit ----------- System initialization Initialize the clock , Adjust the exception vector table perform main ---------- The main function
The chip is going to work , The clock and memory must be initialized ,stm32 Memory usage on chip SRAM, You can use it directly , The clock needs to be initialized ,ARM The chip needs to define the exception vector table , perform C Language code must initialize the stack .
stm32f407 Recommended master clock frequency 168MHz
2. The hardware that generates the original frequency
(1) Crystal oscillator
(2)RC(LC) Oscillating circuit
The original frequency will not be very high , The frequency must be increased before use , Up frequency use PLL( Up frequency ) circuit
CPU General structure of the clock system

3.stm32f407 The original clock
HSI RC -------------- High speed internal oscillation clock 16M HSE OSC ------------- High speed external crystal 4~26M(8M) // The above two clock sources can be directly used as the main clock of the system , It can also be done through PLL As the master clock after frequency rise LSI RC --------------- Low speed internal oscillation clock 32K ----- watchdog LSE OSC -------------- Low speed external crystal 32.768K ----- RTC
stm32f407 Clock tree

PLL The output clock of = PLL Input clock X PLLN / PLLM / PLLP
168M = 8M X 336 / 8 / 2
4. take keil5 The system clock of the project is configured as 168MHz
(1) modify system_stm32f4xx.c Of 254 That's ok
#define PLL_M 8
(2) modify stm32f4xx.h Of 127 That's ok
// This file is a read-only property file , To find the file in the file system , Remove the read-only attribute #define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
practice :
Configure the main frequency of the system to 168M
modify PLL, Adjust the main frequency of the system
#define PLL_N 336//168M #define PLL_N 432//216M Overclock #define PLL_N 168//84M Frequency reduction
System bus clock frequency :
SYSCLK The clock ------------ 168MHz HCLK/AHB Bus ---------- 168MHz APB1 The clock -------------- 42MHz APB2 The clock -------------- 84MHz
__________________________________________________________________________________________________________________________________________________________
Key driven
1. Look at the schematic



As can be seen from the schematic diagram :
Release the button -------- Pin high level
Press the key -------- Pin low level
The pin corresponding to the key :
S1 ----- PA0
S2 ----- PE2
S3 ----- PE3
S4 ----- PE4
How to read the level of the input pin
(1) Read the input data register (IDR) The value of the corresponding bit 1 ----- Input high level 0 ----- Input low level
(2) Bit segment operation PAin(0)==0 ----- Input low level PAin(0)==1 ----- Input high level
(3) Library function uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); // Which group and which foot are introduced , Return the level of this pin
——————————————————————————————————————————————————————————————————————————————————————
practice :
Complete the detection program of the other three keys , Separate use register Bit segment Library function judgment
Realize the function :
Press down S2,D2 bright Press down S3,D3 bright Press down S4,D4 bright
//key.c
#include <stm32f4xx.h>
#include <key.h>
void key_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//1. Turn on GPIOA The clock of
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOE,ENABLE);
//2.GPIO initialization PA0
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;// The input mode
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;// No up and down
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;//PA0
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;//PE2 PE3 PE4
GPIO_Init(GPIOE,&GPIO_InitStruct);//main.c
#include <stm32f4xx.h>
#include <led.h>
#include <sys.h>
#include <key.h>
int main()
{
int key_flag = 0;
// initialization
led_init();
key_init();
while(1){
if(S1==0){
// Delay chattering 10ms
delay(100);
if(S1==0){
// Real key events
if(key_flag==0){// Press and do not release
D1 = ~D1;// Take the opposite
key_flag = 1;
}
}
}
else{
// Delay chattering 10ms
delay(100);
if(S1){
key_flag = 0;
}
}
}
}//lcd.c
include <stm32f4xx.h>
#include <led.h>
void delay(unsigned int ms)
{
int i,j;
for(i=0;i<ms;i++)
for(j=0;j<5000;j++);
}
void led_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
//1. Turn on GPIOE GPIOF The clock of
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOF,ENABLE);
//2.GPIO initialization PF9 PF10 PE13 PE14
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;// The output mode
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;// Push pull output
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;// High speed
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;// No up and down
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;//PF9 PF10
GPIO_Init(GPIOF,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14;//PE13 PE14
GPIO_Init(GPIOE,&GPIO_InitStruct);
//3.LED Off by default
GPIO_SetBits(GPIOF,GPIO_Pin_9|GPIO_Pin_10);
GPIO_SetBits(GPIOE,GPIO_Pin_13|GPIO_Pin_14);
}//lcd.h
#ifndef _LED_H_
#define _LED_H_
#define D1 PFout(9)
#define D2 PFout(10)
#define D3 PEout(13)
#define D4 PEout(14)
void delay(unsigned int ms);
void led_init(void);
#endif//key.h
#ifndef _KEY_H_
#define _KEY_H_
#define S1 PAin(0)
#define S2 PEin(2)
#define S3 PEin(3)
#define S4 PEin(4)
void key_init(void);
#endif//sys.h This file is defined by the system
#ifndef __SYS_H_
#define __SYS_H_
#include "stm32f4xx.h"
//IO Port operation macro definition
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2))
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//IO Port address mapping
#define GPIOA_ODR_Addr (GPIOA_BASE+20) //0x40020014
#define GPIOB_ODR_Addr (GPIOB_BASE+20) //0x40020414
#define GPIOC_ODR_Addr (GPIOC_BASE+20) //0x40020814
#define GPIOD_ODR_Addr (GPIOD_BASE+20) //0x40020C14
#define GPIOE_ODR_Addr (GPIOE_BASE+20) //0x40021014
#define GPIOF_ODR_Addr (GPIOF_BASE+20) //0x40021414 20 = 0x14
#define GPIOG_ODR_Addr (GPIOG_BASE+20) //0x40021814
#define GPIOH_ODR_Addr (GPIOH_BASE+20) //0x40021C14
#define GPIOI_ODR_Addr (GPIOI_BASE+20) //0x40022014
#define GPIOA_IDR_Addr (GPIOA_BASE+16) //0x40020010 16 = 0x10
#define GPIOB_IDR_Addr (GPIOB_BASE+16) //0x40020410
#define GPIOC_IDR_Addr (GPIOC_BASE+16) //0x40020810
#define GPIOD_IDR_Addr (GPIOD_BASE+16) //0x40020C10
#define GPIOE_IDR_Addr (GPIOE_BASE+16) //0x40021010
#define GPIOF_IDR_Addr (GPIOF_BASE+16) //0x40021410
#define GPIOG_IDR_Addr (GPIOG_BASE+16) //0x40021810
#define GPIOH_IDR_Addr (GPIOH_BASE+16) //0x40021C10
#define GPIOI_IDR_Addr (GPIOI_BASE+16) //0x40022010
//IO Mouth operation , Only for single IO mouth !
// Make sure n The value is less than 16!
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) // Output
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) // Input
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) // Output
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) // Input
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) // Output
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) // Input
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) // Output
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) // Input
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) // Output
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) // Input
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) // Output
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) // Input
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) // Output
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) // Input
#define PHout(n) BIT_ADDR(GPIOH_ODR_Addr,n) // Output
#define PHin(n) BIT_ADDR(GPIOH_IDR_Addr,n) // Input
#define PIout(n) BIT_ADDR(GPIOI_ODR_Addr,n) // Output
#define PIin(n) BIT_ADDR(GPIOI_IDR_Addr,n) // Input
#endif
3. Press the key to shake off ———— Delay chattering

The above key pressing procedures do not matter whether there is a key pressing event , Will occupy CPU To judge , This method is called polling , Low efficiency ,CPU Provides a more efficient way --------- interrupt .
边栏推荐
- Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
- 【深入浅出玩转FPGA学习4----漫谈状态机设计】
- 【ARK UI】HarmonyOS ETS的启动页的实现
- P1055 [NOIP2008 普及组] ISBN 号码
- Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
- How does the whole network display IP ownership?
- The difference between self and static in PHP in methods
- The first white paper on agile practice in Chinese enterprises was released | complete download is attached
- Hdu1228 a + B (map mapping)
- MySQL lethal serial question 3 -- are you familiar with MySQL locks?
猜你喜欢
![[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?](/img/66/674a06d8e45a31ae879b81554ef373.png)
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?

如何用list组件实现tabbar标题栏

2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises

Special topic of binary tree -- Logu p1229 traversal problem (the number of traversals in the middle order is calculated when the pre and post order traversals of the multiplication principle are know

V2x SIM dataset (Shanghai Jiaotong University & New York University)

Jsp webshell Free from killing - The Foundation of JSP

二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)
![[TS] 1368 seconds understand typescript generic tool types!](/img/2b/58a850b52ce8a9b2e6e7b6b72b0fe5.jpg)
[TS] 1368 seconds understand typescript generic tool types!

Analysis of hot spots in AI technology industry

二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
随机推荐
LabVIEW为什么浮点数会丢失精度
Jsp webshell Free from killing - The Foundation of JSP
Disassembling Meitu SaaS: driving the plane to change the engine
What are the software product management systems? Inventory of 12 best product management tools
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
2022爱分析· 国央企数字化厂商全景报告
Luogu p4281 [ahoi2008] emergency gathering / gathering (tree doubling LCA)
Shell programming 01_ Shell foundation
Set the playback speed during the playback of UOB equipment
PCL eigen introduction and simple use
正则及常用公式
Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
Uncover the secrets of Huawei application market application statistics
TIPC介绍1
Is the account above changtou school safe?
[TS] 1368 seconds understand typescript generic tool types!
洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
【深入浅出玩转FPGA学习2----设计技巧(基本语法)】
【AI应用】海康威视iVMS-4200软件安装
二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)