当前位置:网站首页>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 .
边栏推荐
- How to transfer event objects and user-defined parameters simultaneously in Huawei express applications
- 二叉树专题--洛谷 P1229 遍历问题(乘法原理 已知前、后序遍历求中序遍历个数)
- Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
- 2. Hacking lab script off [detailed writeup]
- P1055 [NOIP2008 普及组] ISBN 号码
- Static variables in static function
- Learn open62541 -- [66] UA_ Generation method of string
- TIPC Service and Topology Tracking4
- Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)
- QT学习日记7——QMainWindow
猜你喜欢
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
全网显示 IP 归属地,是怎么实现的?
Binary tree topic -- Luogu p3884 [jloi2009] binary tree problem (DFS for binary tree depth BFS for binary tree width Dijkstra for shortest path)
Openmldb meetup No.4 meeting minutes
HDU1236 排名(结构体排序)
QT learning diary 7 - qmainwindow
洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
[in simple terms, play with FPGA learning 3 ----- basic grammar]
HDU1234 开门人和关门人(水题)
【深入浅出玩转FPGA学习3-----基本语法】
随机推荐
QT learning diary 8 - resource file addition
Calculate the sum of sequences
Primary key policy problem
[quick application] there are many words in the text component. How to solve the problem that the div style next to it will be stretched
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
static 函数中的静态变量
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
Openmldb meetup No.4 meeting minutes
Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
Special topic of binary tree -- [deep base 16. Example 7] ordinary binary tree (simplified version) (multiset seeks the precursor and subsequent sentry Art)
Overview of integrated learning
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?
Filtering of PCL
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
MySQL lethal serial question 4 -- are you familiar with MySQL logs?
The most detailed MySQL installation tutorial
JSP webshell free -- webshell free
How does the whole network display IP ownership?
MySQL keyword
Point cloud projection picture