STM32 library function quick start

Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it

1, Port P

Initialization of P port and its related functions

void LED_Init(void)
{
 
 GPIO_InitTypeDef  GPIO_InitStructure;
 	
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);	 //Enable PB,PE port clock
	
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;				 //LED0 -- > Pb. 5 port configuration
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //Push pull output
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;		 //The speed of IO port is 50MHz
 GPIO_Init(GPIOB, &GPIO_InitStructure);					 //Initialize GPIOB.5 according to the set parameters
 GPIO_SetBits(GPIOB,GPIO_Pin_5);						 //PB.5 output high

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;	    		 //LED1 -- > PE. 5 port configuration, push-pull output
 GPIO_Init(GPIOE, &GPIO_InitStructure);	  				 //Push pull output, IO port speed 50MHz
 
 GPIO_SetBits(GPIOE,GPIO_Pin_5); 						 //PE.5 output high 
 GPIO_ResetBits(GPIOE,GPIO_Pin_5); 						 //PE.5 Output Low
 GPIO_write(GPIOE,portval);							    //PE writes a 16 bit number
}
 

2, Interrupt

1. Set the interrupt packet (set the interrupt packet by writing the corresponding value in SCB_AIRCR)

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set NVIC interrupt packet 2: 2-bit preemption priority and 2-bit response priority

2. External interrupt register and working principle

1. In EXTI_InitStructure.EXTI_Mode select interrupt or event:
(1) Interrupt means that when the corresponding p port is triggered externally, the cpu will automatically call the relevant interrupt service program.
(2) Event means that when the corresponding p port is triggered externally, the interrupt controller will automatically generate a pulse to a pin.
2. Interrupts and events have their own mask registers.
3. When the falling edge and rising edge trigger Register in parallel, they can be selected at the same time
4. When the software interrupt event Register is set to 1, the interrupt will be caused whether it is triggered externally or not.

3. External interrupt settings (first configure the internal parameters of EXTIx, and then configure the relevant settings of EXTIx in the interrupt)

1.RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); // Enable multiplexing function clock through RCC_ Set parameters in apb1enr to select the multiplexing clock corresponding to the P port.
2.GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);// Set external interrupt EXTI2 and select PE port 3.exti_ Init(&EXTI_InitStruct ure); // According to exti_ Initialize the peripheral exti register with the parameters specified in initstruct
4.EXTI_ClearITPendingBit(EXTI_Line2); // Clear the interrupt flag bit on LINE2


void EXTIX_Init(void)
{
 
 	EXTI_InitTypeDef EXTI_InitStructure;
 	NVIC_InitTypeDef NVIC_InitStructure;
 	
  	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);	//Enable multiplexing function clock


   //The falling edge of disconnection and interrupt initialization configuration in GPIOE.2 is triggered
  	GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);//Set external interrupt EXTI2 to select PE port
  	EXTI_InitStructure.EXTI_Line=EXTI_Line2;	//Select EXTI2 in EXTI0~15 to initialize external interrupt
  	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	//Select interrupt or event
  	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//Falling edge trigger
  	EXTI_InitStructure.EXTI_LineCmd = ENABLE;//Enable
  	EXTI_Init(&EXTI_InitStructure);	 	//According to EXTI_ Initialize the peripheral EXTI register with the parameters specified in initstruct


   //GPIOE.3 	   Interrupt line and falling edge of interrupt initialization configuration trigger / / KEY1
  	GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource3);
  	EXTI_InitStructure.EXTI_Line=EXTI_Line3;//Select EXTI3 in EXTI0~15 to initialize external interrupt
  	EXTI_Init(&EXTI_InitStructure);	  	//According to EXTI_ Initialize the peripheral EXTI register with the parameters specified in initstruct

  

	//(first configure the internal parameters of EXTIx, and then configure the relevant settings of EXTIx in the interrupt) 
    NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;			//Select EXTI2 interrupt in set interrupt
  	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;	//Preemption priority 2, 
  	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;					//Sub priority 2
  	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;								//Enable external interrupt channel
  	NVIC_Init(&NVIC_InitStructure);


  	NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn;			//Select EXTI2 interrupt in set interrupt
  	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;	//Preemption priority 2 
  	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;					//Sub priority 1 
  	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;								//Enable external interrupt channel
  	NVIC_Init(&NVIC_InitStructure);  	  //According to NVIC_ Initializes the peripheral NVIC register with the parameters specified in initstruct

 
}


 
//External interrupt 2 service program
void EXTI2_IRQHandler(void)
{
	delay_ms(10);//Debounce
	if(KEY2==0)	  //Key KEY2
	{
		LED0=!LED0;
	}		 
	EXTI_ClearITPendingBit(EXTI_Line2);  //Clear the interrupt flag bit on LINE2  
}
//External interrupt 3 service routine
void EXTI3_IRQHandler(void)
{
	delay_ms(10);//Debounce
	if(KEY1==0)	 //Key 1
	{				 
		LED1=!LED1;
	}		 
	EXTI_ClearITPendingBit(EXTI_Line3);  //Clear the interrupt flag bit on LINE3  
}


