Use stm32 to light a lamp [operation register + library function]

Publisher:幸福的人生Latest update time:2017-02-06 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The use of stm32 is different from that of 51 microcontroller. The microcontroller can directly operate the io port by connecting the crystal oscillator and the power supply, but the clock of stm32 is

After being amplified by the frequency multiplier, the phase-locked loop outputs a stable clock frequency.

Doing so brings many benefits. Although the external clock of stm32 is only 8Mhz, after passing through the frequency multiplier, several clock frequencies can be obtained to provide different clock frequencies for different peripherals.

Therefore, STM32 has many buses, the frequencies of these buses are different, and the buses are closed before use. The corresponding buses must be opened before using the peripherals. This is also for the consideration of reducing power consumption for STM32.

 

All peripherals using stm32 must add their corresponding driver files.   

 

One thing to note about the GPIO port of stm32 is that the GPIO port can be configured into 8 working modes through the GPIO register:

  • Floating Input                                         

  • Input with pull-up resistor

  • Input with pull-down resistor

  • Analog Input

  • Open-drain output

  • Push-pull output

  • Multiplexed push-pull output

  • Multiplexed open-drain output

The first four are input states:

 

With pull-up resistor means that the stm32 has a pull-up resistor inside, and the same applies to pull-down.

Floating input means that there is nothing connected inside the stm32, and you need to connect an external pull-up resistor;

Analog input is used for AD conversion.

 

The last four are output states:

 

Open-drain output means that it can output a low level, but if you want to output a high level, you need a pull-up resistor;

Push-pull output means that it can output both high level and low level;

The last two are to open the second function of IO, and they need to be configured to this state when IO port is multiplexed.

 

This article will implement the LED cyclic flashing on the GPIOA_Pin_4 port.


 

Operation Register

 

Each I/O port of stm32 can be freely programmed, and the register of a single I/O port must be accessed as a 32-bit word. Each I/O port of stm32 is controlled by 7 registers:

  • The two 32-bit port configuration registers CRL (low eight-bit I/O port configuration register) and CRH (high eight-bit I/O port configuration register) in the configuration mode control the mode and output rate of each I/O port.

        GPIO.png

 

setmode.png

 

    Description of each bit of CRL output register (same as CRH):

 

QQ screenshot 20120629163941.png

 

    The reset value of this register is 0X4444 4444, which means the port is configured to floating input mode. Each I/O port occupies four configuration bits, the upper two bits are CNF, which sets the input and output mode. The lower two bits are Mode, which sets the output rate.

  • There are two 32-bit data registers, IDR and ODR, but only the lower 16 bits are used and can only be read in 16-bit form. The ODR register can be used to select the resistor pull-up (corresponding position 1) or pull-down mode for each I/O port in input mode; or to set the output level of each I/O port in output mode;

     Description of each bit of ODR output register (same as IRH):

        QQ screenshot 20120629164015.png

  • 1 32-bit set/reset register BSRR;

  • 1 16-bit reset register BRR;

  • 1 32-bit latch register LCKR;

 

The clock of the GPIO port is on the APB2 bus. Change the description of each bit of the clock bus register APB2ENR to:

apb2.png

 

Register operation method code: (sys.h code refers to the  stm32 direct operation register development environment configuration )

#include 	   
#include "system.h"		


//LED port definition

#define LED0 PAout(4) // PA4

void Gpio_Init(void);		   

int main(void)
{				  
	Rcc_Init(9); //System clock settings
	Gpio_Init(); //Initialize the hardware interface connected to the LED
	while(1)
	{
		LED0=0;
		delay(300000); //delay 300ms
		LED0=1;
		delay(300000);
	}	 
}


void Gpio_Init(void)
{
	RCC->APB2ENR|=1<<2; //Enable PORTA clock	   	 
	   	 
	GPIOA->CRL&=0XFFF0FFFF; 
	GPIOA->CRL|=0X00030000; //PA4 push-pull output   	 
        GPIOA->ODR|=1<<4; //PA4 output high
	  
}

 

Library function operation

 

Even if you want to light up an LED, you must first configure the stm32 clock and open the corresponding bus. Before writing the corresponding code, you need to add the peripheral driver files used to the MDK project. You need to add the general io port driver stm32f10x_gpio.c and the clock driver stm32f10x_rcc.c to the project. These two files are under Libraries/src  

 

After adding Keil, it looks like this: 

 

QQ screenshot 20120416231144.png

 

code show as below:  

#include "stm32f10x.h"

void RCC_Configuration(void);
void GPIO_Configuration(void);
void delay(vu32 n); //delay function

int main(void)
{
  	RCC_Configuration();
	GPIO_Configuration();

	while(1){

		GPIO_SetBits(GPIOA,GPIO_Pin_4); //Call library function to set LED_1 to 1 and output high level

		delay(2000000);


		GPIO_ResetBits(GPIOA,GPIO_Pin_4);		

		delay(2000000);
	}
}

void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure; //Structure initialization

  	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
 	GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; 
  	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;	
  	GPIO_Init(GPIOA, &GPIO_InitStructure); 

}

