LED and Push Button code for STM32F4 on Keil uVision5

STM32F4-Discovery
/*----------------------------------------------------------------------------
 * Name:     Toggle.c
 * Purpose:  Blinks LED PD13. Toggles LED PD14 by PA0 Button
 * Author:   Abu Bakar Siddique
 * Software: Keil uVision V5.17.0.0
 * Platform: STM32F401C, STM32F4 Discovery Board
 * Dated:    April 2016
 *----------------------------------------------------------------------------
 * This software is supplied "AS IS" without warranties of any kind.
 *
 * Copyright (c) 2004-2015 Abu Bakar Siddique. All rights reserved.
 *----------------------------------------------------------------------------*/

#include "stm32f4xx_hal.h"              // Keil::Device:STM32Cube HAL:Common

void delay(int time){
	int i, j;
	for (i = time; i > 0; i--){
		for(j = 100; j > 0; j--);
	}
}

int main (void) {
  GPIO_InitTypeDef GPIO_InitStruct;
  HAL_Init();                               /* Initialize the HAL Library     */
  __GPIOA_CLK_ENABLE();
	__GPIOD_CLK_ENABLE();

  GPIO_InitStruct.Pin   = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15 | GPIO_PIN_4 | GPIO_PIN_3;
  GPIO_InitStruct.Mode  = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull  = GPIO_PULLDOWN;
  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

  /* Configure GPIO pin: PA0 (USER) */
  GPIO_InitStruct.Pin = GPIO_PIN_0;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

	HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_RESET);
	HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET);	

  while (1) {

    int flag = 0;
		if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0) == GPIO_PIN_SET && flag == 0) {
			HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_14);
			flag = 1;
		}

		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
		delay(10000);
		flag = 0;

		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_SET);
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
		delay(10000);

		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
		delay(10000);

		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET);
		delay(10000);

	}
}

Advertisements

Comments Invited

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s