Module5
Module5
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 3 0011
preferred).
• Generally used in digital displays, calculators do all their calculations 4 0100
in BCD
• RTC provides the time of day 5 0101
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″ “0″ - 30H (011 0000) is
“1″
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 packed BCD 0x64 to
ASCII and display the bytes on Pot 1 and 2.
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.
253 is the equivalent decimal value of FD
Steps
FD/10
Q=25
FD%10
R=3
25/10,25%10
Q=2,R=5
Write an 8051 C program to convert hexadecimal
value FF into decimal, BCD and ASCII format
• #include<reg51.h>
void main(void)
{
unsigned char x,y,mybyte;
mybyte=0x64;
x=mybyte&0x0F;
P1=x|0x30;
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; }
}