3, Serial communication

1. Process: first enable the serial port clock, then configure and initialize the serial port parameters, configure and initialize the interrupt parameters of the serial port, then set which serial port events can cause interrupts, and finally write the serial port interrupt service function.
2. Main library functions:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); // Enable usart1, external interrupt gpioa clock
USART_ Init(USART1, &USART_InitStructure); // Initialize serial port 1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);// Open serial port to accept interrupt
USART_Cmd(USART1, ENABLE); // Enable serial port 1
NVIC_ Init(&NVIC_InitStructure); // Initializes the VIC register according to the specified parameters
Res =USART_ReceiveData(USART1); // Read received data
USART_SendData(USART1, USART_RX_BUF[t]);// Send data to serial port 1
USART_GetFlagStatus(USART1,USART_FLAG_TC)

u16 USART_RX_STA=0;       //Receive status flag	  
  
void uart_init(u32 bound)
{
	//GPIO port settings
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	//Enable USART1, external interrupt GPIOA clock

	//USART1_TX   GPIOA.9
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//Multiplexed push-pull output
	GPIO_Init(GPIOA, &GPIO_InitStructure);//Initialize GPIOA.9

	//USART1_RX 	   GPIOA.10 initialization
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//Floating input
	GPIO_Init(GPIOA, &GPIO_InitStructure);//Initialize GPIOA.10  

	//Usart1 NVIC configuration
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//Preemption priority 3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//Sub priority 3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ channel enable
	NVIC_Init(&NVIC_InitStructure);	//Initializes the VIC register according to the specified parameters

	//USART initialization settings

	USART_InitStructure.USART_BaudRate = bound;//Serial baud rate
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//The word length is in 8-bit data format
	USART_InitStructure.USART_StopBits = USART_StopBits_1;//A stop bit
	USART_InitStructure.USART_Parity = USART_Parity_No;//No parity bit
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//No hardware data flow control
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;	//Transceiver mode

  USART_Init(USART1, &USART_InitStructure); //Initialize serial port 1
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//Open serial port to accept interrupt
  USART_Cmd(USART1, ENABLE);                    //Enable serial port 1 

}

