C and the 8051
EGRE 631
1
Introduction
• The Silicon Labs ISE uses the Keil C51 compiler.
• The code size is limiter to 2K
• C has replaced PL/M (the original Intel high level
language (HLL) for imbedded systems).
• Versions of basic and a few other HLL such as FOUTH
are available for the 8051.
• The Keil C51 is a complete implementation of ANSI C
with extensions.
– The extensions support various features of the 8051.
– See Chapter 3 of the C51 compiler manual. See c51.pdf at the
bottom of the class web page.
2
3
4
Example program
#include <c8051f020.h> // SFR declarations
extern unsigned char rd_buttons(void); // in uni.c
unsigned char mbtn()
{
unsigned char last_button;
last_button = last_button | rd_buttons();
P1 = P1 | 0x01; //Set P1.0
P1 = P1 ^ 0x40; //Toggle P1.7
P1 = P1 | 0x40; //Set P1.7
return(last_button);
}
5
The Cx51 compiler generates a number of output files during compilation. By
default, each of these output files shares the same filename as the source file.
filename.LST Files with this extension are listing files that contain the formatted source
text along with any errors detected by the compiler. Listing files may
optionally contain the symbols used and the assembly code generated.
For more information, refer to the PRINT directive in the following sections.
filename.OBJ Files with this extension are object modules that contain relocatable object
code. Object modules may be linked to an absolute object module by the
Lx51 Linker/Locator.
filename.I Files with this extension contain the source text as expanded by the
preprocessor. All macros are expanded and all comments are deleted in
this listing. For more information, refer to the PREPRINT directive in the
following sections.
filename.SRC Files with this extension are assembly source files generated from your C
source code. These files can be assembled with the A51 assembler. For
more information, refer to the SRC directive in the following sections.
6
mbtn.lst
1
2
3
4
5
6
7
8
9
10
11
1
1
1
1
1
1
1
#include <c8051f020.h>
// SFR declarations
extern unsigned char rd_buttons(void); // in uni.c
unsigned char mbtn()
{
unsigned char last_button;
last_button = last_button | rd_buttons();
P1 = P1 | 0x01; //Set P1.0
P1 = P1 ^ 0x40; //Toggle P1.7
P1 = P1 | 0x40; //Set P1.7
return(last_button);
18 bytes of code
}
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE
= 18 ---CONSTANT SIZE = ---- ---XDATA SIZE
= ---- ---PDATA SIZE
= ---- ---DATA SIZE
= ---1
IDATA SIZE
= ---- ---BIT SIZE
= ---- ---END OF MODULE INFORMATION.
7
8
mbtn.lst
1
#include <c8051f020.h>
// SFR declarations
2
extern unsigned char rd_buttons(void); // in uni.c
3
unsigned char mbtn()
4
{
5
1
unsigned char last_button;
6
1
last_button = last_button | rd_buttons();
7
1
P1 = P1 | 0x01; //Set P1.0
8
1
P1 = P1 ^ 0x40; //Toggle P1.7
9
1
P1 = P1 | 0x40; //Set P1.7
10
1
return(last_button);
11
1
}
ASSEMBLY LISTING OF GENERATED OBJECT CODE
; FUNCTION mbtn (BEGIN)
; SOURCE LINE # 3
; SOURCE LINE # 4
; SOURCE LINE # 6
0000 120000
E
LCALL
rd_buttons
0003 EF
MOV
A,R7
0004 4200
R
ORL
last_button,A
0006 439001
ORL
P1,#01H ; SOURCE LINE # 7
0009 639040
XRL
P1,#040H ; SOURCE LINE # 8
000C 439040
ORL
P1,#040H ; SOURCE LINE # 9
000F AF00
R
MOV
R7,last_button ; SOURCE LINE # 10
0011
?C0001:
0011 22
RET ; SOURCE LINE # 11
; FUNCTION mbtn (END)
MODULE INFORMATION:
STATIC OVERLAYABLE
CODE SIZE
=
18
----
9
mbtn.lst
1
#pragma CODE
2
#include <c8051f020.h>
// SFR declarations
3
extern unsigned char rd_buttons(void); // in uni.c
4
unsigned char mbtn()
5
{
6
1
unsigned char last_button;
7
1
last_button = last_button | rd_buttons();
8
1
P1 = P1 | 0x01; //Set P1.0
9
1
P1 = P1 ^ 0x40; //Toggle P1.7
10
1
P1 = P1 | 0x40; //Set P1.7
11
1
return(last_button);
12
1
}
ASSEMBLY LISTING OF GENERATED OBJECT CODE
; FUNCTION mbtn (BEGIN)
0000 120000
E
LCALL
rd_buttons
0003 EF
MOV
A,R7
0004 4200
R
ORL
last_button,A
; SOURCE LINE # 8
0006 439001
ORL
P1,#01H
; SOURCE LINE # 9
0009 639040
XRL
P1,#040H
; SOURCE LINE # 10
000C 439040
ORL
P1,#040H
; SOURCE LINE # 11
000F AF00
R
MOV
R7,last_button
; SOURCE LINE # 12
0011
?C0001:
0011 22
RET
; FUNCTION mbtn (END)
10
1
#pragma CODE
2
#pragma SRC
3
#include <c8051f020.h>
4 …
mbtn.src
RSEG ?PR?mbtn?MBTN
mbtn:
USING
// SFR declarations
0
; SOURCE LINE # 5
;{
; SOURCE LINE # 6
unsigned char last_button;
last_button = last_button | rd_buttons();
; SOURCE LINE # 8
LCALL
rd_buttons
MOV
A,R7
ORL
last_button?040,A
; P1 = P1 | 0x01; //Set P1.0
; SOURCE LINE # 9
ORL
P1,#01H
; P1 = P1 ^ 0x40; //Toggle P1.7
; SOURCE LINE # 10
XRL
P1,#040H
; P1 = P1 | 0x40; //Set P1.7
; SOURCE LINE # 11
ORL
P1,#040H
; return(last_button);
; SOURCE LINE # 12
MOV
R7,last_button?040
;}
; SOURCE LINE # 13
?C0001:
RET
; END OF mbtn
;
;
11
Inline assembly language – Use SRC pragma this suppresses .obj file.
Must assemble .src file to generate .obj file.
#pragma CODE
#pragma SRC
#include <c8051f020.h>
// SFR declarations
extern unsigned char rd_buttons(void); // in uni.c
unsigned char mbtn()
{
unsigned char last_button;
last_button = last_button | rd_buttons();
#pragma asm
SETB P1.0 // P1 = P1 | 0x01; //Set P1.0
CPL P1.7 // P1 = P1 ^ 0x40; //Toggle P1.7
SETB P1.7 // P1 = P1 | 0x40; //Set P1.7
#pragma endasm
return(last_button);
}
12
13
sbit - provides access to bit-addressable SFRs and other bit-addressable objects.
For example: sbit EA = 0xAF;
The expression to the right of the equal sign (=) specifies an absolute bit address
for the symbolic name. There are three variants for specifying the address:
Variant 1: sfr_name ^ int_constant
This variant uses a previously declared sfr (sfr_name) as the base address for
the sbit. The expression following the carat symbol (^) specifies the position of
the bit to access with this declaration. The bit position must be a number in the 0
to 7 range. For example:
sfr PSW = 0xD0;
sbit OV = PSW ^ 2;
sbit CY = PSW ^ 7;
Variant 2: int_constant ^ int_constant
This variant uses an integer constant as the base address for the sbit.
example:
sbit OV = 0xD0 ^ 2;
sbit CY = 0xD0 ^ 7;
sbit EA = 0xA8 ^ 7;
Variant 3: int_constant This variant uses an absolute bit address for the sbit.
For
example:
sbit OV = 0xD2;
14
sbit EA = 0xAF;
#pragma CODE
#include <c8051f020.h>
// SFR declarations
extern unsigned char rd_buttons(void); // in uni.c
sbit P1_0 = P1^0;
sbit P1_7 = P1^7;
unsigned char mbtn()
{
unsigned char last_button;
// sbit P1_0 = P1^0; // sbit cannot be declared local
// sbit P1_7 = P1^7; // it must be global
last_button = last_button | rd_buttons();
P1_0 = 1;
P1_7 = ~P1_7;
P1_7 = 1;
return(last_button);
}
15
ASSEMBLY LISTING OF GENERATED OBJECT CODE
; FUNCTION mbtn (BEGIN)
0000 120000
E
LCALL
rd_buttons
0003 EF
MOV
A,R7
0004 4200
R
ORL
last_button,A
0006 D290
SETB
P1_0
0008 B297
CPL
P1_7
000A D297
SETB
P1_7
000C AF00
R
MOV
R7,last_button
000E
?C0001:
000E 22
RET
; FUNCTION mbtn (END)
MODULE INFORMATION:
STATIC OVERLAYABLE
CODE SIZE
=
15
---CONSTANT SIZE
=
------XDATA SIZE
=
------PDATA SIZE
=
------DATA SIZE
=
---1
IDATA SIZE
=
------BIT SIZE
=
------END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
16
17
18
19
20
21
22
23
24
25
26
27
28
Pointers
Generic Pointers - Generic pointers are declared in the same fashion as
standard C pointers. For example:
char *s; /* string ptr */
int *numptr; /* int ptr */
long *state; /* Texas */
Generic pointers are stored using three bytes. The first byte is the
memory type, the second is the high-order byte of the offset, and the third is the
low-order byte of the offset. Generic pointers may be used to access any
variable
regardless of its location in 8051 memory space.
Memory-specific pointers - include a memory type specification in the
pointer declaration and always refer to a specific memory area. For example:
char data *str; /* ptr to string in data */
int xdata *numtab; /* ptr to int(s) in xdata */
long code *powtab; /* ptr to long(s) in code */
Because the memory type is specified at compile-time, the memory type byte
required by generic pointers is not needed by memory-specific pointers.
Memory-specific pointers can be stored using only one byte (idata, data,
bdata,
29
and pdata pointers) or two bytes (code and xdata pointers).
See HW 11.doc on class web page
30
© Copyright 2026 Paperzz