You are on page 1of 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 nn li ngi vit

it bi nhng g va mi hc c. Thi ko long ngong na vo lun vn chnh. Lets Go! Nh ni hm trc, vi cc dng SMT32F103xxx c Flash t 256 n 512Kb 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 s dng th vin, cn STM32F10X_HD l khai bo cho dng High density devices (HD). Bao gm cc chp nh hnh bn di.

- Vy l chng ta c th dng code ca con ny cho con khc trong 9 chip trong khung hnh bn.

Chng ta vn to th vin nh TUT 01 ko c g thay i, ni dung bi ny ti ch cp n v n GPIO, cc s dng hm, v vit 1 hm cho n nh th no cho ph hp, ni dung cp n 1 kha cnh kh nh trong ti nguyn c a chip, nhng n s l bc u cho nhng ngi mi bt u nh ti. Lets Go!

* ngha v ni dung ca Hm main.c Nu cc bn s thy rng hm main.c code rt long ngong vi nhiu nh ngha, v vic xem code chia s ca ngi khc, s thy cng c kh nhiu iu khc bit. Nu bn ko hiu s ko bit c nn dng hm ny hay ko? N c ngha g? Phn ny ti s i ln lt

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN


#include "stm32f10x.h" // Khai b|o thm th vin ring, khai bo hm, bin }y. int main(void) { // CODE HERE ! while (1) { // Cng vic chnh s nm }y. } } // Phn ny khng cn quan tm, n ch dng printf li khi bin dch qua giao thc RS232. -> S dng cho vic Debug kim tra li. // C cng c, m{ del i cng c. #ifdef USE_FULL_ASSERT void assert_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 lp trnh c IO, cho cc chn mc cao v thp theo mun ca mnh th phi lm nh th no ? Cc hm no s thc hin v vic code ra lm sao ? Vic khai bo 1 cng c thc hin bng hm :
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

y i s vo u l 2 Pointer (con tr), ngha l chng ta hon ton c th khai bo 1 lc nhiu Pin v Port ch trong 1 lnh. Vi bin u vo l 1 mng Struct. hiu c vn , chng ta s ln lt xt : GPIO_TypeDef*
typedef struct { __IO uint32_t __IO uint32_t __IO uint32_t __IO uint32_t __IO uint32_t __IO uint32_t __IO uint32_t } GPIO_TypeDef; #define #define #define #define #define #define #define GPIOA GPIOB GPIOC GPIOD GPIOE GPIOF GPIOG CRL; CRH; IDR; ODR; BSRR; BRR; LCKR; v GPIO_InitTypeDef*

((GPIO_TypeDef ((GPIO_TypeDef ((GPIO_TypeDef ((GPIO_TypeDef ((GPIO_TypeDef ((GPIO_TypeDef ((GPIO_TypeDef

*) *) *) *) *) *) *)

GPIOA_BASE) GPIOB_BASE) GPIOC_BASE) GPIOD_BASE) GPIOE_BASE) GPIOF_BASE) GPIOG_BASE)

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN s thy cc thanh ghi GPIOx u do thanh ghi Bus APB2 qun l 72Mhz
#define #define #define #define #define #define #define GPIOA_BASE GPIOB_BASE GPIOC_BASE GPIOD_BASE GPIOE_BASE GPIOF_BASE GPIOG_BASE (APB2PERIPH_BASE (APB2PERIPH_BASE (APB2PERIPH_BASE (APB2PERIPH_BASE (APB2PERIPH_BASE (APB2PERIPH_BASE (APB2PERIPH_BASE + + + + + + + 0x0800) 0x0C00) 0x1000) 0x1400) 0x1800) 0x1C00) 0x2000)

#define APB2PERIPH_BASE

(PERIPH_BASE + 0x10000)

- Nh thy, thc ra l th vin lm cho chng ta ht ri, vic khai bo v gn a ch c #define ht ri, ch cn khai bo dng cng no na l xong.
GPIOA; GPIOB; GPIOC; GPIOD; GPIOE; GPIOF; GPIOG;

By gi mi n phn quan trng: GPIO_InitTypeDef*


