C PROGRAMMING FOR 8051 USING KEIL IDE - … 02.pdf · ต าแหน่งใหม่...
Transcript of C PROGRAMMING FOR 8051 USING KEIL IDE - … 02.pdf · ต าแหน่งใหม่...
C PROGRAMMING FOR 8051
USING KEIL IDE
1
Microcontroller Projects
Life cycle of a microcontroller project
Integrate Development Environment (IDE)
2
Brain storming
Hardware design
Programming & debugging
Transfering to the board
Prototype, Testing
Limitation of Keil, evalation version
•Object code must be less than 2
Kbytes
•Program code starts at address
0x4000
•No floating-point types
Variables
Variables Declaration
storage_class data_types memory_type variable_name= value;
โดยท
storage_class คอ ประเภทของการเกบขอมล
data_type คอ ชนดของขอมล
variable_name คอ ชอของตวแปร
memory_type คอ ชนดของหนวยความจ า
value คอ คาของตวแปร
3
Data Types 4
ระบบไมโครคอนโทรลเลอรม
หนวยความจ า RAM ท
จ ากดแตละชนดของตวแปร
ใชเนอทหนวยความจ าไม
เทากน และ
ไมโครคอนโทรลเลอรยงใช
เวลาในการ process ตว
แปรทใชเนอทมากนานกวา
อกดวย
* มใชใน MCS-51เพออางถง Bit-addressable memory
Data Type Bits Bytes Value Range
bit* 1 -- 0 to 1
sbit* 1 -- 0 to 1
sfr* 8 1 0 to 255
sfr16* 16 2 0 to 65535
char 8 1 -128 to +127
unsigned char 8 1 0 to 255
enum 16 2 -32768 to +32767
int 16 2 -32768 to +32767
unsigned int 16 2 0 to 65535
short 16 2 -32768 to +32767
unsigned short 16 2 0 to 65535
long 32 4 -2147483648 to 2147483647
unsigned long 32 4 0 to 4294967295
float 32 4 ±1.175494E-38 to ±3.402823E+38
double 32 4 ±1.175494E-38 to ±3.402823E+38
Data Types
Constant
ประกาศคาคงทปกต
unsigned char message[500];
message[0]=‘a’;
ประกาศคาคงทเพอเขยนลงใน program
memory
unsigned char code message[500];
message[0]=‘a’;
5
Number representations
Array
char display[10];
display[3] = 60;
Hexadecimal (base 16) Binary (base 2) Decimal (base 10)
0x00 0b00000000 0
0xFF 0b11111111 255
0xAA 0b10101010 170
0xFFFF 0b1111111111111111 65535
0xFFAA 0b1111111110101010 65450
Data Types
6
การเปลยนชนดของขอมล
unsigned char x = 10;
unsigned char y = 5;
unsigned char z ;
z = x * y ;
ผลลพธคอ z = 50
unsigned char x = 10;
unsigned char y = 50;
unsigned char z ;
z = x * y ;
ผลลพธคอ z = 244
เนองจากค าตอบทแทจรงคอ 500 (1F4H) แตตวแปร char มขนาดเพยง 8 บต เลข 1ดานหนาซงเปนหลก
ทเกนจาก 8 บตจงถกตดหายไป
unsigned char x ;
unsigned char y ;
unsigned int z ;
z = (unsigned int) x * (unsigned int) y ;
Storage classes
7
Static (Static variables)
เกบไวทหนวยความจ าขอมลต าแหนงหนงโดยทไมมการ
เปลยนต าแหนงเลยจนกวาจะมการก าหนดใหเปลยน
ต าแหนงใหม สามารถเปนไดทง Global หรอ Local
variable โดยถาเปน Global variable
ต าแหนงของ static จะอยในขอบเขตฟงกชน ทตวมน
ถกประกาศคา
Extern (external)
เปนการประกาศใชตวแปรทมการประกาศไวแลวในฟงชน
อน หรอไฟลอน
Auto (Automatic variables)
เปนคา default ของ storage class ขอมลจะถก
เกบไวทหนวยความจ าต าแหนงหนงโดยทอาจมการ
เปลยนต าแหนงได แตเปน Local variable การใช
งานและการเปลยนต าแหนงจะอยในฟงกชนเทานน
Register
เปนการเรยกใช registers ในการจดเกบขอมลแทน
การใชหนวยความจ า RAM
register data-type name = value;
Memory Types 8
data พ นท 128 bytes แรก เขาใชงานได
โดยวธ direct addressing
unsigned char data fast_variable;
idata พ นททงหมดของ data
memory ภายใน (256 bytes) เขาใช
งานไดโดยวธ indirect addressing ซง
จะชากวาวธ direct addressing
unsigned char idata variable;
bdata พ นท 16 bytes ของ bit-
addressable memory (20h-2Fh) เขา
ใชงานและเปลยนแปลงไดทละบต
unsigned char bdata bdata_var;
far เปนพ นทภายในหรอภายนอก ขนาด 16
Mbytes เขาใชงานโดยการอาง address 24 bit
พ นทหนวยความจ าประเภทนเกดจากการใชเทคนคเพม
ขนาดหนวยความจ าซงมใน ไมโครคอนโทรลเลอรบาง
ตว
หนวยความจ า data memory ภายใน (Internal data memory)
Memory Types
xdata พ นทชวง 64K Bytes การเขาใช
งานโดยออมตองท าผาน data pointer
register
unsigned char xdata variable;
ถาตองการให constants ถกบรรจใน
program memory ตองมการประกาศ
ไว
unsigned char code name1;
pdata พ นทชวง 1 page หรอ 256
bytes การเขาใชงานโดยออมตองท าผาน
registers R0 และ R1 (@R0,
@R1)
unsigned char pdata variable;
program code หรอ functions จะถก
บรรจใน program memory โดย
อตโนมต
unsigned int func (void)
{….;return (0);}
9
หนวยความจ า data memory ภายนอก (External data memory)
หนวยความจ า program memory
Memory Types
Extension Memory Type Related ASM
data Internal data memory addresses 0x00-0x7F
(Directly-addressable)
MOV A, 07FH
idata Internal data memory addresses 0x00-0xFF
(Indirectly-addressable)
MOV R0, #080H,
MOV A, @R0
bdata Internal data memory addresses 0x20-0x2F
(Directly-addressable)
SETB 7FH,
CPL 80H
sfr*
sfr is not an extension
Internal data memory addresses 0x80-0xFF (Directly-
addressable)
MOV P1, A
xdata External data memory 64 Kbytes (Indirectly-
addressable through DPTR)
MOVX A, @DPTR
pdata External data memory 256 bytes (Indirectly-
addressable through R0, R1)
MOVX A, @R0
code Program memory MOVC A, @A+DPTR
far Extended RAM and ROM memory spaces (up to 16MB); accessed by user defined routines or
specific chip extensions (Philips 80C51MX, Dallas 390).
Memory Types 11
Mathematical & logic operations 12
Operator Description
= ก าหนดคา (assignment)
+ บวก - ลบ * คณ
/ หาร % Modulo
! NOT (bit level) Example:
P1^0 = !P1^0;
~ NOT (byte level)
Example: P1 = ~P1;
& AND (byte level)
| OR (byte level)
^ EXCLUSIVE OR(byte
level)
|| OR
&& AND
<< Shift to the left
>> Shift to the right
Operator Description
== Equal to
<, > Smaller than, bigger
than.
<=, >= Smaller than or equal to,
bigger than or equal to.
!= Not equal to
+= บวกและใหเทากบ
-= ลบและใหเทากบ
*= คณและใหเทากบ
/= หารและใหเทากบ
%= Modulo และใหเทากบ
&= ANDและใหเทากบ
|= OR และใหเทากบ
^= EXCLUSIVE OR และใหเทากบ
<<= เลอนซายและใหเทากบ
>>= เลอนขวาและใหเทากบ
Directives and functions
Directives
Define
#define LED1 P1^0
#define led_on_time 184
ASM และ ENDASM
void main (void)
{ test ();
#pragma asm
JMP $ ; endless loop
#pragma endasm
}
หรอ __asm { JMP $ }
13
Functions
ถกประกาศไวใน header file เชน
Function Description
char cabs (char val); Return an the absolute value of a char
variable.
int abs (int val); Return an the absolute value of a int
variable.
float sqrt (float val); Returns the square root of a float variable.
float exp (float val); Returns the value of the Euler number 'e' to
the power of val
float log (float val); Returns the natural logarithm of val
float log10 (float val); Returns the common logarithm of val
unsigned char _crol_ ( unsigned
char c, unsigned char b);
routine rotates the bit pattern for the
character c left b bits
unsigned char _cror_ ( unsigned
char c, unsigned char b);
routine rotates the bit pattern for the
character c right b bits
float pow (float x, float y); Returns x to the power y.
การเขาถง ports
การก าหนดต าแหนงของหนวยความจ า อางถง address ของแตละ port หรอใช ตวแปร
char ชอ P0, P1, P2 และ P3 แตละตวมขนาด 8 บตและแตละขาของ port PX^0
ถง PX^7
#include <C51rd2.h> เปนการเรยกใช P89V51RX2.h ซงเปนไฟลทบรรจ
รายละเอยดของรจสเตอรของไมโครคอนโทรลเลอร P89C51RD2 ส าหรบใชงานกบ C
คอมไพเลอร
#include <reg51.h> เปนการเรยกใช reg51.h ซงเปนไฟลทบรรจรายละเอยด
ของรจสเตอรพ นฐานของไมโครคอนโทรลเลอรตระกล MCS-51สวนใหญซงไมม Timer
2 ส าหรบใชงานกบ C คอมไพเลอร
14
Directives and functions
Representing Program Operation 15
Flowchart
START
Read value from sensor
ADD 7
Stroe in memory
Wait 1 Hour
24 Samples ?
END
Program Organization
16
Initialization
Variables Declarations
Function’s body
*Infinite Loop
Example Programs 17
โปรแกรมทท าให LED ทตออยกบ P1.0 กระพรบ
#include <REG51.h> /* ประกาศใหคอมไพเลอรรจกรจสเตอรและบตควบคมพนฐานของไมโครคอนโทรลเลอรตระกล MCS-51*/
#include <math.h> /*เรยกใช Library math.h*/
sbit led=P1^7; /* ตงชอ Port 1.7 วา led */
void delay (unsigned int y) {
/*ประกาศ Function และตวแปร Local เขามาใชใน Function น*/
unsigned int i; /* ประกาศตวแปร Local*/
for(i=0;i<y;i++){;} // Loop
}
void main() { /* Main Function ของโปรแกรม*/
while(1) { // Infinite Loop
delay(30000);
led = 0;
delay(30000);
led = 1;
}
}
P1.01
P1.12
3P1.2
4P1.3
A0/P0.0
A1/P0.1
A2/P0.2
A3/P0.3
39
38
37
36
RXD
TXD
ALE/P
10
11
30
P89V51RD2
P1.45
P1.56
7P1.6
8P1.7
A4/P0.4
A5/P0.5
A6/P0.6
A7/P0.7
35
34
33
32
A8/P2.0
A9/P2.1
A10/P2.2
A11/P2.3
21
22
23
24INT0
12
INT113
14T0
15T1
A12/P2.4
A13/P2.5
A14/P2.6
A15/P2.7
25
26
27
28
RESET9
X218
X119
WR16
RD13
31EA/VP
PSEN29
Vcc
+5V
Gnd
40
20
Reset
+5V
Example Programs 18
การรบขอมลจากสวทชและแสดงผล #include <REG51.h>
#include <math.h>
sbit led = P1^7;
sbit sw = P2^7
void delay(unsigned int y){
unsigned int i;
for(i=0;i<y;i++){;}
}
void main(){
sw = 1 //Set up P 2.7as input pin
while(1) {
if(sw == 0) { led = 0; //Turn ON the LED}
else { led = 1; //Turn OFF the LED }
}
}
P1.01
P1.12
3P1.2
4P1.3
A0/P0.0
A1/P0.1
A2/P0.2
A3/P0.3
39
38
37
36
RXD
TXD
ALE/P
10
11
30
P89V51RD2
P1.45
P1.56
7P1.6
8P1.7
A4/P0.4
A5/P0.5
A6/P0.6
A7/P0.7
35
34
33
32
A8/P2.0
A9/P2.1
A10/P2.2
A11/P2.3
21
22
23
24INT0
12
INT113
14T0
15T1
A12/P2.4
A13/P2.5
A14/P2.6
A15/P2.7
25
26
27
28
RESET9
X218
X119
WR16
RD13
31EA/VP
PSEN29
Vcc
+5V
Gnd
40
20
Reset
+5V
Variables
Floating-point Numbers
float ใช 4 bytes (32-bits) รปแบบ
ของตวเลขเปนไปตาม IEEE-754
standard
±mantissa × 2exponent
Mantissa มชวงตงแต –127 ถง
+128
Exponent มขนาด 24 bits ซง MSB
มคาเปน 1 และม 1 บตเปน sign bit
ตวอยาง เลข -12.5 Address0 Address1 Address2 Address3
Format SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMM
Binary 11000001 01001000 00000000 00000000
Hex C1 48 00 00
ตวอยาง
unsigned char a,b,c;
signed char d;
a = 100;
b = 200;
c = a - b; (c =155)
d = a - b; (d =-100)
19