Bổ túc kiến thức C sử dụng KeilC ARM Tài liệu lập trình vi điều khiển dòng ARM Một chương trình C trong lập trình nhúng đều bắt đầu với một hàm có tên là main(). Hàm main() được gọi đầu tiên lúc MCU khởi động. Cấu trúc đơn giản của hàm main như sau: include void main() { Printf(“Hello world”); while(1);lặp vô tận. } MCU sẽ hoạt động liên tục đến khi bị reset hoặc mất điện exactwidth signed integer types typedef signed char int8_t; typedef signed short int int16_t; typedef signed int int32_t; typedef signed __int64 int64_t; exactwidth unsigned integer types typedef unsigned char uint8_t; typedef unsigned short int uint16_t; typedef unsigned int uint32_t; typedef unsigned __int64 uint64_t; Enum và macro giúp người lập trình có thể thay thế những số (number) bằng tên gọi, cái mà ta có thể nhớ và liên tưởng tới được công dụng của chúng.
Trang 1Bổ túc kiến thức C sử dụng KeilC ARM 2012
BUỔI 1 : EMBEDDED C TUTORIAL
I Bắt đầu với những khái niệm cơ sở
Một chương trình C trong lập trình nhúng đều bắt đầu với một hàm có tên là main()
Hàm main() được gọi đầu tiên lúc MCU khởi động Cấu trúc đơn giản của hàm main như sau: #include <stdio.h>
void main()
{
Printf(“Hello world”);
while(1);//lặp vô tận
}
MCU sẽ hoạt động liên tục đến khi bị reset hoặc mất điện
Có một vài phần tử cần nhớ trong lúc lập trình C :
; bắt buộc phải xuất hiện để báo thức sự kết thúc của một biểu thức
{} được sử dụng để đánh dấu sự bắt đầu và kết thúc của một hàm, một statement, một block đơn nào đó
// or /* */ được sử dụng khi người lập trình muốn mô tả một cái gì đó, những thứ viết sau
dấu // hoặc nằm trong /* */ sẽ được compiler bỏ qua trong quá trình dịch
II Biến, kiểu dữ liệu và hằng số
Biến và hằng số là một phân vùng để lưu trữ dữ liệu, giá trị lưu trữ trong biến có thể thay đổi, còn hằng số thì không thể thay đổi
Biến và hằng số có một kích thước để lưu trữ, người ta gọi đó là kiểu dữ liệu
1 Biến :
Có 2 loại biến là biến toàn cục(global variable) và biến cục bộ (local variable)
Ví dụ : (tự ghi theo bài giảng trên bảng)
Trang 2Bổ túc kiến thức C sử dụng KeilC ARM 2012
2 Kiểu dữ liệu (data type)
Các loại kiểu dữ liệu trong Keil C
Thư viện 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;
Trang 3Bổ túc kiến thức C sử dụng KeilC ARM 2012
/* 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;
/* 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;
Trang 4Bổ túc kiến thức C sử dụng KeilC ARM 2012
III Macro, enum và struct
Enum và macro giúp người lập trình có thể thay thế những số (number) bằng tên gọi, cái
mà ta có thể nhớ và liên tưởng tới được công dụng của chúng
Enum : được liệt kê dưới dạng hằng số
Ví dụ 1:
enum{ zero_val, one_val, three_val};
uint8_t num_val;
num_val = one_val; // ý nghĩa bằng với num_val = 1
Tên zero_val sẽ bắt đầ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;
Trang 5Bổ túc kiến thức C sử dụng KeilC ARM 2012
Struct :
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;
Trang 6Bổ túc kiến thức C sử dụng KeilC ARM 2012
IV Operator (Toán hạng)
Arithmetic operator
Bitwise operator
Ví dụ :
Trang 7Bổ túc kiến thức C sử dụng KeilC ARM 2012
Logical operator:
Relational operator:
Ví dụ :
Trang 8Bổ túc kiến thức C sử dụng KeilC ARM 2012
Increase and decrement operator:
x= x + 1;
Tương đương với x++;// Tăng sau
Và ++x;//Tăng trước Tương tự với –
Ví dụ :
i = 1;
j = 1;
k = 2*i++;
m = 2*++j;
V Phát biểu điều khiển
a) If else
b) While
While(expr)
{
Statement;
}
c) For
Trang 9Bổ túc kiến thức C sử dụng KeilC ARM 2012 d) Switch case
VI Bài tập :