Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

#define LED_PORT P1; // P1 is accessed using name LED_PORT

#define INPUT_SWITCH P2; // P2 is accessed using name INPUT_SWITCH

Interrupt-service routines are specified by the ‘interrupt’ function attribute. For example, the external
interrupt 0 ISR can be defined as,

void ext0 (void) interrupt 0


{
// body of the interrupt service routine

And timer0 ISR can be defined as


void timer0 (void) interrupt 1
{
// body of the interrupt service routine

}
Constant Value
Decimal is the default number format
int m, n; //16-bit signed numbers
m = 453; n = -25;
Hexadecimal: preface value with 0x or 0X
m = 0xF312; n = -0x12E4;
Octal: preface value with zero (0)
m = 0453; n = -023;
Don’t use leading zeros on “decimal” values. They will be interpreted as octal. Character: character in
single quotes, or ASCII value following “slash”
m = ‘a’; //ASCII value 0x61
n = ‘\13’; //ASCII value 13 is the “return” character

The Super-Loop or infinite loop (while (1) – with no break statement)


If you are familiar with computer programming using C language you must know that the main ()
function of a computer program must end with a return statement. But it is not same for embedded C.
A computer program, after its execution, must return the control to the operating system. But a simple
embedded system doesn’t have an operating system. The single program that we write controls the
embedded system entirely. So, we must write the body of the main program (excluding the variable
declarations and initializations) inside a super-loop or infinite loop.
The program keeps on running until it is reset or turned off by a user. If the chip is the main ()
function will start from the beginning and do all the variable declarations and initializations again. If
the embedded system you want to design is intended to perform a single task and then stop until it is
restarted (or reset) an empty super-loop should be written at the end.
while (1)
{
}

Shift operators
Shift operators:
x >> y (right shift operand x by y bit positions)
x << y (left shift operand x by y bit positions)
Vacated bits are filled with 0’s.
Shift right/left fast way to multiply/divide by power of 2
B = A << 3; A 1 0 1 0 1 1 0 1
(Left shift 3 bits) B 0 1 1 0 1 0 0 0
B = A >> 2; A 1 0 1 1 0 1 0 1
(Right shift 2 bits) B 0 0 1 0 1 1 0 1
B = ‘1’; B = 0 0 1 1 0 0 0 1 (ASCII 0x31)
C = ‘5’; C = 0 0 1 1 0 1 0 1 (ASCII 0x35)
D = (B << 4) | (C & 0x0F);
(B << 4) = 0 0 0 1 0 0 0 0
(C & 0x0F) = 0 0 0 0 0 1 0 1
D = 0 0 0 1 0 1 0 1 (Packed BCD 0x15)

Test TRUE condition Notes Test TRUE condition Notes


(m == b) m equal to b Double =
(m! = b) m not equal to b
(m < b) m less than b 1
(m <= b) m less than or equal to b 1
(m > b) (m > b) m greater than b 1
(m >= b) (m >= b) m greater than or equal to b 1
(m) (m)

8051 Embedded C Example 1


Aim:
To write a program to blink an LED after every 0.5 second using timer. Assume that a 12 MHz crystal is used generate
clock pulses the 8051 microcontrollers.
Calculation of Timer Value:
Clock frequency = 12 MHz = 12,000,000 Hz Time required for 1 clock pulse = 1/12,000,0000 sec 1 machine cycle = 12
clock pulses Hence, time required for 1 machine cycle = 12 x 1/12,000,000 sec = 10
–6
sec In other words, Machine cycles in 10
–6
sec = 1 Machine cycles 0.5 sec = 0.5 x 10
6
= 500,000 A 16-bit timer can count from 0000h to FFFFh, i.e., 0 to 65,535. Let us start the timer from (65,535 – 50,000)
= 15,535 = 3CAFh so that it overflows after 50,000 steps. We need to run the timer 10 times to make it 500,000

Need of Timers

Many timing applications require the generation of accurate time delay between certain events or
counting of events happening outside the microcontroller such as counting number of clock pulses.
The subroutine that generates a time delay is commonly required in the programs. The time delay may
be generated using software loops that effectively do nothing for a specified time period. The
approach of
generating a time delay using software keeps the microcontroller busy and due to that other important
tasks may remain unattended and are not execute.

The basic functions of timers are


1. To measure the time—calculating time elapsed between events. This capability of a timer is
deployed in generating time delays as well as waveforms.
2. To count events or to measure frequency or pulse width of an unknown signal.
3. To provide the clock signal to other circuits.

How Does a Timer Work?


The microcontroller uses a crystal oscillator to generate clock pulses for its operations. The clock
pulses generated by a crystal oscillator is always of fixed time period. Counting these clock pulses is
as good as measuring the elapsed time period between events. The timer registers are incremented by
clock pulses applied to it. When a timer register reaches its maximum value; it overflows on the next
clock pulse and indicates this overflow status by setting a timer overflow flag in its status register or
raising an interrupt. This setting of the flag (or interrupt) is the mechanism used by a timer circuit to
inform the microcontroller that it has finished its work completely, and a necessary action should be
taken by the microcontroller as per requirement.

D7 D6 D5 D4 D3 D2 D1 D0
GATE C/T M1 M0 GATE C/T M1 M0
Timer 1 Timer 0

Gate: Start/stop control using hardware or software. When Gate = 0, start/stop of timer is controlled
only by TR1/0 bits; while Gate = 1, it is controlled by TR 1/0 as well as signal on INT1/0 pin
C/ T = 0 configures the timer as an interval timer (or time-delay generator), C/ T = 1 will configure
the timer as an event counter
M1 Mode select bit 1
M0 Mode select bit 0
M1 M0
0 0 Mode 0; 13-bit timer
0 1 Mode 1; 16-bit timer/counter
1 0 Mode 2; 8-bit auto reload
1 1 Mode 3; split timer mode, TL0 as 8-bit timer/counter and TH0 as 8-bit timer
controlled by control
bits of Timer 0 and Timer 1 respectively. Timer 1 operation timer/counter stopped.

M1 M0 Mode Description
0 0 Mode 0 13-bit timer
0 1 Mode 1 16-bit timer/counter
1 0 Mode 2 8-bit auto reload
1 1 Mode 3 Split timer mode

Operator Operation Example


* Multiplication x*y = 10*2 = 20
/ Division X / y = 10 / 2 = 5
+ Addition x + y = 10 + 2 =12
- Subtraction x – y = 10 - 2 =8
% Modulo x % y = 10 % 2 = 0
Need of Interrupts
An interrupt is a signal generated by an event that causes the microcontroller to stop temporarily its
current program-execution and perform the task to service that event. Interrupts are used to get
microcontroller attention towards important events. They allow the microcontroller system to respond
to the asynchronous events while another task is being executed; therefore, they give the illusion of
handling many tasks simultaneously. The asynchronous event means we do not know in advance
when they will occur.
The real-time systems based on microcontrollers must respond as fast as possible to events generated
by peripheral devices present in a system. Interrupts always provide a more efficient and effective
way to serve many devices. Also, interrupts allow most efficient utilization of time and resources of
the microcontroller.
Interrupts are generated anywhere and anytime in a program, i.e., interrupts are asynchronous events.

D7 D6 D5 D4 D3 D2 D1 D0
EA - - ES ET1 EX1 ET0 EX0

P0 (Port 0): P0 is an 8-bit bidirectional port. Port 0 does not have internal pull-ups. If pull-
ups are needed, external resistors are required. It can be used for both input and output
operations. Each bit of P0 can be individually programmed as input or output.

 Input Mode: When a bit of P0 is configured as an input, it can be used to read external
signals. The state of the pins can be read by reading the contents of the P0 register.

 Output Mode: When a bit of P0 is configured as an output, the microcontroller can drive
the pin either high or low by writing appropriate values to the P0 register.

However, in certain configurations, especially when interfacing with external memory, P0


may be used as the lower 8 bits (A0 to A7) of the address bus.

 External Memory Access: When the 8051 is configured to access external memory, P0 is
used to transmit the lower 8 bits of the memory address to the external memory devices. To
configure P0 as the address bus, you need to set the corresponding bits in the P0 register.
Additionally, you may need to configure other control registers to enable external memory
access mode.
#include <reg51.h>

void main () {
// Configure P0 as an output port
P0 = 0xFF; // Set all bits to 1 (high)

While (1) {
// Your main program logic here
}
}

P1 (Port 1): P1 is also an 8-bit bidirectional port with internal pull-ups. Like P0, each bit of
P1 can be individually programmed as input or output. Port 1 has internal pull-up resistors,
which means that when configured as inputs, the pins are pulled high by default. If external
pull-down resistors are needed, they must be added externally.

P2 (Port 2): P2 is an 8-bit bidirectional port with internal pull-ups, just like P1. Additionally,
P2 can serve as a higher-order address bus (A8 to A15) during external memory access.

P3 (Port 3): P3 is another 8-bit bidirectional port but lacks internal pull-ups. It is often used
as a special-purpose port and has additional control functions, including two external
interrupts (INT0 and INT1).

A 7-segment display is a form of electronic display device used to represent decimal numbers and
other alphanumeric characters. The term "common anode" and "common cathode" refer to how the
LEDs inside the 7-segment display are connected.

Common Anode 7-Segment Display:

In a common anode 7-segment display:

 All the anodes of the seven LEDs are tied together and connected to a common
positive voltage (VCC).
 The cathodes of each LED are connected to individual pins. To light up a specific
segment, you need to ground (connect to GND) the corresponding cathode

Common Cathode 7-Segment Display:

In a common cathode 7-segment display:


 All the cathodes of the seven LEDs are tied together and connected to a common
ground (GND).
 The anodes of each LED are connected to individual pins. To light up a specific
segment, you need to apply a positive voltage (VCC) to the corresponding anode.

The ADC0808 is an 8-bit analog-to-digital converter (ADC) integrated circuit. It is commonly used to
convert analog signals into digital form for processing by a microcontroller or other digital systems.

Key Features of ADC0808:

1. Resolution: 8-bit (provides 256 discrete digital levels for analog input).
2. Channels: It has 8 analog input channels (IN0 to IN7).
3. Conversion Time: Typically, it takes around 100 microseconds to complete a
conversion.
4. Parallel Interface: The ADC0808 has a parallel digital output interface, providing
the digital result on 8 output pins (D0 to D7).

Control Lines:

 A, B, C: These lines are used to select the input channel.


 ALE (Address Latch Enable): Used to latch the address on the rising edge.
 START: Initiates the conversion process.
 EOC (End of Conversion): Indicates when the conversion is complete.
 OE (Output Enable): Enables the output data onto the data bus.
 SC (Start Conversion): Similar to START.

Basic Operation:

1. The microcontroller selects the channel using A, B, C lines.


2. The microcontroller applies the start pulse (START) to begin the conversion.
3. The ADC0808 completes the conversion and asserts EOC.
4. The microcontroller reads the result using the data lines (D0 to D7) after the EOC
signal.

You might also like