SlideShare a Scribd company logo
8051 PROGRAMMING IN C
5th-12th February 2018 & 26th February 2018
Sudhanshu Janwadkar, TA, SV National Institute of Technology, Sura
Why write program in C?
 It is easier and less time consuming to write
codes in C than Assembly
 C is easier to modify and update
 You can use code available in function
libraries
 C code is portable to other microcontroller with
little of no modification
The downside of writing in C
 Compilers produce hex files that is
downloaded to ROM of microcontroller.
 Microcontrollers have limited on-chip ROM
 The size of hex file is the main concern
 C Codes produce a larger hex file.
What is Embedded C?
 Embedded C is a set of language extensions for
the C programming language, enhanced for
different embedded systems.
 The extensions are required to support exotic
features such as fixed-point arithmetic, multiple
distinct memory banks and basic I/O operations.
 Embedded C uses most of the syntax and
semantics of standard C, e.g., main() function,
variable definition, datatype declaration,
conditional statements (if, switch case), loops
(while, for), functions, arrays and strings,
structures and union, bit operations, macros, etc.
Data Types in Embedded C
unsigned char
 Unsigned char is an 8-bit data type in the range of
0 – 255 (00 – FFH)
 The character data type is the most natural
choice because 8051 is an 8-bit
microcontroller
 Used for
 Counter value
 ASCII characters
 C compilers use the signed char as the default if
we do not put the keyword unsigned
signed char
 The signed char is an 8-bit data type
 Use the MSB D7 to represent – or +
 Give 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
 Takes a value in the range of 0 to 65535 (0000 –
FFFFH)
 Used for:
 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
 Use the MSB D15 to represent – or +
 We have 15 bits for the magnitude of the
number from –32768 to +32767
bit, sbit and sfr
 The bit data type allows access to single bits
of bit-addressable memory spaces 20 – 2FH
 To access bits of SFRs, we use sbit data type.
 To access the byte-size SFR registers, we use
the sfr data type
Write an 8051 C program to send values 00 – FF
to port P1.
#include <reg51.h>
void main(void)
{
unsigned char i;
for (i=0;i<=255;i++)
P1=i;
}
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.
#include <reg51.h>
void main(void)
{
unsigned char mynum[]=“012345ABCD”;
unsigned char i;
for (i=0;i<=10;i++)
P1=mynum[i];
}
 Note:
1. Pay careful attention to the size of the data
2. Try to use unsigned char instead of int, whenever
Write an 8051 C program to toggle all the bits of
P1 continuously.
#include <reg51.h>
void main(void)
{
While (1)
{
p1=0x55;
p1=0xAA;
}
}
Write an 8051 C program to send values of –4 to
+4 to port P1.
//Singed numbers
#include <reg51.h>
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char i;
for (i=0;i<=8;i++)
P1=mynum[i];
}
Write an 8051 C program to send values of –4 to
+4 to port P1.
//Singed numbers
#include <reg51.h>
void main(void)
{
signed char i;
for (i=-4;i<=4;i++)
P1=mynum[i];
}
Write an 8051 C program to toggle bit D0 of the
port P1 (P1.0) 50,000 times.
#include <reg51.h>
sbit MYBIT=P1^0;
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}
Note: sbit keyword allows access to the single bits of the SFR
registers
Time delay in C
 There are two ways to create a time delay in
8051 C
 Using the 8051 timer
 Software delay using instructions
 Note that accuracy of the software delay
depends on Compiler choice
 C compiler converts the C statements and
functions to Assembly language
instructions
 Different compilers produce different code
