Embedded C Module 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

EMBEDDED CONTROLLERS(17EI43)

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.

Block diagram of the setup used to count number of objects/ people

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)

The case of GATE bit =1 in TMOD register

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.

Half and Full Duplex transmission.

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.

Serial data transfer


In RS232, a 1 is represented by -3 to -25 V, while a 0-bit is +3 to +25 V. Whereas almost all
microprocessors/microcontrollers are TTL compatible, i.e., a 0-bit is represented by 0V & a ‘1’ bit
is represented by +5V. Hence to interface any RS232 to a microcontroller, an intermediate voltage
converter (such as MAX 232, MAX 233) are used (to convert TTL logic levelsto the RS232
voltage levels and vice-versa).

MADHURA G
EMBEDDED CONTROLLERS(17EI43)

8051 connection to RS232

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.

Basic concepts in serial I/O


(Synchronous vs. Asynchronous transmission
In synchronous format, a receiver and transmitter are synchronized i.e. a block of characters is
transmitted along with the synchronization information as shown in Fig.. This is generally used
for high-speed transmission (more than 20K bits/second).

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)

format is generally used in low speed transmission.

Special function registers of serial port


Special function registers associated with serial port in 8051 are reviewed below.
SCON (98H) – serial control register used to program the start, stop & databits
SBUF (99H) – 8bit register used to transmit and receive serial data.
TMOD (89H) and TCON (88H) – mode and control register associated with timers used for
initializing timer 1 for generating the baud rate.
IP (88H) and IE (A8H) used when programming serial port using interrupts

Review of working of serial port


1. The clock for the serial port to generate the required baud rate of serial data transfer/reception
is generally provided by timer 1 working in mode 2 (auto reload).
2. The SCON register is loaded with the required serial mode, start, stop bits, etc.
3. To serially transmit the data, the data is written to SBUF register.
4. As soon as data is moved to SBUF it is framed with the START and STOP bits and the data
frame (START BIT + 8/9-BITS OF DATA + STOP BIT) are transmitted one bit at a time on the
TxD pin at the initialized baud rate.
5. Once the serial data transmission is complete, the TI (transmit interrupt flag) is set indicating
that the user can write the next data into SBUF.
6. To receive a data serially, the SCON register has the REN BIT enabled, and the required baud
rate is set using Timer 1, mode 2.
7. The 8051 keeps checking the RI bit. The serial data on the RxD pin is loaded into the SBUF
register, and once a complete byte is received, the RI flag is made high. Once the RI flag is high,
the 8051 reads the value of SBUF.

Serial Port Control (SCON) register


The SCON SFR allows us to configure the serial port. Its address is 98H and it is bit addressable.

MADHURA G
EMBEDDED CONTROLLERS(17EI43)

SM0 SM1 – are the serial port mode specifiers.


SM2 – Generally make it ‘0’. It is used for multiprocessor communication feature in modes 2 and
3.
REN – 1/0→Enable/Disable reception = 1 for receiving serial data.
TB8/RB8 – 9th bit of data to be transmitted/received in modes 2 and 3.
TI – Transmit interrupt flag – Set by the hardware at the transmission of stop bit (8th bit in mode
0) indicating the end of transmission of the current byte in SBUF register. Must be cleared by
software.
RI – Receive interrupt flag – Set by hardware at the end of receiving stop bit (or 8th bit in mode
0). It indicates the completion of reception of one full byte in SBUF register. Must be cleared by
software.

Procedure to program the 8051 to transfer data serially


