Vous êtes sur la page 1sur 57

Unit 6

Embedded C programming for 8051

6.1 Introduction

C versus Assembly

Advantages of C
High-level, structured programming language
Compiler relieves programmer from some of the
hardware details
Easier to write large, complex software
Programs more readable

Disadvantages of C
Generally larger machine code
Less control and ability to interact with hardware
Unclear number of cycles to do something
6.2 Data types
Copy Q. 4( c) May 2016 & Part I of Q. 6( c) Dec. 2016 here.

A good understanding of C data types for 8051 can help programmers to create smaller
hex files.

There are various type of Data types in C :

unsigned char
signed char
unsigned int
signed int
sbit (single bit)
bit and sfr

1. unsigned char:
Table 6.1

Programs
6.3

A time delay in 8051 can be created by two ways:


1. By using the 8051 timer
2. With the help of a simple for loop
Copy Q, 4(d) May 2016 & part II of Q. 6(b) Dec. 2016 here.
6.4

6.4.1
6.4.2 Bit-addressable I/O Programming
The I/O ports of P0-P3 are bit addressable. This means a single bit of the port can
be accessed without disturbing the other bits.
The bit can be accessed using the data type sbit. One way to do that is to use the
format Px^y, where x is the port 0, 1, 2, 3 and y is the bit 0-7 of that port.
For example P1^3 indicates P1.3.
6.4.3 Accessing SFR Addresses 80 FFH
The sfr data type can be used to access the SFR RAM space 80H-FFH.
When this data type is used no need of using the header file reg51.h.
Table 6.2 shows the single bit addresses of ports.
6.5 Logic Operation
Copy Q. 6(b), Dec. 2016 here.

The ability to perform bit manipulation is one of the most important and powerful
features of the C language .

The bitwise operators used in C are AND (&), OR ( | ), EX-OR ( ^ ), inverter (~),
Shift right (>>) and Shift left (<<).
These are widely used in software engineering for embedded systems and
control; consequently, understanding them is important in microprocessor and
microcontroller-based system design and interfacing.

The operations are shown in table 6.-- below.


fig. 6.3
6.6 Data conversions
The basic data conversions we will be seeing into are:
Packed BCD to ASCII conversion
ASCII to Packed BCD conversion
Binary (hex) to decimal and ASCII conversion
6.6.1

Data
step b

6.7 Accessing Code ROM Space

6.7.1 RAM data space vs. code data space


6.7.2

To use the code space, we need to put the keyword code in front of the variable
declaration.

Ex. Compile and run the following program on the 8051 simulator and examine the
contents of the code space to locate the ASCII values.
The code space locates the values 41H, 42H, 43H, 44H, etc. the hex values for ASCII
characters of A, B, C, D,

6.8 Data Serialization


A way of sending a byte of data one bit at a time through a single pin of microcontroller
is called serializing data .

In many new generations of devices such as LCD, ADC, and ROM the serial versions are
becoming popular since they take less space on a PCB.

Copy Q. 6(a), Dec. 2016 here.


end of ch 6

Vous aimerez peut-être aussi