Professional Documents
Culture Documents
Embedded C
Embedded C
Interrupt-service routines are specified by the ‘interrupt’ function attribute. For example, the external
interrupt 0 ISR can be defined as,
}
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
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)
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.
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
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.
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.
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
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.
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:
Basic Operation: