Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 1 C and the 8051 EGRE 631

2 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 The Keil C51 is a complete implementation of ANSI C with extensions. –The extensions support various features of the –See Chapter 3 of the C51 compiler manual. See c51.pdf at the bottom of the class web page.

3 3

4 4

5 5 Example program #include // 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); }

6 6 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.

7 7 1 #include // 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 P P1 = P1 ^ 0x40; //Toggle P P1 = P1 | 0x40; //Set P return(last_button); 11 1 } MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = CONSTANT SIZE = XDATA SIZE = PDATA SIZE = DATA SIZE = IDATA SIZE = BIT SIZE = END OF MODULE INFORMATION. mbtn.lst 18 bytes of code

8 8

9 9 1 #include // 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 P P1 = P1 ^ 0x40; //Toggle P P1 = P1 | 0x40; //Set P return(last_button); 11 1 } ASSEMBLY LISTING OF GENERATED OBJECT CODE ; FUNCTION mbtn (BEGIN) ; SOURCE LINE # 3 ; SOURCE LINE # 4 ; SOURCE LINE # E LCALL rd_buttons 0003 EF MOV A,R R ORL last_button,A ORL P1,#01H ; SOURCE LINE # XRL P1,#040H ; SOURCE LINE # 8 000C ORL P1,#040H ; SOURCE LINE # 9 000F AF00 R MOV R7,last_button ; SOURCE LINE # ?C0001: RET ; SOURCE LINE # 11 ; FUNCTION mbtn (END) MODULE INFORMATION: STATIC OVERLAYABLE CODE SIZE = mbtn.lst

10 10 1 #pragma CODE 2 #include // 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 P P1 = P1 ^ 0x40; //Toggle P P1 = P1 | 0x40; //Set P return(last_button); 12 1 } ASSEMBLY LISTING OF GENERATED OBJECT CODE ; FUNCTION mbtn (BEGIN) E LCALL rd_buttons 0003 EF MOV A,R R ORL last_button,A ; SOURCE LINE # ORL P1,#01H ; SOURCE LINE # XRL P1,#040H ; SOURCE LINE # C ORL P1,#040H ; SOURCE LINE # F AF00 R MOV R7,last_button ; SOURCE LINE # ?C0001: RET ; FUNCTION mbtn (END) mbtn.lst

11 11 1 #pragma CODE 2 #pragma SRC 3 #include // SFR declarations 4 … RSEG ?PR?mbtn?MBTN mbtn: USING0 ; SOURCE LINE # 5 ; { ; SOURCE LINE # 6 ; unsigned char last_button; ; last_button = last_button | rd_buttons(); ; SOURCE LINE # 8 LCALLrd_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 mbtn.src

12 12 #pragma CODE #pragma SRC #include // 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); } Inline assembly language – Use SRC pragma this suppresses.obj file. Must assemble.src file to generate.obj file.

13 13

14 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_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; sbit EA = 0xAF;

15 15 #pragma CODE #include // 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); }

16 16 ASSEMBLY LISTING OF GENERATED OBJECT CODE ; FUNCTION mbtn (BEGIN) E LCALL rd_buttons 0003 EF MOV A,R R ORL last_button,A 0006 D290 SETB P1_ 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 = CONSTANT SIZE = XDATA SIZE = PDATA SIZE = DATA SIZE = IDATA SIZE = BIT SIZE = END OF MODULE INFORMATION. C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)

17 17

18 18

19 19

20 20

21 21

22 22

23 23

24 24

25 25

26 26

27 27

28 28

29 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 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, and pdata pointers) or two bytes (code and xdata pointers). Pointers

30 30 See HW 11.doc on class web page


Download ppt "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."

Similar presentations


Ads by Google