SlideShare a Scribd company logo
DR. VIKAS J. DONGRE
HOD ELECTRONICS & TELECOM
GOVERNMENT POLYTECHNIC WASHIM (MS)
EMAIL: DONGREVJ1@GMAIL.COM
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;
}
}
Write an 8051 C program to toggle bits of P1
continuously forever with some delay.
Solution:
#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++);
}
}
Write an 8051 C program to toggle bits of P1
continuously forever with some delay.
Solution:
#include <reg51.h>
void main(void)
{
unsigned int x;
P1=0x55;
for (;;) //repeat forever
{
P1=~P1;
for (x=0;x<40000;x++); //delay size//unknown
}
}
Write an 8051 C program to toggle bits of P1
continuously forever with some delay.
Solution:
#include <reg51.h>
void main(void)
{
unsigned int x;
P1=0x55; //initialize P1 with some input
While(1) //repeat forever
{
P1=~P1; //toggle the data in P1
for (x=0;x<40000;x++); //delay size//unknown
}
}
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;
}
}
To create Time delay --
Three factors that can affect the accuracy of the delay-
1. The 8051 design..
The number of machine cycle
The number of clock periods per machine cycle
2. The crystal frequency connected to the X1 – X2 input pins
3. Compiler choice
C compiler converts the C statements and functions to Assembly
language instructions
Different compilers produce different code
Toggle P1 with fixed 1 second delay
#include <reg51.h>
void MSDelay(void);
void main(void)
{while (1)
{
P1=0x55;
MSDelay();
P1=0xAA;
MSDelay();
}
}
void MSDelay(void)
{
unsigned int i,j;
for (i=0;i<4;i++)
for (j=0;j<1275;j++);
}
Write an 8051 C program to toggle bits of P1 ports
continuously with a 250 ms.
Solution:
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{while (1)
{
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++);
}
Write an 8051 C program to monitor bit P1.5. If it is
high, send 55H to P2; otherwise, send AAH to P2.
#include <reg51.h>
sbit mybit=P1^5;
void main(void)
{
mybit=1;
while (1)
{if (mybit==1)
P2=0x55;
else
P2=0xAA;
}
}
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 3 to P0
1 0 send 7 to P0
1 1 send F to P0
Solution:
#include <reg51.h>
void main(void)
{
unsigned char z;
while(1)
{z=P1; z=z&0x3;
switch(z)
{
case(0): {P0=0x00;break;}
case(1): {P0=0x03;break;}
case(2): {P0=0x07;break;}
case(3): {P0=0x0f;break;}
}}}
Write a program to send ASCII characters ABCDEF on P1
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
code unsigned char mynum[]= "ABCDEF";
unsigned char z;
while (1)
{ {
for(z=0;z<=6;z++)
{ P1=mynum[z];
MSDelay(1000);
}}}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for(i=0;i<itime;i++)
for(j=0;j<1275;j++);
}
Write a program to toggle P1, P2, P3 continuously
sfr P0 = 0x80;
sfr P1 = 0x90;
sfr P2 = 0xA0;
void MSDelay(unsigned int);
void main(void)
{
P0=0x55; P1=0x55; P2=0x55;
while(1)
{
P0=~P0; P1=~P1; P2=~P2;
MSDelay(1000);
}}
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 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;
}
Arithmetic and logic operations in c

More Related Content

What's hot (20)

PPT
Functions & Procedures [7]
ecko_disasterz
 
PPT
Lab 1
emailharmeet
 
PPSX
2-bit comparator
Islam Adel
 
PDF
201506 CSE340 Lecture 21
Javier Gonzalez-Sanchez
 
PPTX
Labreportofai
ilias ahmed
 
PPTX
Assembly Language
AMIT GODRE
 
PPTX
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
PDF
201506 CSE340 Lecture 22
Javier Gonzalez-Sanchez
 
PPT
Expression evaluation
JeeSa Sultana
 
DOCX
Practical no 5
Kshitija Dalvi
 
DOC
C test
Smita Agarwal
 
PDF
Project on digital vlsi design
DINESH DEVIREDDY
 
PPT
Bitwise Operators in C
yndaravind
 
PPT
Csc1100 lecture03 ch03-pt1-s14
IIUM
 
PPT
Csc1100 lecture03 ch03-pt1-s14
IIUM
 
PDF
201506 CSE340 Lecture 23
Javier Gonzalez-Sanchez
 
PPTX
Reactive cocoa 101
Hai Feng Kao
 
PPTX
Testing lecture after lec 4
emailharmeet
 
PPTX
halstead software science measures
Deepti Pillai
 