void delay(vu32 n)
{
 	while(--n);
}


void RCC_Configuration(void)
{
	/* Define enumeration type variable HSEStartUpStatus */
	ErrorStatus HSEStartUpStatus;

  	/* Reset system clock settings */
  	RCC_DeInit();
  	/* Enable HSE*/
  	RCC_HSEConfig(RCC_HSE_ON);
  	/* Wait for HSE to start oscillating and stabilize*/
  	HSEStartUpStatus = RCC_WaitForHSEStartUp();
	/* Determine whether the HSE is successfully started, if yes, enter if() */
  	if(HSESTartUpStatus == SUCCESS)
  	{
    	/* Select HCLK (AHB) clock source as SYSCLK divided by 1 */
    	RCC_HCLKConfig(RCC_SYSCLK_Div1); 
    	/* Select PCLK2 clock source as HCLK (AHB) divided by 1 */
    	RCC_PCLK2Config(RCC_HCLK_Div1); 
    	/* Select PCLK1 clock source as HCLK (AHB) divided by 2 */
    	RCC_PCLK1Config(RCC_HCLK_Div2);
    	/* Set the FLASH delay cycle number to 2 */
    	//FLASH_SetLatency(FLASH_Latency_2);
    	/* Enable FLASH prefetch cache */
    	//FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    	/* Select the phase-locked loop (PLL) clock source as HSE 1 division, the multiplier is 9, then the PLL output frequency is 8MHz * 9 = 72MHz */
    	RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    	/* Enable PLL */ 
    	RCC_PLLCmd(ENABLE);
    	/* Wait for PLL output to stabilize */
    	while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
    	/* Select SYSCLK clock source as PLL */
    	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    	/* Wait for PLL to become the SYSCLK clock source */
    	while(RCC_GetSYSCLKSource() != 0x08);
  	} 


  	/* Turn on the GPIOA clock on the APB2 bus */
  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);		
	
}


Keywords:stm32 Reference address:Use stm32 to light a lamp [operation register + library function]

Previous article:The most convenient timer Systick on stm32 [operation register + library function]
Next article:stm32 direct operation register development environment configuration

Recommended ReadingLatest update time:2024-11-17 00:51

SYSTICK_Init() configuration for STM32
void   SYSTICK_Init(void) {    /* SysTick end of count event each 1ms with input clock equal to 4.5MHz (HCLK/8, default)       SysTick_SetReload(4500);    /* Enable SysTick interrupt     SysTick_ITConfig(ENABLE);    /* Enable the SysTick Counter       SysTick_CounterCmd(SysTick_Counter_Enable); }  The peri
[Microcontroller]
STM32's 8 IO port modes
1. Push-pull output: can output high and low levels and connect digital devices; push-pull structure generally means that two transistors are controlled by two complementary signals, and one transistor is always turned on while the other is turned off. The high and low levels are determined by the power supply of the
[Microcontroller]
The crystal oscillator and clock stability of STM32 should be taken seriously!!!
 I have read many application cases online recently, and found that many of them have failed due to the STM32 crystal oscillator problem. I have also encountered it once myself. It was because the capacitor value was wrong. A netizen ourdev said: his device has system problems every few days and the system clock slo
[Microcontroller]
What does the STM32 series suffix mean?
For example: STM32 F 103 C 8 T 6  The "F" refers to the product type. Now it seems that there is only the general type, namely "F". The "103" refers to the product sub-series, 101 = basic type, 102 = USB basic type, USB 2.0 full-speed device, 103 = enhanced type, 105 or 107 = interconnect type. The "C" refers to the
[Microcontroller]
List of DMA1 channels of stm32, related operations of stm32 using DMA
DMA (Direct Memory Access) is often translated as "direct memory access". DMA applications have been available as early as Intel's 8086 platform. A complete microcontroller usually consists of components such as CPU, memory and peripherals. These components are generally independent in structure and function, and th
[Microcontroller]
List of DMA1 channels of stm32, related operations of stm32 using DMA
STM32 timer timing calculation formula
T=(TIM_Period+1)*(TIM_Prescaler+1)/TIMxCLK Among them, TIMxCLK is its clock frequency. If the crystal oscillator is 8MHz, it is generally 72MHz.
[Microcontroller]
STM32 FSMC study notes + supplement (FSMC configuration of LCD)
FSMC stands for "Static Memory Controller". After using the FSMC controller, the FSMC_A provided by the FSMC can be used as the address line, and the FSMC_D provided by the FSMC can be used as the data bus. (1) When the storage data is set to 8 bits, (FSMC_NANDInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidt
[Microcontroller]
STM32 FSMC study notes + supplement (FSMC configuration of LCD)
STM32-GPIO Study Notes
     STM32F103RB has 4 IO ports (A~D), each IO port is controlled by 7 registers, they are:         Port configuration register (32 bits, two in total, CRL and CRH) Data register (32 bits, two in total, IDR and ODR) Set/Reset Register (32 bits, one, BSRR) Reset register (16 bits, one, BRR) Latch register (32 b
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号