8051 C Programming
8051 C Programming
C FOR MICROCONTROLLERS
R EVIEW OF C BASICS
C O M P I L AT I O N F L O W
C EXTENSIONS
I N - L I N E A S S E M B LY
I N T E R FA C I N G W I T H C
E XAMPLES
A R R AY S AND P OINTERS
I / O C IRCUITRY
F UNCTIONS AND H EADER F ILES
M U LT I TA S K I N G A N D M U LT I T H R E A D I N G
A S S E M B LY L A N G UAG ES
B I T M A N I P U L AT I O N I N S T R U C T I O N S
M O ST M I C R O CO N T R O L L E R S H AV E AVA I L A B L E C
CO M P I L E R S
W RITING IN C S I M P L I F I ES C O D E D E V E LO P M E N T FO R
L A R G E P R OJ EC T S .
C P R O G R A M M I N G I S L ES S T I M E C O N S U M I N G , B U T H A S L A R G E R
HEX FILE SIZE
compile
program.LST program.OBJ
contain the formatted source Reserved memory for global variables.
text along with any errors Public symbol (Variable) names.
detected by the compiler. External symbol (variable) references.
Library files with which to link.
Debugging information to help synchronize
source lines with object code.
build/make
program.M51
Monday, October 08, 2012 5
Like most high level languages, C is a modular
programming language (but NOT an object oriented
language)
Declaration of functions
Main function
Sub-functions
Types:
int (16-bits in our compiler)
char (8-bits)
short (16-bits)
long (32-bits)
Example:
if (x != y) && (c == b)
{
a=c + d*b;
a++;
}
adder.OBJ
build/make
project.M51
adder.SRC adder.OBJ
assemble
rename build/make look here in RAM
adder.asm
when debugging
project.M51
Symbol Table in M51 file:
------ DO
D:0008H SYMBOL x
D:000AH SYMBOL y
Map file shows where variables are stored. D:000CH SYMBOL z
One map file is generated per project. ------- ENDDO
while (condition)
{ statements }
Example:
while (1) ; // loop forever
code
data
idata Others
Memory bdata
Type xdata _at_
far alien
pdata
interrupt
_priority_
Special Function sfr
reentrant
sfr16
Registers sbit _task_
using
bdata
idata
xdata
Example:
bit bit new_flag; //stored in 20-2F
sbit sbit LED = P1^6;
sfr sfr SP = 0x81; //stack pointer
sfr16 sfr16 DP = 0x82; // data pointer
#include <reg51.h>
Ctest2.c
xdata_mov.c
sfr16
sfr16 name = address;
sbit
sbit name = sfr-name ^ bit-position;
sbit name = sfr-address ^ bit-position;
sbit name = sbit-address;
ex_at.c
#pragma asm
put your assembly code here
#pragma endasm
ex_asm.c
type arr_name[dimension]
char temp_array[256]
Level 9
Common Block Subroutines: Detects recurring instruction
sequences and converts them into subroutines. Cx51
evenrearranges code to obtain larger recurring sequences.
ex_opt.c