PDF
Lesson 5.2 logical operators
MLG College of Learning, Inc
 
Functions & Procedures [7]
ecko_disasterz
 
2-bit comparator
Islam Adel
 
201506 CSE340 Lecture 21
Javier Gonzalez-Sanchez
 
Labreportofai
ilias ahmed
 
Assembly Language
AMIT GODRE
 
Prefix, Infix and Post-fix Notations
Afaq Mansoor Khan
 
201506 CSE340 Lecture 22
Javier Gonzalez-Sanchez
 
Expression evaluation
JeeSa Sultana
 
Practical no 5
Kshitija Dalvi
 
Project on digital vlsi design
DINESH DEVIREDDY
 
Bitwise Operators in C
yndaravind
 
Csc1100 lecture03 ch03-pt1-s14
IIUM
 
Csc1100 lecture03 ch03-pt1-s14
IIUM
 
201506 CSE340 Lecture 23
Javier Gonzalez-Sanchez
 
Reactive cocoa 101
Hai Feng Kao
 
Testing lecture after lec 4
emailharmeet
 
halstead software science measures
Deepti Pillai
 
Lesson 5.2 logical operators
MLG College of Learning, Inc
 

Similar to Arithmetic and logic operations in c (20)

PPTX
Intel 8051 Programming in C
Sudhanshu Janwadkar
 
PDF
Chapter 7 8051 programming in c
Abdelrahman Elewah
 
PPTX
8051 programming in c
Dr. Ritula Thakur
 
ODP
8051 -5
Ranjan Horkeri
 
PPTX
8051 programming skills using EMBEDDED C
Aman Sharma
 
PDF
Embedded C programming based on 8051 microcontroller
Gaurav Verma
 
PPTX
Unit -2 and 3 mekirirygiygyuguiguihiiqio
Manikanta Reddy Sakam
 
PDF
SE PAI Unit 5_IO programming in 8051
KanchanPatil34
 
PPTX
8051 C Assignments with all examples covered
AbdulMunaf52
 
PDF
Question Bank microcontroller 8051
Nilesh Bhaskarrao Bahadure
 
PDF
CS3691 ESIOT UNIT 2 EMBEDDED C PROGRAMING 6TH SEM CSE
PREMKUMARS76
 
PPT
Embedded system classes in mumbai
Vibrant Technologies & Computers
 
PDF
Eee iv-microcontrollers [10 es42]-assignment
Gopinath.B.L Naidu
 
PDF
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Sivaranjan Goswami
 
PPTX
Module 3.pptx Microcontrollers for 4th sem
Poorvi80
 
PPTX
8051 Microcontroller Overview by Venkatrao Ramisetti
VenkatraoRamisetti
 
PPT
4221-Microcontroller-8051 89c52 51-1.ppt
ssuser5fe4aa
 
PPTX
Binary to bcd
ASHOKKUMAR3510
 
PPTX
WINSEM2023-24_BECE204L_TH_VL2023240505625_2024-01-29_Reference-Material-I.pptx
SrinivasanUma1
 
PPTX
Introduction to Embedded system programming using 8051
Vikas Dongre
 
Intel 8051 Programming in C
Sudhanshu Janwadkar
 
Chapter 7 8051 programming in c
Abdelrahman Elewah
 
8051 programming in c
Dr. Ritula Thakur
 
8051 programming skills using EMBEDDED C
Aman Sharma
 
Embedded C programming based on 8051 microcontroller
Gaurav Verma
 
Unit -2 and 3 mekirirygiygyuguiguihiiqio
Manikanta Reddy Sakam
 
SE PAI Unit 5_IO programming in 8051
KanchanPatil34
 
8051 C Assignments with all examples covered
AbdulMunaf52
 
Question Bank microcontroller 8051
Nilesh Bhaskarrao Bahadure
 
CS3691 ESIOT UNIT 2 EMBEDDED C PROGRAMING 6TH SEM CSE
PREMKUMARS76
 
Embedded system classes in mumbai
Vibrant Technologies & Computers
 
Eee iv-microcontrollers [10 es42]-assignment
Gopinath.B.L Naidu
 
Introduction to Embedded C for 8051 and Implementation of Timer and Interrupt...
Sivaranjan Goswami
 
Module 3.pptx Microcontrollers for 4th sem
Poorvi80
 
8051 Microcontroller Overview by Venkatrao Ramisetti
VenkatraoRamisetti
 
4221-Microcontroller-8051 89c52 51-1.ppt
ssuser5fe4aa
 
Binary to bcd
ASHOKKUMAR3510
 
WINSEM2023-24_BECE204L_TH_VL2023240505625_2024-01-29_Reference-Material-I.pptx
SrinivasanUma1
 
Introduction to Embedded system programming using 8051
Vikas Dongre
 
Ad

More from Vikas Dongre (20)

PPTX
Lcd interfaing using 8051 and assambly language programming
Vikas Dongre
 
PPTX
Job opportunities for electronics engineering
Vikas Dongre
 
PPTX
Educational video creation: Tools and tips
Vikas Dongre
 
PPTX
Scope of job education and business after HSC
Vikas Dongre
 
PPTX
Introduction to digital logic gates
Vikas Dongre
 
PPTX
Introduction to binary number system
Vikas Dongre
 
PPTX
Timer programming for 8051 using embedded c
Vikas Dongre
 
PPTX
Interrupts programming in embedded C using 8051
Vikas Dongre
 
PPTX
Arithmetic and logic operations in c
Vikas Dongre
 
PPTX
Classification of embedded systems
Vikas Dongre
 
PPTX
Characteristics of embedded systems
Vikas Dongre
 
PPTX
Features of 89c51,pic,avr &amp; arm processors
Vikas Dongre
 
PPTX
Microcontroller architecture
Vikas Dongre
 
PPTX
2. block diagram and components of embedded system
Vikas Dongre
 
PPTX
1. advantages and applications of embedded system
Vikas Dongre
 
PPTX
Serial communication
Vikas Dongre
 
PPTX
Innovative improvements in electronic engineering laboratory education using eml
Vikas Dongre
 
PDF
Devnagari handwritten numeral recognition using geometric features and statis...
Vikas Dongre
 
PDF
Development of comprehensive devnagari numaral and character database
Vikas Dongre
 
PDF
Devnagari document segmentation using histogram approach
Vikas Dongre
 
Lcd interfaing using 8051 and assambly language programming
Vikas Dongre
 
Job opportunities for electronics engineering
Vikas Dongre
 
Educational video creation: Tools and tips
Vikas Dongre
 
Scope of job education and business after HSC
Vikas Dongre
 
Introduction to digital logic gates
Vikas Dongre
 
Introduction to binary number system
Vikas Dongre
 
Timer programming for 8051 using embedded c
Vikas Dongre
 
Interrupts programming in embedded C using 8051
Vikas Dongre
 
Arithmetic and logic operations in c
Vikas Dongre
 
Classification of embedded systems
Vikas Dongre
 
Characteristics of embedded systems
Vikas Dongre
 
Features of 89c51,pic,avr &amp; arm processors
Vikas Dongre
 
Microcontroller architecture
Vikas Dongre
 
2. block diagram and components of embedded system
Vikas Dongre
 
1. advantages and applications of embedded system
Vikas Dongre
 
Serial communication
Vikas Dongre
 
Innovative improvements in electronic engineering laboratory education using eml
Vikas Dongre
 
Devnagari handwritten numeral recognition using geometric features and statis...
Vikas Dongre
 
Development of comprehensive devnagari numaral and character database
Vikas Dongre
 
Devnagari document segmentation using histogram approach
Vikas Dongre
 
Ad

Recently uploaded (20)

PDF
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
PPTX
Precooling and Refrigerated storage.pptx
ThongamSunita
 
PDF
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
PPTX
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
PDF
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
PDF
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
PPT
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
 
PPTX
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
PPTX
Functions in Python Programming Language
BeulahS2
 
PPTX
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
PDF
PRIZ Academy - Process functional modelling
PRIZ Guru
 
PDF
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
PDF
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
PPTX
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
resming1
 
PDF
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
PDF
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
PPTX
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
PDF
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
PDF
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 
Rapid Prototyping for XR: Lecture 6 - AI for Prototyping and Research Directi...
Mark Billinghurst
 
Precooling and Refrigerated storage.pptx
ThongamSunita
 
June 2025 Top 10 Sites -Electrical and Electronics Engineering: An Internatio...
elelijjournal653
 
Tesla-Stock-Analysis-and-Forecast.pptx (1).pptx
moonsony54
 
May 2025: Top 10 Read Articles in Data Mining & Knowledge Management Process
IJDKP
 
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
AsadShad4
 
lesson4-occupationalsafetyandhealthohsstandards-240812020130-1a7246d0.pdf
arvingallosa3
 
SF 9_Unit 1.ppt software engineering ppt
AmarrKannthh
 
Stability of IBR Dominated Grids - IEEE PEDG 2025 - short.pptx
ssuser307730
 
