Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

2 GPIO Programming

Uploaded by

Nam Võ Khắc
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)
14 views

2 GPIO Programming

Uploaded by

Nam Võ Khắc
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/ 26

www. Micro Digital Ed.

com
BIHE university

GPIO Programming

Electronics Department, HCMUT 1


Topics
www. Micro Digital Ed. com
BIHE university

• Start using STM32Cube


• GPIO Programming

Electronics Department, HCMUT 2


Starting to use STM32Cube
www. Micro Digital Ed. com
BIHE university

• Installing required software


• Create a project
• Clock configuration
• Peripheral configuration

Electronics Department, HCMUT 3


BluePill board
www. Micro Digital Ed. com
BIHE university

Electronics Department, HCMUT 4


STM32F103C8 Pinout
www. Micro Digital Ed. com
BIHE university

• Power supply pins


• Reset
• Oscillator pins
3.3V
+3.3V 48 +3.3V
VDD_3 +3.3V
36
VDD_2
24VDD VDD
VDD_1 VDD
9
VDDA
10K 10K 10K

STM32
NRST NRST NRST
47 Reset Reset
35
VSS_3 Button Button
23
VSS_2
VSS_1
8
VSSA

(a) (b) (c)

Electronics Department, HCMUT 5


System Clock
www. Micro Digital Ed. com
BIHE university

PLLMUL 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Multiplication X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16

Electronics Department, HCMUT 6


STM32F10X Clock
www. Micro Digital Ed. com
BIHE university

Electronics Department, HCMUT 7


APB1, APB2, and AHB
www. Micro Digital Ed. com
BIHE university

Electronics Department, HCMUT 8


Enabling Clocks
www. Micro Digital Ed. com
BIHE university

Label Description Label Description


IOPx I/O port x clock enable ADCnEN ADCn clock enable
USARTnEN USARTn clock enable DACnEN DACn clock enable
USBEN USB clock enable TIMnEN TIMn timer clock enable
CANEN CAN clock enable SPInEN SPI n clock enable
PWREN Power interface clock enable BKPEN Backup interface clock enable
WWDG Window watchdog clock enable SDIOEN SDIO clock enable
DMAnEN DMAn clock enable FSMCEN FSMC clock enable
CRCEN CRC clock enable I2CnEN I2Cn clock enable
Note: (0: clock disabled, 1: clock enabled)
Electronics Department, HCMUT 9
Reset, Power, and Crystals
www. Micro Digital Ed. com
BIHE university

3.3V STM32F103
48 VDD_3 20pF
36 OSC_OUT
6
Vcc VDD_2
24 VDD_1
8MHz
9
VDDA 5
OSC_IN
10K
20pF

7
NRST
20pF
OSC32_OUT
4
47
VSS_3
Reset 35
Button VSS_2 32.768KHz
23
VSS_1 3
8 OSC32_IN
VSSA
20pF

Electronics Department, HCMUT 10


GPIO pins
www. Micro Digital Ed. com
BIHE university

• PORTA (PAn)
• PORTB (PBn)
• PORTC (PCn)
• PORTD

Electronics Department, HCMUT 11


I/O unit in ARM
www. Micro Digital Ed. com
BIHE university

RAM Timers

PROGRAM
ROM

Program
Bus Bus
CPU

Interrupt Other
OSC I/O Port
Unit Peripherals

I/O
PINS

Electronics Department, HCMUT 12


GPIO Registers
www. Micro Digital Ed. com
BIHE university

Electronics Department, HCMUT 13


CRL and CRH (Configuration Registers)
www. Micro Digital Ed. com
BIHE university

Output (MODE>00)
CNFx bits MODEx bits Direction Max speed
00 General purpose output push-pull 00 Input
01 General purpose output Open-drain 01 10 MHz
10 Alternate function output Push-pull 10 Output 2 MHz
11 Alternate function output Open-drain 11 50 MHz

Input (MODE=00)
CNFx bits Configuration Description
00 Analog mode Select this mode when you use a pin as an ADC
input.
01 Floating input In this mode, the pin is high-impedance.
10 Input with pull- The value of ODR chooses if the pull-up or pull-
up/pull-down down resistor is enabled. (1: pull-up, 0:pull-down)
11 reserved

Electronics Department, HCMUT 14


IDR (Input Data Reg.) and ODR (Output Data Reg.)
www. Micro Digital Ed. com
BIHE university

GPIOx->ODR: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

GPIOx->IDR: 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Px15 Px14 Px13 Px12 Px11 Px10 Px9 Px8 Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0

Electronics Department, HCMUT 15


Toggle Port A
www. Micro Digital Ed. com
BIHE university

#include "stm32f10x.h“

void delay_ms(uint16_t t) {
volatile unsigned long l = 0;
for(uint16_t i = 0; i < t; i++)
for(l = 0; l < 6000; l++)
{ }
}
int main() {
RCC->APB2ENR |= 0xFC; //Enable the clocks for GPIO ports

GPIOA->CRL = 0x33333333; //PA0 to PA7 as outputs


GPIOA->CRH = 0x33333333; //PA8 to PA15 as outputs

while(1) {
GPIOA->ODR = 0x0000; //make all the pins of Port A low
delay_ms(1000); //wait 1000ms
GPIOA->ODR = 0xFFFF; //make all the pins of Port A high
delay_ms(1000); //wait 1000ms
}
}

Electronics Department, HCMUT 16


Toggling PC13
www. Micro Digital Ed. com
BIHE university

#include <stm32f10x.h>

void delay_ms(uint16_t t);

int main()
{
RCC->APB2ENR |= 0xFC; //Enable GPIO ports clocks

GPIOC->CRH = 0x44344444; //PC13 as output

while(1)
{
GPIOC->ODR ^= (1<<13); //toggle PC13
delay_ms(1000);
}
}

Electronics Department, HCMUT 17


Example: Input
www. Micro Digital Ed. com
BIHE university

• The following code gets the data present at the pins of port A and
sends it to port B indefinitely, after adding the value 5 to it:

#include <stm32f10x.h>

int main()
{
RCC->APB2ENR |= 0xFC; /* Enable GPIO ports clocks */

GPIOA->CRL = 0x33333333; /* PA0-PA7 as outputs */


GPIOA->CRH = 0x33333333; /* PA8-PA15 as outputs */
GPIOB->CRL = 0x44444444; /* PB0-PB7 as inputs */
GPIOB->CRH = 0x44444444; /* PB8-PB15 as inputs */

while(1)
{
GPIOA->ODR = GPIOB->IDR + 5;
/* read from port B and write to port A */
}
}

Electronics Department, HCMUT 18


Example
www. Micro Digital Ed. com
BIHE university

• A switch is connected to pin PB10 and an LED to pin


PC13. Write a program to get the status of SW and send
it to the LED. VCC STM32F103

10K

#include <stm32f10x.h> PC13


PB10
int main() { 100

RCC->APB2ENR |= 0xFC; /* Enable GPIO ports clocks */

GPIOB->CRH = 0x44444444; /* PB8-PB15 as inputs */


GPIOC->CRH = 0x44344444; /* PC13 as output */

while(1) {
if((GPIOB->IDR & (1<<10)) != 0) /* is PB10 high */
GPIOC->ODR |= (1 << 13); /* make PC13 high */
else
GPIOC->ODR &= ~(1 << 13); /* make PC13 low */
}
}

Electronics Department, HCMUT 19


Internal Pull-up/Pull-down resistor
www. Micro Digital Ed. com
BIHE university

bit n of ODR

In CRH:CRL Pin n
0x4 = input bit n of IDR of port
(no pull-up/down)
0x8 = input with
pull-up/down

Electronics Department, HCMUT 20


Example
www. Micro Digital Ed. com

STM32F103
BIHE university

• A switch is connected to pin


PB10 and an LED to pin
PC13. Write a program to get PB10 PC13
100
the status of SW and send it
to the LED.
#include <stm32f10x.h>
int main() {
RCC->APB2ENR |= 0xFC; /* Enable GPIO ports clocks */

GPIOB->CRH = 0x44444844; // pull-up PB10


GPIOB->ODR |= (1<<10); //set bit 10 of ODR to pull-up
GPIOC->CRH = 0x44344444; /* PC13 as output */
while(1) {
if((GPIOB->IDR & (1<<10)) != 0) /* is PB10 high */
GPIOC->ODR |= (1 << 13); /* make PC13 high */
else
GPIOC->ODR &= ~(1 << 13); /* make PC13 low */
}
}

Electronics Department, HCMUT 21


Example
www. Micro Digital Ed. com

STM32F103
BIHE university

• A switch is connected to pin


PB10 and an LED to pin PC13.
#include <stm32f10x.h>
Write a program
void delay_ms(uint16_t t); that toggles PB10 PC13
100
PC13 when the key is pressed.
int main() {
RCC->APB2ENR |= 0xFC; /* Enable GPIO ports clocks */
GPIOC->CRH = 0x44344444; /* PC13 as output */

GPIOA->CRL = 0x44444844; /* PA2 as input with pull-up */


GPIOA->ODR |= (1<<2); /* pull-up PA2 */

while(1) {
if((GPIOA->IDR&(1<<2)) == 0) /* is PA2 low? */
GPIOC->ODR ^= (1<<13); /* toggle PC13 */
else
GPIOC->ODR &= ~(1<<13);
delay_ms(500);
}
}

Electronics Department, HCMUT 22


Clearing pins
www. Micro Digital Ed. com
BIHE university

• BRR (Bit Reset Register)

• Examples:
– GPIOB->BRR = 1<<5; //make PB5 low
– GPIOA->BRR = (1<<3)|(1<<5); /* make PA3 and
PA5 low */

Electronics Department, HCMUT 23


Setting & Clearing Pins
www. Micro Digital Ed. com
BIHE university

• BSRR (Bit Set/Reset Register)

• Examples:
– GPIOC->BSRR = (1<<5); //make PC5 high
– GPIOB->BSRR = (1<<5)|(1<<19); /*makes PB5
high and PB3 low */

Electronics Department, HCMUT 24


The structure of I/O pins
www. Micro Digital Ed. com
BIHE university

IDR

BRR
BSRR

ODR

Electronics Department, HCMUT 25


The structure of 5-volt tolerant I/O pins
www. Micro Digital Ed. com
BIHE university

Electronics Department, HCMUT 26

You might also like