<>1. development environment

<>2. explain

<>3.F4 and F1 Software IIC difference
GPIO Initialization difference us Stage delay difference summary
1. development environment : Hardware :STM32F407IGH6 Development board Any available IIC Communication protocol sensor
Software :Keil CubeMX

2. explain : Bought one from an online store IIC Communication module , The online store provides STM32F103C8T6 Of open source routines , After getting the open source routines from the online store, I F1 Series single chip microcomputer can successfully run through , But change to F4 Several precautions that may affect the routine running through have been found on the single chip microcomputer , Here is a brief introduction
3.F4 and F1 Software IIC difference :
a.GPIO Initialization difference :
SCL --> B10
SDA --> B11
F4 Software on IIC The initialization code is as follows :
GPIO_InitTypeDef GPIO_InitStruct; __HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11; GPIO_InitStruct.Mode =
GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
Here we choose the port B10 B11 As IIC Communication port , Set the mode to open drain output , The speed is medium , Next, let's take a look F1 Software on IIC setup code
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = GPIO_PIN_10|
GPIO_PIN_11; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Speed =
GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
To make the code look more convenient , stay F1 We also choose B10 B11 As IIC Communication port , The setting mode is also open drain output , Speed is high , It can be seen here that F4 There are obvious differences ,
because F4 The dominant frequency of is generally 168Mhz, Some even reach 400Mhz, and IIC The fastest communication speed is 400Kb/s,F1 The dominant frequency of single chip microcomputer is generally 72Mhz
, stay F1 On single chip microcomputer and IIC When the device is communicating , In order to achieve higher communication frequency , Usually set GPIO The port is in high-speed mode , however F4 Main frequency ratio of single chip microcomputer F1 Twice as fast or more , If according to F1 The setting of is also set to high frequency , Will be far more than IIC Maximum frequency of communication protocol , The software cannot be used normally IIC.
Frequent use of software IIC Will find IIC The pin is set with the following code , Especially in F1 in :
#define IIC_SDA_IN() {GPIOB->CRH&=0xFFFF0FFF;GPIOB->CRH|=8<<12;} #define
IIC_SDA_OUT() {GPIOB->CRH&=0xFFFF0FFF;GPIOB->CRH|=3<<12;} #define IIC_SDA_READ
GPIOB->IDR&(1<<11) #define IIC_SCL_H
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_SET) #define IIC_SCL_L
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET) #define IIC_SDA_H
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_SET) #define IIC_SDA_L
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_RESET)
set up IIC High and low level input and output mode of communication port , In particular, the following codes :
#define IIC_SDA_IN() {GPIOB->CRH&=0xFFFF0FFF;GPIOB->CRH|=8<<12;} #define
IIC_SDA_OUT() {GPIOB->CRH&=0xFFFF0FFF;GPIOB->CRH|=3<<12;} #define IIC_SDA_READ
GPIOB->IDR&(1<<11)
stay F1 in CRH CRL
representative GPIO Height of mouth 8 Bit and low 8 position , Everyone knows STM32GPIO Generally 16 individual I/O mouth , hinder ***&=0xFFFF0FFF***, You can convert it with a binary converter , This representative ***11111111111111110000111111111111***, Every four represents one IO mouth
B11 Is high 8 position , Number from right to left , It happens to be No 4 position ( from 8 start , The fourth place happens to be 11), This instruction means initialization B11
, The distribution is set to input and output modes , But in F4 in , I am here F4 of GPIO Not found in register ***CRH and CRL***
, Of course, it may be my lack of talent and learning , What should we do then ,F1 The routine of the device is directly transported to F4 It will report an error , In fact F4 It's simpler :
#define IIC_SDA_READ HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_11) #define IIC_SCL_H
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_SET) #define IIC_SCL_L
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET) #define IIC_SDA_H
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_SET) #define IIC_SDA_L
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_RESET)
Use the above code , Put your hands again F1 of IIC The following codes appearing in the agreement can be commented out :
// IIC_SDA_IN();//SDA Set as input // IIC_SDA_OUT();//SDA Line output

It can be used normally , Don't paste here first IIC The underlying program of the communication protocol , Like punctual atom , Wildfire , Hard stone technology, etc STM32 The routines in the embedded tutorial are about software IIC Or simulation IIC There are many uses of , And each tutorial is basically the same .

b.us Difference of level delay , We mentioned earlier IIC The fastest communication speed of is 400Kb/s, So in F1 Upper and F4 If both are used us Software delay codes at different levels are different , But most of what we buy IIC Communication modules are all for F1 Software delay written by series MCU , But if you move directly F4 It cannot be used directly on , Or even if it can be used , Will also decrease IIC Communication speed of , Here is a basic timer for use 7 Delay procedure for , It can be used after initialization :
delay.c
#include "delay.h" TIM_HandleTypeDef htim7; void MX_TIM7_Init(void) { /* USER
CODE BEGIN TIM7_Init 0 */ /* USER CODE END TIM7_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig= {0}; htim7.Instance = TIM7; htim7.Init.
Prescaler= 0; htim7.Init.CounterMode = TIM_COUNTERMODE_UP; htim7.Init.Period =
65535; htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (
HAL_TIM_Base_Init(&htim7) != HAL_OK) { Error_Handler(); } sMasterConfig.
MasterOutputTrigger= TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode =
TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &
sMasterConfig) != HAL_OK) { Error_Handler(); } } void delay_init() {
MX_TIM7_Init(); __HAL_TIM_ENABLE(&htim7); } void delay_us(u32 nus){
__HAL_TIM_SetCounter(&htim7, 0); while(__HAL_TIM_GetCounter(&htim7) < (42 * nus)
);//TIM7 Mounted on APB1 of peripheral Clock line , Clock is 42Mhz __HAL_TIM_SetCounter(&htim7, 0); while(
__HAL_TIM_GetCounter(&htim7) < (42 * nus));//TIM7 Mounted on APB1 of peripheral Clock line , Clock is 42Mhz
return; }
delay.h
#ifndef __DELAY_H #define __DELAY_H #include <sys.h> #define delay_ms(i)
HAL_Delay(i) extern TIM_HandleTypeDef htim7; void MX_TIM7_Init(void); void
delay_init(void); void delay_us(u32); #endif
In software IIC Medium us Level delay, just call the function directly , The procedure here is F4 Code in ,F1 Pay attention to the bus frequency of the clock .

c. summary : The above is me testing the software IIC Found it during communication F1 and F4 Differences between , What you said is not comprehensive. Welcome the great God to add , But the pits involved , That's all I can think of right now , If you have any ideas, please leave a message and comment , thank you !

Technology
©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?