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

Chapter 3 C Programming For 8051 2023

This chapter discusses C programming concepts for the 8051 microcontroller including I/O programming, logic programming, timer/counter programming, serial communication, and interrupt programming. It provides examples of programming different I/O ports, using logic and bitwise operators, setting up and using timers in different modes, sending data serially, and programming interrupts. The goal is to introduce the basics of programming the 8051 microcontroller in C language.

Uploaded by

trucdo0411
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Chapter 3 C Programming For 8051 2023

This chapter discusses C programming concepts for the 8051 microcontroller including I/O programming, logic programming, timer/counter programming, serial communication, and interrupt programming. It provides examples of programming different I/O ports, using logic and bitwise operators, setting up and using timers in different modes, sending data serially, and programming interrupts. The goal is to introduce the basics of programming the 8051 microcontroller in C language.

Uploaded by

trucdo0411
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Chapter 3

C PROGRAMMING FOR 8051

1
CONTENTS
3.1. Introduction
3.2. C programming for microcontrollers
3.3. I/O programming
3.4. Logic programming

3.5 Timer/ Counter programming

3.6. Serial programming

3.7. Interupt programming

2
3.1. Introduction
• Why program 8051 in C
• Compiler proceduce hex files that is downloaded to ROM of microcontroller
• C programming is less time consuming, but has larger hex file size
• The reasons for writing programs in C.

3
3.1. Introduction
• Data types
• Unsigned char
• Signed char
• Unsigned int
• Signed int
• Sbit
• Bit and sfr

4
3.2. C programming for microcontrollers
• Time delay
• Using a simple for loop
• Using 8051 timer
• Ex 3.1: Write an 8051 C program to toggle bits of P1 continuously forever with
some delay
• Ex 3.2: Write an 8051 C program to toggle bits of P1 continuously with some a
250ms.

5
3.3. I/O programming
• Byte size I/O
• Ex 3.3: LEDs are connected to bits P1 and P2. Write an 8051 C program that shows the
count from 0 to FFH on the LEDs.
• Ex 3.4: Write an 8051 C program to get a byte of data form P1, wait ½ second, and then
send it to P2.
• Ex 3.5: : Write an 8051 C program to get a byte of data form P0, wait ½ second. If it is
less than 100, sen it to P1; otherwise, and then send it to P2.

6
3.3. I/O programming
• Bit-addressable I/O
• Ex 3.6: Write an 8051 C program to toggle only bit P2.4 continuously without disturbing
the rest of the bits of P2.
• Ex 3.7: Write an 8051 C program to monitor bit P1.5. If it is high, send 55h to P0;
otherwise, and then send AAh to P2
• Ex 3.8: A door sensor is connected to the P1.1 pin, and a buzzer is connected to P1.7.
Write an 8051 program to monitor the door sensor, and when it opens, sound the buzzer.
You can sound the buzzer by sending a square wave of a few hundred Hz.
• Ex 3.9: The data pins of an LCD are connected to P1. the information is latched into the
LCD whenever its Enable pin goes from high to low. Write an 8051 C grogram to send
“The Earth is but One Country” to this LCD.

7
3.3. I/O programming
• Accessing SFR Addresses 80 - FFH
• Ex 3.10: Write an 8051 C program to turn bit P1.5 on and off 50,000 times.
• Ex 3.11: Write an 8051 C program to get the status of P1.0, save it, and send it to P2.7,
continuously.

8
3.4. Logic programming
• Bit-wise operators on C
• Logical operators
• AND (&&), OR (||), NOT (!)
• Bit-wise operators
• AND (&), OR (|), Inverter (~), Shift Right (>>), Shift Left (<<)

9
3.4. Logic programming
• Bit-wise operators on C
• Ex 3.12: Write an 8051 C program to toogle all the bits of P0 and P2 continuously with a
250ms delay. Using the inverting and Ex-OR operators, respectively.
• Ex 3.13: Write an 8051 C program to bet bit P1.0 and send it to P2.7 after inverting it.

10
3.4. Logic programming
• RAM data space usage by 8051 C compiler
• Bank 0 – addresses 0-7
• Individual variables – addresses 08 and beyond
• Array elements – addresses right after variables
• Stack – addresses right after array elements.

