[Stm32f] Tut-02 Gpio - Hienclubvn

download [Stm32f] Tut-02 Gpio - Hienclubvn

of 14

description

create a new project on stm32

Transcript of [Stm32f] Tut-02 Gpio - Hienclubvn

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    1/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    Hm nay li l 1 ngy bun nh cn, chn i, i ung ru ko thnh nnli ngi vit bi nhng g va mi hc c. Thi ko long ngong na vo lunvn chnh.Lets Go!

    Nh ni hm trc, vi cc dng SMT32F103xxxc Flash t256n512Kb chng ta u c th dng code cho nhau, v u chn khai bo :USE_STDPERIPH_DRIVER, STM32F10X_HD trong : USE STDPERIPH

    DRIVER l khai bo sdng th vin, cn STM32F10X_HD l khai bo cho dngHigh density devices (HD). Bao gm cc chp nh hnh bn di.

    - Vy l chng tac th dng codeca con ny chocon khc trong 9

    chip trong khung

    hnh bn.

    Chng ta vn to th vin nh TUT 01 ko cg thay i, ni dung bi ny ti chcp n vnGPIO, cc sdng hm, v vit 1 hm cho nnh thno cho ph hp, ni dung cp n 1 khacnh kh nhtrong ti nguyn ca chip, nhng n sl bc u cho nhng ngi mi bt u nh ti.Lets Go!

    * ngha v ni dung ca Hm main.c

    Nu cc bn sthy rng hm main.ccode rt long ngong vi nhiunh ngha, v vic xem code chia sca ngi khc, sthy cng c kh nhiuiu khc bit. Nu bn ko hiu sko bit c nn dng hm ny hay ko? N c ngha g? Phn ny ti si ln lt

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    2/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    #include"stm32f10x.h"// Khai b|o thm th vin ring, khai bo hm, bin }y.intmain(void){

    // CODE HERE !while(1){

    // Cng vic chnh snm }y.}

    }// Phn ny khng cn quan tm, n chdng printf li khi bin dch qua giao thcRS232. -> Sdng cho vic Debug kim tra li.// C cng c, m{ del i cng c.#ifdef USE_FULL_ASSERTvoidassert_failed(uint8_t* file, uint32_t line){

    while(1){}

    }#endif

    Chng ta thy hm Main ca n qu n gin phi ko no ? By gi lptrnh c IO, cho cc chn mc cao v thp theo mun ca mnh th phi lmnh thno ? Cc hm no sthc hin v vic code ra lm sao ?

    Vic khai bo 1 cng c thc hin bng hm :

    voidGPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

    y i svo u l 2 Pointer (con tr), ngha l chng ta hon ton c thkhaibo 1 lc nhiu Pin v Port chtrong 1 lnh. Vi bin u vo l 1 mng Struct.

    hiu c vn , chng ta sln lt xt : GPIO_TypeDef*v GPIO_InitTypeDef*

    typedefstruct{__IO uint32_t CRL;__IO uint32_t CRH;__IO uint32_t IDR;__IO uint32_t ODR;__IO uint32_t BSRR;__IO uint32_t BRR;

    __IO uint32_t LCKR;} GPIO_TypeDef;

    #defineGPIOA ((GPIO_TypeDef *) GPIOA_BASE)#defineGPIOB ((GPIO_TypeDef *) GPIOB_BASE)#defineGPIOC ((GPIO_TypeDef *) GPIOC_BASE)#defineGPIOD ((GPIO_TypeDef *) GPIOD_BASE)#defineGPIOE ((GPIO_TypeDef *) GPIOE_BASE)#defineGPIOF ((GPIO_TypeDef *) GPIOF_BASE)#defineGPIOG ((GPIO_TypeDef *) GPIOG_BASE)

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    3/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    sthy cc thanh ghi GPIOxu do thanh ghi Bus APB2qun l72Mhz

    #defineGPIOA_BASE (APB2PERIPH_BASE + 0x0800)#defineGPIOB_BASE (APB2PERIPH_BASE + 0x0C00)#defineGPIOC_BASE (APB2PERIPH_BASE + 0x1000)#defineGPIOD_BASE (APB2PERIPH_BASE + 0x1400)

    #defineGPIOE_BASE (APB2PERIPH_BASE + 0x1800)#defineGPIOF_BASE (APB2PERIPH_BASE + 0x1C00)#defineGPIOG_BASE (APB2PERIPH_BASE + 0x2000)

    #defineAPB2PERIPH_BASE (PERIPH_BASE + 0x10000)

    -Nh thy, thc ra l th vin lm cho chng ta ht ri, vic khai bo v gna ch c #defineht ri, chcn khai bo dng cng no na l xong.

    GPIOA; GPIOB; GPIOC; GPIOD; GPIOE; GPIOF; GPIOG;

    By gimi n phn quan trng: GPIO_InitTypeDef*

    typedefstruct{uint16_t GPIO_Pin; // Tn Pin

    GPIOSpeed_TypeDef GPIO_Speed; // Tc xung nhp

    GPIOMode_TypeDef GPIO_Mode; // ChPin (input / Output)

    }GPIO_InitTypeDef;

    typedefenum{ GPIO_Mode_AIN = 0x0,GPIO_Mode_IN_FLOATING = 0x04,GPIO_Mode_IPD = 0x28,GPIO_Mode_IPU = 0x48,GPIO_Mode_Out_OD = 0x14,GPIO_Mode_Out_PP = 0x10,GPIO_Mode_AF_OD = 0x1C,GPIO_Mode_AF_PP = 0x18

    }GPIOMode_TypeDef;

    typedefenum{GPIO_Speed_10MHz = 1,GPIO_Speed_2MHz,GPIO_Speed_50MHz

    }GPIOSpeed_TypeDef;

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    4/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    #defineGPIO_Pin_0 ((uint16_t)0x0001) /*!< Pin 0 selected */#defineGPIO_Pin_1 ((uint16_t)0x0002) /*!< Pin 1 selected */#defineGPIO_Pin_2 ((uint16_t)0x0004) /*!< Pin 2 selected */#defineGPIO_Pin_3 ((uint16_t)0x0008) /*!< Pin 3 selected */#defineGPIO_Pin_4 ((uint16_t)0x0010) /*!< Pin 4 selected */#defineGPIO_Pin_5 ((uint16_t)0x0020) /*!< Pin 5 selected */#defineGPIO_Pin_6 ((uint16_t)0x0040) /*!< Pin 6 selected */#defineGPIO_Pin_7 ((uint16_t)0x0080) /*!< Pin 7 selected */#defineGPIO_Pin_8 ((uint16_t)0x0100) /*!< Pin 8 selected */#defineGPIO_Pin_9 ((uint16_t)0x0200) /*!< Pin 9 selected */#defineGPIO_Pin_10 ((uint16_t)0x0400) /*!< Pin 10 selected */#defineGPIO_Pin_11 ((uint16_t)0x0800) /*!< Pin 11 selected */#defineGPIO_Pin_12 ((uint16_t)0x1000) /*!< Pin 12 selected */#defineGPIO_Pin_13 ((uint16_t)0x2000) /*!< Pin 13 selected */#defineGPIO_Pin_14 ((uint16_t)0x4000) /*!< Pin 14 selected */#defineGPIO_Pin_15 ((uint16_t)0x8000) /*!< Pin 15 selected */#defineGPIO_Pin_All ((uint16_t)0xFFFF) /*!< All pins selected */

    Vy n y xem nh chng ta gii quyt xong vn vhm :voidGPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);

    Cng n gin phi ko no ? Cc i s bit hn ri. 1 v dn gin cho biny chng hn : Khi to 1 Port A, vi cc Pin 1,3,5,7 l Input v 0,2,4,6 lOutput , tc 50Mhz.

    /* Khai bo 1 bin GPIO_InitTydeDef */GPIO_InitTypeDef GPIO_A; // C tn l GPIO_A

    /* Enable GPIOA clocks */// Phn cho php bus APB2 hot ng xung nhp 72Mhz n PORT A, phn ny ti sni ksau.RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    /* Configure PA1, PA3, PA5, PA7 as output push-pull */GPIO_A.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_5 | GPIO_Pin_7;GPIO_A.GPIO_Speed = GPIO_Speed_50MHz;GPIO_A.GPIO_Mode = GPIO_Mode_Out_PP; // ChOutput Push - PullGPIO_Init(GPIOA, &GPIO_A); // Khi to

    /* Configure PA0, PA2, PA4, PA6 as input IN_FLOATING */GPIO_A.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_4 | GPIO_Pin_6;

    GPIO_A.GPIO_Speed = GPIO_Speed_50MHz;GPIO_A.GPIO_Mode = GPIO_Mode_IN_FLOATING; // ChInput IN_FloatingGPIO_Init(GPIOA, &GPIO_A); // Khi to.

    Cc bn Lu cho mnh l phi c du & hm: GPIO_Init(GPIOA, &GPIO_A);

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    5/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    Vy l OK, n y cn nh phn khi to GPIO xong, thc srt n gin vs hiu. n on k tip ti s i ln lt 1 s hm trong th vin #include"stm32f10x_gpio.h" xem coi n g ht, v slm nhng cng vic g y.

    @defgroup GPIO_Exported_Functions

    voidGPIO_DeInit(GPIO_TypeDef* GPIOx);voidGPIO_AFIODeInit(void);voidGPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);

    voidGPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);

    uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);voidGPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);voidGPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);voidGPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);

    uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);voidGPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);

    voidGPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);voidGPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);voidGPIO_EventOutputCmd(FunctionalState NewState);voidGPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);voidGPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);void GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface);

    bn trn l tt ccc hm c nh ngha, mnh chyu ssdng

    cc hm trong khung vung, phn GPIO_Initxem nh xong, by gichl vnsdng cc hm Set, Reset, Read, Write phn ny th chc l qu dri, nn kocn ni nhiu na nh? Chng ta svo lun 1 bi ton cth.// Cho GPIOA - Pin 1 v Pin 3 mc Logic cao .GPIO_SetBits(GPIOA, GPIO_Pin_1); // Ln lt tng ci 1 nh.GPIO_SetBits(GPIOA, GPIO_Pin_3); // Ko chi c cmng }u.// Cho GPIOA - Pin 5 v Pin 7 mc Logic thp .GPIO_ResetBits(GPIOA, GPIO_Pin_5);GPIO_ResetBits(GPIOA, GPIO_Pin_7);

    // Kim tra xem nu c t|c ng Button vtr GPIOA pin 0 th GPIOA pin 2 Logic Cao.if(GPIO_ReadInputDataBit(GPIOEA, GPIO_Pin_0)) GPIO_SetBits(GPIOA, GPIO_Pin_2);

    elseGPIO_ResetBits(GPIOA, GPIO_Pin_2);// Cu lnh IF c thc hin khi Pin 0 mc Logic Cao.

    // Mun nhy Led theo kiu Toggle, chst 1 cu lnh nh sau :GPIO_WriteBit(GPIODA, GPIO_Pin_1, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1)));

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    6/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    /*phn ny l 1 phn kh hay, ti mun hng dn cc bn xdng vic dch bt.Cc bn thng thy LED chy dch TRI, ri li dch PHItun t. OK, ta sitm hiu lnh >>v

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    7/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    OK, n y ri, ti v cc bn si to 1 hm Delaynh v ty bin n nh thno cho ph hp.

    // By gichng ta si xy dng 1 hm Delay_msnh.// phn gii sl 1ms, tc gi trt sl : 72.000voidDelay_ms(uint32_t num){

    uint32_t index = 0;

    /* default system clock is 72MHz */for(index = (72000 * num); index != 0; index--){}

    }

    // Hm Delay_usvi phn gii sl 1us, tc gi trt sl : 72voidDelay_us(uint32_t num){

    uint32_t index = 0;

    /* default system clock is 72MHz */for(index = (72 * num); index != 0; index--){}

    }

    Delay_ms(100); // = 100ms Xy dng v gi hm thny cho n tin, v{ n cng ~ quenDelay_us(10); // = 10uS quen thuc tkhi chng ta hc PIC vi CCS.

    By giti si vi cc bn n phn cui cng ca ni dung hng dn, l nhng lu trong hm main.c , nhiu hm con cc bn s thy nhngProject khc c, m y li ko? Bchng c vic sao ko ?

    u tin chng ta thng hay thy c 1 hm nh thny, c cn thit chy hm SystemInit()?

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    8/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    voidRCC_Configuration(void){/* Setup the microcontroller system. Initialize the Embedded Flash Interface,

    initialize the PLL and update the SystemFrequency variable. */SystemInit();

    }

    /*!< At this stage the microcontroller clock setting is already configured, this is donethrough SystemInit()function which is called from startupfile (startup_stm32f10x_xx.s)before to branch to application main.To reconfigure the default setting of SystemInit()function, refer tosystem_stm32f10x.cfile

    *// Vy l ~qu r, chng ta ko cn phi chy hm ny na, v mc nh n ~c chytrc trong tp tin startup_stm32f10x_hd.s m ta ~Add vo Project trc .

    CODE bnwww.arm.vnc vit, chng ta sthy c 1 H{m kim tra xung nhp nh thnyif(SysTick_Config(SystemCoreClock / 1000))

    { while(1);}

    Thc ra th cng ko cn thit, mc ch kim tra, nu ko m bo th ko thc hin cngvic g tip theo.

    Mt ci na cng rt l kh chu, thin nhiu vC thun, srt kh khn cho my bn vami bt u hc vC, l{ hm Delayca code muwww.arm.vnkh l kh hiu.voidDelay(__IO uint32_t nCount);voidTimingDelay_Decrement(void);

    static__IO uint32_t TimingDelay;

    voidDelay(__IO uint32_t nTime){

    TimingDelay = nTime;while(TimingDelay != 0);

    }

    voidTimingDelay_Decrement(void){

    if(TimingDelay != 0x00){

    TimingDelay--;}

    }

    Chng ta nn bt u vi Hm Main ca ti ~ x}y dng trn, rt c bn v dhiu hn.

    1 phn na, m lc trc ti c cp n l sdng thanh ghi Bus APBcp xung nhp cho cc GPIO , chng ta sim qua ci ny 1 cht.

    http://www.arm.vn/http://www.arm.vn/http://www.arm.vn/http://www.arm.vn/http://www.arm.vn/http://www.arm.vn/
  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    9/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    Nhn vo hnh trn chng ta s thy cho cc ti nguyn ca chip hotng th thng qua BusMatrixsti AHB (APB2 v APB1) , r hn chng tasnhn hnh bn di.

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    10/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    Vy mun sdng n ti nguyn no th chng ta phi cp xung cho php n hotng. Gischo php GPIOA thuc APB2 xung nhp 72Mhz.

    // N thuc trong thvin "stm32f10x_rcc.c" m chng ta ~Add vo Project phn trc.

    #include"stm32f10x_rcc.h"voidRCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);// V dcho php GPIOARCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);// 1 lc nhiu GPIO th lm sao ?RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);

    /** Nhng #define ca APB2, tin cho chng ta mun sdng.@defgroup APB2_peripheral*/#defineRCC_APB2Periph_AFIO ((uint32_t)0x00000001)

    #defineRCC_APB2Periph_GPIOA ((uint32_t)0x00000004)#defineRCC_APB2Periph_GPIOB ((uint32_t)0x00000008)#defineRCC_APB2Periph_GPIOC ((uint32_t)0x00000010)#defineRCC_APB2Periph_GPIOD ((uint32_t)0x00000020)#defineRCC_APB2Periph_GPIOE ((uint32_t)0x00000040)#defineRCC_APB2Periph_GPIOF ((uint32_t)0x00000080)#defineRCC_APB2Periph_GPIOG ((uint32_t)0x00000100)#defineRCC_APB2Periph_ADC1 ((uint32_t)0x00000200)#defineRCC_APB2Periph_ADC2 ((uint32_t)0x00000400)#defineRCC_APB2Periph_TIM1 ((uint32_t)0x00000800)#defineRCC_APB2Periph_SPI1 ((uint32_t)0x00001000)#defineRCC_APB2Periph_TIM8 ((uint32_t)0x00002000)#defineRCC_APB2Periph_USART1 ((uint32_t)0x00004000)#defineRCC_APB2Periph_ADC3 ((uint32_t)0x00008000)

    #defineRCC_APB2Periph_TIM15 ((uint32_t)0x00010000)#defineRCC_APB2Periph_TIM16 ((uint32_t)0x00020000)#defineRCC_APB2Periph_TIM17 ((uint32_t)0x00040000)#defineRCC_APB2Periph_TIM9 ((uint32_t)0x00080000)#defineRCC_APB2Periph_TIM10 ((uint32_t)0x00100000)#defineRCC_APB2Periph_TIM11 ((uint32_t)0x00200000)

    /** Tng ti vi APB1@defgroup APB1_peripheral*/

    #defineRCC_APB1Periph_TIM2 ((uint32_t)0x00000001)#defineRCC_APB1Periph_TIM3 ((uint32_t)0x00000002)#defineRCC_APB1Periph_TIM4 ((uint32_t)0x00000004)#defineRCC_APB1Periph_TIM5 ((uint32_t)0x00000008)#defineRCC_APB1Periph_TIM6 ((uint32_t)0x00000010)#defineRCC_APB1Periph_TIM7 ((uint32_t)0x00000020)#defineRCC_APB1Periph_TIM12 ((uint32_t)0x00000040)#defineRCC_APB1Periph_TIM13 ((uint32_t)0x00000080)#defineRCC_APB1Periph_TIM14 ((uint32_t)0x00000100)#defineRCC_APB1Periph_WWDG ((uint32_t)0x00000800)

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    11/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    #defineRCC_APB1Periph_SPI2 ((uint32_t)0x00004000)#defineRCC_APB1Periph_SPI3 ((uint32_t)0x00008000)#defineRCC_APB1Periph_USART2 ((uint32_t)0x00020000)#defineRCC_APB1Periph_USART3 ((uint32_t)0x00040000)#defineRCC_APB1Periph_UART4 ((uint32_t)0x00080000)#defineRCC_APB1Periph_UART5 ((uint32_t)0x00100000)#defineRCC_APB1Periph_I2C1 ((uint32_t)0x00200000)#defineRCC_APB1Periph_I2C2 ((uint32_t)0x00400000)#defineRCC_APB1Periph_USB ((uint32_t)0x00800000)#defineRCC_APB1Periph_CAN1 ((uint32_t)0x02000000)#defineRCC_APB1Periph_CAN2 ((uint32_t)0x04000000)#defineRCC_APB1Periph_BKP ((uint32_t)0x08000000)#defineRCC_APB1Periph_PWR ((uint32_t)0x10000000)#defineRCC_APB1Periph_DAC ((uint32_t)0x20000000)#defineRCC_APB1Periph_CEC ((uint32_t)0x40000000)

    Bn di s l ton b CODE ni li nhng g chng ta hc ctrong hm nay.

    /******************************************************************************* * @CODER: HIENCLUBVN

    * @PROJECT: GPIO DEMO

    * @MCU : SMT32F103xC, SMT32F103xD, CSMT32F103xE*

    * @DATE: 29/06/2013

    * @Mail: [email protected]

    ****************************************************************************** */#include"stm32f10x.h"

    /* Private Function Prototypes -----------------------------------------------*/voidDelay_ms(uint32_t num);voidDelay_us(uint32_t num);

    /* Private typedef -----------------------------------------------------------*/GPIO_InitTypeDef GPIO_C,GPIO_D,GPIO_A,GPIO_E;

    /* Private variables ---------------------------------------------------------*/uint8_t i; // Dung de dich BIT

    uint32_t Pin; //

    /*------------------------------MAIN------------------------------------------*/intmain(void){

    /* Enable GPIOA, GPIOC, GPIOD clocks */RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2

    ph_GPIOE, ENABLE);/* Tien hanh khoi tao chan cho 4 LED sang o muc LOGIC 1*/

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    12/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    /* Configure PC6, PC7 as output push-pull */// 2 LED sang O muc Logic 1.GPIO_C.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;GPIO_C.GPIO_Speed = GPIO_Speed_50MHz;GPIO_C.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOC, &GPIO_C);/* Configure PD6, PD13 as output push-pull */// 2 LED sang O muc Logic 1.GPIO_D.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_13;GPIO_D.GPIO_Speed = GPIO_Speed_50MHz;GPIO_D.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOD, &GPIO_D);/* Configure PA0 -> PA15 as output push-pull */// 16 LED sang O muc Logic 1.GPIO_A.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;GPIO_A.GPIO_Speed = GPIO_Speed_50MHz;GPIO_A.GPIO_Mode = GPIO_Mode_Out_PP;GPIO_Init(GPIOD, &GPIO_A);// Khoi tao BUTTON tu PE2 -> PE5. (4 buttons)GPIO_E.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;GPIO_E.GPIO_Speed = GPIO_Speed_50MHz;GPIO_E.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOE, &GPIO_E);

    // Bat dau chuong trinh TEST Button va Nhay LED.while(1){

    /* 1. Chuong trinh dau tien : Nhan Buton PE2 -> LED PC6 Sang. Ko nhan thi Tat. */

    // Gia su ban dau cac Buttons deu o muc Logic 1.if(!GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2)) GPIO_SetBits(GPIOC, GPIO_Pin_6);

    elseGPIO_ResetBits(GPIOC, GPIO_Pin_6);// Tuong tu Noi dung thu 2 : Nhan Button PE3 -> LED PC7 Sang, Ko nhan thi Tat.if(!GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_3)) GPIO_SetBits(GPIOC, GPIO_Pin_7);

    elseGPIO_ResetBits(GPIOC, GPIO_Pin_7);

    /* 2. Chuong trinh tiep theo l Nhay 2 LED PD6 v PD13, Sang o muc Logic 1. Thoi gian 1*/

    GPIO_WriteBit(GPIOD, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOD,GPIO_Pin_6)));

    Delay_ms(100);GPIO_WriteBit(GPIOD, GPIO_Pin_13, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOD,

    GPIO_Pin_13)));

    /* 3. Dich Bit TRAI (

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    13/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    GPIO_SetBits(GPIOA, Pin i, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, Pin

    i))); // Dich Bit PHAI >>}

    Pin = GPIO_Pin_8; // Gan cho la Pin 9for(i = 0; i

    }

    }}/*----------------------------Delay_ms-----------------------------------------*/voidDelay_ms( uint32_t num){

    uint32_t index = 0;

    /* default system clock is 72MHz */for(index = (72000 * num); index != 0; index--){}

    }/*----------------------------Delay_s-----------------------------------------*/

    voidDelay_us( uint32_t num){

    uint32_t index = 0;

    /* default system clock is 72MHz */for(index = (72 * num); index != 0; index--){}

    }

    /*-----Reports the name of the source file and the source line number----------*/#ifdef USE_FULL_ASSERTvoidassert_failed(uint8_t* file, uint32_t line){

    /* User can add his own implementation to report the file name and line number,

    ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */while(1){}

    }#endif

    /******************* (C) HIENCLUBVN - 2013 *****END OF FILE****/

  • 5/28/2018 [Stm32f] Tut-02 Gpio - Hienclubvn

    14/14

    TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

    Chn thnh cm n cc bn theo di hng dn, hng dn chdo hium vit li, khng theo 1 ti liu chnh thng no c. Vi kin thc c hn v kinh

    nghim hn chnn trong trnh khi sai v thiu st, mong cc bc ng gp hon thin. Thn !

    H Ni, 29/06/2013Tri oi bc, bun- HIENCLUBVN