+ All Categories
Home > Documents > 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code...

1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code...

Date post: 14-Dec-2015
Category:
Upload: emilio-peniston
View: 215 times
Download: 0 times
Share this document with a friend
30
1 C and the 8051 EGRE 631
Transcript
Page 1: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

1

C and the 8051

EGRE 631

Page 2: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

2

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.

Page 3: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

3

Page 4: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

4

Page 5: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

5

Example program#include <c8051f020.h> // SFR declarationsextern unsigned char rd_buttons(void); // in uni.cunsigned 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);}

Page 6: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

6

The Cx51 compiler generates a number of output files during compilation. Bydefault, 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 sourcetext along with any errors detected by the compiler. Listing files mayoptionally 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 objectcode. Object modules may be linked to an absolute object module by theLx51 Linker/Locator.filename.I Files with this extension contain the source text as expanded by thepreprocessor. All macros are expanded and all comments are deleted inthis listing. For more information, refer to the PREPRINT directive in thefollowing sections.

filename.SRC Files with this extension are assembly source files generated from your Csource code. These files can be assembled with the A51 assembler. Formore information, refer to the SRC directive in the following sections.

Page 7: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

7

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 }

MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 18 ---- CONSTANT SIZE = ---- ---- XDATA SIZE = ---- ---- PDATA SIZE = ---- ---- DATA SIZE = ---- 1 IDATA SIZE = ---- ---- BIT SIZE = ---- ----END OF MODULE INFORMATION.

mbtn.lst

18 bytes of code

Page 8: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

8

Page 9: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

9

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 # 60000 120000 E LCALL rd_buttons0003 EF MOV A,R70004 4200 R ORL last_button,A0006 439001 ORL P1,#01H ; SOURCE LINE # 70009 639040 XRL P1,#040H ; SOURCE LINE # 8000C 439040 ORL P1,#040H ; SOURCE LINE # 9000F AF00 R MOV R7,last_button ; SOURCE LINE # 100011 ?C0001:0011 22 RET ; SOURCE LINE # 11 ; FUNCTION mbtn (END)MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = 18 ----

mbtn.lst

Page 10: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

10

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_buttons0003 EF MOV A,R70004 4200 R ORL last_button,A ; SOURCE LINE # 80006 439001 ORL P1,#01H ; SOURCE LINE # 90009 639040 XRL P1,#040H ; SOURCE LINE # 10000C 439040 ORL P1,#040H ; SOURCE LINE # 11000F AF00 R MOV R7,last_button ; SOURCE LINE # 120011 ?C0001:0011 22 RET ; FUNCTION mbtn (END)

mbtn.lst

Page 11: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

11

1 #pragma CODE2 #pragma SRC3 #include <c8051f020.h> // SFR declarations4 …

RSEG ?PR?mbtn?MBTNmbtn:

USING 0; SOURCE LINE # 5

; {; SOURCE LINE # 6

; unsigned char last_button;; last_button = last_button | rd_buttons();

; SOURCE LINE # 8LCALL rd_buttonsMOV A,R7ORL last_button?040,A

; P1 = P1 | 0x01; //Set P1.0; SOURCE LINE # 9

ORL P1,#01H; P1 = P1 ^ 0x40; //Toggle P1.7

; SOURCE LINE # 10XRL P1,#040H

; P1 = P1 | 0x40; //Set P1.7; SOURCE LINE # 11

ORL P1,#040H; return(last_button);

; SOURCE LINE # 12MOV R7,last_button?040

; } ; SOURCE LINE # 13?C0001:

RET ; END OF mbtn

mbtn.src

Page 12: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

12

#pragma CODE#pragma SRC#include <c8051f020.h> // SFR declarationsextern unsigned char rd_buttons(void); // in uni.cunsigned 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);}

Inline assembly language – Use SRC pragma this suppresses .obj file.

Must assemble .src file to generate .obj file.

Page 13: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

13

Page 14: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

14

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_constantThis 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. Forexample:sbit OV = 0xD2;sbit EA = 0xAF;

Page 15: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

15

#pragma CODE#include <c8051f020.h> // SFR declarationsextern unsigned char rd_buttons(void); // in uni.csbit 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);}

Page 16: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

16

ASSEMBLY LISTING OF GENERATED OBJECT CODE; FUNCTION mbtn (BEGIN)0000 120000 E LCALL rd_buttons0003 EF MOV A,R70004 4200 R ORL last_button,A0006 D290 SETB P1_00008 B297 CPL P1_7000A D297 SETB P1_7000C AF00 R MOV R7,last_button000E ?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)

Page 17: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

17

Page 18: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

18

Page 19: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

19

Page 20: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

20

Page 21: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

21

Page 22: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

22

Page 23: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

23

Page 24: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

24

Page 25: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

25

Page 26: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

26

Page 27: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

27

Page 28: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

28

Page 29: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

29

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 thememory type, the second is the high-order byte of the offset, and the third is thelow-order byte of the offset. Generic pointers may be used to access any variableregardless of its location in 8051 memory space.

Memory-specific pointers - include a memory type specification in thepointer 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 byterequired by generic pointers is not needed by memory-specific pointers.Memory-specific pointers can be stored using only one byte (idata, data, bdata,and pdata pointers) or two bytes (code and xdata pointers).

Pointers

Page 30: 1 C and the 8051 EGRE 631. 2 Introduction The Silicon Labs ISE uses the Keil C51 compiler. The code size is limiter to 2K C has replaced PL/M (the original.

30

 See HW 11.doc on class web page


Recommended