ĐH Bách Khoa TP.HCM
Lê Chí Thơng
The 8051 Microcontroller
Chapter 8
C Programming for 8051
TS. Lê Chí Thơng
sites.google.com/site/chithong
Ho Chi Minh City University of Technology
Lê Chí Thơng
1
Outline
•
•
•
•
•
•
•
•
•
Structure of a C program
Using Keil
Declaration of the types of memory
Variable types
Keywords in Keil C51
Functions
Operations – Statements
Time delay
I/O programming
Lê Chí Thơng
sites.google.com/site/chithong
2
1
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
References
• Cx51 Compiler User Guide (pdf)
\nm\raw-attachment\wiki\2010\usb10\C51.PDF
• Matthew Kramer, Writing C Code for the 8051 (pdf)
/>
• M. Mazidi, J. Mazidi, and R. McKinlay, The 8051 Microcontroller
and Embedded Systems Using Assembly and C (2nd ed.)
• Chung-Ping Young, 8051 Programming in C (pdf)
• Ibrahim Kamal, C programming for 8051 using KEIL IDE,
/>
Lê Chí Thơng
3
Blinky Program
(Schematic)
sites.google.com/site/chithong
Lê Chí Thơng
4
2
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Blinky Program
void Delayms (unsigned int ms) {
//Documentatons
unsigned int i;
/**************************
while
(ms) {
Blinky program
i
=
115;
Date: 05-Apr-2012
while (i> 0) i--;
Version: 1.0
ms--;
***************************/
}
//Preprocessor Statements
}
#include <reg51.h>
// Main Program
#define ON 1
main (void) {
#define OFF 0
while (1) {
// Global declarations
LED1 = ON; // Turn on LED1
sbit LED1=P1^0;
Delayms (500); // Delay 500 ms
// Subprograms
LED1 = OFF; // Turn off LED1
/* Delay in milliseconds for a 11.0592 MHz crystal */
Delayms (500); // Delay 500 ms
}
Lê
Chí
Thơng
5
}
source
From the C program
to the machine language
Source: />Lê Chí Thơng
sites.google.com/site/chithong
6
3
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
C Programming for the 8051
• C programming is less time consuming, but has
larger hex file size
• The reasons for writing programs in C
– It is easier and less time consuming to write in C than
Assembly
– C is easier to modify and update
– You can use code available in function libraries
– C code is portable to other microcontroller with little
of no modification
Source: Chung-Ping Young, 8051 Programming in C
Lê Chí Thơng
7
Structure of a C program
Documentations
Preprocessor Statements
Global Declarations
Subprograms (User defined functions)
Main Program (Main function)
Lê Chí Thơng
sites.google.com/site/chithong
8
4
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Structure of a C program
//Documentatons
//Name of the program, programmer,
//and other details
//Preprocessor Statements
#include <stdio.h> //header files
#include <reg51.h>
#define TRUE 1 //symbolic constants
#define FALSE 0
//Global Declarations
unsigned char x,y;
int z;
long n=0;
//Subprograms
Void function1 (int x) {
//Statements
}
//Main function
void main(void)
{
int sum = 0; //Local Declarations
int x;
float y;
//Statements
}
Lê Chí Thơng
9
Keil Product Downloads
/>
Lê Chí Thông
sites.google.com/site/chithong
10
5
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Using Keil
• Open Keil and
start a new project
• Chose a name for
your new project,
create a separate
folder where all
the files of your
project will be
stored, chose a
name and click
save.
Source: />
11
Using Keil
• Select a device
for Target ‘Target
1′: seek
for ATMEL, then
select AT89C51,
AT89S52, …
• You will be
asked whether to
‘copy standard
8051 startup
code‘ click No.
Source: />
sites.google.com/site/chithong
12
6
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Using Keil
• Click File, New
• Click ‘File, Save
as’ and chose a
file name for
your source code
ending with the
letter ‘.c’,
‘code.c’ for
example, and
click save.
Source: />
13
Using Keil
Add file to project:
• Right-click on
‘source group 1‘,
click on ‘Add files
to group…‘,
•Right-click on
Target 1,
click Options for
target ‘Target 1′,
then under the
‘Output‘ tab, check
the box ‘crate HEX
file‘.
Source: />
sites.google.com/site/chithong
14
7
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Using Keil
• Compile your
source code, and
correct eventual
syntax errors:
‘rebuild all
targets’
•Debug:
Source: />
15
Using Keil
• Run:
• Stop:
• Reset:
• Step:
Source: />
sites.google.com/site/chithong
16
8
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Keil Variable Extensions
• Declaration of the types of memory
data
idata
bdata
xdata
internal RAM (direct addressing)
internal RAM (indirect addressing)
bit addressable internal RAM
external data memory
code
code memory (program memory)
Lê Chí Thơng
17
data/idata
Description: The variable will be stored in internal data
memory of controller.
Example:
unsigned char data x;
//or
unsigned char idata y;
Lê Chí Thơng
sites.google.com/site/chithong
18
9
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
bdata
Description: The variable will be stored in bit
addressable memory of controller.
Example:
unsigned char bdata x;
//each bit of the variable x can be accessed
//as follows
x ^ 1 = 1; //1st bit of variable x is set
x ^ 0 = 0; //0th bit of variable x is cleared
Lê Chí Thơng
19
xdata
Description: The variable will be stored in external RAM
memory of controller.
Example:
unsigned char xdata x;
Lê Chí Thơng
sites.google.com/site/chithong
20
10
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
code
Description: This keyword is used to store a constant
variable in code memory. Lets say you have a big string
which is not going to change anywhere in program.
Wasting ram for such string will be foolish thing. So
instead we will make use of the keyword "code" as
shown in example below.
Example:
unsigned char code str="this is a constant string";
Lê Chí Thơng
21
Variable Types
• The Keil C compiler supports most C variable types and
adds several of its own:
– Standard Types
– Keil Types
• A good understanding of C data types for 8051 can help
programmers to create smaller hex files
Lê Chí Thơng
sites.google.com/site/chithong
22
11
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Standard Types
Data type
unsigned char
signed char
unsigned int
signed int
unsigned long
signed long
Bytes
1
1
2
2
4
4
float
4
Range
0 to 255
-128 To +127
0 to 65,535
-32,768 To +32,767
0 to 4,294,967,295
-2,147,483,648 to 2,147,483,647
± 1.175494E-38 to
± 3.402823E +38
• Unsigned char is an 8-bit data type in the range of 0 –
255 (00 – FFH) and is one of the most widely used data
types for the 8051.
Lê Chí Thơng
23
Keil Types
Data type
bit
sbit
sfr
sfr16
Bits
1
1
8
16
Bytes
1
2
Lê Chí Thơng
sites.google.com/site/chithong
Range
0 to 1
0 to 1
0 to 255
0 to 65,535
24
12
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
bit
Description: This keyword is used to define a bit from
bit-addressable on-chip RAM.
Example:
bit = 0x80;
//
Lê Chí Thông
25
sbit
Description: This keyword is used to define a special bit
from SFR (special function register) memory.
Example:
sbit Port0_0 = 0x80;
// Special bit with name Port0_0 is defined
// at address 0x80
Lê Chí Thơng
sites.google.com/site/chithong
26
13
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
sfr
Description: sfr is used to define an 8-bit special
function register from SFR memory.
Example:
sfr Port1 = 0x90;
// Special function register with name Port1
// defined at addrress 0x90
Lê Chí Thơng
27
sfr16
Description: This keyword is used to define a two
sequential 8-bit registers in SFR memory.
Example:
sfr16 DPTR = 0x82;
// 16-bit special function register starting
at 0x82
// DPL at 0x82, DPH at 0x83
Lê Chí Thơng
sites.google.com/site/chithong
28
14
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
unsigned char – Example 1
Source: Chung-Ping Young
Lê Chí Thơng
29
unsigned char – Example 2
Source: Chung-Ping Young
sites.google.com/site/chithong
Lê Chí Thơng
30
15
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
unsigned char – Example 3
Source: Chung-Ping Young
Lê Chí Thơng
31
signed char – Example
Source: Chung-Ping Young
sites.google.com/site/chithong
Lê Chí Thông
32
16
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
sbit, signed int – Example
Source: Chung-Ping Young
Lê Chí Thơng
33
Keywords in Keil C51
• Keil C51 compiler adds few more keywords to the scope
C Language:
_at_
small
far
_priority_
using
large
alien
_task_
interrupt
compact
reentrant
Lê Chí Thơng
sites.google.com/site/chithong
34
17
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
_at_
Description: This keyword is used to store a variable on
a defined location in ram.
Example:
unsigned char idata x _at_ 0x30;
// variable x will be stored at location
// 0x30 in internal data memory
Lê Chí Thơng
35
using
Description: This keyword is used to define register
bank for a function. User can specify register bank 0 to
3.
Example:
void function () using 2{
// code
}
// Function named "function" uses register
bank 2 while executing its code
Lê Chí Thơng
sites.google.com/site/chithong
36
18
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
interrupt
Description: This keyword will tells the compiler that function
described is an interrupt service routine. C51 compiler supports
interrupt functions for 32 interrupts (0-31). Use the interrupt
vector address in the following table to determine the interrupt
number.
Interrupt number
0
1
Address
0003H
000BH
2
3
4
0013H
001BH
0023H
Interrupt source
External 0
Timer 0
External 1
Timer 1
Serial
Lê Chí Thơng
37
interrupt
Example:
void External_Int0() interrupt 0{
//code
}
Lê Chí Thơng
sites.google.com/site/chithong
38
19
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Examples of Data Types
data char Var1;
char code text [] = "Enter temperature:";
unsigned long xdata MyArray [100];
float idata x, y, z;
unsigned int pdata Dimension;
unsigned char xdata vector [10] [4] [4];
char BDATA Indicator;
Lê Chí Thơng
39
Function
• A general form of a C function looks like this:
<return type> FunctionName (Argument1, Argument2, …)
{
Statement1;
Statement2;
…
}
An example of function:
int sum (int x, int y)
{
int result;
result = x + y;
return (result);
}
Lê Chí Thơng
sites.google.com/site/chithong
40
20
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
An Example of Functions
sum4 () {
result4 = a+b;
}
// Examples of functions
#include <stdio.h>
int a,b,result2;
int result3,result4;
int sum (int x, int y) {
int result;
result = x+y;
return (result);
}
void sum2 (int x, int y) {
result2 = x+y;
}
sum3 (int x, int y) {
result3 = x+y;
}
main () {
a=2;
b=3;
sum(a,b);
sum2(a,b);
sum3(a,b);
sum4();
}
Lê Chí Thơng
41
Constant
int const a=12;
int const b[]={2,4,15,0,155};
unsigned char const c=5;
unsigned char const c=’5’;
unsigned char const x[]="ABCDEF";
Lê Chí Thơng
sites.google.com/site/chithong
42
21
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Operators (1)
+
+=
&=
&
&
^=
^
l=
l
Addition Operator
Addition assignment operator, x+=y, is the same as x=x+y
Bitwise and assignment operator, x&=y, is the same as
x=x&y
Address operator
Bitwise and operator
Bitwise exclusive or assignment operator, x^=y, is the
same as x=x^y
Bitwise exclusive or operator
Bitwise inclusive or assignment operator, xl=y, is the same
as x=xly
Bitwise inclusive or operator
Lê Chí Thơng
43
Operators (2)
?:
-/=
/
==
>
>=
++
*
!=
Conditional Expression operator
Decrement
Division assignment operator, x/=y, is the same as x=x/y
Division operator
Equality
Greater than operator
Greater than or equal to operator
Increment
Indirection operator
Inequality
Lê Chí Thơng
sites.google.com/site/chithong
44
22
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Operators (3)
<<=
<
<<
<=
&&
!
ll
%=
%
Left shift assignment operator, x<<=y, is the same as
x=x<
Less than operator
Left Shift operator
Less than or equal to operator
Logical AND operator
Logical negation operator
Logical OR operator
Modules assignment operator x%=y, is the same as x=x%y
Modules operator
Lê Chí Thông
45
Operators (4)
*=
Multiplication assignment operator, x*=y, is the same as
x=x*y
*
Multiplication operator
~
One's complement operator
>>=
Right shift assignment, x>>=y, is the same as x=x>>y
>>
Right shift operator
->
Structure Pointer operation
-=
Subtraction assignment operator
Subtraction operator
sizeof Determines size in bytes of operand
Lê Chí Thơng
sites.google.com/site/chithong
46
23
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Logical Operations
Operator
~
&
|
^
Description
NOT
AND
OR
EXOR
Example:
P1 = P1 & 0xF0; //Adding '0x' before a number
//indicates that it is a hexadecimal one
P1 = P1 & 240; //same effect
P1 = P1 | 0x81; //set the first and last bit of P1,
//without affecting the other
Lê Chí Thơng
47
Shift Operations
Operator
>>
<<
Description
Shift to the right
Shift to the left
Example:
P1 = 0x01; // After that operation, in binary, P1 =
//0000 0001
P1 = (P1 << 7); // After that operation, in binary
//P1 = 1000 0000
Lê Chí Thơng
sites.google.com/site/chithong
48
24
ĐH Bách Khoa TP.HCM
Lê Chí Thơng
Comparison Operations
Operator
==
<, >
<=, >=
!=
Description
Equal to
Smaller than, bigger than.
Smaller than or equal to, bigger than or
equal to.
Not equal to
Example:
Lê Chí Thơng
49
Logical and Shift Operations – Example 1
Source: Chung-Ping Young
Lê Chí Thơng
sites.google.com/site/chithong
50
25