typedef struct { uint16_t GPIO_Pin; // Tn Pin GPIOSpeed_TypeDef GPIO_Speed; // Tc xung nhp GPIOMode_TypeDef GPIO_Mode; // Ch Pin (input / Output) }GPIO_InitTypeDef;

typedef enum { 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;

typedef enum { GPIO_Speed_10MHz = 1, GPIO_Speed_2MHz, GPIO_Speed_50MHz }GPIOSpeed_TypeDef;

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN


#define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define GPIO_Pin_0 GPIO_Pin_1 GPIO_Pin_2 GPIO_Pin_3 GPIO_Pin_4 GPIO_Pin_5 GPIO_Pin_6 GPIO_Pin_7 GPIO_Pin_8 GPIO_Pin_9 GPIO_Pin_10 GPIO_Pin_11 GPIO_Pin_12 GPIO_Pin_13 GPIO_Pin_14 GPIO_Pin_15 GPIO_Pin_All ((uint16_t)0x0001) ((uint16_t)0x0002) ((uint16_t)0x0004) ((uint16_t)0x0008) ((uint16_t)0x0010) ((uint16_t)0x0020) ((uint16_t)0x0040) ((uint16_t)0x0080) ((uint16_t)0x0100) ((uint16_t)0x0200) ((uint16_t)0x0400) ((uint16_t)0x0800) ((uint16_t)0x1000) ((uint16_t)0x2000) ((uint16_t)0x4000) ((uint16_t)0x8000) ((uint16_t)0xFFFF) /*!< /*!< /*!< /*!< /*!< /*!< /*!< /*!< /*!< /*!< /*!< /*!< /*!< /*!< /*!< /*!< /*!< Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin Pin All 0 selected */ 1 selected */ 2 selected */ 3 selected */ 4 selected */ 5 selected */ 6 selected */ 7 selected */ 8 selected */ 9 selected */ 10 selected */ 11 selected */ 12 selected */ 13 selected */ 14 selected */ 15 selected */ pins selected */

Vy n y xem nh chng ta gii quyt xong vn v hm :


void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);

Cng n gin phi ko no ? Cc i s bit hn ri. 1 v d n gin cho bi ny chng hn : Khi to 1 Port A, vi cc Pin 1,3,5,7 l Input v 0,2,4,6 l Output , 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 s ni k sau. 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; // Ch Output Push - Pull GPIO_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; // Ch Input IN_Floating GPIO_Init(GPIOA, &GPIO_A); // Khi to.

Cc bn Lu cho mnh l phi c du & hm:

GPIO_Init(GPIOA, &GPIO_A);

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN Vy l OK, n y cn nh phn khi to GPIO xong, thc s rt n gin v s 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 s lm nhng cng vic g y.
@defgroup GPIO_Exported_Functions void GPIO_DeInit(GPIO_TypeDef* GPIOx); void GPIO_AFIODeInit(void); void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct); void GPIO_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); void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal); uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx); uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx); void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal); void void void void void void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource); GPIO_EventOutputCmd(FunctionalState NewState); GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState); GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource); GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface);

bn trn l tt c cc hm c nh ngha, mnh ch yu s s dng cc hm trong khung vung, phn GPIO_Init xem nh xong, by gi ch l vn s dng cc hm Set, Reset, Read, Write phn ny th chc l qu d ri, nn ko cn ni nhiu na nh? Chng ta s vo lun 1 bi ton c th.
// Cho GPIOA - Pin 1 v Pin 3 mc GPIO_SetBits(GPIOA, GPIO_Pin_1); // GPIO_SetBits(GPIOA, GPIO_Pin_3); // // Cho GPIOA - Pin 5 v Pin 7 mc GPIO_ResetBits(GPIOA, GPIO_Pin_5); GPIO_ResetBits(GPIOA, GPIO_Pin_7); Logic cao . Ln lt tng ci 1 nh. Ko chi c c mng }u. Logic thp .

// Kim tra xem nu c t|c ng Button v tr GPIOA pin 0 th GPIOA pin 2 Logic Cao. if (GPIO_ReadInputDataBit(GPIOEA, GPIO_Pin_0)) GPIO_SetBits(GPIOA, GPIO_Pin_2); else GPIO_ResetBits(GPIOA, GPIO_Pin_2); // Cu lnh IF c thc hin khi Pin 0 mc Logic Cao. // Mun nhy Led theo kiu Toggle, ch st 1 cu lnh nh sau : GPIO_WriteBit(GPIODA, GPIO_Pin_1, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1)));

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

