Bài giảng Lập trình hệ thống nhúng - Bùi Quốc Bảo

34 14 0
Bài giảng Lập trình hệ thống nhúng - Bùi Quốc Bảo

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Hệ thống nhúng (Embedded system) là một thuật ngữ để chỉ một hệ thống có khả năng tự trị được nhúng vào trong một môi trường hay một hệ thống mẹ. Đó là các hệ thống tích hợp cả phần cứng và phần mềm phục vụ các bài toán chuyên dụng trong nhiều lĩnh vực công nghiệp, tự động hoá điều khiển, quan trắc và truyền tin. Đặc điểm của các hệ thống nhúng là hoạt động ổn định và có tính năng tự động hoá cao....

LẬP TRÌNH HỆ THỐNG NHÚNG BÙI QUỐC BẢO BỘ MƠN KỸ THUẬT ðIỆN TỬ-ðH BK TP.HCM Mục tiêu môn học Tự thiết kế phần mềm cho hệ thống nhúng dùng hay nhiều CPU dựa vi ñiều khiển (8051, ARM) dựa phần cứng có sẵn Phần mềm thiết kế có cấu trúc, dựa ngôn ngữ cấp cao (C) sử dụng hợp ngữ C Nắm ñược lý thuyết hệ ñiều hành nhúng; sử dụng ñược hệ ñiều hành nhúng (FreeRTOS) Tài liệu tham khảo C and the 8051, 3rd Edition, Thomas Schultz Embedded C, Michael J Pont ðánh giá kết Giữa kỳ: 20% Bài tập lớn: 30% Cuối kỳ: 50% Khái niệm hệ thống nhúng Hệ thống nhúng kết hợp phần cứng máy tính phần mềm, thiết kế ñể thực chức chuyên biệt Người sử dụng hệ thống nhúng không cần biết hệ thống có máy tính bên VD: remote TV, lị vi sóng,… Vi xử lý hệ thống nhúng Họ 8086 PowerPC MIPS Họ 8051 PIC … Tùy thuộc vào ứng dụng giá thành, người thiết kế ñịnh loại vi xử lý dùng hệ thống nhúng Vi xử lý dùng môn học 8051 ARM Ngơn ngữ lập trình Assembler C Lý sử dụng ngôn ngữ C C cho phép lập trình có cấu trúc Ngơn ngữ C cho phép dễ dàng truy cập đến cấu trúc phần cứng (ví dụ thơng qua trỏ) C dùng phổ biến Hầu hết họ vi xử lý ñều ñược hỗ trợ trình biên dịch C Rất nhiều taì nguyên tham khảo (sách, ví dụ, website …) lập trình C Lý dùng hợp ngữ (Assembler) Cho phép viết chương trình có kích thước nhỏ chạy với tốc độ nhanh Thích hợp cho ứng dụng có yêu cầu nghiêm ngặt thời gian C operator = @ ? < > ! ~ + * / % | & ^ assignment statement address of selection less than greater than logical not (true to false, false to true) 1's complement addition subtraction multiply or pointer reference divide modulo, division remainder logical or logical and, or address of logical exclusive or used to access parts of a structure C operator == = != > ++ -&& || equal to comparison less than or equal to greater than or equal to not equal to shift left shift right increment decrement boolean and boolean or += -= *= /= |= &= ^= = %= -> add value to subtract value to multiply value to divide value to or value to and value to exclusive or value to shift value left shift value right modulo divide value to pointer to a structure Comments (chú thích) // This is a comment line /* This is a comment block */ Preprocessor Directives (chỉ dẫn tiền xử lý) ðược xử lý q trình biên dịch Bắt đầu từ khóa # VD: #include “LCD.h" #define LCD *(unsigned char volatile *)(0x1003) Key word (Từ khóa) asm auto break case char const continue default double else extern float Insert assembly code Specifies a variable as automatic (created on the stack) Causes the program control structure to finish One possibility within a switch statement bit integer Defines parameter as constant in ROM Causes the program to go to beginning of loop Used in switch statement for all other cases Used for creating program loops Specifies variable as double precision floating point Alternative part of a conditional Defined in another module Specifies variable as single precision floating point Key word (Từ khóa) for goto if int long register return short Used for creating program loops Causes program to jump to specified location Conditional control structure 16 bit integer 32 bit integer store the variable onto the CPU register if space on the register is available Leave function 16 bit integer Key word (Từ khóa) signed sizeof static struct switch typedef unsigned void volatile while Specifies variable as signed (default) Built-in function returns the size of an object Stored permanently in memory, accessed locally Used for creating data structures Complex conditional control structure Used to create new data types Always greater than or equal to zero Used in parameter list to mean no parameter Can change implicitly Used for creating program loops Dấu chấm phẩy (semicolons) Chấm dứt câu lệnh VD: For (i=0;imode=0; (dùng dấu -> ñể truy cập phần tử trỏ kiểu struct) 22 Khởi tạo struct port PortE={1, (unsigned char volatile *)(0x100A), (unsigned char volatile *)(0)}; Struct and union typedef struct { union { IO uint32_t u32ISPCON; struct { IO uint32_t ISPEN:1; IO uint32_t BS:1; I uint32_t RESERVE0:2; IO uint32_t CFGUEN:1; IO uint32_t LDUEN:1; IO uint32_t ISPFF:1; IO uint32_t SWRST:1; IO uint32_t PT:3; I uint32_t RESERVE1:1; IO uint32_t ET:3; I uint32_t RESERVE2:17; } ISPCON; }; union { IO uint32_t u32ISPADR; IO uint32_t ISPADR; } }FMC_T; #define AHB_BASE (( uint32_t)0x50000000) #define FMC_BASE #define FMC (AHB_BASE +0x0C000) ((FMC_T *)FMC_BASE) FMC->ISPCON.ISPEN = FMC->ISPADR = 0x20000000; 23 Function (Hàm, chương trình con) Một chương trình lớn thường chia thành nhiều khối (module) Module tác vụ nhận liệu vào (input), xử lý xuất kết (output) Các module ñược tạo hàm (function) Function Một module (Nguồn: EmbeddedProg/Developing Embedded Software in C) int FtoC(int TempF){ int TempC; TempC=(5*(TempF-32))/9; // conversion return TempC;} Note: Hạn chế truy cập đến biến tồn cục I/O function 24 Mô tả hàm (function declaration) Mô tả hàm cho ta biết tên hàm, kiểu tham số kiểu kết trả // declaration input output void Ritual(void); char InChar(void); void OutChar(char letter); short InSDec(void); void OutSDec(short i); char Max(char in1,char in2); int EMax(int in1,int in2); void OutString(char* mystring); char *alloc(int size); int // none none // none 8-bit // 8-bit none // none 16-bit // 16-bit none // two 8-bit 8-bit // two 16-bit 16-bit // pointer to 8-bit none // 16-bit pointer to 8-bit Exec(void(*fnctPt)(void)); // function pointer 16-bit ðịnh nghĩa hàm (function definition) Một ñịnh nghĩa hàm cách mà hàm thực thi type Name(parameter list){ CompoundStatement }; int FtoC(int TempF) { int TempC; TempC=(5*(TempF-32))/9; // conversion return TempC; } 25 Mô tả hàm Một module thường bao gồm file: Header file (dùng chứa mô tả hàm) Source file (dùng chứa ñịnh nghĩa hàm) Extern function Khi ta cần khai báo hàm ñược ñịnh nghĩa module khác, ta dùng từ khóa extern extern void InitSCI(void); extern char InChar(void); extern void OutChar(char character); 26 Truyền tham số (Arguments passing) Có hai phương pháp truyền tham số cho chương trình con: Truyền tham chiếu (call by reference) Truyền giá trị (call by value) Truyền tham chiếu (call by reference) Tham chiếu (con trỏ) ñến tham số đưa vào chương trình Các tính tốn chương trình ảnh hưởng đến biến ñược truyền vào int increment(int *num) { *num++; return *num; } Void main(void) { int number = 1; int temp; temp = increment(& number ); } 27 Truyền giá trị (call by value) Giá trị biến ñược chép truyền vào chương trình Các phép tính tham số chương trình khơng ảnh hưởng biến ban ñầu int increment(int num) { num++; return num; } Void main(void) { int number = 1; int temp; temp = increment(number ); } Struct làm tham số chương trình struct theport { int mode; // for I/O, for in only -1 for out only unsigned char volatile *addr; // pointer to its address unsigned char volatile *ddr; // pointer to its direction reg }; typedef struct theport port; unsigned char Input(port thePort) { return (thePort.addr); } unsigned char Input(port *thePort) { return (thePort->addr); } Function tốt hơn? 28 Con trỏ hàm (function pointer) Con trỏ hàm trỏ ñến hàm int (*fp)(int); // pointer to a function with input and output int fun1(int input) { return(input+1); // this adds }; int fun2(int input) { return(input+2); // this adds }; void Setp(void) { int data; fp=&fun1; // fp points to fun1 data=(*fp)(5); // data=fun1(5); fp=&fun2; // fp points to fun2 data=(*fp)(5); // data=fun2(5); }; Question: Giải thích khác giữa: int (*fp)(int); int *fp(int); 29 Con trỏ hàm tham số hàm khác int fun1(int input){ return(input+1); }; // this adds int fun2(int input){ return(input+2); // this adds }; int execute(int (*fp)(int)) { int data; data=(*fp)(5); // data=fun1(5); return (data); }; void main(void) { int result; result=execute(&fun1); // result=fun1(5); result=execute(&fun2); // result=fun2(5); }; Hàm callback Hàm callback hàm gọi có kiện xảy MyButton void create_button( int x, int y, const char *text, function callback_func ); ButtonClickedEvent callback_func MyButton click 30 Ví dụ hàm callback void cbfunc() { printf("called"); } int main () { /* function pointer */ void (*callback)(void); /* point to your callback function */ callback=(void *)cbfunc; /* perform callback */ callback(); return 0; } Truyền tham số cho hàm callback typedef struct _myst { int a; char b[10]; }myst; void cbfunc(myst *mt) { fprintf(stdout,"called %d %s.",mt->a,mt->b); } int main() { /* func pointer */ void (*callback)(void *); //param myst m; m.a=10; strcpy(m.b,"123"); /* point to callback function */ callback = (void*)cbfunc; /* perform callback and pass in the param */ callback(&m); return 0; } 31 Chỉ dẫn tiền biên dịch (Preprocessor Directives ) #define size 100 int data[size]; #define LCD *(unsigned char *)0x2000 Chỉ dẫn tiền biên dịch (Preprocessor Directives ) #ifdef A … #endif #ifndef B … #endif 32 Chỉ dẫn tiền biên dịch (Preprocessor Directives ) #define Debug #define LCD *(unsigned char *)0x2000 void LCD_write(char data) { #ifdef Debug printf(“entering LCD_write”); #endif LCD = data; #ifdef Debug printf(“exitting LCD_write”); #endif return; } Debug macro #ifdef PRGDEBUG #define DBG(x) printf(x) #else #define DBG(x) /* nothing */ #endif 33 Chỉ dẫn tiền biên dịch (Preprocessor Directives ) #include "Filename" #include 34 ... 8-bit // 8-bit none // none 16-bit // 16-bit none // two 8-bit 8-bit // two 16-bit 16-bit // pointer to 8-bit none // 16-bit pointer to 8-bit Exec(void(*fnctPt)(void)); // function pointer 16-bit... Bài tập lớn: 30% Cuối kỳ: 50% Khái niệm hệ thống nhúng Hệ thống nhúng kết hợp phần cứng máy tính phần mềm, ñược thiết kế ñể thực chức chuyên biệt Người sử dụng hệ thống nhúng khơng cần biết hệ. .. hệ thống có máy tính bên VD: remote TV, lị vi sóng,… Vi xử lý hệ thống nhúng Họ 8086 PowerPC MIPS Họ 8051 PIC … Tùy thuộc vào ứng dụng giá thành, người thiết kế ñịnh loại vi xử lý dùng hệ thống

Ngày đăng: 08/05/2021, 19:17

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan