?? c514.txt
字號:
發信人: reflection (似水流年), 信區: EEtechnology
標 題: C51 Primer (3) Variables and Constants
發信站: 南京大學小百合站 (Wed Nov 24 09:50:49 1999), 轉信
3 Declaring Variables And Constants
3.1 Constants
The most basic requirement when writing any program is to know how to alloca
te storage for program data. Constants are the simplest; these can reside in
the code (Eprom) area or as constants held in RAM and initialised at runtim
e. Obviously, the former really are constants and cannot be changed.
While the latter type are relatively commonplace on big systems (Microsoft C
), in 8051 applications the code required to set them up is often best used
elsewhere. Also, access is generally faster to ROMmed constants than RAM one
s if the RAM is external to the chip, as ROM "MOVC A,@DPTR" instruction cycl
e is much faster than the RAM "MOVX A,@DPTR".
Examples of Eprommed constant data are:
code unsigned char coolant_temp = 0x02 ;
code unsigned char look_up table[5]='1','2','3','4''} ;
code unsigned int pressure = 4 ;
Note that "const" does not mean "code". Objects declared as "const" will act
ually end up in the data memory area determined by the current memory model.
Obviously, any large lookup tables should be located in the CODE area - a de
claration might be:
/* Base FuelMap */
/* x = Load : y = engine speed : output = Injector PW, 0 - 8.16ms */
/* (x_size,y_size,
x_breakpoints,
y_breakpoints,
map_data)
*/
code unsigned char default_base_fuel_PW_map[] = {
0x08,0x08,
0x00,.0x00,0x00,0x09,0x41,0x80,0xC0,0xFF,
0x00,0x00,0x13,0x1A,0x26,0x33,0x80,0xFF,
0x00,0x00,0x00,0x09,0x41,0x80,0x66,0x66,
0x00,0x00,0x00,0x09,0x41,0x80,0x66,0x66,
0x00,0x00,0x00,0x00,0x4D,0x63,0x66,0x66,
0x00,0x00,0x00,0x02,0x4D,0x63,0x66,0x66,
0x00,0x00,0x00,0x05,0x4A,0x46,0x40,0x40,
0x00,0x00,0x00,0x08,0x43,0x43,0x3D,0x3A,
0x00,0x00,0x00,0x00,0x2D,0x4D,0x56,0x4D,
0x00,0x00,0x00,0x00,0x21,0x56,0x6C,0x6F
} ;
With large objects like the above it is obviously important to state a memor
y space. When working in the SMALL model in particular, it is very easy to f
ill up the on-chip RAM with just a single table!
RAM constants would be:
unsigned char scale_factor = 128 ;
unsigned int fuel_constant = 0xFD34 ;
These could, however, have their values modified during program execution. A
s such, they are more properly thought of as initialised variables - see sec
tion 3.2.2
3.2 Variables
3.2.1 Uninitialised Variables
Naturally, all variables exist in RAM, the configuration of which is given i
n section 2.1.1.
The #pragma SMALL line will determine the overall memory model. In this case
, all variables are placed within the on-chip RAM. However, specific variabl
es can be forced elsewhere as follows:
#pragma SMALL
.
.
xdata unsigned char engine_speed ;
xdata char big_variable_array[192] ;
This will have engine_speed placed in an external RAM chip. Note that no ini
tial value is written to engine_speed, so the programmer must not read this
before writing it with a start value! This xdata placement may be done to al
low engine_speed to be traced "on the fly", by an in-circuit emulator for ex
ample.
In the case of the array, it would not be sensible to place this in the on-c
hip RAM because it would soon get filled up with only 128 bytes available. T
his is a very important point - never forget that the 8051 has very limited
on-chip RAM.
Another example is:
.
#pragma LARGE
.
.
.
function(data unsigned char para1)
{
data unsigned char local_variable ;
.
.
.
.
}
Here the passed parameters are forced into fast directly addressed internal
locations to reduce the time and code overhead for calling the function, eve
n though the memory model would normally force all data into XDATA.
In this case it would be better to declare the function as SMALL, even thoug
h the prevailing memory model is large. This is extremely useful for produci
ng a few fast executing functions within a very big LARGE model program.
On a system using paged external RAM on Port 0, the appropriate directive is
"pdata".
See notes in section 2.1.3 for details on how to best locate variables.
3.2.2 Initialised Variables
To force certain variables to a start value in an overall system setup funct
ion, for example, it is useful to be able to declare and initialise variable
s in one operation. This is performed thus:
unsigned int engine_speed = 0 ;
function()
{
.
.
.
}
Here the value "0" will be written to the variable before any function can a
ccess it. To achieve this, the compiler collects together all such initialis
ed variables from around the system into a summary table. A runtime function
named "C_INIT" is called by the "startup.obj" program which writes the tabl
e values into the appropriate RAM location, thus initialising them.
Immediately afterwards, the first C program "main()" is called. Therefore no
read before write can occur, as C_INIT gets there first. The only point to
note is that you must modify the "startup.a51" program to tell C_INIT the lo
cation and size of the RAM you are using. For the large model, XDATASTART an
d XDATALEN are the appropriate parameters to change.
3.3 Watchdogs With Large Amounts Of Initialised Data
In large programs the situation may arise that the initialisation takes long
er to complete than the watchdog timeout period. The result is that the cpu
will reset before reaching main() where presumably a watchdog refresh action
would have been taken.
To allow for this the INIT.A51 assembler file, located in the \C51p\LIB dire
ctory, should be modified.
;__________________________________________________________;
This file is part of the C-51 Compiler package Copyright KEIL ELEKTRONIK Gm
bH 1990
;__________________________________________________________;
INIT.A51: This code is executed if the application program contains initial
ised variables at file level.
; _________________________________________________________;
; User-defined Watch-Dog Refresh.
;
; If the C application containing many initialised variables uses a watchdo
g it
; might be possible that the user has to include a watchdog refresh in the
; initialisation process. The watchdog refresh routine can be included in t
he
; following MACRO and can alter all CPU registers except DPTR.
;
WATCHDOG MACRO
;Include any Watchdog refresh code here
P6 ^= watchdog_refresh ;Special application code
ENDM
;____________________________________
NAME ?C_INIT
?C_C51STARTUP SEGMENT CODE
?C_INITSEG SEGMENT CODE ; Segment with Initialising Data
EXTRN CODE (MAIN)
PUBLIC ?C_START
RSEG ?C_C51STARTUP INITEND: LJMP MAIN
?C_START:
MOV DPTR,#?C_INITSEG
LOOP:
WATCHDOG ;<<_ WATCHDOG REFRESH CODE ADDED HERE!
CLR A
MOV R6,#1
MOVC A,@A+DPTR
JZ INITEND
INC DPTR
MOV R7,A
.
.
.
. Large initialisation loop code
.
.
.
XCH A,R0
XCH A,R2
XCH A,DPH
XCH A,R2
DJNZ R7,XLoop
DJNZ R6,XLoop
SJMP Loop
LJMP MAIN ; C51 Program start
RSEG ?C_INITSEG
DB 0
END
A special empty macro named WATCHDOG is provided which should be altered to
contain your normal watchdog refresh procedure. Subsequently, this is automa
tically inserted into each of the initialisation loops within the body of IN
IT.A51.
3.4 C51 Variables
3.4.1 Variable Types
Variables within a processor are represented by either bits, bytes, words or
long words, corresponding to 1, 8, 16 and 32 bits per variable. C51 variabl
es are similarly based, for example:
bit =1 bit 0 - 1
char =8 bits 0 - +/- 127
unsigned char =8 bits 0 - 255
int =16 bits 0 - +/-32768
unsigned int =16 bits q0 - 65535
long =32 bits 0 - +/- 2.147483648x109
unsigned long =32 bits 0 - 4.29496795x109
float =32 bits +/-1.176E-38
to +/-3.4E+38
pointer =24/16/8 bits Variable address
Typical declarations would be:
xdata unsigned char battery_volts ;
idata int correction_factor ;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -