当前位置:网站首页>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 .
边栏推荐
- 一招快速实现自定义快应用titlebar
- 使用华为性能管理服务,按需配置采样率
- [ark UI] implementation of the startup page of harmonios ETS
- static 函数中的静态变量
- Use of vscode tool
- 二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
- The URL in the RTSP setup header of the axis device cannot take a parameter
- Hdu1234 door opener and door closer (water question)
- 二叉树专题--AcWing 47. 二叉树中和为某一值的路径(前序遍历)
- Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
猜你喜欢

JSP webshell free -- webshell free

Dialogue Wu Gang: why do I believe in the rise of "big country brands"?

快应用中实现自定义抽屉组件

如何使用IDE自动签名调试鸿蒙应用

Shell programming 01_ Shell foundation

QT learning diary 8 - resource file addition

Huawei game failed to initialize init with error code 907135000

Read H264 parameters from mediarecord recording

What are the software product management systems? Inventory of 12 best product management tools

LabVIEW为什么浮点数会丢失精度
随机推荐
Static variables in static function
[paid promotion] collection of frequently asked questions, recommended list FAQ
Oracle notes
Flick two open, realized a batch lookup join (with source code)
洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
力扣(LeetCode)182. 查找重复的电子邮箱(2022.07.01)
TIPC messaging3
Oracle 笔记
In the face of uncertainty, the role of supply chain
洛谷 P4281 [AHOI2008]紧急集合 / 聚会(树上倍增 LCA)
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
JVM garbage collector
[AGC] build service 3 - authentication service example
Learn open62541 -- [66] UA_ Generation method of string
V2X-Sim数据集(上海交大&纽约大学)
Flink two Open, implement Batch Lookup join (attached source)
二叉树专题--AcWing 1589. 构建二叉搜索树
MySQL environment configuration
如何用list组件实现tabbar标题栏
二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)