Write an 8051 C program to toggle bits of P1
continuously forever with some delay.
//Toggle P1 forever with some delay in between “on” and “off”
#include <reg51.h>
void main(void)
{
unsigned int x;
for (;;) //repeat forever
{
p1=0x55;
for (x=0;x<40000;x++); //runs loop for 4000 times; without any operation
p1=0xAA;
for (x=0;x<40000;x++); //This will create some delay
}
}
Write an 8051 C program to toggle bits of P1 ports
continuously with a 250 ms delay.
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
while (1) //repeat forever
{
p1=0x55;
MSDelay(250);
p1=0xAA;
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
Byte Size I/O
 This slide is intentionally left blank
LEDs are connected to bits P1 and P2. Write an 8051 C
program that shows the count from 0 to FFH (0000 0000 to
1111 1111 in binary) on the LEDs.
#include <reg51.h>
#define LED P2;
void main(void)
{
P1=00; //clear P1
LED=0; //clear P2
while(1)
{
P1++; //increment P1
LED++; //increment P2
}
}
Note: Ports P0 – P3 are byte-accessable and we can use the P0 – P3
labels as defined in the 8051 header file <reg51.h>
Write an 8051 C program to get a byte of data form P1,
wait 1/2 second, and then send it to P2.
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
unsigned char mybyte;
P1=0xFF; //make P1 input port
while (1)
{
mybyte=P1; //get a byte from P1
MSDelay(500);
P2=mybyte; //send it to P2
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
Write an 8051 C program to get a byte of data form P0. If it
is less than 100, send it to P1; otherwise, send it to P2.
#include <reg51.h>
void main(void)
{
unsigned char mybyte;
P0=0xFF; //make P0 input port
while (1)
{
mybyte=P0; //get a byte from P0
if (mybyte<100)
P1=mybyte; //send it to P1
else
P2=mybyte; //send it to P2
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
I/O PROGRAMMING Bit-addressable I/O
 This slide is intentionally left blank
Write an 8051 C program to toggle only bit P2.4
continuously without disturbing the rest of the bits of P2.
//Toggling an individual bit
#include <reg51.h>
sbit mybit=P2^4;
void main(void)
{
while (1)
{
mybit=1; //turn on P2.4
mybit=0; //turn off P2.4
}
}
Note:
 Ports P0 – P3 are bit-addressable and we use sbit data type to
access a single bit of P0 - P3
 Use the Px^y format, where x is the port 0, 1, 2, or 3 and y is the bit
0 – 7 of that port
Write an 8051 C program to monitor bit P1.5. If it
is high, send 55H to P0; otherwise, send AAH to
P2.
#include <reg51.h>
sbit mybit=P1^5;
void main(void)
{
mybit=1; //make mybit an input
while (1)
{
if (mybit==1)
P0=0x55;
else
P2=0xAA;
}
}
A door sensor is connected to the P1.1 pin, and a buzzer is
connected to P1.7. Write an 8051 C program to monitor the door
sensor, and when it opens, sound the buzzer. You can sound the
buzzer by sending a square wave of a few hundred Hz.
#include <reg51.h>
void MSDelay(unsigned int);
sbit Dsensor=P1^1;
sbit Buzzer=P1^7;
void main(void)
{
Dsensor=1; //make P1.1 an input
while (1)
{
while (Dsensor==1)//while it opens
{
Buzzer=0;
MSDelay(200);
Buzzer=1;
MSDelay(200);
}
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
Write an 8051 C program to toggle all the bits of
P0, P1, and P2 continuously with a 250 ms delay.
Use the sfr keyword to declare the port addresses.
sfr P0=0x80;
sfr P1=0x90;
sfr P2=0xA0;
void MSDelay(unsigned int);
void main(void)
{
while (1)
{
P0=0x55;
P1=0x55;
P2=0x55;
MSDelay(250);
P0=0xAA;
P1=0xAA;
P2=0xAA;
MSDelay(250);
}
}
void MSDelay(unsigned int itime)
{
unsigned int i,j;
for (i=0;i<itime;i++)
for (j=0;j<1275;j++);
}
The data pins of an LCD are connected to P1. The
information is latched into the LCD whenever its Enable
pin goes from high to low. Write an 8051 C program to
send “ECED-SVNIT” to this LCD.
#include <reg51.h>
#define LCDData P1 //LCDData declaration
sbit En=P2^0; //the enable pin
void main(void)
{
unsigned char message[] =“ECED-SVNIT”;
unsigned char z;
for (z=0;z<10;z++) //send 10 characters
{
LCDData=message[z];
En=1; //a high-
En=0; //-to-low pulse to latch data
}
}
Write an 8051 C program to turn bit P1.5 on and
off 50,000 times.
#include <reg51.h>
sbit MYBIT=0x95;
void main(void)
{
unsigned int z;
for (z=0;z<50000;z++)
{
MYBIT=1;
MYBIT=0;
}
}
Note
 We can access a single bit of any SFR if we specify the bit
address
I/O PROGRAMMING: Using bit
Data Type for Bit-addressable
RAM
 Write an 8051 C program to get the status of bit P1.0, save it, and send
it to P2.7 continuously.
#include <reg51.h>
sbit inbit=P1^0;
sbit outbit=P2^7;
bit membit; //use bit to declare bit- addressable memory
void main(void)
{
while (1)
{
membit=inbit; //get a bit from P1.0
outbit=membit; //send it to P2.7
}
}
 We use bit data type to access data in a bit-addressable section of the data
RAM space 20 – 2FH
Operators in C
 Logical operators
 AND (&&), OR (||), and NOT (!)
 Bit-wise operators
 AND (&), OR (|), EX-OR (^), Inverter (~),
 Shift Right (>>), and Shift Left (<<)
Write an 8051 C program to toggle all the bits of P0 and
P2 continuously with a 250 ms delay. Using the inverting
and Ex-OR operators, respectively.
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
P0=0x55;
P2=0x55;
while (1)
{
P0=~P0;
P2=P2^0xFF;
MSDelay(250);
}
}
Write an 8051 C program to get bit P1.0 and send it to
P2.7 after inverting it.
#include <reg51.h>
sbit inbit=P1^0;
sbit outbit=P2^7;
bit membit;
void main(void)
{
while (1)
{
membit=inbit; //get a bit from P1.0
outbit=~membit; //invert it and send it to P2.7
}
}
 Write an 8051 C program to read the P1.0 and
P1.1 bits and issue an ASCII character to P0
according to the following table.
P1.1 P1.0
0 0 send ‘0’ to P0
0 1 send ‘1’ to P0
1 0 send ‘2’ to P0
1 1 send ‘3’ to P0
#include <reg51.h>
void main(void)
{
unsignbed char z;
z=P1;
z=z&0x3;
switch (z)
{
Case 0: P0=‘0’;
break;
Case 1: P0=‘1’;
break;
Case 2: P0=‘2’;
break;
Case 3: P0=‘3’;
break;
}
}
DATA CONVERSION: Packed BCD to ASCII
Conversion
Write an 8051 C program to convert packed BCD 0x29 to
ASCII and display the bytes on P1 and P2.
#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;
}
DATA CONVERSION: ASCII to Packed BCD
Conversion
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’;
unsigned char z=‘7’;
w=w&0x0F;
w=w<<4;
z=z&0x0F;
bcdbyte=w|z;
P1=bcdbyte;
}
DATA CONVERSION: Checksum
Byte
Write an 8051 C program to calculate the checksum byte for the data
25H, 62H, 3FH, and 52H.
#include <reg51.h>
void main(void)
{
unsigned char mydata[ ]={0x25,0x62,0x3F,0x52};
unsigned char sum=0;
unsigned char x;
unsigned char chksumbyte;
for (x=0;x<4;x++)
{
P2=mydata[x];
sum=sum+mydata[x];
}
chksumbyte=~sum+1;
P2=chksumbyte;
}
Write an 8051 C program to perform the checksum
operation to ensure data integrity. If data is good, send
ASCII character ‘G’ to P0. Otherwise send ‘B’ to P0.
#include <reg51.h>
void main(void)
{
unsigned char mydata[
]={0x25,0x62,0x3F,0x52,0xE8};
unsigned char chksum=0;
unsigned char x;
for (x=0;x<5;x++)
chksum=chksum + mydata[x];
if (chksum==0)
P0=‘G’;
else
P0=‘B’;
DATA CONVERSION: Binary (hex) to Decimal and ASCII
Conversion
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(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;
}
RAM Data Space Usage by 8051 C Compiler
 The 8051 C compiler allocates RAM locations
 Bank 0 :- addresses 0 – 7
 Individual variables :- addresses 08 and
beyond
 Array elements:- addresses right after
variables
 Array elements need contiguous RAM
locations and that limits the size of the array
due to the fact that we have only 128 bytes of
RAM for everything
 Stack:- addresses right after array elements
ACCESSING CODE ROM
 To make the C compiler use the code space instead of the
RAM space, we need to put the keyword code in front of the
variable declaration
Example:
#include <reg51.h>
void main(void)
{
code unsigned char mynum[ ]=“ABCDEF”;
unsigned char z;
for (z=0;z<=6;z++)
P1=mynum[z];
}
Compare and contrast the following programs and
discuss the advantages and disadvantages of
each one.
#include <reg51.h>
void main(void)
{
unsigned char mydata[]=“HELLO”;
unsigned char z;
for (z=0;z<=5;z++)
P1=mydata[z];
}
(a)
#include <reg51.h>
void main(void)
{
code unsigned char mydata[]=“HELLO”;
unsigned char z;
for (z=0;z<=5;z++)
P1=mydata[z];
}
(b)
 (a) uses the RAM data space to store array
elements, therefore the size of the array is
limited
 (b) uses a separate area of the code space for
data. This allows the size of the array to be as
long as you want if you have the on-chip ROM.
 However, the more code space you use for
data, the less space is left for your program
code
DATA SERIALIZATION
 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:
 Using the serial port
 Transfer data one bit a time and control the
sequence of data and spaces in between them
(to be studied here)
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 ACCLSB=ACC^0;
void main(void)
{
unsigned char conbyte=0x44;
unsigned char x;
ACC=conbyte;
for (x=0;x<8;x++)
{
P1b0=ACCLSB;
ACC=ACC>>1;
}
}
Write a C program to send out the value 44H
serially one bit at a time via P1.0. The MSB should
go out first.
#include <reg51.h>
sbit P1b0=P1^0;
sbit ACCMSB=ACC^7;
void main(void)
{
unsigned char sendbyte=0x44;
unsigned char x;
ACC = sendbyte;
for (x=0;x<8;x++)
{
P1b0=ACCMSB;
ACC=ACC<<1;
}
}
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.
#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++)
{
membit=P1b0;
ACC=ACC>>1;
ACCMSB=membit;
}
P2=ACC;
}
Write a C program to bring in a byte of data
serially one bit at a time via P1.0. The MSB should
come in first.
#include <reg51.h>
sbit P1b0=P1^0;
sbit ACCLSB=ACC^0;
bit membit;
void main(void)
{
unsigned char x;
for (x=0;x<8;x++)
{
membit=P1b0;
ACC=ACC<<1;
ACCLSB=membit;
}
P2=ACC;
}

More Related Content

What's hot (20)

PPTX
8051 Microcontroller ppt
Rahul Kumar
 
PPTX
Microcontroller presentation
xavierpaulino
 
PPTX
Serial Communication in 8051
Sudhanshu Janwadkar
 
PDF
Embedded C programming based on 8051 microcontroller
Gaurav Verma
 
PDF
DAC Interfacing with 8051.pdf
Srikrishna Thota
 
PPTX
INTRODUCTION TO MICROCONTROLLER
Ankita Jaiswal
 
DOCX
8085 interfacing with memory chips
Srikrishna Thota
 
PDF
Introduction to VLSI Design
Kalyan Acharjya
 
PPT
Microcontroller-8051.ppt
Dr.YNM
 
PPTX
Transistor Transistor Logic
surat murthy
 
PPT
Memory & I/O interfacing
deval patel
 
PPT
8051 MICROCONTROLLER
THANDAIAH PRABU
 
PPTX
8051 memory
Mayank Garg
 
PPTX
Architecture of 8085 microprocessor
AMAN SRIVASTAVA
 
PDF
8051 interfacing
KanchanPatil34
 
PPT
8086 micro processor
Poojith Chowdhary
 
PDF
Verilog full adder in dataflow & gate level modelling style.
Omkar Rane
 
PPTX
Pass Transistor Logic
Sudhanshu Janwadkar
 
PPTX
program status word
sheetalverma38
 
PPTX
Classification of embedded systems
Vikas Dongre
 
8051 Microcontroller ppt
Rahul Kumar
 
Microcontroller presentation
xavierpaulino
 
Serial Communication in 8051
Sudhanshu Janwadkar
 
Embedded C programming based on 8051 microcontroller
Gaurav Verma
 
DAC Interfacing with 8051.pdf
Srikrishna Thota
 
INTRODUCTION TO MICROCONTROLLER
Ankita Jaiswal
 
8085 interfacing with memory chips
Srikrishna Thota
 
Introduction to VLSI Design
Kalyan Acharjya
 
Microcontroller-8051.ppt
Dr.YNM
 
Transistor Transistor Logic
surat murthy
 
Memory & I/O interfacing
deval patel
 
8051 MICROCONTROLLER
THANDAIAH PRABU
 
8051 memory
Mayank Garg
 
Architecture of 8085 microprocessor
AMAN SRIVASTAVA
 
8051 interfacing
KanchanPatil34
 
8086 micro processor
Poojith Chowdhary
 
Verilog full adder in dataflow & gate level modelling style.
Omkar Rane
 
Pass Transistor Logic
Sudhanshu Janwadkar
 
program status word
sheetalverma38
 
Classification of embedded systems
Vikas Dongre
 

Similar to Intel 8051 Programming in C (20)

PPTX
8051 programming in c
Dr. Ritula Thakur
 
PPTX
Unit -2 and 3 mekirirygiygyuguiguihiiqio
Manikanta Reddy Sakam
 
PPTX
Arithmetic and logic operations in c
Vikas Dongre
 
PPTX
8051 programming skills using EMBEDDED C
Aman Sharma
 
PPTX
Programming 8051 with C and using Keil uVision5.pptx
Shyamkant Vasekar
 
PPTX
Introduction to Embedded system programming using 8051
Vikas Dongre
 
ODP
8051 -5
Ranjan Horkeri
 
PPT
Embedded system classes in mumbai
Vibrant Technologies & Computers
 
PPTX
Vinod ppt on es31 08 15
Govt. Engg. Collage Ajmer
 
PDF
CS3691 ESIOT UNIT 2 EMBEDDED C PROGRAMING 6TH SEM CSE
PREMKUMARS76
 
PDF
Embedded C Programming Module 5 Presentation
MarkkandanS
 
PPTX
Embedded system (Chapter 5) part 1
Ikhwan_Fakrudin
 
PPTX
Embedded system (Chapter )
Ikhwan_Fakrudin
 
PPT
Ch3 ppt
wasz123
 
PPTX
Arithmetic and Logic instructions in Embedded C
Vikas Dongre
 
PPT
4221-Microcontroller-8051 89c52 51-1.ppt
ssuser5fe4aa
 
PPTX
module-2.pptx
Ambika Naik
 
PPTX
Architecture of the Intel 8051 Microcontroller
Sudhanshu Janwadkar
 
PPTX
1st unit - microcontroller architechture and pin diagram
gokikayal1998
 
PPTX
SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT III The 8051 Microcontrollers
Arti Parab Academics
 
8051 programming in c
Dr. Ritula Thakur
 
Unit -2 and 3 mekirirygiygyuguiguihiiqio
Manikanta Reddy Sakam
 
Arithmetic and logic operations in c
Vikas Dongre
 
8051 programming skills using EMBEDDED C
Aman Sharma
 
Programming 8051 with C and using Keil uVision5.pptx
Shyamkant Vasekar
 
Introduction to Embedded system programming using 8051
Vikas Dongre
 
Embedded system classes in mumbai
Vibrant Technologies & Computers
 
Vinod ppt on es31 08 15
Govt. Engg. Collage Ajmer
 
CS3691 ESIOT UNIT 2 EMBEDDED C PROGRAMING 6TH SEM CSE
PREMKUMARS76
 
Embedded C Programming Module 5 Presentation
MarkkandanS
 
Embedded system (Chapter 5) part 1
Ikhwan_Fakrudin
 
Embedded system (Chapter )
Ikhwan_Fakrudin
 
Ch3 ppt
wasz123
 
Arithmetic and Logic instructions in Embedded C
Vikas Dongre
 
4221-Microcontroller-8051 89c52 51-1.ppt
ssuser5fe4aa
 
module-2.pptx
Ambika Naik
 
Architecture of the Intel 8051 Microcontroller
Sudhanshu Janwadkar
 
1st unit - microcontroller architechture and pin diagram
gokikayal1998
 
SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT III The 8051 Microcontrollers
Arti Parab Academics
 
Ad

More from Sudhanshu Janwadkar (20)

PPTX
Presentation on Elementary Data Link Protocols
Sudhanshu Janwadkar
 
PPTX
Error Correcting and Error Detecting Codes.pptx
Sudhanshu Janwadkar
 
PPTX
DSP Processors versus ASICs
Sudhanshu Janwadkar
 
PDF
Keypad Interfacing with 8051 Microcontroller
Sudhanshu Janwadkar
 
PPT
ASIC design Flow (Digital Design)
Sudhanshu Janwadkar
 
PPTX
Fpga architectures and applications
Sudhanshu Janwadkar
 
PPTX
LCD Interacing with 8051
Sudhanshu Janwadkar
 
PPTX
Interrupts in 8051
Sudhanshu Janwadkar
 
PPT
SPI Bus Protocol
Sudhanshu Janwadkar
 
PPTX
I2C Protocol
Sudhanshu Janwadkar
 
PPTX
Introduction to 8051 Timer/Counter
Sudhanshu Janwadkar
 
PPTX
Hardware View of Intel 8051
Sudhanshu Janwadkar
 
PPTX
Introduction to Embedded Systems
Sudhanshu Janwadkar
 
PPTX
CMOS Logic
Sudhanshu Janwadkar
 
PPTX
Interconnects in Reconfigurable Architectures
Sudhanshu Janwadkar
 
PPTX
Introduction to FPGAs
Sudhanshu Janwadkar
 
PDF
Design and Implementation of a GPS based Personal Tracking System
Sudhanshu Janwadkar
 
PDF
Embedded Logic Flip-Flops: A Conceptual Review
Sudhanshu Janwadkar
 
PPTX
Memory and Processor Testing
Sudhanshu Janwadkar
 
PDF
Pass Transistor Logic
Sudhanshu Janwadkar
 
Presentation on Elementary Data Link Protocols
Sudhanshu Janwadkar
 
Error Correcting and Error Detecting Codes.pptx
Sudhanshu Janwadkar
 
DSP Processors versus ASICs
Sudhanshu Janwadkar
 
Keypad Interfacing with 8051 Microcontroller
Sudhanshu Janwadkar
 
ASIC design Flow (Digital Design)
Sudhanshu Janwadkar
 
Fpga architectures and applications
Sudhanshu Janwadkar
 
LCD Interacing with 8051
Sudhanshu Janwadkar
 
Interrupts in 8051
Sudhanshu Janwadkar
 
SPI Bus Protocol
Sudhanshu Janwadkar
 
I2C Protocol
Sudhanshu Janwadkar
 
Introduction to 8051 Timer/Counter
Sudhanshu Janwadkar
 
Hardware View of Intel 8051
Sudhanshu Janwadkar
 
Introduction to Embedded Systems
Sudhanshu Janwadkar
 
Interconnects in Reconfigurable Architectures
Sudhanshu Janwadkar
 
Introduction to FPGAs
Sudhanshu Janwadkar
 
Design and Implementation of a GPS based Personal Tracking System
Sudhanshu Janwadkar
 
Embedded Logic Flip-Flops: A Conceptual Review
Sudhanshu Janwadkar
 
Memory and Processor Testing
Sudhanshu Janwadkar
 
Pass Transistor Logic
Sudhanshu Janwadkar
 
Ad

Recently uploaded (20)

PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Introduction to Indian Writing in English
Trushali Dodiya
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
infertility, types,causes, impact, and management
Ritu480198
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Controller Request and Response in Odoo18
Celine George
 

Intel 8051 Programming in C

  • 1. 8051 PROGRAMMING IN C 5th-12th February 2018 & 26th February 2018 Sudhanshu Janwadkar, TA, SV National Institute of Technology, Sura
  • 2. Why write program in C?  It is easier and less time consuming to write codes in C than Assembly  C is easier to modify and update  You can use code available in function libraries  C code is portable to other microcontroller with little of no modification
  • 3. The downside of writing in C  Compilers produce hex files that is downloaded to ROM of microcontroller.  Microcontrollers have limited on-chip ROM  The size of hex file is the main concern  C Codes produce a larger hex file.
  • 4. What is Embedded C?  Embedded C is a set of language extensions for the C programming language, enhanced for different embedded systems.  The extensions are required to support exotic features such as fixed-point arithmetic, multiple distinct memory banks and basic I/O operations.  Embedded C uses most of the syntax and semantics of standard C, e.g., main() function, variable definition, datatype declaration, conditional statements (if, switch case), loops (while, for), functions, arrays and strings, structures and union, bit operations, macros, etc.
  • 5. Data Types in Embedded C
  • 6. unsigned char  Unsigned char is an 8-bit data type in the range of 0 – 255 (00 – FFH)  The character data type is the most natural choice because 8051 is an 8-bit microcontroller  Used for  Counter value  ASCII characters  C compilers use the signed char as the default if we do not put the keyword unsigned
  • 7. signed char  The signed char is an 8-bit data type  Use the MSB D7 to represent – or +  Give us values from –128 to +127  We should stick with the unsigned char unless the data needs to be represented as signed numbers
  • 8. unsigned int  The unsigned int is a 16-bit data type  Takes a value in the range of 0 to 65535 (0000 – FFFFH)  Used for:  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
  • 9. signed int  Signed int is a 16-bit data type  Use the MSB D15 to represent – or +  We have 15 bits for the magnitude of the number from –32768 to +32767
  • 10. bit, sbit and sfr  The bit data type allows access to single bits of bit-addressable memory spaces 20 – 2FH  To access bits of SFRs, we use sbit data type.  To access the byte-size SFR registers, we use the sfr data type
  • 11. Write an 8051 C program to send values 00 – FF to port P1. #include <reg51.h> void main(void) { unsigned char i; for (i=0;i<=255;i++) P1=i; }
  • 12. 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. #include <reg51.h> void main(void) { unsigned char mynum[]=“012345ABCD”; unsigned char i; for (i=0;i<=10;i++) P1=mynum[i]; }  Note: 1. Pay careful attention to the size of the data 2. Try to use unsigned char instead of int, whenever
  • 13. Write an 8051 C program to toggle all the bits of P1 continuously. #include <reg51.h> void main(void) { While (1) { p1=0x55; p1=0xAA; } }
  • 14. Write an 8051 C program to send values of –4 to +4 to port P1. //Singed numbers #include <reg51.h> void main(void) { char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4}; unsigned char i; for (i=0;i<=8;i++) P1=mynum[i]; }
  • 15. Write an 8051 C program to send values of –4 to +4 to port P1. //Singed numbers #include <reg51.h> void main(void) { signed char i; for (i=-4;i<=4;i++) P1=mynum[i]; }
  • 16. Write an 8051 C program to toggle bit D0 of the port P1 (P1.0) 50,000 times. #include <reg51.h> sbit MYBIT=P1^0; void main(void) { unsigned int z; for (z=0;z<=50000;z++) { MYBIT=0; MYBIT=1; } } Note: sbit keyword allows access to the single bits of the SFR registers
  • 17. Time delay in C  There are two ways to create a time delay in 8051 C  Using the 8051 timer  Software delay using instructions  Note that accuracy of the software delay depends on Compiler choice  C compiler converts the C statements and functions to Assembly language instructions  Different compilers produce different code
  • 18. Write an 8051 C program to toggle bits of P1 continuously forever with some delay. //Toggle P1 forever with some delay in between “on” and “off” #include <reg51.h> void main(void) { unsigned int x; for (;;) //repeat forever { p1=0x55; for (x=0;x<40000;x++); //runs loop for 4000 times; without any operation p1=0xAA; for (x=0;x<40000;x++); //This will create some delay } }
  • 19. Write an 8051 C program to toggle bits of P1 ports continuously with a 250 ms delay. #include <reg51.h> void MSDelay(unsigned int); void main(void) { while (1) //repeat forever { p1=0x55; MSDelay(250); p1=0xAA; MSDelay(250); } } void MSDelay(unsigned int itime) { unsigned int i,j; for (i=0;i<itime;i++) for (j=0;j<1275;j++); }
  • 20. Byte Size I/O  This slide is intentionally left blank
  • 21. LEDs are connected to bits P1 and P2. Write an 8051 C program that shows the count from 0 to FFH (0000 0000 to 1111 1111 in binary) on the LEDs. #include <reg51.h> #define LED P2; void main(void) { P1=00; //clear P1 LED=0; //clear P2 while(1) { P1++; //increment P1 LED++; //increment P2 } } Note: Ports P0 – P3 are byte-accessable and we can use the P0 – P3 labels as defined in the 8051 header file <reg51.h>
  • 22. Write an 8051 C program to get a byte of data form P1, wait 1/2 second, and then send it to P2. #include <reg51.h> void MSDelay(unsigned int); void main(void) { unsigned char mybyte; P1=0xFF; //make P1 input port while (1) { mybyte=P1; //get a byte from P1 MSDelay(500); P2=mybyte; //send it to P2 } } void MSDelay(unsigned int itime) { unsigned int i,j; for (i=0;i<itime;i++) for (j=0;j<1275;j++); }
  • 23. Write an 8051 C program to get a byte of data form P0. If it is less than 100, send it to P1; otherwise, send it to P2. #include <reg51.h> void main(void) { unsigned char mybyte; P0=0xFF; //make P0 input port while (1) { mybyte=P0; //get a byte from P0 if (mybyte<100) P1=mybyte; //send it to P1 else P2=mybyte; //send it to P2 } } void MSDelay(unsigned int itime) { unsigned int i,j; for (i=0;i<itime;i++) for (j=0;j<1275;j++); }
  • 24. I/O PROGRAMMING Bit-addressable I/O  This slide is intentionally left blank
  • 25. Write an 8051 C program to toggle only bit P2.4 continuously without disturbing the rest of the bits of P2. //Toggling an individual bit #include <reg51.h> sbit mybit=P2^4; void main(void) { while (1) { mybit=1; //turn on P2.4 mybit=0; //turn off P2.4 } } Note:  Ports P0 – P3 are bit-addressable and we use sbit data type to access a single bit of P0 - P3  Use the Px^y format, where x is the port 0, 1, 2, or 3 and y is the bit 0 – 7 of that port
  • 26. Write an 8051 C program to monitor bit P1.5. If it is high, send 55H to P0; otherwise, send AAH to P2. #include <reg51.h> sbit mybit=P1^5; void main(void) { mybit=1; //make mybit an input while (1) { if (mybit==1) P0=0x55; else P2=0xAA; } }
  • 27. A door sensor is connected to the P1.1 pin, and a buzzer is connected to P1.7. Write an 8051 C program to monitor the door sensor, and when it opens, sound the buzzer. You can sound the buzzer by sending a square wave of a few hundred Hz. #include <reg51.h> void MSDelay(unsigned int); sbit Dsensor=P1^1; sbit Buzzer=P1^7; void main(void) { Dsensor=1; //make P1.1 an input while (1) { while (Dsensor==1)//while it opens { Buzzer=0; MSDelay(200); Buzzer=1; MSDelay(200); } } } void MSDelay(unsigned int itime) { unsigned int i,j; for (i=0;i<itime;i++) for (j=0;j<1275;j++); }
  • 28. Write an 8051 C program to toggle all the bits of P0, P1, and P2 continuously with a 250 ms delay. Use the sfr keyword to declare the port addresses. sfr P0=0x80; sfr P1=0x90; sfr P2=0xA0; void MSDelay(unsigned int); void main(void) { while (1) { P0=0x55; P1=0x55; P2=0x55; MSDelay(250); P0=0xAA; P1=0xAA; P2=0xAA; MSDelay(250); } } void MSDelay(unsigned int itime) { unsigned int i,j; for (i=0;i<itime;i++) for (j=0;j<1275;j++); }
  • 29. The data pins of an LCD are connected to P1. The information is latched into the LCD whenever its Enable pin goes from high to low. Write an 8051 C program to send “ECED-SVNIT” to this LCD. #include <reg51.h> #define LCDData P1 //LCDData declaration sbit En=P2^0; //the enable pin void main(void) { unsigned char message[] =“ECED-SVNIT”; unsigned char z; for (z=0;z<10;z++) //send 10 characters { LCDData=message[z]; En=1; //a high- En=0; //-to-low pulse to latch data } }
  • 30. Write an 8051 C program to turn bit P1.5 on and off 50,000 times. #include <reg51.h> sbit MYBIT=0x95; void main(void) { unsigned int z; for (z=0;z<50000;z++) { MYBIT=1; MYBIT=0; } } Note  We can access a single bit of any SFR if we specify the bit address
  • 31. I/O PROGRAMMING: Using bit Data Type for Bit-addressable RAM  Write an 8051 C program to get the status of bit P1.0, save it, and send it to P2.7 continuously. #include <reg51.h> sbit inbit=P1^0; sbit outbit=P2^7; bit membit; //use bit to declare bit- addressable memory void main(void) { while (1) { membit=inbit; //get a bit from P1.0 outbit=membit; //send it to P2.7 } }  We use bit data type to access data in a bit-addressable section of the data RAM space 20 – 2FH
  • 32. Operators in C  Logical operators  AND (&&), OR (||), and NOT (!)  Bit-wise operators  AND (&), OR (|), EX-OR (^), Inverter (~),  Shift Right (>>), and Shift Left (<<)
  • 33. Write an 8051 C program to toggle all the bits of P0 and P2 continuously with a 250 ms delay. Using the inverting and Ex-OR operators, respectively. #include <reg51.h> void MSDelay(unsigned int); void main(void) { P0=0x55; P2=0x55; while (1) { P0=~P0; P2=P2^0xFF; MSDelay(250); } }
  • 34. Write an 8051 C program to get bit P1.0 and send it to P2.7 after inverting it. #include <reg51.h> sbit inbit=P1^0; sbit outbit=P2^7; bit membit; void main(void) { while (1) { membit=inbit; //get a bit from P1.0 outbit=~membit; //invert it and send it to P2.7 } }
  • 35.  Write an 8051 C program to read the P1.0 and P1.1 bits and issue an ASCII character to P0 according to the following table. P1.1 P1.0 0 0 send ‘0’ to P0 0 1 send ‘1’ to P0 1 0 send ‘2’ to P0 1 1 send ‘3’ to P0
  • 36. #include <reg51.h> void main(void) { unsignbed char z; z=P1; z=z&0x3; switch (z) { Case 0: P0=‘0’; break; Case 1: P0=‘1’; break; Case 2: P0=‘2’; break; Case 3: P0=‘3’; break; } }
  • 37. DATA CONVERSION: Packed BCD to ASCII Conversion Write an 8051 C program to convert packed BCD 0x29 to ASCII and display the bytes on P1 and P2. #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; }
  • 38. DATA CONVERSION: ASCII to Packed BCD Conversion 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’; unsigned char z=‘7’; w=w&0x0F; w=w<<4; z=z&0x0F; bcdbyte=w|z; P1=bcdbyte; }
  • 39. DATA CONVERSION: Checksum Byte Write an 8051 C program to calculate the checksum byte for the data 25H, 62H, 3FH, and 52H. #include <reg51.h> void main(void) { unsigned char mydata[ ]={0x25,0x62,0x3F,0x52}; unsigned char sum=0; unsigned char x; unsigned char chksumbyte; for (x=0;x<4;x++) { P2=mydata[x]; sum=sum+mydata[x]; } chksumbyte=~sum+1; P2=chksumbyte; }
  • 40. Write an 8051 C program to perform the checksum operation to ensure data integrity. If data is good, send ASCII character ‘G’ to P0. Otherwise send ‘B’ to P0. #include <reg51.h> void main(void) { unsigned char mydata[ ]={0x25,0x62,0x3F,0x52,0xE8}; unsigned char chksum=0; unsigned char x; for (x=0;x<5;x++) chksum=chksum + mydata[x]; if (chksum==0) P0=‘G’; else P0=‘B’;
  • 41. DATA CONVERSION: Binary (hex) to Decimal and ASCII Conversion 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(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; }
  • 42. RAM Data Space Usage by 8051 C Compiler  The 8051 C compiler allocates RAM locations  Bank 0 :- addresses 0 – 7  Individual variables :- addresses 08 and beyond  Array elements:- addresses right after variables  Array elements need contiguous RAM locations and that limits the size of the array due to the fact that we have only 128 bytes of RAM for everything  Stack:- addresses right after array elements
  • 43. ACCESSING CODE ROM  To make the C compiler use the code space instead of the RAM space, we need to put the keyword code in front of the variable declaration Example: #include <reg51.h> void main(void) { code unsigned char mynum[ ]=“ABCDEF”; unsigned char z; for (z=0;z<=6;z++) P1=mynum[z]; }
  • 44. Compare and contrast the following programs and discuss the advantages and disadvantages of each one. #include <reg51.h> void main(void) { unsigned char mydata[]=“HELLO”; unsigned char z; for (z=0;z<=5;z++) P1=mydata[z]; } (a) #include <reg51.h> void main(void) { code unsigned char mydata[]=“HELLO”; unsigned char z; for (z=0;z<=5;z++) P1=mydata[z]; } (b)
  • 45.  (a) uses the RAM data space to store array elements, therefore the size of the array is limited  (b) uses a separate area of the code space for data. This allows the size of the array to be as long as you want if you have the on-chip ROM.  However, the more code space you use for data, the less space is left for your program code
  • 46. DATA SERIALIZATION  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:  Using the serial port  Transfer data one bit a time and control the sequence of data and spaces in between them (to be studied here)
  • 47. 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 ACCLSB=ACC^0; void main(void) { unsigned char conbyte=0x44; unsigned char x; ACC=conbyte; for (x=0;x<8;x++) { P1b0=ACCLSB; ACC=ACC>>1; } }
  • 48. Write a C program to send out the value 44H serially one bit at a time via P1.0. The MSB should go out first. #include <reg51.h> sbit P1b0=P1^0; sbit ACCMSB=ACC^7; void main(void) { unsigned char sendbyte=0x44; unsigned char x; ACC = sendbyte; for (x=0;x<8;x++) { P1b0=ACCMSB; ACC=ACC<<1; } }
  • 49. 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. #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++) { membit=P1b0; ACC=ACC>>1; ACCMSB=membit; } P2=ACC; }
  • 50. Write a C program to bring in a byte of data serially one bit at a time via P1.0. The MSB should come in first. #include <reg51.h> sbit P1b0=P1^0; sbit ACCLSB=ACC^0; bit membit; void main(void) { unsigned char x; for (x=0;x<8;x++) { membit=P1b0; ACC=ACC<<1; ACCLSB=membit; } P2=ACC; }