
1
H th ng nhúngệ ố
Th c sĩ Lê M nh H iạ ạ ả
Embedded Systems

2
Môû ñaàu
I M c đích môn h c:ụ ọ
•Cung c p ki n th c v l p trình nhúng trên Microchip PIC24ấ ế ứ ề ậ
•Rèn luy n k năng đ c sách chuyên ngành b ng ti ng Anhệ ỹ ọ ằ ế
II. Th i gian:ờ
•30 ti t lý thuy t (3 ĐVHT) + 30 ti t th c hànhế ế ế ự
III Giáo trình và tài li u tham kh oệ ả
•Programming 16-Bit PIC Microcontrollers in C: Learning to Fly the
PIC24. Lucio Di Jasio. Elsevier. 2007
•Designing Embedded Systems with PIC Microcontrollers. Principles
and applications.Tim Wilmshurst. Elsevier. 2007
IV. Đánh giá:
•Thi k t thúc môn:70%. Có m t bài báo cáo k thu t. ế ộ ỹ ậ
•V. Giáo viên:
•Th c sĩ Lê M nh H i. Tel: 0985399000. Không g i đi n tho i đ ạ ạ ả ọ ệ ạ ể
h i hay xin đi m, email: hailemanh@yahoo.com ỏ ể

3
Lesson 1 : THE FIRST FLIGHT
•Flight plan: Every flight should have a purpose
–PIC24 16-bit microcontroller PIC24FJ128GA010
–MPLAB® IDE
–MPLAB C30
•The flight
–Our first line of code is going to be:
#include <p24fj128ga010.h>
This is not yet a proper C statement, but more of a pseudo-
instruction for the preprocessor telling the compiler to
read the content of a device-specific file before
proceeding any further. The content of the device-specifi
c “.h” file chosen is nothing more than a long list of the
names (and sizes) of all the internal special-function
registers (SFRs) of the chosen PIC24 model.

4
C programming
Going back to our “Hello.c” source fi le, let’s add a couple more lines that
will introduce you to the main() function:
1. main()
2. {
3. }
We said our mission was to turn on one or more I/O pins: say PORTA,
pins RA0–7. In assembly, we would have used a pair of mov
instructions to transfer a literal value to the output port. In C it is
much
easier—we can write an “assignment statement” as in the following
example:
1. #include <p24fj128ga010.h>
2. main()
3. {
4. PORTA = 0xff;
5. }
Is it correct?

5
Compiling and linking
•This operation is called a Project Build. The sequence of events
is fairly long and complex, but it is composed mainly of two
steps:
•Compiling: The C compiler is invoked and an object code file
(.o) is generated. This file is not yet a complete executable.
While most of the code generation is complete, all the addresses
of functions and variables are still undefi ned. In fact, this is also
called a relocatable code object. If there are multiple source files,
this step is repeated for each one of them.
•Linking: The linker is invoked and a proper position in the
memory space is found for each function and each variable. Also
any number of precompiler object code fi les and standard library
functions may be added at this time as required. Among the
several output files produced by the linker is the actual binary
executable file (.hex).
•All this is performed in a very rapid sequence as soon as you
select the option “Build All” from the Project menu.

