Design For Accessibility: Getting it right from the start
Introduction to Embedded system programming using 8051
1. DR. VIKAS J. DONGRE
HOD ELECTRONICS & TELECOM
GOVERNMENT POLYTECHNIC WASHIM (MS)
EMAIL: DONGREVJ1@GMAIL.COM
Unsigned char
Signed char
Unsigned int
Signed int
Sbit (SFR bit)
Bit
sfr
2. Microcontroller 8051 Assembly language
programming
Need to know architecture
Need to know all SFRs
Need to know various registers, register banks
Need to know various instructions
A
B
R0
R1
R2
R3
R4
R5
R6
R7
6. The reasons for writing programs in C
It is easier and less time consuming to write in C than Assembly
C is easier to modify and update
C supports I/O
You can use code available in function libraries
One doesn't need to remember all architecture, registers,
instructions of specific microcontroller
C code is portable to other microcontroller with little of no
modification
A good understanding of C data types for 8051 can help
programmers to create smaller hex files
7. Comparison of Assembly language and Embedded C
Assembly program Embedded C program
1. Program executes faster Program executes slower
2. Hard to develop and maintain Easy to develop and maintain
3.Requires less memory Requires huge memory
4.Not portable as machine dependent Portable as machine independent
6. Higher software development cost Less software development cost
7.Less scalable and reliable More scalable and reliable
5. Size of program is less Size of program is largs
8. Supported data types in embedded C
Unsigned char
Signed char
Unsigned int
Signed int
Sbit (single bit)
Bit
sfr
9. Data types and their ranges
Data types Size Range
bit 1-bit RAM bit-addressable only
sbit 1-bit SFR bit-addressable only
(signed) int 16-bit -32768 to +32767
unsigned int 16-bit 0 to 65535
(signed) char 8-bit -128 to +127
unsigned char 8-bit 0 to 255
Sfr 8-bit 0 to 255
C compilers use the signed char as the default if we do not put the
keyword unsigned
10. Write an 8051 C program to send values 00 – FF
to port P1.
#include <reg51.h>
void main(void)
{
unsigned char z;
for (z=0;z<=255;z++)
P1=z;
}
Solution:
11. Write an 8051 C program to send hex values for ASCII
characters of 0, 1, 2, 3, 4, 5, A, B, C, and D to port P1.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char
mynum[]={'0','1','2','3','4',’5’6',
'7‘,’8','9','A'};
unsigned char z;
for (z=0;z<=10;z++)
P1=mynum[z];
}
12. Write an 8051 C program to toggle all the bits of P1
continuously.
Solution:
//Toggle P1 forever
#include <reg51.h>
void main(void)
{
for (;;)
{
P1=0x55;
P1=0xAA;
}
}
13. Write an 8051 C program to toggle bit D0 of the
port P1 (P1.0) 50,000 times.
Solution:
#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}