11
3.4. Logic programming
• Data serialization
• 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
• Ex: Write a C program to send out the value 44H serially one bit at a time via P1.0: the
LSB should go first out first.
• Ex: Write a C program to send out the value 44H serially one bit at a time via P1.0: the
MSB should go first out first.

12
3.5 Timer/ Counter programming
• Timers programming
• Two timers/counters
• 16 bits wide
• Low byte and high byte

13
3.5 Timer/ Counter programming
• Timers programming
• TMOD register: 8 bit
• The lower 4 bits are for Timer 0
• The upper 4 bits are for Timer 1

14
3.5 Timer/ Counter programming
• Mode 1 programming
• It is a 16-bit timer; therefor, it allows value of 0000 to FFFFh to be loaded into the timer’s
register TL, TH
• After TH and TL are loaded with a 16-bit initial value, the timer must be started
• After the time started, it starts to count up until FFFFH
• When it rolls over from FFFFh to 0000, it sets high a flag bit called TF (TF0, TF1)
• When this timer flag is raised, one option would be to stop the timer with the instruction CLR TR0 or
CLR TR1.
• After the timer reaches its limit and rolls over, in order to repeat the process

15
3.5 Timer/ Counter programming
• Mode 1 programming
• Finding the loaded Timer value
• Assume XTAL = 11.0592 MHz, we can use the following steps for finding the TH, TL registers’s
values
• Divide the designed time delay by 1.085us
• Perform 65536 – n, where n is the decimal value we got in Step 1
• Convert the result of step 2 to hex, yyxx
• Set TL = xx, and TH yy

16
3.5 Timer/ Counter programming
• Mode 2 programming
• 8 bit timer: 00 – FFh
• After TH is loaded with the 8 bit value, the 8051 gives a copy of it to TL
• After the timer is started, it starts to count up by incrementing the TL register
• When the TL register rolls from FFh to 0 and TF is set to 1, TL is reloaded automatically
with the original value kept by the TH register

17
3.5 Timer/ Counter programming
• Mode 2 programming
• Load the TMOD value register indicating which timer is to be used, and the timer mode is
selected
• Load the TH registers with the initial count value
• start timer
• Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to see whether it
is raised
• Clear the TF flag
• Go back to Step 4 since mode 2 is autoload

18
3.5 Timer/ Counter programming
• Counter programming
• Timers can also be used as counters counting events happing outside the 8051
• The C/T bit in the TMOD registers decides the source of the clock for the timer

19
3.5 Timer/ Counter programming
• C/T bit in TMOD Register

20
3.5 Timer/ Counter programming
• TCON register
• 8 bit

21
3.5 Timer/ Counter programming
• TCON register
• A bit-addressable register

22
3.5 Timer/ Counter programming
• Ex: Write an 8051 C program to toogle all the bits of port P1 continuously with
some delay in between. Use Timer 0, 16 bit mode to generate the delay.
• Ex: Write an 8051 C program to toogle only bit P1.5 continuously every 50ms.
Use Timer 0, mode 1, 16 bit mode to generate the delay.

23
3.6. Serial communication
• Serial communication
• Computer transfer data in two ways:
• Parallel and Serial

• Two method: synchronous and assynchronous

24
3.6. Serial communication
• Serial communication
• Ex: Write an 8051 C program to transfer the message “YES” serially at 9600 baud, 8-bit
data, 1 stop bit. Do this continuously.
• Ex: Program the 8051 in C to receive bytes of data serially and put them in P1. Set the
baud rate at 4800, 8 bit data, and the 1 stop bit.

25
3.7. Interupt programming
• The TCON register holds four of the interrupt flags, in the 8051 the SCON
register has the RI and TI flags.

26
3.7. Interupt programming
• When the 8051 is powered up, the priorities are assigned according to the
following

27
3.7. Interupt programming

28
3.7. Interupt programming
• Ex: Write a C program that continously gets a single bit of data from P1.7 and
sends it to P1.0, while simultaneously creating a square wave of 200us priod on
pin P2.5. Use timer 0 to create the square wave. Assume that XTAL = 11.0592
MHz.
• Ex: Write a C program using interrupts to do the following:
• a. Receive data serically and send it to P0.
• b. Read port P1, transmit data serially, and give a copy to P2
• c. make timer 0 generate a square wave of 5kHz frequency on P0.1. Assume that
XTAL = 11.0592 MHz. Set the baud rate at 4800.

29

You might also like