0% found this document useful (0 votes)
71 views

Unit 04 - Program

notes

Uploaded by

kheldarpranav03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Unit 04 - Program

notes

Uploaded by

kheldarpranav03
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

8051 Programming in C

Unit 04 :8051 Programming in C


Data Types for 8051:-
A good understanding of C data types for 8051 can help programmers
to create smaller hex files. The different data types are:

• unsigned char
• signed char
• unsigned int
• signed int
• sbit (single bit)
• bit and sfr
❖ unsigned char:
• As 8051 is an 8-bit microcontroller, the character data type is the most
natural choice for many applications.
• The unsigned char is an 8-bit data type takes a value in the range of 0
– 255 (00H – FFH) and the most widely used data type for 8051.
• The unsigned char data type can be used in situations such as setting a
counter value and ASCII characters, instead of signed char.
• It is important to specify the keyword unsigned infront of the char else
compiler will use the signed char as the default
❖ signed char:
• The signed char is an 8-bit data type that uses the MSB D7 to represent
– or +value.
• This implies there are only seven bits for the magnitude of a signed
number, giving us values from –128 to +127.
• We should stick with the unsigned char unless the data needs to be
represented as signed numbers.
❖ unsigned int:
• The unsigned int is a 16-bit data type that takes a value in the range of
0 to 65535 (0000 – FFFFH).
• This is used to define 16-bit variables such as memory addresses, set
counter values of more than 256.
• Since registers and memory accesses are in 8-bit chunks, the misuse of
int variables will result in a larger hex file.
❖ signed int:
• Signed int is a 16-bit data type that uses the MSB D15 to represent – or
+value. We have 15 bits for the magnitude of the number from –32768
to +32767.
❖ sbit (Single bit):
• The 8 bit keyword is a widely used 8051 C data types which is used to
access single-bit addressable register.
• It allows access to the single bits of the SFR registers. As we know
and have studied, some of the SFRs are bit addressable.

November 8, 2023 1
8051 Programming in C

• Among the SFRs that are widely used are also bit addressable ports P0-
P3.
❖ bit and sfr:
• The bit data type allows access to single bits of bit-addressable
memory spaces 20 – 2FH.
• To access the byte-size SFR registers, we use the sfr data type

Program 1: Write an 8051 C program to get a byte of data from Port P1. If it is less than
100, send it to P0; otherwise, send it to P2.
Solution :-
#include <reg51.h>
void main(void)
{
unsigned char mybyte;
P1 = 0xFF; //make P0 input port
while (1)
{
mybyte=P1; //get a byte from P0
if (mybyte<100)
P0 = mybyte; //send it to P1
else
P2=mybyte; //send it to P2
}
}

November 8, 2023 2
8051 Programming in C

Program 2: Write an 8051 C program to toggle all the bits of P0 and P2 continuously with a
250 ms delay. Use the inverting operator
Solution :-
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
P0=0x55;
P2=0x55;
while (1)
{
P0=~P0;
P2=~P2;
MSDelay(250);
}
}
Program 3: Write an 8051 C program to toggle only bit P2.4 continuously without
disturbing the rest of the bits of P2.
Solution :-
#include <reg51.h>
sbit mybit=P2^4;
void main(void)
{
while (1)
{
mybit=1; //turn on P2.4
mybit=0; //turn off P2.4
}
}

November 8, 2023 3
8051 Programming in C

Program 4: Write an 8051c program to convert packed BCD 0x29 to ASCII and display the
bytes on Port 1 and Port 2.
Solution :-
#include <reg51.h>
void main(void)
{
unsigned char x,y,z;
unsigned char mybyte=0x29;
x=mybyte&0x0F;
P1=x|0x30;
y=mybyte&0xF0;
y=y>>4;
P2=y|0x30;
}

Program 5: Write an 8051C program to convert ASCII digits “4” and “7” to packed BCD
and display it on Port 1.
Solution :-
#include <reg51.h>
void main(void)
{
unsigned char bcdbyte;
unsigned char w = ‘4’;
unsigned char z = ‘7’;
w = w&0x0F;
w = w<<4;
z = z&0x0F;
bcdbyte = w|z;
P1 = bcdbyte;
}

November 8, 2023 4
8051 Programming in C

Program 6: Write an 8051 C program to convert 11111101(FD) Hex data to decimal and
display the digits on P0, P1, and P2.
Solution :-
#include <reg51.h>
void main(void)
{
unsigned char x, binbyte, d1, d2, d3;
binbyte=0xFD;
x=binbyte/10;
d1=binbyte%10;
d2=x%10;
d3=x/10;
P0=d1;
P1=d2;
P2=d3;
}
Program 7: .Write a C Program to send out a the value 44H serially one bit at a time through
any port pin. The LSB should go out first
Solution :-
#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;
}

November 8, 2023 5
8051 Programming in C

Program 8: Write a C Program to send out a data serially one bit at a time through any port
pin. The MSB should go out first.
Solution:-
#include <reg51.h>
sbit P1b0=P1^0;
sbit regAMSB=ACC^7;
void main(void)
{
unsigned char conbyte=0x44;
unsigned char x;
ACC=conbyte;
for (x=0;x<8;x++)
{
P1b0=regAMSB;
ACC=ACC<<1;
}
}
Program 9: Write a C Program to bring in a byte of data serially one bit at a time via P1.0.
The LSB should come in first.
Solution:
#include <reg51.h>
sbit P1b0=P1^0;
sbit ACCMSB=ACC^7;
bit membit;
void main(void)
{
unsigned char x;
for (x=0;x<8;x++)
{

November 8, 2023 6
8051 Programming in C

membit=P1b0;
ACC=ACC>>1;
ACCMSB=membit;
}
P2=ACC;
}

November 8, 2023 7

You might also like