当前位置:网站首页>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 .
边栏推荐
- Disassembling Meitu SaaS: driving the plane to change the engine
- 【快应用】Win7系统使用华为IDE无法运行和调试项目
- php中self和static在方法中的区别
- 【深入浅出玩转FPGA学习3-----基本语法】
- 【付费推广】常见问题合集,推荐榜单FAQ
- How to transfer event objects and user-defined parameters simultaneously in Huawei express applications
- TIPC messaging3
- MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
- 洛谷 P4281 [AHOI2008]紧急集合 / 聚会(树上倍增 LCA)
- 华为快应用中如何实现同时传递事件对象和自定义参数
猜你喜欢
How to implement tabbar title bar with list component
Special topic of binary tree -- acwing 3540 Binary search tree building (use the board to build a binary search tree and output the pre -, middle -, and post sequence traversal)
Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
Read H264 parameters from mediarecord recording
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
Open the encrypted SQLite method with sqlcipher
[applinking practical case] share in app pictures through applinking
Huawei game failed to initialize init with error code 907135000
Easyexcel, a concise, fast and memory saving excel processing tool
TIPC Cluster5
随机推荐
实验电镜距离测量之Matlab处理
JSP webshell免杀——JSP的基础
TIPC 寻址2
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
【深入浅出玩转FPGA学习3-----基本语法】
Leetcode+ 76 - 80 storm search topic
Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
HDU1234 开门人和关门人(水题)
Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)
Open the encrypted SQLite method with sqlcipher
二叉树专题--【深基16.例7】普通二叉树(简化版)(multiset 求前驱 后继 哨兵法)
二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)
全网显示 IP 归属地,是怎么实现的?
正则及常用公式
UWA report uses tips. Did you get it? (the fourth bullet)
Shell programming 01_ Shell foundation
QT learning diary 8 - resource file addition
Logu p3398 hamster looks for sugar (double LCA on the tree to judge whether the two paths in the tree intersect)
2022爱分析· 国央企数字化厂商全景报告
2. Hacking lab script off [detailed writeup]