[go: up one dir, main page]

0% found this document useful (0 votes)
31 views12 pages

Embedded C' Programming - GPIO

Uploaded by

Mr Lowkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views12 pages

Embedded C' Programming - GPIO

Uploaded by

Mr Lowkey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Embedded ‘c’ Programming

Configuration of Ports

Dr. Veena N. Hegde


Port Input Output operation

• Given
• Port A 5th Bit has to be configured as input Pin- A switch is connected
• Port C 13th Bit has been configured as Output Pin –LED Connected

• If the switch is closed , the LED has to glow.


Connection diagram for a Switch and LED

PA5

PC13
Clock is Chosen as HSE = 8MHz

HSE=8MHz
HSE=8MHz

HSE = 8MHz when Passed through PLL becomes 48 MHz


which is system clock
Configuration of Ports – Using IAR work bench
• The GPIO Configuration is available in GPIO Driver file
• Open GPIO Header file available in GPIO.C Driver file
• The structure GPIO is provided in Header File
• The function for GPIO Mode , Type , speed identification is written in
the GPIO.C Driver file
• Using the structure in the GPIO.h file the parameters are defined in
Configuration file , created for GPIO
Configuration files
• RCC has to be enabled for Both Port A as well as Port C
• The AHB Clock Enable has to be set both for Port A as well as Port C

• Since the Input Output initialization operation () function has to be


called in configuration file for Input and Output operations separately,

• Prior to initialization corresponding structure parameters are defined


• There has to be separate set of structure parameters for input as well as output
Main .c File for PORT operation
• Main.h file has to be included
• stm32_f0xx.h has to be included
• System_stm32f0xx.h has to be included
• The Clock configuration has to be included
• The Systic_clock function has to be called
• GPIO Configuration function has to be called
Configuration .c file
• //Pin 5 of Port A has been configured as input
• #include”config.h”
• void GPIO_Config(void)
• {
• GPIO_InitTypeDef GPIO_InitStructure;
• /* GPIOA Periph clock enable */
• RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOC, ENABLE);
• // RCC->AHBENR |= ((1<<17) | (1<<19));

• /* Configure GPIOA Pin5 as Input */
• GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
• GPIO_InitStructure.GPIO_Mode = 0x00;
• GPIO_InitStructure.GPIO_Speed = 0x03;
• GPIO_InitStructure.GPIO_PuPd = 0x00;
• GPIO_Init(GPIOA, &GPIO_InitStructure);
Output Port –Config.c file
• /* Configure the GPIOC Pin13 as Output */
• GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
• GPIO_InitStructure.GPIO_Mode = 0x01;
• GPIO_InitStructure.GPIO_OType = 0x00;
• GPIO_InitStructure.GPIO_Speed = 0x03;
• GPIO_InitStructure.GPIO_PuPd = 0x00;
• GPIO_Init(GPIOC, &GPIO_InitStructure);
• }
Main.c file for Port operation

• * @file Project/STM32F0xx_StdPeriph_Templates/main.c
• * @version V1.3.1
• * @date 14-May-2021
• * @brief Main program body
• ******************************************************************************
• /* Includes ------------------------------------------------------------------*/
• #include "main.h"
• #include "Config.h"
• #include "stm32f0xx_it.h"
• #include <stdio.h>
• #include <string.h>
• #include <stdint.h>

Main.c file for Port operation…Contd
/** @addtogroup STM32F0xx_StdPeriph_Templates
uint8_t DataRead = 0;
/**
* @brief Main program.
/
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f0xx.s) before to branch to application main. To
reconfigure the default setting of SystemInit() function, refer to
system_stm32f0xx.c file
*/ /*Configure systick for Every 1 milli Second*/
SysTick_Config(SystemCoreClock /1000);
Main.c file for Port operation…Contd
/* Call the GPIO Configuration function */
GPIO_Config();
/* Infinite loop */
while (1)
{
DataRead = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5);
if(DataRead == 1)
{
GPIO_WriteBit(GPIOC, GPIO_Pin_13, 0x01);
}
else
{
GPIO_WriteBit(GPIOC, GPIO_Pin_13, 0x00);
}

You might also like