Micro Control 4th Sem Notes
Micro Control 4th Sem Notes
DPTR IS 16-bit Register. It is made up of two 8-bit registers called DPH and DPL. Separate
addresses are assigned to each of DPH and DPL. These 8-bit registers are used for the
storing the memory addresses that can be used to access internal and external data/code
DPL (Data Pointer Low): Holds the lower 8 bits of the 16-bit address
DPH (Data Pointer High): Holds the higher 8 bits of the 16-bit address.
Bit 7
Bit Bit Bit Bit Bit Bit Bit
6 5 4 3 2 1 0
SM0 SM1 SM2 REN TB8 RB8 TI RI
Bit 7: SM0 (Serial Mode Bit 0) - Mode selection.
Bit 6: SM1 (Serial Mode Bit 1) - Mode selection.
Bit 5: SM2 (Serial Mode 2 Bit) - Multiprocessor communication.
Bit 4: REN (Receive Enable Bit) - Enables serial reception.
Bit 3: TB8 (Transmit Bit 8) - 9th bit of data to transmit.
Bit 2: RB8 (Receive Bit 8) - 9th bit of received data.
Bit 1: TI (Transmit Interrupt Flag) - Set when transmission is complete.
Bit 0: RI (Receive Interrupt Flag) - Set when data is received.
8. Timer Mode (TMOD) Register ; TMOD is an 8-bit register used to set timer mode of timer0
and timer1.
Bit 7 Bit 6
Bit Bit Bit 3 Bit 2 Bit Bit
5 4 1 0
GATE C1/T1 M1 M0 GATE C1/T0 M1 M0
TIM1 TIM0 TOM1 TOM0
Its lower 4 bits are used for Timer0 and the upper 4 bits are used for Timer1
Bit 7,3 – GATE:
1 = Enable Timer/Counter only when the INT0/INT1 pin is high and TR0/TR1 is set.
0 = Enable Timer/Counter when TR0/TR1 is set.
Bit 6,2 - C/(Counter/Timer): Timer or Counter select bit
If C/T = 0: The timer operates as a timer, incrementing with the internal clock.
If C/T = 1: The timer operates as a counter, incrementing with external pulses on T0 or T1 pins.
Bit 5:4 & 1:0 - M1:M0: Timer/Counter mode select bit
M1, M0 (Mode Selection): These bits define the mode of the timer. There are four modes:
M1 M0 Mode
0 0 Mode 0: 13 bit mode (seldom used).
0 1 Mode 1: 16-bit mode
1 0 Mode 2: 8-bit mode (with auto-reload feature)
1 1 Mode 3: 8-bit split timer
Default Behavior:
All interrupts are initially set to low priority (0) by default.
The 8051 services interrupts based on the natural priority order if all are low-priority:
o External INT0
o Timer 0
Bit Bit Bit Bit Bit Bit Bit Bit
o External INT1
7 6 5 4 3 2 1 0
o Timer 1
o Serial
PS PT1 PT0 PX1 PX0 - - -
Assigning Priorities:
A bit set to 1 in the IP register assigns a high priority to the corresponding interrupt.
If multiple interrupts occur:
o High-priority interrupts are serviced first.
o Among interrupts with the same priority level, the natural priority order is followed.
High vs Low Priority:
High-priority interrupts can interrupt an ISR (Interrupt Service Routine) for a low-
priority interrupt.
Low-priority interrupts cannot interrupt an ISR of a high-priority interrupt.
Stack Pointer (SP) Register
Bit Bit Bit Bit Bit Bit Bit Bit
7 6 5 4 3 2 1 0
D D D D D D D D
Bits 7-0: Address of the top of the stack.
Functions of the Stack Pointer (SP)
The Stack Pointer (SP) is a special-purpose register in a microcontroller or processor that
keeps track of the top of the stack in memory. It plays a crucial role in stack operations, which are
fundamental for managing function calls, local variables, and interrupt handling.
Stack Operations:
o Push Operation: When a new item is pushed onto the stack, the SP is decremented (in a
stack that grows downwards) or incremented (in a stack that grows upwards), and the item
is written to the memory location pointed to by the SP.
o Pop Operation: When an item is popped from the stack, it is read from the memory
location pointed to by the SP, and then the SP is incremented (or decremented) to reflect
the removal.
#include <reg51.h> // Function to initialize the LCD
Void lcd_init()
// define LCD control pins {
sbit RS = P2^0; lcd_command(0x02); // Initialize in 4-bit
sbit E = P2^1; mode
sbit D4 = P2^4; lcd_command(0x28); // 2 line, 5x7 matrix
sbit D5 = P2^5; lcd_command(0x0C); // Display on, cursor
sbit D6 = P2^6; off
sbit D7 = P2^7; lcd_command(0x06); // Increment cursor
lcd_command(0x01); // Clear display
// Function to provide delay delay(2);
Void delay (unsigned int time) }
{ // Function to send a string to the LCD
Unsigned int i, j; Void lcd_string(char *str)
For (i = 0; i < time; i++) {
For (j = 0; j < 1275; j++); While (*str) {
} lcd_data(*str++);
// Function to send a command to the }
LCD }
Void lcd_command(unsigned char Void main()
cmd) {
{ lcd_init();
RS = 0; // Command mode // Initialize the LCD
D4 = (cmd & 0x10) >> 4; lcd_command(0x80); // Move the cursor to
D5 = (cmd & 0x20) >> 5; the first line
D6 = (cmd & 0x40) >> 6; lcd_string("Hello, World!");
D7 = (cmd & 0x80) >> 7; lcd_command(0xC0); // Move the cursor to
E = 1; the second line
delay (1); lcd_string("LCD Interface");
E = 0;
while(1); // Infinite loop to keep the display
D4 = cmd & 0x01; on
D5 = (cmd & 0x02) >> 1; }
D6 = (cmd & 0x04) >> 2;
D7 = (cmd & 0x08) >> 3;
E = 1;
delay (1);
E = 0;
}
Components Needed:
1. 8051 Microcontroller Stepper Motor potentiometer (for speed control)
2. ULN2003A or any other suitable motor driver IC
Basic Concept:
Stepper Motor: A stepper motor moves in discrete steps, with the rotation direction
controlled by the sequence in which the coils are energized.
ULN2003A Driver: The 8051 microcontroller cannot directly drive a stepper motor due
to its current and voltage limitations, so a driver like ULN2003A is used.
Speed Control: This can be achieved by adjusting the delay between steps.
Direction Control: Changing the sequence of the pulses to the motor coils will reverse the
direction.
Connection Setup:
1. Connect the stepper motor to the ULN2003A driver IC.
2. Connect the inputs of the ULN2003A to four output pins of the 8051 (P1.0 to P1.3).
3. Use two buttons for direction control (one for clockwise and another for counterclockwise).
4. Optionally, use a potentiometer connected to an ADC pin for speed control.
#include <reg51.h> }
}
// Define the connections to the
motor driver // Function to move stepper motor
sbit IN1 = P1^0; counter-clockwise
sbit IN2 = P1^1; void
sbit IN3 = P1^2; rotate_counter_clockwise(unsigned
sbit IN4 = P1^3; int speed)
{
// Define buttons for direction IN1 = 0; IN2 = 0; IN3 = 0; IN4 = 1;
control delay(speed);
sbit CW_BUTTON = P2^0; // IN1 = 0; IN2 = 0; IN3 = 1; IN4 = 0;
Clockwise button delay(speed);
sbit CCW_BUTTON = P2^1; // IN1 = 0; IN2 = 1; IN3 = 0; IN4 = 0;
Counter-Clockwise button delay(speed);
IN1 = 1; IN2 = 0; IN3 = 0; IN4 = 0;
// Function to provide delay delay(speed);
void delay(unsigned int ms) { }
unsigned int i, j; void main() {
for(i=0; i<ms; i++) unsigned int speed = 5; // Adjust this
for(j=0; j<1275; j++); value for speed control
}
// Function to move stepper motor while(1) {
clockwise // Check for direction control
void rotate_clockwise(unsigned if(CW_BUTTON == 0) { // Button
int speed) pressed for clockwise rotation
{ rotate_clockwise(speed);
IN1 = 1; IN2 = 0; IN3 = 0; IN4 = 0; }
delay(speed); if(CCW_BUTTON == 0) { // Button
IN1 = 0; IN2 = 1; IN3 = 0; IN4 = 0; pressed for counter-clockwise rotation
delay(speed); rotate_counter_clockwise(speed);
IN1 = 0; IN2 = 0; IN3 = 1; IN4 = 0; }
delay(speed); }
IN1 = 0; IN2 = 0; IN3 = 0; IN4 = 1;
Stepper motor interface for direction and
delay(speed); speed control.
Calculate the memory map of AT89C51 microcontroller with 4 KB of on chip ROM
AT89C51 Memory Map with 4KB On-Chip ROM
The AT89C51 microcontroller has a specific memory map that defines how its memory is
organized. Here's a breakdown of the memory map, considering a 4KB on-chip ROM:
Program Memory (ROM):
Size: 4KB (4096 bytes)
Address Range: 0000H to 0FFFh 0000H - 0FFFh: Program Memory
Purpose: Stores the program instructions. (ROM)
Data Memory (RAM): 00H - 7FH: Data Memory (RAM)
Size: 128 bytes 80H - FFH: Special Function
Address Range: 00H to 7FH Registers (SFRs)
Purpose: Stores temporary data and variables.
Special Function Registers (SFRs):
Size: 128 bytes
Address Range: 80H to FFH
Purpose: Controls the operation of the microcontroller's peripherals, timers, serial port,
and other components.
XTAL=22MHz, calculate the TH1 value for the following baud rates(i)1200 ii)2400
we need to consider the following formula:
TH1 = (256 - (XTAL / 12 / 32 / baud rate))
Where:
TH1 is the value to be loaded into the TH1 register.
XTAL is the crystal frequency (22 MHz in this case).
Baud rate is the desired baud rate.
Calculation for Baud Rate 1200: Calculation for Baud Rate 2400:
TH1 = (256 - (22000000 / 12 / 32 / 1200)) TH1 = (256 - (22000000 / 12 / 32 / 2400))
TH1 = (256 - 120.83) TH1 = (256 - 60.42)
TH1 ≈ 135 TH1 ≈ 195
Microcontroller
All-in-one: Integrates a CPU, memory (RAM and ROM), input/output ports, and
peripherals onto a single chip.
Dedicated Tasks: Designed for specific tasks or embedded systems, such as controlling
appliances, automotive systems, and industrial automation.
Lower Processing Power: Generally less powerful than microprocessors.
Lower Cost: Due to their simpler architecture and mass production, they are often more
cost-effective.
Examples: 8051, Arduino, Raspberry Pi Pico
Microprocessor
CPU Only: Consists primarily of a CPU, requiring external components like memory and
I/O devices.
General-purpose: More flexible and can be used for a wide range of applications,
including personal computers, servers, and smartphones.
Higher Processing Power: Offers higher processing power and performance compared
to microcontrollers.
Higher Cost: More complex architecture and additional components contribute to a
higher cost.
Examples: Intel Core i7, AMD Ryzen, ARM Cortex-A series
Using DAC interface, write a Draw the interfacing diagram of 0808 ADC with
program to generate staircase microcontroller and write a program to interface
waveform. ADC . Assume Channel 3 to read the analog data.
Assume number of steps to be
5. ; Define port assignments (adjust as needed)
ALE EQU P3.3
SC EQU P3.4
; Initialize P2 as output port EOC EQU P3.5
MOV P2, #00H ADDA EQU P3.0
ADDB EQU P3.1
; Define the step size (255/5 = ADDC EQU P3.2
51)
MOV R0, #51H ; Initialize port directions
; Loop to generate the staircase MOV P3, #00H ; Set P3 as output port
waveform
LOOP: ; Select channel 3 (binary 011)
MOV A, #00H MOV ADDA, #0
MOV R1, #5 ; Number of steps MOV ADDB, #1
MOV ADDC, #1
STEP:
ADD A, R0 ; Start conversion
MOV P2, A MOV ALE, #1 ; Latch address
ACALL DELAY ; Call delay MOV SC, #1 ; Start conversion
subroutine MOV ALE, #0 ; Clear ALE
DJNZ R1, STEP
; Wait for conversion to complete
SJMP LOOP ; Repeat the LOOP:
process MOV C, EOC
JC LOOP ; Jump if conversion is not complete
; Delay subroutine
DELAY: ; Read the result
MOV R2, #250 ; Delay loop MOV P1, #0FF ; Set P1 as input port
counter MOV R0, P1 ; Store the 8-bit result in R0
DLY1:
MOV R3, #250 ; You can now use R0 to process the digital value
DLY2:
DJNZ R3, DLY2
DJNZ R2, DLY1
RET
Circuit of “Interfacing ADC0808
with 8051” is little complex which
contains more connecting wire for
connecting device to each other. In this
circuit we have mainly used AT89s52 as
8051 microcontroller, ADC0808,
Potentiometer and LCD.
*A 16x2 LCD is connected with 89s52
microcontroller in 4-bit mode. Control
pin RS, RW and En are directly
connected to pin P2.0, GND and P2.2.
And data pin D4-D7 is connected to pins
P2.4, P2.5, P2.6 and P2.7 of 89s52.
ADC0808 output pin are directly connected to port P1 of AT89s52. Address line pins ADDA, ADDB,
AADC are connected at P3.0, P3.1, and P3.2.
*ALE (Address latch enable), SC (Start conversion), EOC (End of conversion), OE (Output enable)
and clock pins are connected at P3.3, P3.4, P3.5, P3.6 and P3.7.
*And here we have used three potentiometers connected at pin 26, 27, and 28 of ADC0808.
*A 9 volt battery and a 5 volt voltage regulator namely 7805 are used for powering the circuit.
Explain the difference between unsigned and signed char and 𝒊𝒏𝒕 declarations in
8051 C. Give examples.
Unsigned Char: Use for storing small, non-negative values, such as counters, indexes, or
ASCII characters.
Signed Char: Use for storing small integer values that can be both positive and negative.
𝑰𝒏𝒕: Use for storing larger integer values, especially when the range of values exceeds that
of a signed char.
Data Type Range Storage Sign Examples.
unsigned char 0 to 255 8 bits Unsigned unsigned char a = 100;
signed char -128 to 127 8 bits Signed signed char b = -50;
int -32768 to 32767 16 bits Signed int c = 1000;
Interfacing MAX1112 with 8051
The MAX1112 is a serial ADC that offers 8 single-ended input channels. It's a versatile choice for
interfacing with 8051 microcontrollers due to its simplicity and ease of use.
Key Connections:
1. Power Supply: Connect Vcc
and GND pins of both MAX1112
and 8051 to a common 5V supply.
2. Serial Clock (SCLK): Connect
the SCLK pin of MAX1112 to a
suitable output pin of the 8051
(e.g., P1.0).
3. Serial Data Input (DIN):
Connect the DIN pin of MAX1112
to a suitable output pin of the
8051 (e.g., P1.1).
4. Serial Data Output (DOUT):
Connect the DOUT pin of
MAX1112 to a suitable input pin
of the 8051 (e.g., P1.2).
5. Chip Select (CS): Connect the CS pin of MAX1112 to a suitable output pin of the 8051
(e.g., P1.3).