You are on page 1of 11

!"##$%&'(&)%*+$,-.)%/&.0'&123&450'#6.

Obiectives:
1.! Using C51 compiler.
2.! Test run simple c program that blinks onboard LED.
The c compiler is a program that translates source code written with ANSI c standard to
the assembly code. The assembler then translates the assembly code into machine code.
C51 is a popular c compiler Ior 8051 compatible chips. This lab will introduce how to use
C51 compiler Ior our learning board 8051SBC.
Let`s see the sample program. exp1.c that blinks onboard LED with simple delay
Iunction.
// EXP1 blinking LED
// makes the onboard LED blink with simple delay function
#include <reg52.h>
delay(int j)
{
int i;
for(i=0; i<j; i++)
;
}
main ()
{
while(1)
{
P1 ^= 0x80; // toggle P1.7
delay(10000);
}
}
This program has only two Iunctions. i.e.
delay(int j)
main()
Function delay is simple loop with deIinite counting value. It will repeat until Ior..loop
condition is Ialse.
Function main is simple Iorever loop controlled by while(1) block. Our board has LED
connected to P1.7. We can use EX-OR to toggle the output latch. In c statement the
operator EX-OR is represented by symbol. `. The meaning oI P1` 0x80 is P1 P1 EX-
OR with 0x80. In assembly code.
XRL P1.#80H
This statement includes 8052`s register declaration. so the compiler will know what is
address oI P1.
The modiIer. sIr is the extension oI c51 compiler designed Ior 8051 chip. It declares
symbol with 8-bit constant. We see that P1 is special Iunction register having direct byte
address equal to 0x90.
#include <reg52.h>
Here is the content oI reg52.h
/* BYTE Registers */
sfr P0 = 0x80;
sfr P1 = 0x90;
sfr P2 = 0xA0;
sfr P3 = 0xB0;
sfr PSW = 0xD0;
sfr ACC = 0xE0;
sfr B = 0xF0;
sfr SP = 0x81;
sfr DPL = 0x82;
sfr DPH = 0x83;
sfr PCON = 0x87;
sfr TCON = 0x88;
sfr TMOD = 0x89;
sfr TL0 = 0x8A;
sfr TL1 = 0x8B;
sfr TH0 = 0x8C;
sfr TH1 = 0x8D;
sfr IE = 0xA8;
sfr IP = 0xB8;
sfr SCON = 0x98;
sfr SBUF = 0x99;
/* 8052 Extensions */
sfr T2CON = 0xC8;
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2 = 0xCC;
sfr TH2 = 0xCD;
/* BIT Registers */
/* PSW */
sbit CY = PSW^7;
sbit AC = PSW^6;
sbit F0 = PSW^5;
sbit RS1 = PSW^4;
sbit RS0 = PSW^3;
sbit OV = PSW^2;
sbit P = PSW^0; //8052 only
/* TCON */
sbit TF1 = TCON^7;
sbit TR1 = TCON^6;
sbit TF0 = TCON^5;
sbit TR0 = TCON^4;
sbit IE1 = TCON^3;
sbit IT1 = TCON^2;
sbit IE0 = TCON^1;
sbit IT0 = TCON^0;
/* IE */
sbit EA = IE^7;
sbit ET2 = IE^5; //8052 only
sbit ES = IE^4;
sbit ET1 = IE^3;
sbit EX1 = IE^2;
sbit ET0 = IE^1;
sbit EX0 = IE^0;
/* IP */
sbit PT2 = IP^5;
sbit PS = IP^4;
sbit PT1 = IP^3;
sbit PX1 = IP^2;
sbit PT0 = IP^1;
sbit PX0 = IP^0;
/* P3 */
sbit RD = P3^7;
sbit WR = P3^6;
sbit T1 = P3^5;
sbit T0 = P3^4;
sbit INT1 = P3^3;
sbit INT0 = P3^2;
sbit TXD = P3^1;
sbit RXD = P3^0;
/* SCON */
sbit SM0 = SCON^7;
sbit SM1 = SCON^6;
sbit SM2 = SCON^5;
sbit REN = SCON^4;
sbit TB8 = SCON^3;
sbit RB8 = SCON^2;
sbit TI = SCON^1;
sbit RI = SCON^0;
/* P1 */
sbit T2EX = P1^1; // 8052 only
sbit T2 = P1^0; // 8052 only
/* T2CON */
sbit TF2 = T2CON^7;
sbit EXF2 = T2CON^6;
sbit RCLK = T2CON^5;
sbit TCLK = T2CON^4;
sbit EXEN2 = T2CON^3;
sbit TR2 = T2CON^2;
sbit C_T2 = T2CON^1;
sbit CP_RL2 = T2CON^0;
The modiIier. sbit is to declare the symbol to be bit address. For example TR0 can be
declared as TCON`4.
The diagram below shows sequence oI c compiler running. Suppose our proiect Iile
contains two Iiles. i.e. exp1.c and STARTUP.A51. The exp1.c is a c source Iile and
STARTUP.A51 is assembly source code Ior startup code. The startup code initializes
stack location. clear some data RAM. then call to MAIN Iunction. We can speciIy the
start code segment by modiIying startup code directly.
Preprocessor rearranges the source code by replacing any constant deIinitions. include
Iiles. The compiler then translates c source code into assembly code. Again we see that
assembler then convert it to obiect Iile or hex Iile. The STARTUP.A51 is assembly code.
so the assembler can convert it to obiect Iile directly. Finally the linker combines all oI
obiect Iiles into single Iile and put the real address. For our 8051SBC. the board accepts
Intel hex Iile. C51 has obiect to hex converter utility. The hex Iile can then be
downloaded to our board to test it.
exp1.c
STARTUP.A51
Preprocessor
Compiler
Assembler
Linker
Obj ect code, e.g. HEX file
Proj ect files
"7"+.)#"&'(&Edit the source code exp1.c and modiIy STARTUP code.
1.! Change to value oI Code Segment to 8000h
CSEG AT 8000h
2.! Create new proiect exp1. Add source both Iiles to the proiect Iile.
3.! Select OPTION Ior Target1. add the value as shown below window.
4.! Go to C51 window. enter start oI interrupt vector to 0x8000.
5.! Open OUTPUT window. mark CREATE HEX FILE.
6.! Click BUILD proiect. or press F7. The status window will show result oI
compilation. II no error. the HEX Iile will be created.
We will get Intel HEX Iile as shown below.
:0380000002801BE0
:0C801B00787FE4F6D8FD7581070280270D
:10800300E4FDFCC3ED9FEE6480F8EC6480985007B8
:088013000DBD00010C80EC2200
:0C8027006390807F107E2712800380F49D
:00000001FF
7.! Download the hex Iile to 8051SBC. Disassemble it. we will get.
8000: 02 80 1B LJMP 801B ; where we set it by CSEG
8003: E4 CLR A
8004: FD MOV R5, A
8005: FC MOV R4, A
8006: C3 CLR C
8007: ED MOV A, R5
8008: 9F SUBB A, R7
8009: EE MOV A, R6
800A: 64 80 XRL A, #80
800C: F8 MOV R0, A
800D: EC MOV A, R4
800E: 64 80 XRL A, #80
8010: 98 SUBB A, R0
8011: 50 07 JNC 801A
8013: 0D INC R5
8014: BD 00 01 CJNE R5, #00, 8018
8017: 0C INC R4
8018: 80 EC SJMP 8006
801A: 22 RET
801B: 78 7F MOV R0, #7F
801D: E4 CLR A
801E: F6 MOV @R0, A
801F: D8 FD DJNZ R0, 801E
8021: 75 81 07 MOV SP, #07
8024: 02 80 27 LJMP 8027
8027: 63 90 80 XRL P1, #80 ; P1 ^= 0x80;
802A: 7F 10 MOV R7, #10 ; arguments for delay
802C: 7E 27 MOV R6, #27 ; function
802E: 12 80 03 LCALL 8003 ; call delay
8031: 80 F4 SJMP 8027 ; while(1)
8.! Test the program with command JUMP to 8000.
9.! Can we change rate oI LED blinking? How?
Now. remember we have the 8-bit output port. 74HC573 or GPIO1. The location oI
GPIO1 is 0x100. Let us test another simple program making rotation oI an 8-bit LED.
"7"+.)#"&8(
1.! Edit source code below.
// EXP1 rotating LED
#include <reg52.h>
xdata char GPIO1 _at_ 0x100; // declare location of GPIO1
delay(int j)
{
int i;
for(i=0; i<j; i++)
;
}
main ()
{
char temp;
temp=1;
while(1)
{
P1 ^= 0x80; // toggle P1.7
GPIO1 = temp;
temp<<=1;
delay(10000);
if(temp==0) temp =1;
}
}
We learn new modiIier. xdata.
xdata char GPIO1 _at_ 0x100;
This line tells compiler that the symbol GPIO1 is the location oI external data memory at
address 0x100. We see that we can have assignment statement to set value at GPIO1
easily by.
GPIO1 = temp; // MOVX @DPTR,temp
And here is standard c operator Ior shiIting bit.
temp<<=1; // temp = temp<<1
2.! Test the code and see will your LED be running?
3.! Can you make LED running go and back?
4.! Insert statement that prints 'hello world to the main loop. Compare the size oI
code? How big it is?
You may reIresh c operators and keywords as shown below. We will learn more in the
next lab.
() Parentheses (grouping)
[] Brackets (array subscript)
. Member selection via object name
-> Member selection via pointer
++ -- Unary preincrement/predecrement
+ - Unary plus/minus
! ~ Unary logical negation/bitwise complement
(type) Unary cast (change type)
* Dereference
& Address
sizeof Determine size in bytes
* / % Multiplication/division/modulus
+ - Addition/subtraction
<< >> Bitwise shift left, Bitwise shift right
< <= Relational less than/less than or equal to
> >= Relational greater than/greater than or equal to
== != Relational is equal to/is not equal to
& Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
&& Logical AND
|| Logical OR
?: Ternary conditional
= Assignment
+= -= Addition/subtraction assignment
*= /= Multiplication/division assignment
%= &= Modulus/bitwise AND assignment
^= |= Bitwise exclusive/inclusive OR assignment
<<= >>= Bitwise shift left/right assignment
, Comma (separate expressions)
There are 32 keywords that. when combined with the Iormal C syntax. Iorm the C
language as deIined by the ANSI C.
All keywords use lowercase letters. In C. uppercase and lowercase are diIIerent. Ior
instance. else is a keyword. ELSE is not a keyword though.
An alphabetical summary oI each oI the keywords is as Iollows:
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

You might also like