Professional Documents
Culture Documents
Introduction Week1 PDF
Introduction Week1 PDF
NHÚNG
ARM Cortex M3
ARM Cortex A8
Ngôn ngữ lập trình
Assembler
C
Phần cứng và phần mềm
CodeBlock Software
https://www.youtube.com/watch?v=3B4h
PHZNtNw&feature=youtu.be
Phần cứng và phần mềm
Kit Raspberry PI
Raspberry PI Simulator:
https://docs.microsoft.com/en-gb/azure/iot-hub/iot-hub-raspberry-pi-web-simulator-get-started
Phần cứng và phần mềm
Beagle Bone Black
Software: Qemu
Các phần cứng khác
https://www.st.com/en/evaluation-tools/32f746gdiscovery.html
Lý do sử dụng ngôn ngữ C
#include “LCD.h"
#define LCD *(unsigned char volatile *)(0x1003)
Key word (Từ khóa)
asm Insert assembly code
auto Specifies a variable as automatic (created
on the stack)
break Causes the program control structure to finish
case One possibility within a switch statement
char 8 bit integer
const Defines parameter as constant in ROM
continue Causes the program to go to beginning of loop
default Used in switch statement for all other cases
do Used for creating program loops
double Specifies variable as double precision floating point
else Alternative part of a conditional
extern Defined in another module
float Specifies variable as single precision floating point
Key word (Từ khóa)
for Used for creating program loops
goto Causes program to jump to specified
location
if Conditional control structure
int 16 bit integer
long 32 bit integer
register store the variable onto the CPU register if
space on the register is available
return Leave function
short 16 bit integer
Key word (Từ khóa)
signed Specifies variable as signed (default)
sizeof Built-in function returns the size of an
object
static Stored permanently in memory, accessed
locally
struct Used for creating data structures
switch Complex conditional control structure
typedef Used to create new data types
unsigned Always greater than or equal to zero
void Used in parameter list to mean no parameter
volatile Can change implicitly
while Used for creating program loops
Dấu chấm phẩy (semicolons)
void main(void)
{
volatile unsigned char xdata *p = (char xdata *) 0x8000;
while (*p == 0);
}
Từ khóa extern
int *p;
p = 0x8000;
int *p = (int *)0x8000;
int k;
p = &k;
#define SW *(unsigned char volatile *)(0x2000)
Phép toán với con trỏ
Phép cộng:
Con trỏ luôn chỉ vào địa chỉ đầu của một đối
tượng (object). Cộng 1 vào con trỏ làm nó
chỉ đến đối tượng tiếp theo.
Phép so sánh:
Khi so sánh 2 con trỏ, giá trị chúng đang
mang được coi như số không dấu
int *p; p
0x01 0x2000
0x00 0x2001
int *k;
k 0x02 0x2002
p = 0x2000; 0x00 0x2003
*p = 1;
k = p+1;
*k = *p+1;
Mảng và chuỗi (array and string)
struct theport
{
int mode; // 0 for I/O, 1 for in only -1 for out only
unsigned char volatile *addr; // pointer to its address
unsigned char volatile *ddr; // pointer to its direction reg
};
struct theport PortA, PortB, PortC;
Structure
struct theport
{
int mode ; // 0 for I/O, 1 for in only -1 for out only
PortC.mode=0;
PortC.addr=(unsigned char volatile *)(0x1003);
PortC.ddr=(unsigned char volatile *)(0x1007);
(*PortC.ddr)=0;
(*PortB.addr)=(*PortC.addr);
Truy cập struct thông qua pointer
port PORTA;
port *p;
p = &PORTA;
p->mode=0; (dùng dấu -> để truy cập phần
tử con của 1 con trỏ kiểu struct)
Khởi tạo struct
port PortE={1,
(unsigned char volatile *)(0x100A),
(unsigned char volatile *)(0)};
Kiểu Union
union Data
{ int i; float f; char str[20]; } data;
Union Example
#include <stdio.h>
#include <string.h>
union Data { int i; float f; char str[20]; };
int main( )
{ union Data data;
printf( "Memory size occupied by data :
%d\n", sizeof(data));
return 0;
}
Union Example
#include <stdio.h>
#include <string.h>
union Data { int i; float f; char str[20]; };
int main( )
{ union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}
Write a C program to extract
individual bytes from a float
variable and write them to address
2000H to 2003H
Function (Hàm, chương trình con)
Note:
Hạn chế truy cập đến biến toàn cục và I/O trong 1 function
Mô tả hàm (function declaration)
Mô tả hàm cho ta biết tên hàm, kiểu của các
tham số và kiểu kết quả trả về.
// declaration input output
void Ritual(void); // none none
char InChar(void); // none 8-bit
void OutChar(char letter); // 8-bit none
short InSDec(void); // none 16-bit
void OutSDec(short i); // 16-bit none
char Max(char in1,char in2); // two 8-bit 8-bit
int EMax(int in1,int in2); // two 16-bit 16-bit
void OutString(char* mystring); // pointer to 8-bit none
char *alloc(int size); // 16-bit pointer to
8-bit
int Exec(void(*fnctPt)(void)); // function pointer 16-bit
Định nghĩa hàm (function definition)
return *num;
}
Void main(void)
{
int number = 1;
int temp;
temp = increment(& number );
}
Truyền giá trị (call by value)
Giá trị của biến sẽ được sao chép và truyền
vào chương trình con.
Các phép tính trên tham số trong chương trình
con 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ố của chương
trình con
struct theport
{
int mode; // 0 for I/O, 1 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;
void main(void)
{ int result;
result=execute(&fun1); // result=fun1(5);
result=execute(&fun2); // result=fun2(5);
};
Hàm callback
MyButton
ButtonClickedEvent callback_func
MyButton
click
Ví dụ về 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;
}
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
Chỉ dẫn tiền biên dịch
(Preprocessor Directives )
#define Debug 1
#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
Chỉ dẫn tiền biên dịch
(Preprocessor Directives )
#include "Filename"
#include <Filename>
Recursion (Đệ qui)
Be careful to define an
exit condition
Circular buffer
Head is equal to tail -> the buffer is empty
(Head + 1) is equal to tail -> the buffer is full