CH 7 - 8051 in C Programming - Coteng
CH 7 - 8051 in C Programming - Coteng
PROGRAMMING
NMJ32404 Embedded System Design
Objectives
• Upon completion of this chapter, you will be able to:
• Examine the C data type for the 8051.
• Code 8051 C programs for time delay and I/O operations.
• Code 8051 C programs for I/O bit manipulation.
• Code 8051 C programs for logic and arithmetic operations.
• Code 8051 C programs for ASCII and BCD data conversion.
• Code 8051 C programs for binary (hex) to decimal conversion.
• Code 8051 C programs to use the 8051 code space.
• Code 8051 C programs for data serialization
❑ Compilers produce hex files that is
WHY
WHY
PROGRAM
downloaded to ROM of microcontroller
PROGRAM ➢ The size of hex file is the main concern
8051 IN C
8051 IN C ▪ Microcontrollers have limited on-chip ROM
▪ Code space for 8051 is limited to 64K bytes
❑ C programming is less time consuming,
but has larger hex file size
❑ The reasons for writing programs in C
➢ It is easier and less time consuming to
write in C than Assembly
➢ C is easier to modify and update
➢ You can use code available in function
libraries
➢ C code is portable to other microcontroller
with little of no modification
DATA TYPES ❑ A good understanding of C data types
DATA TYPES
for 8051 can help programmers to
create smaller hex files
➢ Unsigned char
➢ Signed char
➢ Unsigned int
➢ Signed int
➢ Sbit (single bit)
➢ Bit and sfr
❑ The character data type is the most
DATA TYPES
TYPES
DATA natural choice
Unsigned char
char ➢ 8051 is an 8-bit microcontroller
Unsigned
❑ Unsigned char is an 8-bit data type in
the range of 0 – 255 (00 – FFH)
➢ One of the most widely used data types
for the 8051
▪ Counter value
▪ ASCII characters
❑ C compilers use the signed char as the
default if we do not put the keyword
unsigned
Write an 8051 C program to send values 00 – FF to port P1.
Solution: 1. Pay careful attention to
DATA TYPES #include <reg51.h> the size of the data
void main(void) 2. Try to use unsigned char
{ instead of int if possible
Unsigned char unsigned char z;
(cont’) for (z=0;z<=255;z++)
P1=z;
}
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}
❑ The bit data type allows access to
DATA TYPES
single bits of bit-addressable memory
Bit and sfr spaces 20 – 2FH
❑ To access the byte-size SFR registers,
we use the sfr data type
Data Type Size in Bits Data Range/Usage
unsigned char 8-bit 0 to 255
(signed) char 8-bit -128 to +127
unsigned int 16-bit 0 to 65535
(signed) int 16-bit -32768 to +32767
sbit 1-bit SFR bit-addressable only
bit 1-bit RAM bit-addressable only
sfr 8-bit RAM addresses 80 – FFH only
❑ There are two ways to create a time
TIME DELAY delay in 8051 C
➢ Using the 8051 timer (Chap. 9)
➢ Using a simple for loop
be mindful of three factors that can affect
the accuracy of the delay
▪ The 8051 design
– The number of machine cycle
– The number of clock periods per machine
cycle
▪ The crystal frequency connected to the X1 – X2
input pins
▪ Compiler choice
– C compiler converts the C statements and
functions to Assembly language instructions
– Different compilers produce different code
TIME DELAY Write an 8051 C program to toggle bits of P1 continuously forever
(cont’) with some delay.
Solution:
//Toggle P1 forever with some delay in between
//“on” and “off”
#include <reg51.h> We must use the oscilloscope to
void main(void) measure the exact duration
{
unsigned int x;
for (;;) //repeat forever
{
P1=0x55;
for (x=0;x<40000;x++); //delay size
//unknown
P1=0xAA;
for (x=0;x<40000;x++);
}
}
Write an 8051 C program to toggle bits of P1 ports continuously with
a 250 ms.
TIME DELAY
(cont’) Solution:
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
while (1) //repeat forever
{
p1=0x55;
MSDelay(250);
p1=0xAA;
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
I/O LEDs are connected to bits P1 and P2. Write an 8051 C program that
PROGRAMMING shows the count from 0 to FFH (0000 0000 to 1111 1111 in binary)
on the LEDs.