0% found this document useful (0 votes)
11 views

MicroprocessorBasedSystems Term-II Lec4 Embedded C Programming IO TimerCounter1

The document discusses programming 8051 microcontrollers in embedded C, including I/O configuration, data types, time delays, accessing GPIO and SFR addresses, bit-wise operations, RAM and code space usage, and data serialization. Example code is provided to demonstrate various C programming techniques for the 8051 architecture.

Uploaded by

Mohamed Salah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

MicroprocessorBasedSystems Term-II Lec4 Embedded C Programming IO TimerCounter1

The document discusses programming 8051 microcontrollers in embedded C, including I/O configuration, data types, time delays, accessing GPIO and SFR addresses, bit-wise operations, RAM and code space usage, and data serialization. Example code is provided to demonstrate various C programming techniques for the 8051 architecture.

Uploaded by

Mohamed Salah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

E1322

Microprocessor based Systems II

‘Embedded C Programming
(I/O Configuration)’

Dr. Ahmed El-Awamry

Date: 02.03.2024, Time: 15:00


Objective

▪ Be able to program the 8051 GPIOs in


embedded C.

© Dr. Ahmed El-Awamry 01.03.2024 2


Why Program 8051 in C?

▪ Compilers produce hex files that is downloaded


to ROM of microcontroller
▪ The size of hex file is the main concern
▪ 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
© Dr. Ahmed El-Awamry 01.03.2024 3
Data Types

▪ A good understanding of C 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

© Dr. Ahmed El-Awamry 01.03.2024 4


Data Types, Unsigned char

▪ The character data type is the most natural


choice
▪ 8051 is an 8-bit microcontroller
▪ 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
(counters and ASCII characters)
▪ C compilers use the signed char as the default if
we do not put the keyword unsigned

© Dr. Ahmed El-Awamry 01.03.2024 5


Data Types, Unsigned char

© Dr. Ahmed El-Awamry 01.03.2024 6


Data Types, Unsigned char

© Dr. Ahmed El-Awamry 01.03.2024 7


Data Types, Signed char

▪ The signed char is an 8-bit data type


▪ Use the MSB D7 to represent – or +
▪ Give us values from –128 to +127
▪ We should stick with the unsigned char unless
the data needs to be represented as signed
numbers
▪ Temperature

© Dr. Ahmed El-Awamry 01.03.2024 8


Data Types, Unsigned and Signed int

▪ The unsigned int is a 16-bit data type


▪ Takes a value in the range of 0 to 65535 (0000 –
FFFFH)
▪ Define 16-bit variables such as memory addresses
▪ Set counter values of more than 256
▪ Since registers and memory accesses are in 8-bit
chunks, the misuse of int variables will result in a larger
hex file
▪ Signed int is a 16-bit data type
▪ Use the MSB D15 to represent – or +
▪ We have 15 bits for the magnitude of the number from
–32768 to +32767
© Dr. Ahmed El-Awamry 01.03.2024 9
Data Types, Unsigned and Signed int

© Dr. Ahmed El-Awamry 01.03.2024 10


Data Types, Bit and sfr

▪ The bit data type allows access to single bits


of bit-addressable memory spaces 20 – 2FH
▪ To access the byte-size SFR registers, we use
the sfr data type

© Dr. Ahmed El-Awamry 01.03.2024 11


Time Delay

▪ There are two ways to create a time delay in


8051 C
▪ Using the 8051 timer
▪ Using a simple for loop
▪ Delay accuracy is affected by the compiler
choice
▪ C compiler converts the C statements and functions
to Assembly language instructions
▪ Different compilers produce different code

© Dr. Ahmed El-Awamry 01.03.2024 12


Time Delay

© Dr. Ahmed El-Awamry 01.03.2024 13


Time Delay

© Dr. Ahmed El-Awamry 01.03.2024 14


Byte Size IO

© Dr. Ahmed El-Awamry 01.03.2024 15


Byte Size IO

© Dr. Ahmed El-Awamry 01.03.2024 16


Byte Size IO

© Dr. Ahmed El-Awamry 01.03.2024 17


Bit-Addressable I/O

© Dr. Ahmed El-Awamry 01.03.2024 18


Bit-Addressable I/O

© Dr. Ahmed El-Awamry 01.03.2024 19


Bit-Addressable I/O

© Dr. Ahmed El-Awamry 01.03.2024 20


Bit-Addressable I/O

© Dr. Ahmed El-Awamry 01.03.2024 21


Accessing SFR Addresses 80 - FFH

© Dr. Ahmed El-Awamry 01.03.2024 22


Accessing SFR Addresses 80 - FFH

© Dr. Ahmed El-Awamry 01.03.2024 23


Using bit Data Type for Bit-addressable RAM

© Dr. Ahmed El-Awamry 01.03.2024 24


Bit-wise Operators in C

▪ Logical operators
▪ AND (&&), OR (||), and NOT (!)
▪ Bit-wise operators
▪ AND (&), OR (|), EX-OR (^), Inverter (~), Shift
Right (>>), and Shift Left (<<)
▪ These operators are widely used in software engineering for
embedded systems and control

© Dr. Ahmed El-Awamry 01.03.2024 25


Bit-wise Operators in C

© Dr. Ahmed El-Awamry 01.03.2024 26


Bit-wise Operators in C

© Dr. Ahmed El-Awamry 01.03.2024 27


Bit-wise Operators in C, Packed BCD to ASCII

Packed BCD 2 9

© Dr. Ahmed El-Awamry 01.03.2024 28


Bit-wise Operators in C, ASCII to Packed BCD

© Dr. Ahmed El-Awamry 01.03.2024 29


Checksum Operation

© Dr. Ahmed El-Awamry 01.03.2024 30


RAM Data Space Usage by 8051 C Compiler

▪ The 8051 C compiler allocates RAM locations


▪ Bank 0 – addresses 0 – 7
▪ Individual variables – addresses 08 and beyond
▪ Array elements – addresses right after variables
▪ Array elements need contiguous RAM locations and that
limits the size of the array due to the fact that we have
only 128 bytes of RAM for everything
▪ Stack – addresses right after array elements

© Dr. Ahmed El-Awamry 01.03.2024 31


RAM Data Space Usage by 8051 C Compiler

© Dr. Ahmed El-Awamry 01.03.2024 32


Code ROM Space Usage by 8051 C Compiler

© Dr. Ahmed El-Awamry 01.03.2024 33


Data Serialization

▪ Serializing data is a way of sending a byte of


data one bit at a time through a single pin of
microcontroller
▪ Using the serial port
▪ Transfer data one bit a time and control the
sequence of data and spaces in between them
▪ 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

© Dr. Ahmed El-Awamry 01.03.2024 34


Data Serialization

© Dr. Ahmed El-Awamry 01.03.2024 35


Data Serialization

© Dr. Ahmed El-Awamry 01.03.2024 36


Data Serialization

© Dr. Ahmed El-Awamry 01.03.2024 37


Reg51.h

▪ See the PDF file

Required: Calculator Project in Embedded C

© Dr. Ahmed El-Awamry 01.03.2024 38


Appendix

You might also like