LESSON 02: STM32 GPIO PROGRAMMING CONTROL LED BLINK

posted in: Microcontroller | 0

1. Theory of GPIO

GPIO (General Purpose Input/Output)  is a shared output or input pin.

Why shared?

Because each GPIO pin in addition to Input / Output functions, they also have other functions corresponding to other peripherals. For example: I2C, SPI, UART, ADC… All these pins can be used as an I/O port to read or output data in Bits. In STM32, the GPIO pins are divided into many Ports eg: PortA, PortB… The number of ports depends on the number of pins (pins) and the calling method depends on the manufacturer (for example, VK X has PortA but does not). have PortD). Each port usually has 16 pins numbered from 0 -> 15 corresponding to each pin is 1 bit. العاب تربح مال  Each pin has a different function such as analog input, external interrupt.. لعبه القمار روليت or simply output signal on/off at 0.1 level.

Configuration of GPIO STM32F1

STM32 GPIO modes:

Input:

  • Input pull up: Input with pull-up (default voltage on pin is Vcc)
  • Input pull down: Input with pull-down resistor (default voltage on pin is 0V)
  • Floating Input: Floating input, non-stationary voltage ranging from 0V to Vcc
  • Analog: Analog input, used to measure ADC

Output:

  • Ouput Push Pull: Output push-pull, the signal will only have Vcc or 0V respectively with Bits 1 and 0 written to that pin
  • Ouput Opendrain: Open drain output. It can only be pulled to 0V by writing bit 0, when writing bit 1, the IO pin will have a voltage corresponding to the source connected to that IO. ماكينات القمار على الانترنت
  • Alternate function Push Pull: Push-pull output for use in peripherals
  • Alternate function Open Drain: Open drain output, used in peripherals (common in I2C)




2. Programming with ST’s standard library

  • LED configuration program
void Led_Init()
{
	GPIO_InitTypeDef  GPIO_InitStructure; 		
	// cap xung clock cho GPIOC
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);	    	
	// Cau hinh chan LED PC13
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;			    //LED0-->PC13
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 	 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	 
  GPIO_Init(GPIOC, &GPIO_InitStructure);			    	
}

The above program provides pulses for Port GPIOC, and at the same time configures Output push-pull mode for pin PC13.

  • Program relative delay
void Delay_ms(uint32_t ms)
{
	for(uint32_t i = 0;i<ms; i++)
		for(volatile uint32_t j = 0; j<525; j++);
}
  • Program in function main
int main(void)
 {	
	 Led_Init();
  
 
  while(1)
	{
	    GPIO_SetBits(GPIOC,GPIO_Pin_13);
		  Delay_ms(1000);
			GPIO_ResetBits(GPIOC,GPIO_Pin_13);
		  Delay_ms(1000);
	}
 }

Use function GPIO_SetBits(GPIOC,GPIO_Pin_13) to set pin PC13 to 1, GPIO_ResetBits(GPIOC,GPIO_Pin_13) to reset pin PC13 to 0.

Sample code link

Leave a Reply

Your email address will not be published.