STM32F103RCT6 control LED

Publisher:真实幻想Latest update time:2018-08-12 Source: eefocusKeywords:STM32F103RCT6 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Regardless of the microcontroller, if you want to control the LED light, you can only do so by controlling the level of the I/O pin of the microcontroller chip.
The same is true for ST microcontrollers. On ST microcontrollers, I/O pins can be set by software to various functions, such as input or output, so they are called GPIO (General-purpose I/O). The
GPIO pins are divided into different groups such as GPIOA, GPIOB...GPIOG. Each group of ports is divided into 0~15, for a total of 16 different pins.
For different types of chips, the number of port groups and pins is different. For details, please refer to the datasheet of the corresponding ST microcontroller chip model. 


According to the GPIO characteristics of the ST microcontroller, the steps to control the LED light are as follows: 
1. Select the specific pin to be controlled from the many GPIO port pins [the control pin connected to the LED] 
2. Configure the specific function required by the GPIO according to the peripheral 

3. Control the LED on and off by setting the GPIO output voltage

Circuit Diagram



From the circuit diagram, we can see that the low level LED is on and the high level LED is off


So we set up the following program:

The following is library function programming, which requires loading the following library files

main.c


/*********************************************************

* Platform: MDKARM 5.14

* Main control: STM32F103RCT6

**********************************************************/

#include "stm32f10x.h"

#include "led.h"

 

//Delay function

void Delay(unsigned int t)

{

while(t != 0)

{

t--;

}

}

 

int main (void)

{

LED_Heat();

while (1) //Step 3

{

        LED_Not(0); //LED flashes alternately on and off

for (j=0; j<30; j++)

{

Delay(60000);

}

}

}

led.c

#include "led.h"

//LED port initialization//step 2

void LED_Init(void)

{

GPIO_InitTypeDef GPIO_InitStructure; //Define a structure of type GPIO_InitTypeDef

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE); // Enable the GPIO peripheral clock

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; //Select control pin //Step 1

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Set the pin to general push-pull output  

  GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //Set the speed of the control pin

  GPIO_Init(GPIOC, &GPIO_InitStructure); //Initialize the structure

GPIO_SetBits(GPIOC, GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3); //Initialize LED off

}

// Light up 1 LED

void LED_On(int n)

{

switch(n)

{

case 0: GPIO_ResetBits(GPIOC,GPIO_Pin_0); //Control pin to output low level

break;

case 1: GPIO_ResetBits(GPIOC,GPIO_Pin_1);

break;

case 2: GPIO_ResetBits(GPIOC,GPIO_Pin_2);

break;

case 3:  GPIO_ResetBits(GPIOC,GPIO_Pin_3);

break;

default: break;

}

}

//

void LED_Off(int n)

{

switch(n)

{

case 0: GPIO_SetBits(GPIOC,GPIO_Pin_0); //Control pin to output high level

break;

case 1: GPIO_SetBits(GPIOC,GPIO_Pin_1);

break;

case 2: GPIO_SetBits(GPIOC,GPIO_Pin_2);

break;

case 3:  GPIO_SetBits(GPIOC,GPIO_Pin_3);

break;

default: break;

}

}

 

 

void LED_Not(int n)

{

switch(n)

{

case 0: GPIO_WriteBit(GPIOC, GPIO_Pin_0,(BitAction)(1-(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_0))));

break;

case 1: GPIO_WriteBit(GPIOC, GPIO_Pin_1,(BitAction)(1-(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_1))));

break;

case 2: GPIO_WriteBit(GPIOC, GPIO_Pin_2,(BitAction)(1-(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_2))));

break;

case 3:  GPIO_WriteBit(GPIOC, GPIO_Pin_3,(BitAction)(1-(GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_3))));

break;

default: break;

}

}


After compiling and downloading to MCU, you can see the LED flashing


Keywords:STM32F103RCT6 Reference address:STM32F103RCT6 control LED

Previous article:STM32F401 ILI9235 LCD screen 2.8 inch driver experience
Next article:STM32F103RCT6 pin functions and usage

Latest Microcontroller Articles
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号