Professional Documents
Culture Documents
Emb c Merged
Emb c Merged
MODULE - 2 2
FEATURES
8-bit CPU
64K bytes on-chip program memory (ROM)
128 bytes on-chip data memory (RAM)
32 I/O pins arranged as four 8-bit ports (P0 - P3)
32 general purpose registers each of 8-bit
Special Function Registers (SFRs) of 128 bytes
16-bit Program Counter
8-bit Processor Status Word (PSW) & Stack Pointer
Two 16-bit timer/counters : T0 and T1
Two external and three internal vectored interrupts
One full duplex serial I/O (UART)
MODULE - 2 3
8051 FAMILY SERIES
MODULE - 2 4
8051 FAMILY SERIES
Although the 8051 is the most popular member of the 8051
family, you will not see “8051″ in the part number.
MODULE - 2 6
8051 MAJOR MANUFACTURERS
ATMEL
ANALOG DEVICES
ST MICROELECTRONICS
DALLAS
MAXIM
SILICON LABS
TEXAS INSTRUMENTS
MICROCHIP
ZILOG
MODULE - 2 7
8051 ATMEL SERIES
Part Number ROM RAM I/O pins Timer Interrupt Vcc Packaging
AT89C51 4K 128 32 2 6 5V 40
AT89C52 8K 256 32 3 8 5V 40
AT89C1051 1K 64 15 1 3 3V 20
AT89C2051 2K 128 32 3 8 3V 20
AT89LV51 4K 128 32 2 6 3V 40
AT89LV52 8K 128 32 3 8 3V 40
MODULE - 2 8
8051 PIN DETAILS
Although 8051 family members (e.g., 8751, 89C51,
89C52, DS89C4xO) come in different packages, such DIP QFP
as DIP (dual in-line package), QFP (quad flat package),
and LLC (leadless chip carrier)
MODULE - 2 10
8051 PIN DETAILS
Of these pins, six (Vcc, GND, XTAL1, XTAL2, RST, and EA)
are used by all members of the 8051 and 8031 families.
The other two pins, PSEN and ALE, are used mainly in 8031-
based systems.
MODULE - 2 11
8051 SCHEMATICS
MODULE - 2 12
8051 PIN DIAGRAM
MODULE - 2 13
8051 PIN DESCRIPTION
Pin-40 : Vcc is the main power source. Usually its +5V DC.
MODULE - 2 14
Pin-31:- ALE - Address Latch Enable. It is especially used
for 8031 IC to connect it to the external memory. It can be
used while deciding whether P0 pins will be used as Address
bus or Data bus. When ALE = 1, then the P0 pins work as
Data bus and when ALE = 0, then the P0 pins act as Address
bus.
Pins 10 – 17:- Port 3 (P 3.0 to P 3.7) It is also of 8 bits and can crystal
be used as Input/Output. This port provides some extremely
important signals.
P3.6 and P3.7 are Write (WR) and Read (RD) pins.
MODULE - 2 17
Pin 9:- RESET pin is used to set the 8051 microcontroller to
its initial values, while the microcontroller is working or at
the initial start of application. The RESET pin must be set
high for 2 machine cycles.
MODULE - 2 18
8051 ARCHITECTURE (Simplified)
MODULE - 2 19
CENTRAL PROCESSING UNIT (CPU)
Central Processor Unit (CPU) is the brain of any processing
device of the microcontroller.
The User has no control over the work of the CPU directly.
Two external interrupts (INT0 & INT1), two timer (TF0 &
TF1) interrupts and one serial port (RI / TI) interrupt.
MODULE - 2 33
DATA MEMORY – REGISTER BANKS
Registers are used to store data or operands during
executions. Register banks form the lowest 32 bytes on
internal RAM memory.
MODULE - 2 41
DATA MEMORY – SFRs (Special Function
Registers)
SFRs are accessed just like normal Internal RAM
locations.
MODULE - 2 42
DATA MEMORY – SFRs
MODULE - 2 45
SFRs A- Register
MODULE - 2 46
SFRs A- Register
The most important of all special function register is
Accumulator which is also known as ACC or A.
MODULE - 2 48
SFRs B- Register
MODULE - 2 49
SFRs - PSW REGISTER
It is 8 bit register and it is bit and byte accessible.
It contains several status bits that reflects the status of the operation that is being
carried out in the processor.
It has 4 conditional flags or math flags (CY, AC, OV, P)which sets or resets
according to condition of result.
It has 3 control flags (F0, RS0, RS1) by setting or resetting bit required operation
or function can be achieved.
MODULE - 2 50
SFRs - PSW REGISTER
CY, the carry flag: This flag is set whenever there is a carry out from
the D7 bit. This flag bit is affected after an 8-bit addition or
subtraction.
F0, the Flag 0 : The PSW.5 and PSW.1 bits are general-purpose
status flag bits and can be used by the programmer for any purpose. In
other words, they are user definable.
MODULE - 2 52
SFRs - PSW REGISTER
RS0, RS1 - Register bank select bits. These two bits are used to
select one of four register banks of RAM. By setting and clearing these
bits, registers R0-R7 are stored in one of four banks of RAM.
MODULE - 2 53
SFRs - PSW REGISTER
OV, overflow flag:
This flag is set whenever the result of a signed number operation
is too large, causing the high-order bit to overflow into sign bit.
In general, the carry flag is used to detect errors in unsigned
arithmetic operations.
The overflow flag is only used to detect errors in signed
arithmetic operations
P, the parity flag: The parity flag reflects the number of 1 s in the A
(accumulator) register only. If the A register contains an odd number
of 1’s, then P = 1. Therefore, P = 0 if A has an even number of 1s.
MODULE - 2 54
SFRs - P0, P1, P2, P3 - Input/Output Registers
There are 4 ports with in total of 32 input/output pins are
available for connection to peripheral environment.
So 4 Input/Output ports named P0, P1, P2 and P3 has got
four corresponding port registers P0, P1, P2 and P3. All 4
port registers are bit as well as byte addressable.
Data must be written into port registers first to send it out
to any other external device through ports.
MODULE - 2 56
SFRs - P0, P1, P2, P3 - Input/Output Registers
MODULE - 2 57
SFRs - P0, P1, P2, P3 - Input/Output Registers
Upon reset and power-on, all port bits are set (1), which
means that all appropriate pins will be configured as inputs.
MODULE - 2 58
SFRs - DPTR REGISTER
It is a 16 bit register used to hold address of external or
internal RAM where data is stored or result is to be stored.
It can be divided into two 8-bit registers, DPH-data pointer
higher order and DPL-data pointer lower order.
Each register can be used as general purpose register to
store 8 bit data and can also be used to store memory
location.
It functions as Base register in base relative addressing
mode and indirect jump.
MODULE - 2 59
SFRs - DPTR REGISTER
MODULE - 2 60
SFRs - STACK POINTER
It is 8-bit register. It is byte addressable.
MODULE - 2 62
PC - PROGRAM COUNTER (not a part of SFRs)
MODULE - 2 63
MACHINE CYCLES
The CPU takes a certain number of clock cycles to execute
an instruction.
MODULE - 2 64
MACHINE CYCLES
Pulse - One complete oscillation of the clock source; State -
Two pulses;
1 machine cycle 8051 = six states or 12 clock pulses
MODULE - 2 65
MACHINE CYCLES
If an instruction takes one machine cycle to execute, it will
take 12 pulses of the crystal to execute.
The 8051 also has two really slow instructions that require a
full 4 cycles to execute-those instructions you’d find
performance to be about 230,395 instructions per second.
MODULE - 2 67
EXAMPLE - 1
Lets find the time period of the machine cycle in each case for the
following crystal frequency of different 8051 based systems: 11.0592
MHz, 16 MHz, 20 MHz.
Answer:
11.0592 MHz:
11.0592/12 = 921.6 KHz
Machine cycle = 1/921.6 KHz = 1.085us [us=microsecond]
16 MHz:
16MHz/12 = 1.333 MHz
Machine cycle = 1/1.333 MHz = 0.75us [us=microsecond]
20MHz:
20MHz/12 = 1.66 MHz
Machine Cycle = 1/1.66 MHz = 0.60us [us=microsecond]
MODULE - 2 68
EXAMPLE - 2
Lets find how long it takes to execute each of the following
instructions, for a crystal frequency of 11.0592 MHz. The machine
cycle of a system of 11.0592.z is 1.085 us
INSTRUCTION MACHINE CYCLE TIME TO EXECUTE
MOV R2,#55H 1 1x1.085 us = 1.085 us
DEC R2 1 1x1.085 us = 1.085 us
DJNZ R2,target 2 2x1.085 us = 2.17 us
LJMP 2 2x1.085 us = 2.17 us
SJMP 2 2x1.085 us = 2.17 us
NOP 1 1x1.085 us = 1.085 us
MUL AB 4 4x1.085 us = 4.34 us
MODULE - 2 69
8051 Timers
• The 8051 comes -with two 16 bit timers, both of which may
be controlled, set, read, and configured individually. The 8051
timers have three general functions:
• Programming predefined length of time, and then issuing an
interrupt request.
• Counting the transitions on an external pin,
• Generating baud rates for the serial port.
8051 Timers
• Basically the timers are the digital counters which are
incremented at the pulses given to it. The timers can be
controlled to -function through four SFRs namely, TMOD,
TCON, TH0/TL0 and TH1/TL1.
• The timers will have overflow when it counts to full value and
resets to 0 upon next count.
• The overflow in the timers will set the two bits in the TCON
SFR. This overflow can be programmed to interrupt the
microcontroller execution and execute a predefined
subroutine.
8051 Timers
• If the timer registers are incremented by the internal clock
pulses from the microcontroller, then the operation is termed
as ‘Timing’ operation.
• Meanwhile if the timer registers get their clock pulses from
an external device through the port 3 pins of 8051, then the
operation is termed as ‘Counting’.
• Timer 0 external input pin P3.4 (T0) is used give clock input to
timer 0 to act as counter.
• Timer 1 external input pin P3.5 (T1) is used give clock input to
timer 1.
Timer SFRS
• The 8051 has two timers and each of them will have similar
operations and functions. The timer in 8051 is basically a 16-
bit register which can be incremented depending upon the
clock pulses applied to it.
• But, as it is reload mode, the TL0 will be loaded with TH0 i.e.,
FDh. The value of TH0 will never be changed. TH0/1 is set to a
known value and TL0/1 is the SFR that is constantly
incremented.
Mode 2 – 8-bit Timer Auto
Reload Mode
• The auto-reload mode is very commonly used for
establishing a baud rate for Serial Communications.
• In mode 3, all the bits that are related to Real Timer 1 will
simply hold its count and will not run and the situation is
similar to keeping TR1=0.
Mode 3 – Split Timer Mode
• In Split Timer mode of Timer 0, the real Timer 1 (i.e. TH1
and TL1) can not be started or stopped since the bits that
do that are now linked to TH0. The real timer 1, in this
case, will be incremented every machine cycle no matter
what.
Write TCON
Start timer
Continue
Timer Control and Operation
• To set the bit TR1 of TCON (D6 bit), any one of the following
two commands can be used -MOV TCON, #40h OR SETB TR1
• If the event being counted occurs more than 500,000 times per
second, it will not be able accurately counted by the 8051.
Interrupt Sources and Interrupt
Vector Addresses
• 8051 basically has following five interrupt sources so
that any of the following events will make 8051 to
execute an interrupt service routine.
• Timer 0 Overflow.
• Timer 1 Overflow.
• Reception/Transmission of Serial Character.
• External hardware interrupt 0.
• External hardware interrupt 1.
Interrupt Sources and Interrupt
Vector Addresses
• Different interrupt sources have to be distinguished
and 8051 must execute different subroutines
depending –interrupt triggered. This is accomplished
by jumping or calling to a fixed address when
interrupt occurs.
• These addresses are called interrupt vector addresses
or interrupt handler addresses.
Interrupt Sources and Interrupt
Vector Addresses
Interrupt Flag Interrupt Vector Address
Bit AF AC AB AA A9 A8
Address
Bit position D7 D6 D5 D4 D3 D2 D1 D0
Bit Address
BC BB BA B9 B8
Name - - PS PT1 PX1 PT0 PX0
-
Explanation
1
IT1 IE1
INT1 Interrupt
0 polling
sequence
TF1
RI
TI
Interrupt
enables
Global
Enable Interrupt
enabled
Timing of Interrupts
• 8051 micro-controller samples the hardware signal level on its pins once in
every machine cycle. A machine cycle is the time taken by the controller to
access one memory location or I/O device.
• As 8051 takes 12 clock cycles to complete one machine cycle, the interrupt
signal applied at the pins of 8051 must be available for at least 12 clock
periods.
• External interrupts are applied at the pins INT0 and INT1. The sensing of
voltage level applied to this pin can also programmed in 8051. The interrupts
can be either level triggered or edge triggered as set by the IT0 and IT1 bits of
the SFR TCON
• A ‘0’ on these bit positions will make both the hardware interrupts to be level
triggered. Level triggered means a low level voltage on the interrupt pins will
activate the interrupts.
Bit Patterns for the TCON SFR
Bit position D7 D6 D5 D4 D3 D2 D1 D0
Bit Address 8F 8E 8D 8C 8B 8A 89 88
Name TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
Interrupt 0
Interrupt 1 type
Timer 0
Timer 1 Extern type control. control. Set
run
run al Set to 1 by External to 1 by
Timer control
Timer 1 control Interru software for Interrupt software
0 bit. Set
Explanation Overflow bit. Set to pt 1 edge 0 edge for edge
Overflo to 1 by
flag 1 by edge triggering detect triggering
w flag softwar
software detect and cleared bit and
e to
to run. bit for level cleared for
run.
triggering level
triggering
Bit Address 8F 8E 8D 8C 8B 8A 89 88
Interrupt Priorities and Polling
Sequence
• A ‘1’ on the IT0 and IT1 bits of the SFR TCON will program the
hardware interrupts as edge triggered. Edge triggering means
the change of voltage from high state to low state will activate
the interrupt.
• When an interrupt is triggered, the micro controller takes the
following actions automatically:
• The current Program Counter is saved on the stack, low-byte
first.
• Interrupts of the same and lower priority are blocked.
• In the case of Timer and External interrupts, the corresponding
interrupt flag is cleared.
Interrupt Priorities and Polling
Sequence
• Program execution transfers to the corresponding interrupt
handler vector address.
• The Interrupt Handler Routine executes.
• An interrupt ends when your program executes the RETI
(Return from Interrupt) instruction. When the RETI instruction
is executed the micro controller takes the following actions:
• Two bytes are popped off the stack into the Program Counter
to restore normal program execution.
• Interrupt status is restored to its pre-interrupt status.
Interrupt Priorities and Polling Sequence
• The internal architecture of 8051 is such that the external
hardware interrupts will be cleared automatically when the
interrupt service routine is executed only if is programmed to be
edge or transition triggered.
• However, if the first byte still hasn’t been read by the time
reception of the second byte is complete, one of the bytes will
be lost. The serial port receive and transmit registers are both
accessed at Special Function Register SBUF.
8051 Serial Ports
• Data to be transmitted are written to the register SBUF and
the data received by the serial port are read from the register
SBUF. Physically reading and writing SBUF actually accesses
two separate registers.
• The SM0 and SM1 bits can select any one of the four
operating modes described in the next section.
Definition of Bits SM0 and SM1 in
SCON SFR
Bit Pattern - Description
• The next bit, SM2, is a flag used for enabling "Multiprocessor
communication" in modes 2 and 3. If SM2 is set to 1 in modes 2
and 3, the “Receive Interrupt” RI flag will not be activated if the
received 9th data bit is 0. If SM2 is set in Mode1, then “Receive
Interrupt” RI flag will not be activated if valid stop bit is not
received.
• The reception is started by enabling REN in SCON register. Once the data
reception is complete, the RI flag is set.
• The baud rate in Mode 0 is fixed at one twelfth of the clock frequency. Baud
rate= (Clock frequency/12)
Signal Transmission / Reception
pattern
Reception / Transmission
300 A0h
1200 D0h
2400 FAh
9600 FDh
Baud Rate
Following set of instructions will set the timer for the baud
rate of 9600.
– MOV TMOD, #00100000B ;timer/counter 1 set for mode 2, 8-
bit TIMER
– ;operation
– MOV TH1, #0FDh ; timer/counter 1 is timed
for 9600 baud
– SETB TR1 ; timer/counter 1 is
enabled for free run
• For initializing the serial port for mode 3 operation, the
following instruction can be used.
– MOV SCON, #11010000B
Transmitting and Receiving Data
Using Serial Port
• Once the Serial Port has been properly configured as explained
above, the serial port is ready to be used to send data and
receive data.
• To write a byte to the serial port one must simply write the
value to be transmitted to the SBUF (99h) SFR. For example, to
send the letter "A" to the serial port, the following instruction
can be written.
MOV SBUF, #’A’
Transmitting and Receiving Data
Using Serial Port
• Upon execution of the above instruction the 8051 will begin
transmitting the character via the serial port. Once the transmission is
complete, the serial port transmit interrupt flag TI is set. Since the 8051
does not have a serial output buffer, a character can not be written to
SBUF before the previous written character is completely transmitted.
This can be accomplished by checking the TI flag.
• Reading data received by the serial port is equally easy. To read a byte
from the serial port one just needs to read the value stored in the SBUF
(99h) SFR after the 8051 has automatically set the RI flag in SCON.
8051 Interface Examples
Interfacing 8255 to 8051
• When the 8051 is connected to external memory, port 0 (P0) is used for the
lower-order address and data bus and port 2 (P2) is used for the higher-order
address bus.
• Since the port 3 pins have an alternative function, the net result is that only
P1 is left for input and output operation.
• One way to expand the number of I/O ports is to connect the 8255
programmable peripheral interface with the 8051.
• The interfacing of the 8255 with the 8051 is done assuming the 8255 as a
memory location, because the 8051 supports only memory-mapped I/O.
• For accessing the external memory in the 8051, the MOVX instruction is
used.
• The lower-order address bus and the data bus are multiplexed and are
available in the port 0 pins.
• This is de-multiplexed using a latch and the ALE signal.
Learning Outcomes
• The switches are connected in such a way that when it is open, it connects
a logic 0 i.e. 0Volts to the port and when it is closed, it connects logic 1 i.e.
5Volts to the port pins.
Interfacing of switches to port pins
Interfacing of Push button switches & LEDs
• The circuit connections are shown in Figure are such that the port is not
damaged and also the port is not sourcing over current. This ensures safe
operation of the ports and switches.
• An LED gives out light illuminating when it is forward biased and has enough
current flowing through it. LED indicators are easy to interface with the
microcontrollers as output device.
• LEDs can be driven with a minimum current of about 10mA. A driver IC can be
used for illuminating LEDs.
Interfacing LED with driver to port pins
• Figure shows a method of using an inverter driver for connecting ports to LEDs.
Description
– Start: MOV A, P3
– MOV P1, A
• In common anode display, the anode of all segment LEDs are tied
together and taken out.
• So, to illuminate a segment, the common anode is connected to
the supply and then the segment input i.e. a to f is connected to a
low level logic 0.
• In common cathode display, the cathodes of all the LEDs are
connected together and taken out.
• So, to illuminate a segment, the corresponding segment input is
connected to the high level voltage or logic 1 and the common
cathode is connected to the ground. This will forward bias the LEDs
and illuminate them.
Seven Segment Display
Interfacing Description
• Interfacing of Seven segment displays to 8051 microcontroller needs a driver
as shown in Figure. Here 74240 TTL inverter driver IC is used for driving the
seven segment displays.
• Also the seven segment display is assumed to be common anode type. The
common anode is connected to the supply +5V.
• The interfacing diagram for displaying the BCD code obtained from key
connected to port3 and displaying the same in seven segment display
connected to port 1
Interfacing Switches and seven segment
display to 8051
Interfacing Description
• The segments can be illuminated
if the segments pins are
connected to the low level of
supply or to ground.
• This is done by connecting the
segment inputs of logic 0 through
74240. The 74240 IC will invert the
inputs applied to it. So, if we want
to illuminate a particular segment,
the corresponding output for that
segment has to be logic 1.
• The data output for displaying
decimal digits 0 to 9
8051 Programming in C
Number System
Exercise - I
Number
system 10 16 32 127 255 512 1023
Decimal
Binary 1010 ? ? ? ? ? ?
Base 2
Octal 12 ? ? ? ? ? ?
(Base 8)
Hexadeci A ? ? ? ? ? ?
mal
(Base 16)
Answer
Number
system 10 16 32 127 255 512 1023
Decimal
Binary 1010 10000 100000 1111111 11111111 1000000000 1111111111
Base 2
Octal 12 20 40 177 377 1000 1777
(Base 8)
Hexadecimal A 10 20 7F FF 200 3FF
(Base 16)
sbit, sfr, and bit
#include<reg51.h>
sbit inbit = P1^0;
//sbit datatype and inbit variable
sbit outbit = P2^7;
bit membit =27H;
//bit addresable memory for store the single bit
void main(void){
while(1){
membit= inbit;
// read the single bit data from port
outbit = membit;
// out the single bit data to port
}
}
sbit, sfr, and bit.
#include<reg51.h>
sbit sw = P1^7
void main(void)
{ 1
while(1){
if(sw == 1)
{
P0 = 0x55; }
else
{P2 = 0XAA;
}}}
/* Switch variable corresponds to PORT 1 Pin7. It is
used as an input. We put 0x55 on P0 if the P1.7 status
is 1 else we put 0xAA on P2*/
Producing delay using loops
#include <reg51.h>
void main(void)
{
unsigned int x;
for (;;) //repeat forever
{
p1=0x55;
for (x=0;x<40000;x++); //delay size unknown
p1=0xAA;
for (x=0;x<40000;x++);
}
}
Programs on logic operations
Bitwise Operations
1 0001
• BCD is a form of binary encoding method
• Each digit in a decimal number is represented in the form of binary 2 0010
bits.
• Encoding can be done in either 4-bit or 8-bit (usually 4-bit is preferred). 3 0011
• Generally used in digital displays, calculators do all their calculations in
BCD 4 0100
• RTC provides the time of day
5 0101
• Saves storage space compared to binary representations
• Ease of use in coding, fewer computations and less computational 6 0110
errors.
7 0111
8 1000
9 1001
ASCII
• ASCII code is an alphanumeric code used for data communication in
digital computers.
• it uses eight bits to represent a letter or a punctuation mark.
• keyboard, printers, and monitors all use ASCII
• On ASCII keyboards, when press the key “0″ - 30H (011 0000) is
forward to CPU, Similarly, 31H (011 0001) is provided for the key “1″
Data conversion
Decimal number into BCD
Packed BCD to ASCII conversion
ASCII to packed BCD conversion
Binary to decimal and ASCII conversion in C
Checksum byte in ROM
Convert 29 into ASCII character 32 and 39 for
display in LCD
#include <reg51.h>
void main(void)
{
unsigned char x,y,z;
unsigned char mybyte=0x29;
x=mybyte&0x0F; X=09
P1=x|0x30; P=39
y=mybyte&0xF0; X=20
y=y>>4; Y=02 shift right
P1=y|0x30; P1 = 32
}
write a C program to convert 8 bit BCD number
(29) into ASCII Code
#include <reg51.h> unsigned char x,y,z;
void main() { unsigned char mybyte=0x29;
unsigned char bcdNumber = 0x29; // 8-bit BCD number
x=mybyte&0x0F; X=09
P1=x|0x30; P=39
unsigned char tensDigit, onesDigit;
y=mybyte&0xF0; X=20
// Extract tens digit y=y>>4; Y=02 shift right
tensDigit = (bcdNumber >> 4) & 0x0F; //02 P1=y|0x30; P1 = 32
// Extract ones digit
onesDigit = bcdNumber & 0x0F; //09
// Convert BCD digits to their ASCII representation
tensDigit += '0’; //32
onesDigit += '0’; //39
// Output the ASCII representation on Port 1
P1 = (tensDigit << 4) | onesDigit; //outputs 39
// Infinite loop
while(1);
}
Write an 8051 C program to convert ASCII digits of ‘4’ and ‘7’ to
packed BCD and display them on P1.
#include <reg51.h>
void main() {
unsigned char bcd4, bcd7;
// Infinite loop
while(1);
}
Write an 8051 C program to convert ASCII digits of ‘4’ and ‘7’ to
packed BCD and display them on P1.
#include <reg51.h>
void main(void)
{
unsigned char bcdbyte;
unsigned char w=‘4’; 34
unsigned char z=‘7’; 37
w=w&0x0F; 04
w=w<<4; 40
z=z&0x0F; 07
bcdbyte=w|z; 47
P1=bcdbyte;
}
Write an 8051 C program to convert 11111101 (FD hex) to
decimal and display the digits on P0, P1 and P2.
#include <reg51.h>
void main() {
unsigned char binaryNumber = 0xFD; // Binary number 11111101
unsigned char decimalNumber = 0;
unsigned char digit;
// Convert binary number to decimal
decimalNumber = 0;
for(int i = 0; i < 8; i++) {
digit = (binaryNumber >> i) & 0x01;
decimalNumber += digit << i;
}
// Display decimal digits on P0, P1, and P2
P0 = decimalNumber % 10; // Least significant digit on P0
P1 = (decimalNumber / 10) % 10; // Tens digit on P1
P2 = (decimalNumber / 100) % 10; // Hundreds digit on P2
// Infinite loop
while(1);
}
Write an 8051 C program to convert hexadecimal
value FF into decimal, BCD and ASCII format
Write an 8051 C program to calculate the checksum byte for
the data 25H, 62H, 3FH, and 52H.
Steps to calculate Checksum byte in ROM are:
1. Add the bytes together and drop carries.
2. Take the 2’s complement (invert and then add 1) of the total sum. This is
the Checksum byte, which becomes the last byte of the series.
Data serialization with I/O ports.
• Serializing data is a way of sending a byte of data one bit at a time
through a single pin of microcontroller.
• There are two ways to transfer a byte of data serially:
• Serializing data and transfer data the one bit a time through bit
wise data transfer method
• Using the serial port
Write A C Program To Send Out The Value 44H Serially
One Bit At A Time Via P1.0. The LSB Should Go Out First.
• #include <reg51.h>
• sbit P1b0 = P1^0;
• sbit regALSB = ACC^0;
• void main(void) {
• unsigned char conbyte = 0x44;
• unsigned char x;
• ACC = conbyte;
• for (x=0; x<8; x++) {
• P1b0 = regALSB;
• ACC = ACC >> 1; }
• }
Program 1---16 bit timer in mode1
Assume that XTAL = 11.0592 MHz, write a C program continuously gets a single bit
of data from PI. 7 and sends it to P1.0, Simultaneously generate a square wave of 2 kHz
frequency on pin P1.5. Crystal frequency is 11.0592MHz.
(b) 1 / 2 of it for the high and low portion of the pulse is 250 us.
(c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in hex FF1AH.
#include <reg51.h>
sbit SW =P1^7;
sbit IND =P1^0;
sbit WAVE =P1^5;
while (1)
{
IND=SW; //send switch to LED
}
}
Write a C program that continuously gets a single bit of data from PI. 7 and sends it to P1.0,
while simultaneously creating a square wave of 200us (as period) on pin P1.5. Use timer 0 in
mode 2 to create the square wave. Assume that XTAL = 11.0592 MHz.
#include <reg51.h>
sbit SW =P1^7;
void main()
TMOD=0x02;
TH0=0xA4; //TH0=-92
TR0=1;
while (1)
Program 3
A switch is connected to pin P3.2. When switch is pressed the corresponding line goes low Display 0A at
port0. Write a C program to blink alternate LEDS connected to Port 1 Simultaneously.
#include <reg51.h>
sbit SW =P3^2;
unsigned int i;
P0=0x0A;
void main()
//unsigned int i;
SW=1;
IE=0x81;
while(1)
P1=0x00;
for(i=250;i>0;i--)
{}
P1=0xAA;
for(i=250;i>0;i--)
{}
(b) Assume that the clock pulse is connected to external interrupt1 EX1. Count the pulses and
display it on P0. Assume that XTAL = 11.0592 MHz. Set the baud rate at 9600.
#include <reg51.h>
void main()
TMOD=0x02;
#include <reg51.h>
WAVE=~WAVE;
cnt++;
P0 = cnt;
void main()
cnt = 0;
TMOD |= 0x02;
ET0 = 1;
TR0 = 1;
IT0 = 1; // Edge-triggered
EA = 1;
// Main loop
while (1) {
Let’s program the external interrupt such that, when falling edge is detected on the INT0 pin then
the microcontroller will toggle the P1.0 pin.
Programming steps
/*
* 8051_External_Interrupt
*/
void Ext_int_Init()
}
void External0_ISR() interrupt 0
void main()
while(1);