Professional Documents
Culture Documents
Ngôn NG C Cho ARm
Ngôn NG C Cho ARm
Ngôn NG C Cho ARm
2012
Mt chng trnh C trong lp trnh nhng u bt u vi mt hm c tn l main(). Hm main() c gi u tin lc MCU khi ng. Cu trc n gin ca hm main nh sau: #include <stdio.h> void main() { Printf(Hello world); while(1);//lp v tn. } MCU s hot ng lin tc n khi b reset hoc mt in. C mt vi phn t cn nh trong lc lp trnh C : ; bt buc phi xut hin bo thc s kt thc ca mt biu thc. {} c s dng nh du s bt u v kt thc ca mt hm, mt statement, mt block n no . // or /* */ c s dng khi ngi lp trnh mun m t mt ci g , nhng th vit sau du // hoc nm trong /* */ s c compiler b qua trong qu trnh dch.
II.
1. Bin :
Bui 1 - Page 1
2012
Th vin stdint.h /* exact-width signed integer types */ typedef signed char int8_t; typedef signed short int int16_t; typedef signed int int32_t; typedef signed __int64 int64_t; /* exact-width unsigned integer types */ typedef unsigned char uint8_t; typedef unsigned short int uint16_t; typedef unsigned int uint32_t; typedef unsigned __int64 uint64_t; /* 7.18.1.2 */
Robotechvn.com ARM VN - STMicroelectronics Bui 1 - Page 2
2012
/* 7.18.1.4 integer types capable of holding object pointers */ typedef signed int intptr_t; typedef unsigned int uintptr_t; /* 7.18.1.5 greatest-width integer types */ typedef signed __int64 intmax_t; typedef unsigned __int64 uintmax_t;
Bui 1 - Page 3
2012
Enum v macro gip ngi lp trnh c th thay th nhng s (number) bng tn gi, ci m ta c th nh v lin tng ti c cng dng ca chng. Enum : c lit k di dng hng s. V d 1: enum{ zero_val, one_val, three_val}; uint8_t num_val; num_val = one_val; // ngha bng vi num_val = 1. Tn zero_val s bt u = 0, one_val s l 1, v three_val s l 2 V d 2: Enum{ start = 9, next1, next2, end_val}; Next1 = 11, next2 = 12, end_val = 12 V d 3: typedef enum { Bit_RESET = 0, Bit_SET }BitAction; GPIO_WriteBit(GPIOB, GPIO_Pin_8, Bit_SET); Macro : V d 1: #define TRUE 0xFF #define FALSE 0x00 Uint16_t a; a = TRUE;
Bui 1 - Page 4
2012
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 struct { uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured. This parameter can be any value of @ref GPIO_pins_define */
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins. This parameter can be a value of @ref GPIOSpeed_TypeDef */
GPIOMode_TypeDef GPIO_Mode;
Bui 1 - Page 5
2012
Bitwise operator
V d :
Bui 1 - Page 6
2012
Relational operator:
V d :
Bui 1 - Page 7
2012
V.
Bui 1 - Page 8
2012
VI.
Bi tp :
Bui 1 - Page 9