One of the first experiments to do is about the LED, is the output of the IO port use , this section of the experiment is the use of independent keys, that is, IO port input use
The Ministm32 Development Board has a total of 3 independent keys, respectively, the KEY0 KEY1 wk_up schematic diagram is as follows:
Note: The KEY0 KEY1 is active low (that is, they are low for the button press) and the WK_UP is high active, why? Obviously, this problem to analyze the above schematic diagram to know, well, the slag did not learn a number of mold electrical circuit also barely 70 points (half book. Have not finished learning), just bite the bullet to analyze it (right and wrong is not guaranteed AH. QAQ): Obviously there are two symbols that are not known, GND and VCC Niang a bit
GND is grounded to mean that the level of the GND is 0, and VCC is probably the voltage source (3.3V), which means that the VCC end level of 1 first key0,key1 are connected with the GND, so they want to function level is certainly consistent, then why is low active? First, for the switch, imagine a wire is truncated by a switch, and then this wire is disconnected, how to let it connect it? If the two-point level is consistent at the switch: So this wire doesn't even get up? Seems a little farfetched Orz)
Ok really does not have to remember, next will configure the button. New two files key.c key.h import Project
#include "key.h" #include "delay.h" void Key_init (void) {gpio_inittypedef gpio_ist;//enable PORTA,PORTC clock rcc_ Apb2periphclockcmd (rcc_apb2periph_gpioa| rcc_apb2periph_gpioc,enable); Turn off Jtag Enable Swdgpio_pinremapconfig (gpio_remap_swj_jtagdisable, enable); Pc5-->key0gpio_ist. Gpio_pin=gpio_pin_5; Gpio_ist. gpio_mode=gpio_mode_ipu;//pull-up input gpio_init (GPIOC, &gpio_ist);//pa15-->key1gpio_ist. Gpio_pin=gpio_pin_15; Gpio_ist. gpio_mode=gpio_mode_ipu;//pull-up input gpio_init (GPIOA, &gpio_ist);//pa0-->wk-upgpio_ist. Gpio_pin=gpio_pin_0; Gpio_ist. gpio_mode=gpio_mode_ipd;//drop-down input gpio_init (Gpioa, &gpio_ist);} Parameter mode:0 The representative does not support continuous keys not 0 for support of continuous keys U8 Key_scan (U8 mode) {static U8 key_up=1;if (mode) key_up=1;if (key_up&& (key0==0| | key1==0| | Wk_up==1) {Delay_ms (ten); Key_up=0;if (key0==0) return Key0_pres;else if (key1==0) return Key1_pres;else if (wk_up==1) return wk_up_pres;} else if (key0==1&&key1==1&&wk_up==0) Key_up=1;return 0;}Gpio_pinremapconfig (gpio_remap_swj_jtagdisable, ENABLE); This statement: First see KEY1 corresponding PA15 that mouth behind also more a bunch of jtdi PS what thing, the manual said PA15 occupies a jtag io, this statement is prohibit JTAG, open SWD let PA15 use as normal IO output (honestly didn't understand)
Look at the following button scanning function, the parameters determine whether support continuous keys, OK, analysis does not support the situation of continuous keys, if the parameter is 0, press the key after the first scan, KEY_UP changed to 0, note the second scan, due to static keyword KEY_UP will not be re-assigned a value of 1 (this can be a Niang static use), still 0, then will return 0,. Then nothing will be done.
Until you let go.
Next write Key.h is mainly a macro definition of some keywords
#ifndef _key_h#define _key_h#include "sys.h" #define KEY0 gpio_readinputdatabit (gpioc,gpio_pin_5) #define KEY1 gpio_readinputdatabit (gpioa,gpio_pin_15) #define WK_UP gpio_readinputdatabit (gpioa,gpio_pin_0) #define KEY0_ PRES 1#define key1_pres 2#define wk_up_pres 3void key_init (void); U8 Key_scan (U8 mode); #endif
The main function (very simple control: Can read and pull at a glance)
#include "led.h" #include "sys.h" #include "delay.h" #include "key.h" int main (void) {U8 t;delay_init (); Led_init (); Key_init (); Led0=0;while (1) {t=key_scan (0); switch (t) {case key0_pres:led0=! Led0;break;case key1_pres:led1=! Led1;break;case wk_up_pres:led0=! LED0; led1=! Led1;break;//default:d Elay_ms (10);}}
Forget to use the button control led of course also to configure the LED can be the last time to do led experiment written LED.C Led.h added project inside to
Put it on again.
Led.c
#include "led.h" void Led_init (void) {gpio_inittypedef gpio_ist;//led0 rcc_apb2periphclockcmd (rcc_apb2periph_ gpioa| rcc_apb2periph_gpiod,enable); Gpio_ist. Gpio_pin=gpio_pin_8;//led0-->pa.8 gpio_ist. GPIO_MODE=GPIO_MODE_OUT_PP; Gpio_ist. Gpio_speed=gpio_speed_50mhz; Gpio_init (gpioa,&gpio_ist); Gpio_setbits (gpioa,gpio_pin_8);//led1gpio_ist. gpio_pin=gpio_pin_2; Gpio_init (gpiod,&gpio_ist); Gpio_setbits (gpiod,gpio_pin_2); }
Led.h
#ifndef _led_h#define _led_h#include "sys.h" #define LED0 paout (8)//pa 8#define LED1 pdout (2)//pd 2void led_init (void); #e Ndif
Cortex_m3_stm32 Embedded Learning Note (ii): Independent key Experiment (IO input)