Embedded C Module 4
Embedded C Module 4
Embedded C Module 4
Counter Application
• In all the above programs we have used the timers in timer mode; C/T = 0 in TMOD register.
In the timer mode, the timers count the clock pulses from 8051’s crystal oscillator, where the
clock pulse frequency is crystal frequency/ 12 .
• In the counter mode of operation; C/T = 1 in TMOD register and the timers count the clock
pulses from an external source (outside 8051).
• Timer 0 in counter mode will count the clock pulses given at P3.4 (port 3) and timer 1 will
count the clock pulses given at P3.5.
The counter mode of operation has many applications. It can be used to count the number of people
entering a point; the number of objects moving on a conveyor belt, the number of wheel rotations, etc.
Fig. illustrates the counting of the number of people entering through a particular door (entrance/arch,
etc.). On either side of the door a LED (can be infrared) acting as a source and
a photodetector (photodiode, etc.) acting as a detector are placed. When no one passes through the
entrance, the LED’s output will fall on the photodetector and the photodetector will give a high output.
When a person/object passes through the entrance, the LED’s output will be cut off and the
photodetector will give a low output. Hence for every person there is a high to low (& back to high)
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
transition. In other words a pulse is generated for every person. These pulses can be fed to the counter
to be counted.
Ex 1: Consider that a 1 Hz external clock is connected to 8051. Write a program for counter 0 in
mode 1 to count the pulses and display the count value continuously on P2&P1
Soln:
Algorithm:
1. Initialize TMOD for counter 0, mode 1 (TMOD = 0000 0101 = 05H)
2. Initialize initial count of counter 0 as 00 (i.e., TH0:TL0 = 0000)
3. Start counter (TR0 = 1)
4. Move the values of TL0 and TH0 to ports P1 and P2
5. Repeat step 4 until counter 0 overflows (TF1 = 0)
6. Repeat from step 2
ALP:
C program
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Ex 2: Assume that a 1 Hz external clock is being fed into pin T1 (P3.5). Implement a 8-bit upcounter
on port P1 to count from an initial value to FFh.
Soln:
Use counter 1 (because of T1) in mode 2 (for auto reload from initial value).
ALP
Ex 3: Repeat Example 2 with the modification that the 8-bit binary count should be converted to
ASCII. Display the ASCII digits (in binary) on P0, P1, P2, where P0 has the least significant digit.
Set the initial value of TH0 to 0.
Soln:
Algorithm:
1. Initialize TMOD for counter 1, mode 2
2. Initialize the initial count in TH1
3. Start counter
4. Get count in counter (in TL1)
5. Call subroutine to convert the count to ASCII and display on ports
6. Repeat from step 4 until counter overflows (TF1 = 1)
7. Stop counter, clear timer flag and repeat from step 3
Subroutine algorithm
1. Get the unit digit of the 8-bit count value by using modulo division by 10
2. Divide the 8-bit count value by 10, so that the new value (quotient) contains
only hundreds and tens).
3. Use modulo 10 division on the quotient in step 2 to get tens digit.
4. Divide the quotient in step 2 by 10 to get the hundreds digit.
5. Add 30H to the units, tens, hundreds to get the ASCII value.
6. Display the ASCII values on the ports.
ALP
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
C program
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Timer/ Counter 0
Timer/Counter 1
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
SERIAL COMMUNICATION
Serial Communication is used for transferring data between two systems located at distances of
hundreds of feet to millions of miles apart. The fact that serial communication uses a single data
line instead of 8 bit data line of parallel communication not only makes it much cheaper but also
enables two computers located in two different cities to communicate.
In data transmission if the data can be transmitted and received, it is called duplex transmission,
This is in contrast to simplex transmissions such as with printers, in which the computer only
sends data. Duplex transmissions can be half or full duplex, depending on whether or not data
transfer can be simultaneous. If data is transmitted one way at a time, it is referred to as half
duplex. If the data can go both ways at the same time, it is full duplex.
The main difference between parallel I/O and serial I/O is in the number of lines used for data
transfer—the parallel I/O uses the entire data bus and the serial I/O uses only one data line. The
8051 has a serial data communication circuit that uses register SBUF to hold data. Serial
transmission and reception cannot be done simultaneously. SBUF is actually two physical
registers. One is write only and is used to hold data which is transmitted out of the 8051 via TxD
(P3.1). Another is read only and holds data received from external sources via RxD (P3.0). The
two registers use the same address 99H. Register SCON controls the data communication and
register PCON controls data rates.
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
DB9 is a 9-pin serial port connector (remember the 25 pin printer port → parallel port connector
& the smaller 9 pin serial port connector in almost all PCs). Of the 9 pins, a minimum of 3 pins:
TxD, RxD and ground (common b/w 8051 & DB9) are needed for serial full duplex transmission
between a 8051 and a serial device (modem, PC serial port, etc.). Generally the Tx pin of 8051
should be connected to RxD pin of DB9 and Rx to TxD. But because of voltage level mismatch,
the MAX232 takes in data from Tx pin of 8051 and converts to RS232 level of RxD pin. It uses
T1 and T1 pins as shown in Fig. Similarly the data from TxD pin of DB9 is converted to TTL level
and given to Rx pin of 8051 by the MAX232.
Synchronous transmission
Asynchronous transmission
The asynchronous format is character-oriented. Each character carries the information of the start
and stop bits as shown in Fig.. Transmission begins with one start bit (low, called space), followed
by a character and one or two stop bits (high, called mark). This is also called framing.
Asynchronous
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
The 8051 transfers and receives data serially at many different baud rates. The baud rate in the
8051 is programmable. This is done with the help of Timer 1.
C Program
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Ex 2: Read port P1 data and transfer this data serially continuously at a baud rate of 4800.
Soln:
ALP:
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
C program
Ex 3: Consider that a switch SW is connected to pin P2.3. Monitor the SW status and if SW = 0:
send ‘Hello’ and if SW = 1: send ‘world’ serially. Assume XTAL = 11.0592 MHz, baud rate of
9600, 8 bit data, 1 stop bit.
Soln:
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
ALP
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
C program
Ex 4: Write a program to serially transmit the message ‘HELLO’ continuously at a baud rate of
9600, 8-bit data and 1 stop bit.
Soln:
ALP
C program
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Ex 1: Write a program to receive serial data and place it in RAM memory location 62H and also
send it to port P2.
Soln:
ALP
C program
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
INTERRUPTS PROGRAMMING
A single microcontroller can serve several devices. There are two methods to do this. The first method is
called polling wherein the CPU continuously checks whether any device needs to be serviced. In the
interrupt method; the external device sends a signal to interrupt whatever the microcontroller is doing, and
serve the device. Polling method does not use the microcontroller efficiently and precious CPU time is
wasted. Further assigning priority is not possible. In the following sections, we will see the details of
programming the 8051 with interrupts in both assembly language and C.
1. In polling method, the microcontroller is not used efficiently. When this method is used to service
more than one device, it cannot assign priority, but serves in round robin fashion, and hence cannot
ignore (mask) a device request for service.
2. In interrupt method, the microcontroller is free from checking the status of the devices. Only when
the devices are ready, they interrupt the microcontroller and get the service. In the meantime the
microcontroller can be programmed for some useful tasks.
3. The other advantage is that priority can be assigned to the interrupts and the interrupts can be
masked (ignored).
There are five specified interrupts in 8051 - INT0, INT1, TF0, TF1 and serial interrupts (RI/TI). Whenever
an interrupt occurs, the microcontroller suspends, its normal operation and services the interrupt. Reset pin
when activated also interrupts the 8051. Hence Reset is also classified as one of the interrupts (hence six
interrupts). When an interrupt occurs, the 8051 jumps to a specific location in the (ROM) program space.
The locations are specified as shown in Table below. The 8051 knows an interrupt has occurred when the
corresponding interrupt flag is activated. The flags are reset when the interrupt is serviced, some are cleared
automatically and some require the programmer to clear the flag specifically.
Upon reset, all interrupts are disabled (masked) and the microcontroller will not respond to them. The
interrupts are enabled by software, using the bit- addressable IE register (IE – interrupt enable).
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
D7 D6 D5 D4 D3 D2 D1 D0
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Example 1. Write a program to generate a square wave of 10 kHz with timer 0 in mode 2 at port pin 1.3
using interrupt mode. Also display a value of ‘A’ at port 2 and ‘B’ at port 0. XTAL = 22MHz.
Solution
The black schematic of the above problem statement is shown in Fig. below.
Algorithm
Main program
1 Initialize TMOD for timer 0, mode 2
2 Load initial count in TH0 for 10 kHz
3 Enable interrupts (EA = 1) and timer 0 interrupt (ET0 = 1) in IE register
4 Start timer 0
5 Send ASCII value of ‘A’ to port P2 and ‘B’ to port P0
6 Repeat step 5 continuously.
ISR (at 000BH)
1 Toggle port pin P1.3
2 Return from interrupt.
Initialization of TMOD
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
ALP
C Program
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Example 2. Write a program to generate a square wave on P2.4 with a high portion of 1 ms and low portion
of 2 ms using timer 1 in interrupt mode with a crystal frequency of 11.0592 MHz. also read the value of
port 0 and display it at port 1.
Solution
Since the ON-time (high portion) and off-time (low portion) of the square wave are different, we cannot
use mode 2 (auto reload). We use mode 1 to generate the required time delays.
Initial value (TH1 and TL1) to generate 2 ms delay during OFF time
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Algorithm
1 Initialize TMOD for timer 1, mode 1
2 Initialize IE for enabling Timer 1 interrupt
3 Make port pin P2.4 high and load initial value into TH1, TL1 for on-time
delay
4 Start timer
5 Read value at port 0
6 Write the value back to port 1
7 Repeat from step 5 continuously
ALP
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
C Program
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Example 3. Write a program to generate two square waves of 5 kHz and 25 kHz at pins P1.3 and
P2.3 respectively, with crystal frequency of 22 MHz and in interrupt mode.
Solution
Use timer 0 and timer 1 in mode 2 (auto reload) to generate the two square waves continuously.
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Algorithm:
1 Initialize timer 0 and 1 in mode 2.
2 Enable interrupts of timer0&1using IE register.
3 Load initial values into TH1, TH0 for the corresponding 5 kHz & 25 kHz.
4 Start timer 0& 1.
5 Goto step 5 i.e., wait indefinitely in the main loop.
ALP
C Program
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Example 4. Write a program to toggle pin P1.4 every second using interrupts for a frequency of 22MHz.
Use timer 1 in mode 1.
Solution
The maximum delay from Timer 1 in mode 1 (with initial values of TH1:TL1 = 0000H) is 0.0711
seconds. (from previous programs) To get 1 second delay repeat the delay 14 times.
Algorithm
1 Initialize timer 1 in mode 1
2 Enable interrupt of timer 1 using IE register (IE=88H, Example 9.3)
3 Load initial values into TH1 and TL1 as 0000H
4 Load counter R0 with 14 to get 1 second delay
5 Start timer 1
6 Wait indefinitely by jumping to step 6.
ISR at 001BH
Jump to intserve routine.
Intserve subroutine
1. Stop timer 1
2. Decrement counter (R0)
3. If counter (R0) is not zero, then return to the main program (stop)
4. If counter (R0) is zero (then 1 second delay is over), toggle pin 1.4
5. Reload the initial values into timer1
6. Start the timer 1
7. Return to the main program
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
ALP
C Program
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Let us consider the situation when all the interrupts are enabled at the same time using the IE register.
Now if more than one interrupt is activated at the same time (i.e., the interrupts are occurring at the same
time) then the 8051 does the following.
1 It latches all the interrupts that are activated at the same time.
2 It services the interrupts with highest priority.
3 After servicing, instead of returning to the main program, it will service the next priority interrupt that
occurred. This will continue till all the interrupts have been serviced and then return to the main program.
The interrupt priority level structure that is implemented by 8051 at power up is given in Table below.
When the priority bit of an interrupt is 1, the corresponding interrupt is grouped as a high priority interrupt,
similarly those interrupts whose priority bits are made zero are grouped as low priority interrupts.
MADHURA G
EMBEDDED CONTROLLERS(17EI43)
Now when an interrupt occurs it is grouped as a high priority or low priority interrupt (depending on IP
bits). Next in the high priority group the interrupt with the highest priority (according to priority level at
reset) is serviced first.
Next all the other interrupts in the high priority group (according to priority level) are serviced. Once all
interrupts that were activated in the high priority group are serviced, the interrupts in the low priority group
are serviced (once again priority among them decided according to reset level priority). This is
illustrated with below example.
Example 5. Consider that IP is loaded with 15 H and the interrupts INT1, TF0, TI and TF1 have occurred
at the same time. Explain the sequence of ISRs that 8051 will execute.
Solution
IP = 0001 0101 implies the bits PS, PX1 and PX0 which are 1 makes the corresponding interrupts of
higher priority. Hence the high priority group consists of the 3 interrupts in the priority levels INT0, INT1
and serial interrupt (RI/TI). The remaining interrupts (priority bits = 0) are of low priority in the order
TF0, TF1. Now of the 4 interrupts INT1, TF0, TF1 and TI occurring simultaneously the high priority
interrupts are serviced first and then low priority levels. The new priority order is shown below.
MADHURA G