Tutorial Microcontroller MCS-51 ATMEL ISP
 

 

Super MCS51 Trainer

Standart MCS51 Trainer

Standart AVR Trainer

Programmer USBASP

Standart ARM Trainer

 

2. LED ON-OFF from Switch

In this experiment, we are going to turn ON and OFF led that connected to PORT IO microcontroller STM32F407 by using SWITCH push button. In this experiment, LED are connected to PORTB12, PORTB13 and PORTB14, and SW are connected to PORTE4, PORTE5, and PORTE6. Coocox software are used to edit, to compile and to upload, in discovery board STM32F407. Dont forget to use 3.3V power suppply in schematic as shown in Fig 1. Standart library used in this experiment are CMIS library: STM32f4xx_gpio.h, STM32f4xx_rcc.h.

Fig.1. LED and Switch schematic interface

Fig.2. CMIS Library for LED and SWITCH experiment

#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
unsigned char data;
void ledInit(void)
{
           GPIO_InitTypeDef GPIO_InitStructure;
           RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14 ;
           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
           GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void switchInit(void)
{
           GPIO_InitTypeDef GPIO_InitStructure;
           RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6 ;
           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
           GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
           GPIO_Init(GPIOE, &GPIO_InitStructure);
}
int main(void)
{
 switchInit();
 ledInit();
           while (1)
           { // about 50ns per iteration
           data=GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4);
           if (data==1)
           {
           GPIO_SetBits(GPIOB,GPIO_Pin_12);
           GPIO_SetBits(GPIOB,GPIO_Pin_13);
           GPIO_SetBits(GPIOB,GPIO_Pin_14);
           }
           else if (data==0)
           {
           GPIO_ResetBits(GPIOB,GPIO_Pin_12);
           GPIO_ResetBits(GPIOB,GPIO_Pin_13);
           GPIO_ResetBits(GPIOB,GPIO_Pin_14);
           }
           }
}