/* phn ny l 1 phn kh hay, ti mun hng dn cc bn x dng vic dch bt. Cc bn thng thy LED chy dch TRI, ri li dch PHI tun t. OK, ta s i tm hiu lnh >> v << trong Keil C ARM */ }y ti s chia lm thnh 2 thnh phn, Phn 1 : T Pin 0 n Pin 8 v Phn 2 : T Pin 9 n Pin 15. V sao li th cc bn s thy ngay by gi. Nu Pin 0 a ch 0x0001 th lc Pin0 << 8 = 0x100 = Pin 8 Nu 0x0001 << 9 = 0x200 vn bng Pin 9, nhng nu tip tc : 0x001 << 10 = 10000 khng ging Pin no c, vy l sai quy lut. Nn t Pin 0 s dch t 0 n 8 ln lt tr th{nh Pin 0 n Pin 8 v Pin 9 dch ln lt t 0 n 6 t t Pin 9 n Pin 15. uint8_t i; // Khai bo 1 bin i thuc kiu Unsiged Interger 8 bit dch bt. uint32_t Pin0; // Khai bo 1 bin Pin0 Pin0 = GPIO_Pin_0; // Gn Pin0 bng GPIO_Pin_0 a ch : 0x0001 // Nu chng ta dng cu lnh dch bt sang TRI << nh th ny : // Pin0 << 0 = GPIO_Pin_0; Pin0 << 1 = GPIO_Pin_1; Pin0 << 2 = GPIO_Pin_2; Pin0 << 2 = GPIO_Pin_3; .... /* By gi th 1 lnh lm sng ln lt t GPIO_Pin_0 n GPIO_Pin_8 nh */ for (i = 0; i<=8; i++) { GPIO_SetBits(GPIOB, Pin0 <<i); // Tt nhin trc chng ta ~ phi khi to GPIOB l Output ht ri. } Nu lm dch t Pin 9 n Pin 15 th s lm nh th ny: uint8_t i; // Khai bo 1 bin i thuc kiu Unsiged Interger 8 bit dch bt. uint32_t Pin; // Khai bo 1 bin Pin Pin0 = GPIO_Pin_9; // Gn Pin0 bng GPIO_Pin_0 a ch : 0x0200 // Nu chng ta dng cu lnh dch bt sang TRI << nh th ny : // Pin << 0 = GPIO_Pin_9; Pin << 1 = GPIO_Pin_10; Pin << 2 = GPIO_Pin_11; Pin << 3 = GPIO_Pin_12; .... /* By gi th 1 lnh lm sng ln lt t GPIO_Pin_9 n GPIO_Pin_15 nh */ for (i = 0; i<=6; i++) { GPIO_SetBits(GPIOB, Pin <<i); // Tt nhin trc chng ta ~ phi khi to GPIOB l Output ht ri. } /* Nu lm chp sng tt th lin tc v chy ln lt th theo cu lnh ny*/ for (i = 0; i<=6; i++) { GPIO_WriteBit(GPIOB, Pin<<i, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, Pin<<i))); // Tt nhin trc chng ta ~ phi khi to GPIOB l Output ht ri. }

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN OK, n y ri, ti v cc bn s i to 1 hm Delay nh v ty bin n nh th no cho ph hp.

// By gi chng ta s i xy dng 1 hm Delay_ms nh. // phn gii s l 1ms, tc gi tr t s l : 72.000 void Delay_ms(uint32_t num) { uint32_t index = 0; /* default system clock is 72MHz */ for(index = (72000 * num); index != 0; index--) {} } // Hm Delay_us vi phn gii s l 1us, tc gi tr t s l : 72 void Delay_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 th ny cho n tin, v{ n cng ~ quen Delay_us(10); // = 10uS <=> quen thuc t khi chng ta hc PIC vi CCS.

By gi ti s i 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 nhng Project khc c, m y li ko? B chng c vic sao ko ? u tin chng ta thng hay thy c 1 hm nh th ny, c cn thit chy hm SystemInit() ?

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN


void RCC_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 done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ / Vy l ~ qu r, chng ta ko cn phi chy hm ny na, v mc nh n ~ c chy trc trong tp tin startup_stm32f10x_hd.s m ta ~ Add vo Project trc .

CODE bn www.arm.vn c vit, chng ta s thy c 1 H{m kim tra xung nhp nh th ny if (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 cng vic g tip theo.

Mt ci na cng rt l kh chu, thin nhiu v C thun, s rt kh khn cho my bn va mi bt u hc v C, l{ hm Delay ca code mu www.arm.vn kh l kh hiu. void Delay(__IO uint32_t nCount); void TimingDelay_Decrement(void); static __IO uint32_t TimingDelay; void Delay(__IO uint32_t nTime) { TimingDelay = nTime; while(TimingDelay != 0); } void TimingDelay_Decrement(void) { if (TimingDelay != 0x00) { TimingDelay--; } } Chng ta nn bt u vi Hm Main ca ti ~ x}y dng trn, rt c bn v d hiu hn.

1 phn na, m lc trc ti c cp n l s dng thanh ghi Bus APB cp xung nhp cho cc GPIO , chng ta s i m qua ci ny 1 cht.

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

Nhn vo hnh trn chng ta s thy cho cc ti nguyn c a chip hot ng th thng qua BusMatrix s ti AHB (APB2 v APB1) , r hn chng ta s nhn hnh bn di.

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN Vy mun s dng n ti nguyn no th chng ta phi cp xung cho php n hot ng. Gi s cho php GPIOA thuc APB2 xung nhp 72Mhz.
// N thuc trong th vin "stm32f10x_rcc.c" m chng ta ~ Add vo Project phn trc. #include "stm32f10x_rcc.h" void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState); // V d cho php GPIOA RCC_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 s dng. @defgroup APB2_peripheral */ #define RCC_APB2Periph_AFIO ((uint32_t)0x00000001) #define RCC_APB2Periph_GPIOA ((uint32_t)0x00000004) #define RCC_APB2Periph_GPIOB ((uint32_t)0x00000008) #define RCC_APB2Periph_GPIOC ((uint32_t)0x00000010) #define RCC_APB2Periph_GPIOD ((uint32_t)0x00000020) #define RCC_APB2Periph_GPIOE ((uint32_t)0x00000040) #define RCC_APB2Periph_GPIOF ((uint32_t)0x00000080) #define RCC_APB2Periph_GPIOG ((uint32_t)0x00000100) #define RCC_APB2Periph_ADC1 ((uint32_t)0x00000200) #define RCC_APB2Periph_ADC2 ((uint32_t)0x00000400) #define RCC_APB2Periph_TIM1 ((uint32_t)0x00000800) #define RCC_APB2Periph_SPI1 ((uint32_t)0x00001000) #define RCC_APB2Periph_TIM8 ((uint32_t)0x00002000) #define RCC_APB2Periph_USART1 ((uint32_t)0x00004000) #define RCC_APB2Periph_ADC3 ((uint32_t)0x00008000) #define RCC_APB2Periph_TIM15 ((uint32_t)0x00010000) #define RCC_APB2Periph_TIM16 ((uint32_t)0x00020000) #define RCC_APB2Periph_TIM17 ((uint32_t)0x00040000) #define RCC_APB2Periph_TIM9 ((uint32_t)0x00080000) #define RCC_APB2Periph_TIM10 ((uint32_t)0x00100000) #define RCC_APB2Periph_TIM11 ((uint32_t)0x00200000)

/** Tng t i vi APB1 @defgroup APB1_peripheral */ #define #define #define #define #define #define #define #define #define #define RCC_APB1Periph_TIM2 RCC_APB1Periph_TIM3 RCC_APB1Periph_TIM4 RCC_APB1Periph_TIM5 RCC_APB1Periph_TIM6 RCC_APB1Periph_TIM7 RCC_APB1Periph_TIM12 RCC_APB1Periph_TIM13 RCC_APB1Periph_TIM14 RCC_APB1Periph_WWDG ((uint32_t)0x00000001) ((uint32_t)0x00000002) ((uint32_t)0x00000004) ((uint32_t)0x00000008) ((uint32_t)0x00000010) ((uint32_t)0x00000020) ((uint32_t)0x00000040) ((uint32_t)0x00000080) ((uint32_t)0x00000100) ((uint32_t)0x00000800)

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN


#define #define #define #define #define #define #define #define #define #define #define #define #define #define #define RCC_APB1Periph_SPI2 RCC_APB1Periph_SPI3 RCC_APB1Periph_USART2 RCC_APB1Periph_USART3 RCC_APB1Periph_UART4 RCC_APB1Periph_UART5 RCC_APB1Periph_I2C1 RCC_APB1Periph_I2C2 RCC_APB1Periph_USB RCC_APB1Periph_CAN1 RCC_APB1Periph_CAN2 RCC_APB1Periph_BKP RCC_APB1Periph_PWR RCC_APB1Periph_DAC RCC_APB1Periph_CEC ((uint32_t)0x00004000) ((uint32_t)0x00008000) ((uint32_t)0x00020000) ((uint32_t)0x00040000) ((uint32_t)0x00080000) ((uint32_t)0x00100000) ((uint32_t)0x00200000) ((uint32_t)0x00400000) ((uint32_t)0x00800000) ((uint32_t)0x02000000) ((uint32_t)0x04000000) ((uint32_t)0x08000000) ((uint32_t)0x10000000) ((uint32_t)0x20000000) ((uint32_t)0x40000000)

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


/* ****************************************************************************** * @CODER: HIENCLUBVN * @PROJECT: GPIO DEMO * @MCU : * @DATE: * @Mail: SMT32F103xC, SMT32F103xD, CSMT32F103xE * 29/06/2013 danghien719@gmail.com

****************************************************************************** */ #include "stm32f10x.h" /* Private Function Prototypes -----------------------------------------------*/ void Delay_ms(uint32_t num); void Delay_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------------------------------------------*/ int main(void) { /* Enable GPIOA, GPIOC, GPIOD clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Peri ph_GPIOE, ENABLE); /* Tien hanh khoi tao chan cho 4 LED sang o muc LOGIC 1*/

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_6| GPIO_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 if (!GPIO_ReadInputDataBit(GPIOE, else GPIO_ResetBits(GPIOC, // Tuong tu Noi dung thu 2 : Nhan if (!GPIO_ReadInputDataBit(GPIOE, else GPIO_ResetBits(GPIOC, */ 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 (<<) Sang LED lien tuc tu PA0 den PA15, Sang o Muc Logic 1 */ GPIO_Write(GPIOA, 0x0000); // Cho GPIOA o muc Logic 0 het. Pin = GPIO_Pin_0; // Gan cho la Pin 0 for (i = 0; i<=8; i++) { // Sang Lan luot tu PA0 den PA8. GPIO_SetBits(GPIOA, Pin << i); } Pin = GPIO_Pin_9; // Gan cho la Pin 9 for (i = 0; i<=6; i++) { // Sang Lan luot tu PA9 den PA15. o muc Logic 1. GPIO_Pin_2)) GPIO_SetBits(GPIOC, GPIO_Pin_6); GPIO_Pin_6); Button PE3 -> LED PC7 Sang, Ko nhan thi Tat. GPIO_Pin_3)) GPIO_SetBits(GPIOC, GPIO_Pin_7); GPIO_Pin_7);

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

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN


GPIO_SetBits(GPIOA, Pin << i); } /* 4. Phan nay cung tuong tu nhu tren, nhung Sang nhap nhay tu PA15 tro ve PA0 - Dich bit PHAI.*/ Pin = GPIO_Pin_15; // Gan cho la Pin 15 for (i = 0; i<=6; i++) { // Nhap nhay Lan luot tu PA15 den PA9. GPIO_WriteBit(GPIOB, Pin >> i, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, Pin >> i))); // Dich Bit PHAI >> } Pin = GPIO_Pin_8; // Gan cho la Pin 9 for (i = 0; i<=8; i++) { // Nhap nhay Lan luot tu PA8 den PA0. GPIO_WriteBit(GPIOB, Pin << i, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, Pin << i))); // Dich Bit PHAI >> } } } /*----------------------------Delay_ms-----------------------------------------*/ void Delay_ms( uint32_t num) { uint32_t index = 0; /* default system clock is 72MHz */ for(index = (72000 * num); index != 0; index--) {} } /*----------------------------Delay_s-----------------------------------------*/ void Delay_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_ASSERT void assert_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****/

TUTORIAL 02 HNG DN LP TRNH GPIO STM32F - HIENCLUBVN

Chn thnh cm n cc bn theo di hng dn, hng dn ch do hiu m vit li, khng theo 1 ti liu chnh thng no c. Vi kin thc c hn v kinh nghim hn ch nn trong trnh khi sai v thiu st, mong cc bc ng gp hon thin. Thn ! H Ni, 29/06/2013 Tri oi bc, bun - HIENCLUBVN

You might also like