You are on page 1of 10

PROGRAMMING WITH

EMBEDDED ‘C’ in 8051


By
Siva . J
Memory Areas
Program Memory: may be accessed using the code
memory type specifier in the Cx51 compiler.

External Data Memory


The Cx51 Compiler offers two different memory types that
access external data: xdata and pdata.

Xdata : refers to any location in the 64 Kbyte address space


of external data memory.

pdata :memory type specifier refers to only one (1) page


or 256 bytes of external data memory.
Memory Areas (Contd…)
Internal Data Memory: can be broken down into three distinct
memory types data, idata, and bdata.

data :refers to the first 128 bytes of internal data memory.


Variables stored here are accessed using direct
addressing.
Idata : refers to all 256 bytes of internal data memory;
however,this memory type specifier code is generated by
indirect addressing which is slower than direct
addressing.
bdata : refers to the 16 bytes of bit-addressable memory In
the internal data area (20h to 2Fh). This memory type
specifier, allows you to declare data types that can also be
accessed at the bit level.
Memory Models
The memory model determines the default
memory type to use for function arguments,
automatic variables, and declarations with
no explicit memory type specifier.
• Small Model
• Compact Model
• Large Model
Absolute Variable Location:
type memory_space variable_name _at_
constant;
where:
• memory_space is the memory space for the
variable. If missing from the declaration, the
default memory space is used.
• type is the variable type.
• variable_name is the name of the variable.
• constant is the address at which to locate the
variable.
Special Function Registers: Sfr16, sfr, sbit.
Variable Decleration -
type memory_space example
variable_name _at_ constant;

unsigned char xdata port_a _at_ 0xe000;


data unsigned char i _at_ 0x34;
unsigned char data text[16] _at_ 0x40;
unsigned char mess[ ] = { 'W','A','I','T‘ };
bit b0;

unsigned char bdata flag _at_ 0x20;


sbit fbit = flag ^ 7;

sbit P1_2 = 0x90 ^ 2;

sfr scon = 0x98;


Sfr16 data_ptr = 0x82;
Function Declarations
The Cx51 compiler provides a number
of
extensions for standard C function
declarations.
These extensions allow you to:
• Specify a function as an interrupt
procedure.
• Choose the register bank to be used.
• Select the memory model.
• Specify reentrancy.
Cx51 function declaration
return_type funcname (args) {small / compact / large}
[reentrant] [interrupt n] [using n]
where:
• return_type is the type of the value returned from the
function.
If no type is specified, int is assumed.
• funcname is the name of the function.
• args is the argument list for the function.
• small, compact, or large is the explicit memory model for
the function.
• reentrant indicates that the function is recursive or
reentrant.
• interrupt indicates that the function is an interrupt
function.
• using specifies which register bank the function uses.
Function Declaration -
exemple
return_type funcname (args) {small / compact / large}
[reentrant] [interrupt n] [using n]

Void timer0_int ( ) interrupt 1


void serial_int ( ) interrupt 4
void Disp_Key (unsigned char c)
void new (unsigned char c) small
void old (unsigned char t) small
reentrant
void new (void) using 2
Differences from ANSI C
Compiler-related Differences
• Wide Characters
Wide 16-bit characters are not
supported by Cx51. ANSI provides wide
characters for future support of an
international character set.
• Recursive Function Calls
Recursive function calls are not
supported by default. Functions that are
recursive must be declared using the
reentrant function attribute.

You might also like