Professional Documents
Culture Documents
• Output
• LEDs
APB2
PD [15:0] GPIO port D
GPIO PIN
OUTPUT CURRENT
MCU
…and let there be light!
GPIO: What
I want to use a GPIO. What do I need to know?
• Which bus the GPIOs are connected to?
• GPIO ports are on the AHB1 bus
• Which Port/PIN the GPIO is on?
• Depends on the application/board
I want to use a LED on the Discovery board. What do I need to know?
• Which PIN the LED is connected to?
• look at your board schematic!
• Four user LEDs: LD3 (orange), LD4 (green), LD5 (red) and LD6 (blue):
• User LD3 orange: connected to the I/O PD13
• User LD4 green: connected to the I/O PD12
• User LD5 red: connected to the I/O PD14
• User LD6 blue: connected to the I/O PD15
• What do I need to do with this GPIO? (input, output, ...)
• I need to write (output)
GPIO: Where
int main(void) {
GPIO_InitTypeDef GPIO_InitStructure; /* Enable the GPIO_LED Clock */
while(1);
}
GPIO: LED blink
int main(void) {
. . .
while(1) {
myDelay(1000000);
GPIO_SetBits(GPIOD,GPIO_Pin_15);
myDelay(1000000);
GPIO_ResetBits(GPIOD,GPIO_Pin_15); Check the code example:
} main-lab2-GPIO.c
}
return;
}
GPIO: Use library
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
int main(void) {
STM_EVAL_LEDInit(LED6)
while(1) {
myDelay(1000000); Check the code example:
STM_EVAL_LEDToggle(LED6); main-lab2-LED.c
}
}
return;
}
SysTick
SysTick: Intro
What if… I need to repeat a periodic task or put a delay in my code?
• Very common problem in all embedded applications!
• Example: blink a LED every second
if (SysTick_Config(SystemCoreClock / 1000)) {
/* Capture error */ ISR executed every 1 ms
while(1);
}
}
void SysTick_Handler(void) {
static u16 counter = 0; static variable
counter++;
1.2) Write a code to use all LEDs and use them to compose a blinking pattern. The pattern
can be anything but not all LEDs together (e.g. circular pattern, alternate 2 and 2, different
frequencies for each LED, …).
• Do not use STM_EVAL_xxx functions, write the code using only GPIO functions.
• Use Delay function, not SysTick
GPIO: Questions
Look at the Reference manual, stm32f10x_gpio.h/stm32f4xx_gpio.c (and
google).
The answers can be in Italian or in English
1.c) What is the purpose of the configurations “Input pull-up” and “Input pull-
down”?
SysTick: Assignments
1.3) Write a code to blink two or more LEDs with different patterns/blinking frequency.
• You can use STM_EVAL_xxx functions or write your functions using GPIO functions.
• Use only the SysTick, no Delay functions
1.4) Write a code to blink more than one LED, with different patterns/blinking frequency. All
the blinking frequencies should be below 1Hz.
• You can use STM_EVAL_xxx functions or write your functions using GPIO functions.
• Use only the SysTick, no Delay functions
SysTick: Questions
1.d) What is SystemCoreClock? Where is it defined and what is its value?