void USART1_IRQHandler(void)                	//Serial port 1 interrupt service program
{
	u8 Res;
#if SYSTEM_SUPPORT_OS  		// If system_ SUPPORT_ If OS is true, you need to support OS
	OSIntEnter();    
#endif
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //Receive interrupt (the received data must end in 0x0D and 0x0a)
		{
		Res =USART_ReceiveData(USART1);	//Read received data
		
		if((USART_RX_STA&0x8000)==0)//Reception incomplete
			{
			if(USART_RX_STA&0x4000)//0x0d received
				{
				if(Res!=0x0a)USART_RX_STA=0;//Receive error, restart
				else USART_RX_STA|=0x8000;	//Reception is complete 
				}
			else //I haven't received 0X0D yet
				{	
				if(Res==0x0d)USART_RX_STA|=0x4000;
				else
					{
					USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
					USART_RX_STA++;
					if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//Error receiving data, restart receiving	  
					}		 
				}
			}   		 
     } 

4, DMA

1.DMA principle block diagram:
Each DMA has 6 channels, and each channel can trigger DMA communication, so STM32 has 4 priorities to set the request of each channel.
Each channel has 3 ~ 8 channel requests. When using X channel, you need to open a channel request of X channel, that is, set a channel request to DMA mode.


2. Main library functions:
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); // Enable DMA transmission
DMA_DeInit(DMA_CHx); // Reset the DMA channel 1 register to the default value
DMA_ Init(DMA_CHx, &DMA_InitStruct ure); // According to DMA_ Initialization with the parameters specified in initstruct
DMA_SetCurrDataCounter(DMA_CHx,DMA1_MEM_LEN);// The number of DMA transfers per time. The function overrides the number of transfers set during reinitialization
DMA_Cmd(DMA_CHx, ENABLE); // Enable the channel indicated by USART1 TX DMA1
DMA_ClearFlag(DMA1_FLAG_TC4);// Clear channel 4 transmission completion flag
DMA_GetFlagStatus(DMA1_FLAG_TC4)
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); // Enable DMA transmission of serial port 1, USART_ In CR3, you can select whether to enable DMA mode for serial port transmission. This function determines to use USART transmission request of DMA channel x as peripheral request port of DMA channel.

u16 DMA1_MEM_LEN;//Save the length of each DMA data transfer 	    
//Configuration of each channel of DMA1
//The transmission form here is fixed, which should be modified according to different situations
//From memory - > peripheral mode / 8-bit data width / memory increment mode
//DMA_CHx:DMA channel CHx
//cpar: peripheral address
//cmar: memory address
//cndtr: data transmission volume 

void MYDMA_Config(DMA_Channel_TypeDef* DMA_CHx,u32 cpar,u32 cmar,u16 cndtr)
{
 	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);	//Enable DMA transmission
	
	  DMA_DeInit(DMA_CHx);   //Reset the DMA channel 1 register to the default value


	DMA_InitStructure.DMA_PeripheralBaseAddr = cpar;  //DMA peripheral base address
	DMA_InitStructure.DMA_MemoryBaseAddr = cmar;  //DMA memory base address
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;  //Data transmission direction, read from memory and sent to peripherals
	DMA_InitStructure.DMA_BufferSize = cndtr;  //Size of DMA cache for DMA channel
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;  //Peripheral address register unchanged
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;  //Memory address register increment
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;  //The data width is 8 bits
	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //The data width is 8 bits
	DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;  //Operating in normal mode
	DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMA channel x has medium priority 
	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;  //DMA channel x is not set for memory to memory transfer
	DMA_Init(DMA_CHx, &DMA_InitStructure);  //According to DMA_ Initialize DMA channel usart1 with the parameters specified in initstruct_ Tx_ DMA_ Register identified by channel
	  	
}

 USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE); Enable serial port 1 DMA send out, USART_CR3 You can choose whether to enable or not DMA Serial communication.
 
void MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx)
{ 
	DMA_Cmd(DMA_CHx, DISABLE );  //Close the channel indicated by USART1 TX DMA1      
 	DMA_SetCurrDataCounter(DMA_CHx,DMA1_MEM_LEN);//Size of DMA cache for DMA channel
 	DMA_Cmd(DMA_CHx, ENABLE);  //Enable the channel indicated by USART1 TX DMA1 
}	  

Tags: Single-Chip Microcomputer stm32 ARM

Posted on Sun, 05 Dec 2021 18:26:40 -0500 by briant