Ngôn NG C Cho ARm

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

B tc kin thc C s dng KeilC ARM BUI 1 : EMBEDDED C TUTORIAL

I. Bt u vi nhng khi nim c s

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.

Bin, kiu d liu v hng s


Bin v hng s l mt phn vng lu tr d liu, gi tr lu tr trong bin c th thay i, cn hng s th khng th thay i. Bin v hng s c mt kch thc lu tr, ngi ta gi l kiu d liu C 2 loi bin l bin ton cc(global variable) v bin cc b (local variable). V d : (t ghi theo bi ging trn bng)

1. Bin :

Robotechvn.com ARM VN - STMicroelectronics

Bui 1 - Page 1

B tc kin thc C s dng KeilC ARM

2012

2. Kiu d liu (data type)


Cc loi kiu d liu trong Keil C

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

B tc kin thc C s dng KeilC ARM


/* smallest type of at least n bits */ /* minimum-width signed integer types */ typedef signed char int_least8_t; typedef signed short int int_least16_t; typedef signed int int_least32_t; typedef signed __int64 int_least64_t; /* minimum-width unsigned integer types */ typedef unsigned char uint_least8_t; typedef unsigned short int uint_least16_t; typedef unsigned int uint_least32_t; typedef unsigned __int64 uint_least64_t; /* 7.18.1.3 */ /* fastest minimum-width signed integer types */ typedef signed int int_fast8_t; typedef signed int int_fast16_t; typedef signed int int_fast32_t; typedef signed __int64 int_fast64_t; /* fastest minimum-width unsigned integer types */ typedef unsigned int uint_fast8_t; typedef unsigned int uint_fast16_t; typedef unsigned int uint_fast32_t; typedef unsigned __int64 uint_fast64_t;

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;

Robotechvn.com ARM VN - STMicroelectronics

Bui 1 - Page 3

B tc kin thc C s dng KeilC ARM


III. Macro, enum v struct

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;

Robotechvn.com ARM VN - STMicroelectronics

Bui 1 - Page 4

B tc kin thc C s dng KeilC ARM


Struct :

2012

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

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;

/*!< Specifies the operating mode for the selected pins.

This parameter can be a value of @ref GPIOMode_TypeDef */ }GPIO_InitTypeDef; GPIO_InitTypeDef GPIO_InitStructure;

Robotechvn.com ARM VN - STMicroelectronics

Bui 1 - Page 5

B tc kin thc C s dng KeilC ARM


IV. Operator (Ton hng) Arithmetic operator

2012

Bitwise operator

V d :

Robotechvn.com ARM VN - STMicroelectronics

Bui 1 - Page 6

B tc kin thc C s dng KeilC ARM


Logical operator:

2012

Relational operator:

V d :

Robotechvn.com ARM VN - STMicroelectronics

Bui 1 - Page 7

B tc kin thc C s dng KeilC ARM


Increase and decrement operator:
x= x + 1; Tng ng vi x++;// Tng sau V ++x;//Tng trc Tng t vi V d : i = 1; j = 1; k = 2*i++; m = 2*++j;

2012

V.

Pht biu iu khin a) If else

b) While While(expr) { Statement; } c) For

Robotechvn.com ARM VN - STMicroelectronics

Bui 1 - Page 8

B tc kin thc C s dng KeilC ARM


d) Switch case

2012

VI.

Bi tp :

Robotechvn.com ARM VN - STMicroelectronics

Bui 1 - Page 9

You might also like