#include "stm32f10x_lib.h"
#include "stm32f10x.h"
GPIO_InitTypeDef GPIO_InitStructure; //Define GPIO macro operation structure
void Delay(__IO uint32_t nCount) //__IO macro definition volatile
{
for(; nCount != 0; nCount--);
}
/*Initialize the embedded Flash interface and initialize the PLL to reach the system available frequency*/
void RCC_Configuration(void)
{
/* Setup the microcontroller system. Initialize the Embedded Flash Interface,
initialize the PLL and update the SystemFrequency variable. */
SystemInit();
}
int main()
{
int i;
RCC_Configuration(); //Initialize FLASH and its PLL, system clock configuration
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB,ENABLE); //Peripheral clock configuration, turn on the GPIOC clock
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Configure the PC6\7\9 port as a general push-pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //Port line flip speed is 50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //Configure GPIOC port
while(1)
{
GPIO_SetBits(GPIOC, GPIO_Pin_6); //PC6 port outputs high level
GPIO_SetBits(GPIOC, GPIO_Pin_7); //PC7 port outputs a high level GPIO_SetBits(GPIOC, GPIO_Pin_9); //PC9 port outputs a high level GPIO_SetBits(GPIOC, GPIO_Pin_9); //PC9 port outputs a high level
for(i=0;i<1000000;i++);
GPIO_ResetBits(GPIOC, GPIO_Pin_6);
GPIO_ResetBits(GPIOC, GPIO_Pin_7);
// GPIO_ResetBits(GPIOC, GPIO_Pin_9);
// Delay(0xAFFFF);
for(i=0;i<1000000;i++);
}
}
The specific code project is in my resources: free of charge http://download.csdn.net/detail/yx_l128125/4494861
Step 1: declare the GPIO structure:
GPIO_InitTypeDef GPIO_InitStructure;
Step 2 is to assign values to the members of the variable GPIO_InitStructure. If we only set some of the members, we need the following code:
/** * LED1->PC6,LED2->PC7,LED3->PC9 */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
In fact, one function is omitted here:
GPIO_StructInit, which is used to initialize variables
GPIO_InitStructure, after experimentation, I found that it is also OK without it. You can try it:) Then
Modify the members of this variable. There are three members. On the STM32 development board, the GPIO port is connected to
PC6, PC7, PC9 pins. Therefore, we assign GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9 to the GPIO_Pin member.
Assign GPIO_Speed_50MHz to the GPIO_Speed member.
The GPIO_Mode member is set to GPIO_Mode_Out_PP, indicating push-pull output mode.
Push-pull output -> (the output high level current is as large as the output low level current)
The push-pull circuit is a circuit in which two transistors or MOSFETs with the same parameters exist in the circuit in a push-pull manner, each responsible for the waveform amplification task of the positive and negative half cycles. When the circuit is working, only one of the two symmetrical power switch tubes is turned on at a time, so the conduction loss is small and the efficiency is high. The output can either inject current into the load or extract current from the load.
The push-pull circuit is suitable for low voltage and high current situations and is widely used in power amplifier circuits and switching power supplies.
The advantages are: simple structure, high utilization rate of the switching transformer core, and when the push-pull circuit is working, only one of the two symmetrical power switch tubes is turned on at a time, so the conduction loss is small.
The disadvantages are: the transformer has a center tap, and the switch tube has a high withstand voltage; due to the existence of the primary leakage inductance of the transformer, a large voltage spike will be generated at the drain-source electrode at the moment the power switch tube is turned off. In addition, the input current ripple is large, so the input filter is larger in size.
The simplified circuit diagram of the push-pull part of the transistor is shown in the figure
Step 3: Call the function GPIO_Init() to initialize the peripheral GPIO. The code is as follows:
GPIO_Init(GPIOC,&GPIO_InitStructure);
Step 4: Enable. Note that in the firmware library, GPIO does not have a GPIO_Cmd function, so this step is omitted.
Through the above four steps, we have set up GPIO. There is still one problem that cannot be ignored:
Before setting up the peripheral, we must call a clock function to enable the peripheral clock.
In the CPU user manual, we know that stm32 has several clocks. Which one should we use now?
What about the clock? Open the P25 page of the Chinese version of the "STM32F10xxx Reference Manual" and take a screenshot as shown below.
We are using the GPIOC port, so APB2 is used.
Step 5: After determining the function corresponding to this APB2, we call it:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
Among them, the first parameter needs to indicate which port's clock to enable, RCC_APB2Periph_GPIOx is to enable the GPIOx clock, and the second parameter needs to indicate whether to enable or disable, ENABLE/DISABLE.
Note that the clock enable function should be placed first. The clock must be available before subsequent operations can be performed.
Let's first look at the schematic diagram to see how the LED is connected:
From the schematic diagram, we can see that in order to make all three LEDs light up, the corresponding pins must be
Clear to 0. Setting to 1 will turn off the LED. Now open the STM32 firmware library document and find section 10.2, GPIO
Library functions. All functions for GPIO settings are here. Let's see which function of the GPIO library we want to use.
Found: GPIO_SetBits(); and GPIO_ResetBits();
According to the description, these two functions set a pin to high level and low level respectively.
The GPIO_SetBits function sets the high level.
The GPIO_ResetBits function is a reset operation.
With the information obtained, write the following code: Our goal is to make all LEDs flash regularly and in an infinite loop.
We also need to call SystemInit() to initialize the entire system, including setting the clock to
72MHZ. After the above configuration is completed, you can download HEX according to the relevant tutorials of MDK+Jlink.
The file is loaded into the board for debugging.
JLINK burning method is in my resource: "Struggle version STM32 development board JTAG download steps" http://download.csdn.net/detail/yx_l128125/4494855
Promise! I must publish several articles about system clocks - tick timers, ordinary timers, and 485 communications before this Friday. Due to time constraints, I have not been able to organize them into a blog. I promise to put some pressure on myself! I will not embarrass myself!
Previous article:STM32 Novice Growth Record---General Timer Application
Next article:STM32 Novice Growth Record---1
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- The actual output voltage of the SEPIC circuit does not match the theoretical value.
- Transistor push-pull output circuit
- Sharing of national competition experience
- MCU debugging method: using serial port to realize simple shell function
- [ST MEMS waterproof pressure sensor LPS27HHW review] + serial communication based on STM32F411RE
- Last day! Prize-winning quiz: Let’s explore together - the hard-core technology behind energy storage applications
- Testing the Kalman Filter
- "Date with Spring" + Balcony Plants
- Ltspice .save
- SinlinxA33 development board uses MIPI DSI screen