Functions in Python Programming Language
BeulahS2
 
MATERIAL SCIENCE LECTURE NOTES FOR DIPLOMA STUDENTS
SAMEER VISHWAKARMA
 
PRIZ Academy - Process functional modelling
PRIZ Guru
 
Rapid Prototyping for XR: Lecture 5 - Cross Platform Development
Mark Billinghurst
 
Decision support system in machine learning models for a face recognition-bas...
TELKOMNIKA JOURNAL
 
CST413 KTU S7 CSE Machine Learning Neural Networks and Support Vector Machine...
resming1
 
輪読会資料_Miipher and Miipher2 .
NABLAS株式会社
 
13th International Conference of Security, Privacy and Trust Management (SPTM...
ijcisjournal
 
Kel.3_A_Review_on_Internet_of_Things_for_Defense_v3.pptx
Endang Saefullah
 
تقرير عن التحليل الديناميكي لتدفق الهواء حول جناح.pdf
محمد قصص فتوتة
 
Rapid Prototyping for XR: Lecture 1 Introduction to Prototyping
Mark Billinghurst
 

Arithmetic and logic operations in c

  • 1. DR. VIKAS J. DONGRE HOD ELECTRONICS & TELECOM GOVERNMENT POLYTECHNIC WASHIM (MS) EMAIL: [email protected]
  • 2. 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; } }
  • 3. Write an 8051 C program to toggle bits of P1 continuously forever with some delay. Solution: #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++); } }
  • 4. Write an 8051 C program to toggle bits of P1 continuously forever with some delay. Solution: #include <reg51.h> void main(void) { unsigned int x; P1=0x55; for (;;) //repeat forever { P1=~P1; for (x=0;x<40000;x++); //delay size//unknown } }
  • 5. Write an 8051 C program to toggle bits of P1 continuously forever with some delay. Solution: #include <reg51.h> void main(void) { unsigned int x; P1=0x55; //initialize P1 with some input While(1) //repeat forever { P1=~P1; //toggle the data in P1 for (x=0;x<40000;x++); //delay size//unknown } }
  • 6. 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; } }
  • 7. To create Time delay -- Three factors that can affect the accuracy of the delay- 1. The 8051 design.. The number of machine cycle The number of clock periods per machine cycle 2. The crystal frequency connected to the X1 – X2 input pins 3. Compiler choice C compiler converts the C statements and functions to Assembly language instructions Different compilers produce different code
  • 8. Toggle P1 with fixed 1 second delay #include <reg51.h> void MSDelay(void); void main(void) {while (1) { P1=0x55; MSDelay(); P1=0xAA; MSDelay(); } } void MSDelay(void) { unsigned int i,j; for (i=0;i<4;i++) for (j=0;j<1275;j++); }
  • 9. Write an 8051 C program to toggle bits of P1 ports continuously with a 250 ms. Solution: #include <reg51.h> void MSDelay(unsigned int); void main(void) {while (1) { 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++); }
  • 10. Write an 8051 C program to monitor bit P1.5. If it is high, send 55H to P2; otherwise, send AAH to P2. #include <reg51.h> sbit mybit=P1^5; void main(void) { mybit=1; while (1) {if (mybit==1) P2=0x55; else P2=0xAA; } }
  • 11. 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 3 to P0 1 0 send 7 to P0 1 1 send F to P0 Solution:
  • 12. #include <reg51.h> void main(void) { unsigned char z; while(1) {z=P1; z=z&0x3; switch(z) { case(0): {P0=0x00;break;} case(1): {P0=0x03;break;} case(2): {P0=0x07;break;} case(3): {P0=0x0f;break;} }}}
  • 13. Write a program to send ASCII characters ABCDEF on P1 #include <reg51.h> void MSDelay(unsigned int); void main(void) { code unsigned char mynum[]= "ABCDEF"; unsigned char z; while (1) { { for(z=0;z<=6;z++) { P1=mynum[z]; MSDelay(1000); }}} void MSDelay(unsigned int itime) { unsigned int i, j; for(i=0;i<itime;i++) for(j=0;j<1275;j++); }
  • 14. Write a program to toggle P1, P2, P3 continuously sfr P0 = 0x80; sfr P1 = 0x90; sfr P2 = 0xA0; void MSDelay(unsigned int); void main(void) { P0=0x55; P1=0x55; P2=0x55; while(1) { P0=~P0; P1=~P1; P2=~P2; MSDelay(1000); }} void MSDelay(unsigned int itime) { unsigned int i, j; for(i=0;i<itime;i++) for(j=0;j<1275;j++); }
  • 15. 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; }