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

The port of the 8051 microcontroller i) CLR A: Clears the accumulator (A Write 8051 C program to generate

that requires external pull-up resistors register) in the 8051 microcontrollers, square wave with 2500 Hz frequency
is Port 0 (P0). setting all bits to 0. on pin 2.7. Use Timer 1 in mode 2.
In the 8051-microcontroller architecture, ii) NOP: No operation instruction, which Assume XTAL frequency = 12 MHz
the registers used as data pointers in does nothing and allows the #include <reg51.h>
indirect addressing mode are DPTR microcontroller to proceed to the next // Define XTAL frequency
(Data Pointer). DPTR is a 16-bit register. instruction. #define XTAL_FREQ 12000000UL
What extension is used to save ‘C’ iii) RR A: Right rotates the bits in the void main () {
Language program. In the context of accumulator through the carry flag, // Configure Timer 1 in mode 2
Microcontroller Architecture and shifting in the carry bit from the right. TMOD = 0x20;
Programming, 'C' language programs are iv) SUBB A, #05H: Subtracts the // Calculate Timer 1 reload value for 2500
typically saved with a ".c" file extension. immediate value 05H from the Hz square wave
This extension is used to indicate that the accumulator A with borrow. // Reload Value = (2^16 - (XTAL_FREQ
file contains source code written in the C v) SWAP A: Swaps the nibbles (4-bit / (2 * desired frequency)))
programming language. For example, a C groups) within the accumulator A, // In this case, desired frequency =
program file might be named something effectively reversing their order. 2500 Hz
like "program.c." The internal RAM of the 8051 TH1 = 0xFD;
In the 8051 microcontrollers, the timer microcontroller is organized into four TL1 = 0xFD;
register that is bit-addressable is the distinct banks, each of 128 bytes. The // Set P2.7 as output
TCON (Timer Control) register. RAM addresses are from 00H to 7FH, P2 = P2 & 0x7F;
The step angle of a stepper motor is and each bank is associated with a // Start Timer 1
the angular displacement produced by specific range of addresses: TR1 = 1;
one step of the motor, expressed in Bank 0 (00H - 7FH): This is the general- while (1) {
degrees or radians. purpose RAM accessible by all // Toggle P2.7 to generate square wave
In a half-duplex, data is transmitted in instructions. The first 32 bytes (00H - P2_7 = ~P2_7;
only one direction at a time-comment. 1FH) are also bit-addressable. }
In half-duplex communication, data is sent Bank 1 (80H - FFH): Special function }
in one direction at a time, allowing for registers (SFRs) are located in this bank. This 8051 C program configures Timer 1
efficient use of resources, but it can cause These registers control various functions in mode 2 to generate a square wave with
delays as devices take turns transmitting. of the microcontroller, such as timers, a frequency of 2500 Hz on pin 2.7. The
To interface external 16KB RAM with an serial communication, and interrupt XTAL frequency is assumed to be 12
8051 microcontroller, connect the address control. MHz the calculation for the Timer 1 reload
lines A0-A13 and data lines D0-D7 of the Bank 2 (00H - 7FH): Similar to Bank 0, value is based on the formula for mode 2
microcontroller to the RAM. Use the this is another block of general-purpose and the desired frequency. The program
PSEN (Program Store Enable) signal for RAM. Accessing Bank 2 is done using the then continuously toggles the state of pin
enabling the RAM's chip select, and PSW (Program Status Word) register. 2.7 to produce the square wave.
connect RD (Read) and WR (Write) Bank 3 (80H - FFH): This bank is bit- brief explanation of the classification
signals to control data transfer. addressable, allowing manipulation of of 8051 instructions:
Communication Direction: individual bits in registers. It is accessed Data Transfer Instructions:
Simplex: In simplex communication, data through the PSW register. Understanding MOV: Moves data from one
flows in only one direction, either from the and utilizing the organization of internal register/memory location to another.
transmitter to the receiver or vice versa. RAM is crucial for effective programming Arithmetic Instructions:
Full Duplex: Full duplex communication and resource management on the 8051 ADD, SUB, INC, DEC: Perform arithmetic
allows data to flow in both directions microcontrollers. operations on data in the accumulator or
simultaneously. It supports two-way Interrupts in the 8051 microcontrollers registers.
communication, similar to a telephone play a crucial role in handling external Logical Instructions:
conversation. events and enhancing program efficiency. ANL, ORL, XRL: Perform bitwise logical
Example: Here are key points for interrupts: operations between accumulator and
Simplex: A typical example of simplex Types of Interrupts: register/memory.
communication is a one-way radio The 8051 microcontroller supports two Bit-Level Instructions:
broadcast, where the transmitter sends types of interrupts: External Hardware SETB, CLR, CPL: Set, clear, or
information, but there is no capability for Interrupts (INT0, INT1) and Internal Timer complement individual bits in registers or
the receiver to send data back. Interrupts (Timer 0 and Timer 1). the internal RAM.
Full Duplex: An example of full duplex Interrupt Vector Addresses: Branching Instructions:
communication is a telephone call, where Each type of interrupt has a specific JMP, JZ, JNZ, JC, JNC: Control the
both parties can speak and listen at the interrupt vector address where the program flow by changing the sequence
same time, enabling a two-way microcontroller jumps to when the of instructions based on certain
conversation. interrupt occurs. These addresses are conditions.
fixed and known in advance. These categories cover the fundamental
Understanding interrupts is essential for operations that the 8051 microcontrollers
handling time-sensitive tasks or can perform, including data manipulation,
responding to external events without arithmetic calculations, logical operations,
continuously polling for them, thereby bit-level manipulations, and control flow.
improving the overall efficiency of the Understanding these instruction types is
microcontroller. essential for effective programming on the
8051 architecture.
Write a C program for LCD interface // Display 'ELECTRONICS' Timer Mode 2 in the 8051
to 8051 for displaying ‘ELECTRONICS’ lcd_data('E'); microcontroller is an 8-bit
#include <reg51.h> lcd_data('L'); timer/counter with auto-reload
// Function to provide a delay lcd_data('E'); capability. Here are key points for
void delay (unsigned int count) { lcd_data('C'); Timer Mode 2:
unsigned int i, j; lcd_data('T'); Auto-Reload:
for (i = 0; i < count; i++) lcd_data('R'); Timer Mode 2 operates in auto-
for (j = 0; j < 1275; j++); lcd_data('O'); reload mode, where it automatically
} lcd_data('N'); reloads the initial value from TH2 and
// Function to send a command to lcd_data('I'); TL2 into the timer registers (RCAP2H
the LCD lcd_data('C'); and RCAP2L) after overflow.
void lcd_command(unsigned char lcd_data('S'); Clock Source:
command) { while (1) { The timer in Mode 2 uses the percale
// RS=0 for command // Infinite loop and the external input (TCLK) to
RS = 0; } generate clock pulses. The percale
// RW=0 for write } divides the system clock to
RW = 0; Addressing Range: determine the input frequency for
// Send the command to the LCD LCALL: LCALL has a broader the timer. The timer increments on
LCD_DATA = command; addressing range and can call each pulse.
// Enable pulse subroutines from anywhere in the 64 Understanding Timer Mode 2 is
EN = 1; KB program memory space. It uses a essential for applications requiring
delay(1); 16-bit address. precise timing, as it provides a
EN = 0; ACALL: ACALL is more limited, meant convenient auto-reload feature for
} for calling subroutines within the continuous timing operations.
// Function to send data to the LCD same 2 KB page of program memory. Immediate addressing mode in
void lcd_data(unsigned char dat) { It utilizes an 11-bit address. microcontroller programming
// RS=1 for data Instruction Size: involves specifying the operand
RS = 1; LCALL: The LCALL instruction is 3 directly within the instruction. Here
// RW=0 for write bytes long, as it requires more are key points for immediate
RW = 0; memory to store the 16-bit address addressing:
// Send the data to the LCD of the subroutine. Operand Specification:
LCD_DATA = dat; ACALL: ACALL is a more compact 2- In immediate addressing mode, the
// Enable pulse byte instruction, utilizing an 11-bit actual data or operand is provided in
EN = 1; address and taking up less memory the instruction itself.
delay(1); space. Example:
EN = 0; The 8051 microcontroller has four For example, in the instruction MOV
} parallel I/O ports, namely P0, P1, P2, A, #35H, the value 35H is the
// Function to initialize the LCD and P3. General-Purpose I/O: P0, P2, immediate data, and it is directly
void lcd_init() { P3: These ports are general-purpose moved to the accumulator A.
// Function set: 8-bit mode, 2 lines, I/O ports, and each of them is 8 bits Understanding immediate addressing
5x7 font wide. P0 is often used for external is important for handling constant
lcd_command(0x38); data bus connections, P2 is data or values directly within the
// Display on, cursor off, blinking commonly used for interfacing with instruction, providing a quick and
off external memory, and P3 is direct way to work with specific
lcd_command(0x0C); frequently employed for special numerical values in the program.
// Entry mode set: Increment functions like interrupts.
cursor, no display shift Special Function I/O: P1: Port P1 is
lcd_command(0x06); also 8 bits wide but has additional
} functions associated with it. It can be
void main() { configured as a general-purpose I/O
// Initialize LCD port or used for special functions like
lcd_init(); Timer 2 external count input and
external interrupt input.

You might also like