Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
gpio.c
00001 #include "gpio.h" 00002 #include "main.h" 00003 #include "stm32f4xx_rcc_mort.h" 00004 00005 00006 /* Below are defined Address of Port F and corresponding registers*/ 00007 #define PORTF_BASE_ADDRESS ((uint32_t)0x40021400) 00008 #define PORTF_MODER_REGISTER (PORTF_BASE_ADDRESS + 0x00) 00009 #define PORTF_OTYPER_REGISTER (PORTF_BASE_ADDRESS + 0x04) 00010 #define PORTF_OSPEEDR_REGISTER (PORTF_BASE_ADDRESS + 0x08) 00011 #define PORTF_PUPDR_REGISTER (PORTF_BASE_ADDRESS + 0x0C) 00012 #define PORTF_IDR_REGISTER (PORTF_BASE_ADDRESS + 0x10) 00013 #define PORTF_ODR_REGISTER (PORTF_BASE_ADDRESS + 0x14) 00014 #define PORTF_AFR1_REGISTER (PORTF_BASE_ADDRESS + 0x20) 00015 00016 /* Below are defined Address of Port B and corresponding registers*/ 00017 #define PORTB_BASE_ADDRESS ((uint32_t)0x40020400) 00018 #define PORTB_MODER_REGISTER (PORTB_BASE_ADDRESS + 0x00) 00019 #define PORTB_OTYPER_REGISTER (PORTB_BASE_ADDRESS + 0x04) 00020 #define PORTB_OSPEEDR_REGISTER (PORTB_BASE_ADDRESS + 0x08) 00021 #define PORTB_PUPDR_REGISTER (PORTB_BASE_ADDRESS + 0x0C) 00022 #define PORTB_IDR_REGISTER (PORTB_BASE_ADDRESS + 0x10) 00023 #define PORTB_ODR_REGISTER (PORTB_BASE_ADDRESS + 0x14) 00024 #define PORTB_AFR1_REGISTER (PORTB_BASE_ADDRESS + 0x20) 00025 00026 00027 00028 void InitPortFPin7asAnalog(void) 00029 { 00030 uint32_t *reg; /*Define Pointer*/ 00031 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); /* Enable the clock */ 00032 00033 reg = (uint32_t *)PORTF_MODER_REGISTER; 00034 *reg = *reg & (~((uint32_t)(0x03<<14))); /*Clear bits 14 & 15 of Moder Register*/ 00035 *reg = *reg | (uint32_t)(0x03<<14); /* Set the bits 14 & 15 as 11 - Analog Output Mode */ 00036 00037 reg = (uint32_t *)PORTF_OTYPER_REGISTER; 00038 *reg = *reg & (~((uint32_t)(0x01<<7))); /* Statement is to set the OTYPER REGISTER of Pin 7 TO 0 - Push Pull Type */ 00039 00040 00041 reg = (uint32_t *)PORTF_PUPDR_REGISTER; 00042 *reg = *reg & (~((uint32_t)(0x03<<14))); /* Statement is to set the PUPDR REGISTER TO 00 - No Pull Up No Pull Down Type*/ 00043 00044 } 00045 00046 void InitPortFPin7Pin8Pin9asAnalog(void) 00047 { 00048 uint32_t *reg; /*Define Pointer*/ 00049 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); /* Enable the clock */ 00050 00051 reg = (uint32_t *)PORTF_MODER_REGISTER; 00052 *reg = *reg & (~((uint32_t)(0x03<<14))+ (uint32_t)(0x03<<16) + (uint32_t)(0x03<<18)); /*Clear bits 14, 15, 16, 17, 18, 19 of Moder Register*/ 00053 *reg = *reg | ((uint32_t)(0x03<<14) + (uint32_t)(0x03<<16) + (uint32_t)(0x03<<18)); /* Set the bits 14, 15, 16, 17, 18, 19 as 11 - Analog Output Mode */ 00054 00055 reg = (uint32_t *)PORTF_OTYPER_REGISTER; 00056 *reg = *reg & (~((uint32_t)(0x01<<7)+ (uint32_t)(0x01<<8) + (uint32_t)(0x01<<9))); /* Statement is to set the OTYPER REGISTER of Pin 7, 8, 9 TO 0 - Push Pull Type */ 00057 00058 00059 reg = (uint32_t *)PORTF_PUPDR_REGISTER; 00060 *reg = *reg & (~((uint32_t)(0x03<<14)+ (uint32_t)(0x03<<16) + (uint32_t)(0x03<<18) )); /* Statement is to set the PUPDR REGISTER OF PIN 7,8,9 TO 00 - No Pull Up No Pull Down Type*/ 00061 00062 } 00063 00064 00065 void InitPortBPin0asOutput(void) 00066 { 00067 uint32_t *reg; /*Define Pointer*/ 00068 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* Enable the clock */ 00069 00070 reg = (uint32_t *)PORTB_MODER_REGISTER; 00071 *reg = *reg & (~((uint32_t)0x03)); /*Clear last two bits of Moder Register*/ 00072 *reg = *reg | ((uint32_t)0x01); /* Set the last two pins corresponding to Pin0 as Output Mode - 01 */ 00073 00074 reg = (uint32_t *)PORTB_OTYPER_REGISTER; 00075 *reg = *reg & (~((uint32_t)0x01)); /* Clear the last bit of OTYPER REGISTER*/ 00076 *reg = *reg | ((uint32_t)0x00);/*Statement is to set the OTYPER REGISTER TO 0 - Push Pull Type*/ 00077 00078 reg = (uint32_t *)PORTB_PUPDR_REGISTER; 00079 *reg = *reg & (~((uint32_t)0x03)); /* Clear the last two bits of PUPDR REGISTER*/ 00080 *reg = *reg | ((uint32_t)0x00);/*Statement is to set the PUPDR REGISTER TO 00 - No Pull Up No Pull Down Type*/ 00081 00082 reg=(uint32_t *)PORTB_OSPEEDR_REGISTER; 00083 *reg=*reg&(~((uint32_t)0x11)); /* Clear the last two bits of OSPEEDR REGISTER*/ 00084 *reg=*reg|((uint32_t)0x11); /* Set the last two bits of OSPEEDR REGISTER to High Speed*/ 00085 00086 reg = (uint32_t *)PORTB_ODR_REGISTER; 00087 *reg = *reg | ((uint32_t)0x01); /* Setting the ODR REGISTER TO HIGH TO START WITH - Last bit is 1*/ 00088 } 00089 00090 00091 00092 void toggleGPIOB0(void) 00093 { 00094 uint32_t value; 00095 uint32_t *reg; /* Defining the variables*/ 00096 00097 reg = (uint32_t *)PORTB_ODR_REGISTER; /*Initializing the current value of ODR REGISTER*/ 00098 value = *reg & ((uint32_t)0x1); /* Reading the value of last bit of current ODR REGISTER - Stored in reg*/ 00099 00100 if (value > 0) 00101 { 00102 /* The bit is high initially*/ 00103 /*Need to set it to low now*/ 00104 *reg = *reg & (~((uint32_t)0x1)); 00105 } 00106 00107 else 00108 { 00109 /* The bit is low initially*/ 00110 /* Need to set it to high now*/ 00111 *reg = *reg | ((uint32_t)0x01); 00112 } 00113 } 00114 00115 00116 00117 00118
Generated on Mon Jan 16 2023 14:41:56 by
1.7.2