Application Note AN0008 - Revised 28, April 2013 How to set Keil compiler environment v1.0 1. Introduction This application note describes the Keil 8051 compiler environment setting for ABOV 8051 MCU when using C language. Keil is 3rd party compiler, ABOV do not provide it. The ABOV 8051 MCU has M8051EW mentor core and many useful peripheral I/O. If MCU does not have any program in code memory, it can t operate and use. The Keil supports 8051 ASM and C development tools and makes HEX file for MCU operating. We use these tools for MCU S/W development. It is very simple to set the Keil 8051 compiler environment. (Note: this document only supports MC96FB504 series.) 2. Environment setting description 2.1. New project First of all, you have a project to develop some code for 8051. If you don t have it, you make a new project. Project New Project. After making a new project, select device for target. Mentor Graphics Co. -> M8051EW. See Figure 2-1. Then you can configure target option. Figure 2-1. CPU select
2.2. Options for target setting In the options for target tap, set Memory Model, Code Rom Size and etc for MCU. See Figure 2-2. Figure 2-2. Options for target Memory Model: The memory model determines the default memory type used for function arguments, automatic variables, and variables declared with no explicit memory type. Code Rom Size: specifies the size of the program memory. Off-chip Code memory and Off-chip Xdata memory: specifies CODE and XDATA memory areas of target MCU. Example) Table 2-1. The setting option by the taget Target MCU Memory Model Code Rom Size MC96FB504 Small Large Off-chip Code Memory Start:0x0000 Size:0x1000 Off-chip Xdata Memory X Note ROM:4kB XRAM:X 2
2.3. Options for output setting In the options for output tap, decide the Name of Executable which is output filename and select the Create HEX File to make hex output file (in this figure, FB504_timer0_timer1.hex) See Figure 2-3 Figure 2-3. Options for output 2.4. Options for A51 setting In the options for A51 tap, don t select Define 8051 SFR Names in Special Function Registers because of using specific MCU definition such as MC96FB504.h. See Figure 2-4. Figure 2-4. Options for A51 3
2.5. Add Program Source Code Finally, in the project workspace, add your source code or make new program file. See Figure 2-5. Figure 2-5. Add source file 4
3. Program setting description 3.1. Startup.a51 define The startup.a51 file is 8051 assembly code provided by Keil that sets up the C environment. Start-up code is always necessary in C applications that run in an embedded environment in which there is no desktop-style operation system. The startup.a51 is generated when New Project is created, if you add startup.a51 file. There is check point that is memory size. In startup.a51 file, the memory size defines initialize RAM contents as 0. So programmer checks these parameters and has to modify them if not correct. Default memory size of startup.a51 is IDATALEN EQU 80, XDATALEN EQU 0H Example) Table 3-1. The values that define.a51 options for each a target IDATALEN XDATALEN Note MC96FB504 0C0H 0H RAM : 192B XRAM : 0B Note) for more detail, check memory section for start address and size on the datasheet. 3.2. Stack point Stack area is located in IDATA. Stack pointer initially has default 0x07. Keil compiler can change stack pointer value in IRAM area (0x00~0xff) according to variables size of IDATA used in C application. 3.3. Interrupt vector The interrupt vector is located in code address 0x00~0xff range, each address is 8xN+3, N=interrupt _number. Interrupt routines are declared as follows: void function_name(void) interrupt interrupt_number [using register_bank] In C application, the interrupt_number(=n) determines the interrupt vector address of the interrupt function. Example) N=14 (8x14+3=115 (73h: interrupt vector address)) void T1_INT_HANDLER(void) interrupt 14 { } 5
3.4. Setup ROM/RAM data in C 3.4.1. ROM data define In ANSI C, the const type qualifier is used to define and access objects that are constant and may not be changed. But In Keil compiler, variables declared with the const type qualifier alone are stored in the memory area (data, idata, xdata, and so on) associated with their definition. So variables you want to locate in ROM must be declared with the code memory type. Example) const char ROM_Table[ ] = { 1,2,3,4 }; code char ROM_Table[ ] = { 1,2,3,4 }; 3.4.2. RAM data define If you set up Memory Model Small variable in DATA at Project option, DATA (0x00~0xFF) can be used as default memory. If you set up Memory Model Large variable in XDATA at Project option, XDATA can be used as default memory. Accessing DATA memory is considerably faster than accessing XDATA memory. For this reason, you should place frequently used variables in DATA memory and less frequently used variables in XDATA memory. 6
3.5. Using bit variable All bit variables are stored in a bit segment located in the internal memory area of the 8051. Because this area that has 0x20~0x2f address is only 16 bytes long, a maximum of 128 bit variables may be declared within any one scope. But in C language, when using a bit field structure in a union, order of value stored in the bit fields is very important. First define bit is LSB and last define bit is MSB in structure. Some other MCU has different order of bit. See the Example 2). Example 1) bit runflag; runflag = 0; // bit variable can get 0 or 1 Example 2) //keil bit field map typedef struct { unsigned char b0:1; // LSB unsigned char b1:1; unsigned char b2:1; unsigned char b3:1; unsigned char b4:1; unsigned char b5:1; unsigned char b6:1; unsigned char b7:1; // MSB } BYTE_FIELD; /*==== BYTE ====*/ typedef union { unsigned char byte; BYTE_FIELD bits; // bit is keil keyword. So bit bits } TYPE_BYTE; Pos 7 6 5 4 3 2 1 0 bit 1 0 0 1 0 1 1 0 byte 0x96 // keil unsigned char tresb; TYPE_BYTE tt1; Table 3-2. The order of value stored ni the bit fields tt1.bits.b0= 0;tt1.bits.b1= 1;tt1.bits.b2= 1;tt1.bits.b3= 0; tt1.bits.b4= 1;tt1.bits.b5= 0;tt1.bits.b6= 0;tt1.bits.b7= 1; tresb=tt1.byte; // Tresb = 0x96 7