MC Unit 2,3
MC Unit 2,3
• Arithmetic instructions
• instructions to increment and decrement the contents of registers and RAM
• Perform signed and unsigned addition and subtraction
• Perform unsigned multiplication and division
• Perform BCD addition
• There are 24 arithmetic opcodes in this group
• INC destination ; Increment destination by 1
• DEC destination ; Decrement destination by 1
• ADD/ADDC destination, source ;ADD source to destination without/with Carry flag
• SUBB destination, source ;Subtract,with borrow source from destination
• MUL AB ;Multiply the contents of registers A and B
• DIV AB ;Divide the contents of registers A by the contents of register B
• DA A ;Decimal Adjust the A register.
Unsigned addition:
• The numbers that are 8 bit positive binary numbers ranging from 00h to FFh.
• Carry flag is used to detect when the result of an ADD operation is a number larger that FFh.
The OV flag is not used for unsigned addition and subtraction
Signed Addition:
• Signed numbers use bit 7(MSB) of the number as a sign bit , Bits 0 to 6 is the magnitude of the
number.
• Signed numbers use a 1 in bit position D7 of the MSB as a negative sign and a 0 as a positive
sign.
• The negative numbers are in two’s complement form.
• In signed form,a single byte number may range in size from 10000000b(=-128d) to
01111111b(=+127d).
• Addition of Unlike Signed numbers:
The result cannot be greater than -128d or +128d and the sign of the result will always be correct.
-001d = 11111111b =FFh
+027d = 00011011b =1Bh
______ __________ ____
+026d 1)00011010b 1)1Ah
There is a carry from bit 7,hence carry flag is 1. There is also a carry from bit 6,and OV flag is 0. For this
condition, no action need be taken by the program to correct the sum.
The programming actions needed for the C and OV flags are:
Flags Action
C OV
0 0 None
0 1 Complement the sign
1 0 None
1 1 Complement the sign
The OV flag also indicates that the sum exceeds the largest positive or negative numbers range.
Decimal Arithmetic
• No other flags, other than the Carry flag in RRC and RLC are affected.
Bit Jumps: operate according to the status of the Carry flag in the PSW or the status of any bit-
addressable location.All bit jumps are relative to the program counter.
JC radd Jump relative if the Carry flag is set to 1
JNC radd
JB b, radd Jump relative if addressable bit is set to 1
JNB b, radd
JBC b, radd Jump relative if addressable bit is set, and clear the
addressable bit to 0.
Byte Jumps: jump instructions that tests bytes of data.
If the condition that is tested is true, the jump is taken.
If the condition is false, the instruction after jump is executed.
All byte jumps are relative to the program counter.
CJNE A,add,radd Compare the contents of the A register with the contents of the direct address;if
they are not equal,then jump to the relative address;set the carry flag to 1 if A<contents of direct
address.
CJNE A,#n,radd Similar to the above instruction
CJNE Rr,#n,radd
CJNE @Rp,#n,radd
DJNZ Rr,radd ;no flags are affectd
DJNZ add,radd ;Decrement the direct address by 1 and jump to the relative address if the
result is not zero;no flags are affected unless the direct address is the PSW.
JZ radd ;Jump to the relative address if register content is zero
JNZ radd
Unconditional Jumps:
• They do not test any bit or byte to determine whether the jump should be taken.
• All jump ranges are found in this group of jumps as the jump can be to any location in memory.
JMP @A+DPTR ;Jump to the address formed by adding A to the DPTR;the address can be anywhere in
the program memory;A,DPTR and the flags are unchanged.
AJMP sadd ;Jump to absolute short range address sadd;no flags are affected
LJMP ladd ;Jump to absolute long range address ladd;no flags are affected.
SJMP radd ;Jump to relative address radd; no flags affected.
NOP(no operation) ;Do nothing and go to the next instruction; used in a software timing loop;no
flags affect
CALL Instruction- Subroutine call
Subroutines are subtasks often called by main program .
Writing subroutines makes main program more structured.
Instructions under this –
ACALL address -2K range
LCALL address -64k range.
When subroutines are called
1.Address of next instruction below the CALL (PC address) is saved on to the stack.
2. PC is now loaded with address specified in front of CALL instruction.(where subroutine is written)
3.After execution of subroutine RET instruction shifts back to main program, wherein the PC address on
stack is POPed back.
Programmer must therefore ensure RET instruction at end of subroutine.
How to generate time delays
ORG 0
BACK: MOV A,#55H ;load A with 55H
MOV P1,A ;send 55H to port 1
LCALL DELAY ;time delay
MOV A,#0AAH ;load A with AA (in hex)
MOV P1,A ;send AAH to port 1
LCALL DELAY
SJMP BACK ;keep doing this indefinitely ...
END ;end of asm program
As machine cycles of each instruction is known , given clock frequency we can use machine cycles to
generate delays.
Data serialisation
Write an ALP to send 55h in A register serially to port p2.2
MOV A, #55h
MOV R0, #08h
BACK:RRC A
MOV P2.2,C
DJNZ R0, BACK
END
• As RAM space constitute register banks , stack and general purpose area,
• In 8051 compiler first allocates 8bytes of RAM for registers(bank0) rest of allocation is then for
variables and stack declared.
• So compiler allocates
• Bank 0 addresses 0-7
• Individual variables addressess 08 onwards
• Array addresses right after above declared
variables
• Stack after array
Write an 8051 C program to
a) toggle bits of P1 ports continuously with a 100ms using delay routine and assume DS8051 is
employed with 1mesc =1275count value.
b) Toggle P2.0 continouly 100msec
#include <reg51.h>
void Delay1(unsigned int);
void main(void)
{
while (1) //repeat forever
{ P1=0x55; Delay1(100);
P1=0xAA; Delay1(100);
} }
void Delay1(unsigned int TIME)
{
unsigned int i,j;
for (i=0;i<TIME;i++)
for (j=0;j<1275;j++);
}
LOGICAL OPERATORS
• The most important feature of the C language is its ability to perform bit manipulation.
• The bitwise operators used in C are
• AND (&),
• OR ( | ),
• EX-OR ( ^ ),
• inverter (~),
• Shift right (>>)
• Shift left (<<).
• Packed BCD to ASCII conversion
Example : Write an 8051 C program to convert packed BCD number 0x45 to ASCII and
display on P1 and P2.
#include <reg51.h>
void main(void)
{
unsigned char x,y,z;
unsigned char data=0x45;
x=data&0x0F; // 05
P1=x|0x30; // 35
y=data&0xF0; //40
y=y>>4; //04
P2=y|0x30; //34
}
Example : Write a C program to send out the value 55H serially one bit at a time via P1.0. The
LSB should go out first.
#include <reg51.h>
sbit P1out=P1^0;
sbit AD0=ACC^0;
void main(void)
{
unsigned char databyte=0x55;
unsigned char x;
for (x=0;x<8;x++)
{
ACC = databyte;
P1out = AD0;
ACC = ACC>>1;
databyte = ACC;
} }
Packed BCD to ASCII conversion
Example : Write an 8051 C program to convert
packed BCD number 0x45 to ASCII and display on
P1 and P2.
#include <reg51.h>
void main(void)
{
unsigned char x,y,z;
unsigned char data=0x45;
x=data&0x0F; // 05
P1=x|0x30; // 35
• Programming 8051 Timers:
y=data&0xF0; //40
• The 8051 has two timers/counters
• They can be used eithery=y>>4; //04or as event counters to count
as Timers to generate a time delay
events happening outside the microcontroller.
P2=y|0x30; //34
}
•
•
•
•
• Timers start and stop by either by software or hardware control.
• The start and stop of the timer are controlled by program(software )by the TR (timer run) bits
TR0 or TR1. The SETB instruction starts it and it is stopped by the CLR instruction.
• These instructions start and stop the timers as long as GATE=0 in the TMOD register.
• The hardware control of start and stop of the timer can be done external source used to make
GATE=1 in the TMOD register.
•
• Timer 0 and Timer 1 are 16 bits wide. Since 8051 has an 8-bit architecture, each 16-bits timer
is accessed as two separate registers of low byte and high byte.
• The low byte register is called TL0/TL1 and the high byte register is called TH0/TH1.
• These registers can be accessed like any other register.
MOV TL0,#4FH
MOV R5,TH0
• TIMER
• In the "timer" function mode, it is incremented in every machine cycle.
• We can count machine cycles. So rate is 1/12th of Osc frequency
• COUNTER
• In the "counter" function mode, the register is incremented in response to a 1 to 0 transition
at its corresponding external input pin (T0 or T1).
• It requires 2 machine cycles to detect a high to low transition. Hence maximum count rate is
1/24 th of oscillator frequency.
Delay=[max value of count-initial value]*clk period in decimal
• Timer Mode-0
f/12
The lower 5 bits of TLX and 8 bits of THX are used for the 13
bit count . Upper 3 bits of TLX are ignored. When the maximum
value of IFFFh is reached -rolls over to 0000h, TFX flag is set
If TR1/0 bit is 1 and Gate bit is 0, the timer uses reference crystal
frequency 11.0598Mhz. This mode is useful to measure the
width of a given pulse fed to input.
Input pulse frequency to TLX is f/12
TLX divides this by 5
THX divides by 8 therefore f/12*32*256
Generates timing =0.00088secs
• Timer Mode-1
ALP:
MOV TMOD ,#10h
BACK:MOV TH1 #0FFh
MOV TL1,#OE9H
SETB TR1
HLD: JNB TF1 HLD
CLR TR1
CLR TF1
SJMP BACK
C program
#include <reg 51.h>
Void main()
{
TMOD=0x10;
While(1)
{
TL1=0xEA;
TH1=0xFF;
TR1=1;
While(TF1==0);
TR1=0;
TF1=0;
}}
Timer Mode 2
1. This is a 8 bit counter/timer operation.
2. THX is loaded with 8bit count value , which is automatically reloaded to TLX register which
increments every machine cycle.
3. when the timer overflows i.e. TLX reaches FFH, it is fed with the count value stored in THX.
4. This mode is useful in applications like fixed time sampling, baud rate generation
Delay
• TH can have 8 bit value of maximum FF(256).
• (256 - 05) î 1.085 us = 251 î 1.085 us = 272.33 us is the high
portion of the pulse. Since it is a 50% duty cycle square wave,
the period T is twice that; as a result T = 2 î 272.33 us =
544.67 us and the frequency = 1.83597 kHz
•
• COUNTER
• Timers can also be used as counters, counting events happening real world. When it is used as
a counter, it is an external pulse that increments the TH, TL register.
• TMOD and TH, TL registers are used as in timer programming except for the source of the
frequency.
• C/T bit in TMOD register
• The C/T bit in the TMOD registers decides the source of the clock for the timer. When C/T = 1,
the timer is used as a counter and gets its pulses from outside. The counter counts up as
pulses are fed from pins 14 and 15, these pins are called T0 (timer 0 input) and T1 (timer 1
input).
• Example : Assuming that clock pulses are fed into pin T1, write a program for counter 1 in
mode 2 to count the pulses
• MOV TM0D,#60h ;counter 1, mode 2,C/T=1
• MOV TH1,#0 ;clear TH1
• SETB P3.5 ;make T1 input
• L1: SETB TR1 ;start the counter
• BK:JNB TF1,BK ;in loop do nothing, if TF = 0
• CLR TR1 ;stop the counter 1
• CLR TF1 ;make TF=0
• SJMP L1 ;Repeat
NOTE: THIS IS ONLY REFERENCE MATERIAL FOR SUPPORT AND
NOT COMPLETE NOTES. STUDENT HAS TO REFER TO
PRESCRIBED TEXT/REFERENCEs and LAB excercises FOR
PROGRAMS