The following steps give the steps to program 8051 for serial data transfer.
1. Initialize TMOD with 20 H, to make use of timer 1 in mode 2 (auto reload) to set the baud rate.
2. Load the initial value into TH1 for the required baud rate.
3. Initialize SCON register, generally 50H for serial mode 1, 8-bit data, start and stop bits.
4. Start timer (TR1 = 1).
5. Clear TI flag.
6. Move the 8-bit data to be transmitted serially into SBUF register
7. Wait until TI flag is set (remember TI flag is set after the contents of SBUF register, along with
START and STOP bits have been transmitted serially on Tx pin.
8. For transferring another 8-bit data (character), repeat from step 5.

Procedure to program the 8051 to transfer data serially


The following steps give the steps to program 8051 for serial data transfer.
1. Initialize TMOD with 20 H, to make use of timer 1 in mode 2 (auto reload) to set the baud rate.
2. Load the initial value into TH1 for the required baud rate.
3. Initialize SCON register, generally 50H for serial mode 1, 8-bit data, start and stop bits.
4. Start timer (TR1 = 1).
5. Clear TI flag.
6. Move the 8-bit data to be transmitted serially into SBUF register.
7. Wait until TI flag is set (remember TI flag is set after the contents of SBUF register, along with START
and STOP bits have been transmitted serially on Tx pin.
8. For transferring another 8-bit data (character), repeat from step 5.

MADHURA G
EMBEDDED CONTROLLERS(17EI43)

Generation of Baud Rate

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.

Ex 1: Write a program to transfer a letter ‘H’ serially at 9600 baud continuously.

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)

Procedure for programming the 8051 to receive data serially


All the above programs involved transmission of serial data. In this section we deal with the
reception of character bytes serially. The following steps are needed.
1 Load TMOD register with 20H (for Timer 1, in mode 2) to generate serial clock.
2 Load initial value in TH1 for the required baud rate.
3 Load SCON with 50H (for serial mode 1, 8-bit data, start & stop bits and receive enable turned
on)
4 Start timer 1 (TR1 = 1).
5 Clear RI flag (to enable reception).
6 Wait till RI flag goes high. RI = 1 when a character is received completely in SBUF.
7 Read SBUF for the received character.
8 Repeat from step 5 for receiving next character

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.

Advantages of interrupt method over polling method

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).

Interrupt Vector Table

MADHURA G
EMBEDDED CONTROLLERS(17EI43)

Execution of an interrupt by 8051 (On the receipt of interrupt request)


1 The 8051 finishes the current instruction it is executing and saves the address of the next instruction
(present in PC) on the stack (remember the 8051 should come back to the main program after servicing the
interrupt).
2 It also saves the current status of all the interrupts internally (optional, not on the stack).
3 It jumps to specified location in the interrupt vector table to execute the ISR.
4 After executing the ISR, the last instruction of ISR which is RETI (return from interrupt) will get the
stored address from the stack into PC and hence continues with the main program.

Difference between RET and RETI:


Both return the control to the calling program (generally main program) by popping off the top two bytes
of the stack into the program counter. RETI has the extra job of clearing the interrupt flags (TF1/TF0 for
timers and IE0/IE1 for external edge triggered interrupts). So no need to use the instruction CLR TF1, etc.
before coming out of the ISR. If an ordinary RET is used in place of RETI, then CLR TF1/0, etc. the
corresponding clear instruction should be used, else the ISR will be called again as flags are still high.
Interrupts in 8051 have the option of being enabled or disabled (masked). On reset, all interrupts are masked.
The interrupts are enabled using the IE register.

Interrupt Enable (IE) SFR

D7 D6 D5 D4 D3 D2 D1 D0

EA - ET2 ES ET1 EX1 ET0 EX0

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 1 ms delay during ON time

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

ISR (at 001BH)


1 jump to ‘serve’ subroutine.
Serve Subroutine
1 Stop timer
2 If port pin is high then load initial value for OFF time into TH1 & TL1 and
make port low.
3 Else (if port pin is low), load initial values for ON time into TH1 and TL1
and make port pin high
4 Start timer
5 Return from interrupt.

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.

Timer 0 initial value initialization of 5kHz in mode 2

Timer 1 initial value initialization of 25kHz in mode 2

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.

ISR (Suboroutine) for timer 0 (at 000BH)


Toggle port pin P1.3 & return to main program.
ISR (Subroutine) for timer 1 (at 001BH)
Toggle port pin P2.3 and return to main program.

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)

Priority Implementation for 8051 interrupts


Upon reset (i.e., as soon as power is applied to 8051), all interrupts are disabled. The interrupts are
enabled by software using the IE register as shown in above sections.

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.

Changing interrupt priority


In the 8051 using IP register the priority structure at reset can be changed. The IP register is elaborated as
shown below.

Interrupt Priority (IP) SFR


The priority of the interrupts is determined by the bits of Interrupt Priority (IP) SFR. Bits which are set
to 1, have a high priority, and bits with 0 have a lower priority. Two or more interrupts with same
priority, will have ranking as shown earlier. Interrupts with high priority

• PX0, PX1 are priority bits assigned to INT0 and INT1


• PT0, PT1 are priority bits assigned to Timer 0 and Timer 1
• PS is priority bit assigned to the serial port interrupts.

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.

(1) INT1 ISR


(2) TI ISR
(3) TF0 ISR
(4) TF1 ISR.

MADHURA G

You might also like