SlideShare a Scribd company logo
1
Chapter 7
8051 Programming in C
2
Sections
7.1 Data types and time delay in 8051 C
7.2 I/O programming in 8051 C
7.3 Logic operations in 8051 C
7.4 Data conversion programs in 8051 C
7.5 Accessing code ROM space in 8051 C
7.6 Data serialization using 8051 C
3
Why Program the 8051 in C
• It is easier and less time consuming to write 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 microcontrollers with
little or no modification.
Sequence for any code
4
1. if statement (selection) Structure
5
6
2. switch statement (selection) Structure
3. while Looping (Repetition) Structure
7
4. for Looping (Repetition) Structure
8
9
Translation between C and Assembly
• A loop in Assembly
MOV R2,#255
ABC:MOV P1,R2
DJNZ R2,ABC
• A for-loop in C
for (int z=255; z>0; z--)
P1=z;
10
Section 7.1
Data Types and Time Delay in 8051 C
11
Re-examine C Data Type
• Understanding the data types of C can help
programmers to write an efficient code.
• We will focus on (See Table7-1)
– unsigned char, signed char
– unsigned int, signed int,
– Single bit: sbit (sfr) , bit (bit-addressable RAM)
– Special function register: sfr
• C compiler will allocate a RAM space for your
variables (char, int & bit).
12
Table 7-1. Some Widely Used Data Types
for 8051 C
13
Unsigned Char
• The most widely used data types for the 8051
• 8-bit data type
• The range of unsigned char: 0-255 (00-FFH)
• When do you use unsigned char?
– To set counter value (Example 7-1)
– The string of ASCII character (Example 7-2)
– For toggling ports (Example 7-3)
14
Signed Char
• 8-bit data type
• 2’s complement representation
• The range of unsigned char: -128:+127 (00-FFH)
• When do you use signed char?
– To present a given quantity such as temperature
15
Example 7-1 (unsigned char)
Write an 8051 C program to send values 00-FF to port P1.
Solution:
#include <reg51.h>
void main(void)
{
unsigned char z;
for (z=0; z<=255; z++)
P1=z;
}
16
Example 7-2 (unsigned char)
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[]=“012345ABCD”;
unsigned char z;
for (z=0; z<10; z++)
P1=mynum[z];
}
Note: “012345ABCD” is stored in RAM and can be changed.
17
Example 7-3 (unsigned char)
Write an 8051 C program to toggle all the bits of P1
continuously.
Solution:
#include <reg51.h>
void main(void) {
for ( ; ; ) { //repeat forever
P1=0x55; //0x: in hex (binary)
P1=0xAA;
}
}
18
Example 7-4 (signed char)
Write an 8051 C program to send values of -4 to 4 to port P1.
Solution:
#include <reg51.h>
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0; z<8; z++)
P1=mynum[z];
}
19
Integer
• 16-bit data type
– The range of unsigned int: 0-65535
– The range of signed int: -32,768-32,767
• Since the 8051 is an 8-bit microcontroller and the
int data type takes two bytes of RAM, we must not
used the int data type unless we have to.
• You should try to use unsigned char instead on int.
20
Example 7-5 (unsigned int, sbit)
Write an 8051 C program to toggle bit D0 of P1 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;
}
}
21
Time Delay
• Three factors that can affect the accuracy of the
time delay:
– Crystal frequency of 8051 system
– 8051 machine cycle timing
– Compiler used for 8051 C
22
Example 7-6 (time delay)
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 (;;) {
P1=0x55;
for (x=0;x<40000;x++); //time delay
P1=0xAA;
for (x=0;x<40000;x++); }}
23
Example 7-7 (1/2)
Write an 8051 C program to toggle bits of P1 continuously with a
250 ms delay. Solution:
#include <reg51.h>
void MSDelay(unsigned int);
void main(void) {
while(1) { //repeat forever
P1=0x55;
MSDelay(250); //time delay
P1=0xAA;
MSDelay(250) }} Assume the program is tested
for the DS89C420 with XTML=11.0592MHz.
90ns X 1275 = 114750ns = 1ms
void MSDelay(unsigned int itime) {
unsigned int i,j;
for (i=0; i<itime; i++)
for (j=0; j<1275; j++); // 1ms delay}
24
Section 7.2
I/O programming in 8051 C
• Way to access SFR and single bit of SFR
• Access Bit-addressable RAM
25
Access SFR
• Way to access SFR
Use name of SFR and reg51.h
#include <reg51.h>
P1=0x55; //P1=55H
• reg51. h is necessary.
• See Example 7-1.
2. Use the sfr data type and declare by yourself
sfr P1 = 0x90;
P1=0x55; //P1=55H
• reg51. h is not necessary.
8051 256Byte RAM
26
8015 256Byte RAM
27
28
Access SFR
• Way to access SFR
1. Use name of SFR and reg51.h
#include <reg51.h>
P1=0x55; //P1=55H
• reg51. h is necessary.
• See Example 7-1.
2. Use the sfr data type and
declare by yourself
sfr P1 = 0x90;
P1=0x55; //P1=55H
• reg51. h is not necessary.
29
Example 7-16 Use the sfr data type andeclare by yourself
Write an 8051 C program to toggle all the bit of P0, P1
and P2 continuously with a 250 ms time dealy.
Solution (a):
sfr P0 = 0x80; // declare by yourself
sfr P1 = 0x90; // declare by yourself
sfr P2 = 0xA0; // declare by yourself
void MSDelay(unsigned int);
// MSDelay is as same as one in
// Example7-1. Ignored it here.
void main(void) {
unsigned int z;
while(1) { //repeat forever
P0=0x55;
P1=0x55;
P2=0x55;
MSDelay(250); //time delay
P0=0xAA;
P1=0xAA;
P2=0xAA;
MSDelay(250); //time delay
} }
30
Example 7-16 Use name of SFR and reg51.h
Write an 8051 C program to toggle all the bit of P0, P1
and P2 continuously with a 250 ms time dealy.
Solution (a):
void MSDelay(unsigned int);
// MSDelay is as same as one in
// Example7-1. Ignored it here.
void main(void) {
unsigned int z;
while(1) { //repeat forever
P0=0x55;
P1=0x55;
P2=0x55;
MSDelay(250); //time delay
P0=0xAA;
P1=0xAA;
P2=0xAA;
MSDelay(250); //time delay
} }
sfr P0 = 0x80; // declare by yourself
sfr P1 = 0x90; // declare by yourself
sfr P2 = 0xA0; // declare by yourself
31
Example 7-16 Use name of SFR and reg51.h
Write an 8051 C program to toggle all the bit of P0, P1
and P2 continuously with a 250 ms time dealy.
Solution (a):
void MSDelay(unsigned int);
// MSDelay is as same as one in
// Example7-1. Ignored it here.
void main(void) {
unsigned int z;
while(1) { //repeat forever
P0=0x55;
P1=0x55;
P2=0x55;
MSDelay(250); //time delay
P0=0xAA;
P1=0xAA;
P2=0xAA;
MSDelay(250); //time delay
} }
#include <reg51.h>
32
sfr P0 = 0x80; // declare by yourself
sfr P1 = 0x90; // declare by yourself
sfr P2 = 0xA0; // declare by yourself
#include <reg51.h>?
33
34
Access Single Bit of SFR
• Way to access a single bit of SFR
– Use sbit and name of SFR
#include <reg51.h>
sbit MYBIT = P1^5; //D5 of P1
• See Example 7-5.
– Use sbit to declare the bit of SFR
and declare by yourself
sbit MYBIT = 0x95; //D5 of P1
• reg51. h is not necessary.
• See Example 7-17.
35
Example 7-17 (sbit)
Write an 8051 C program to turn bit P1.5 on and off 50,000 times.
Solution (a):
void main(void) {
unsigned int z;
for (z=0;z<50000;z++) {
MYBIT=1;
MYBIT=0;
} }
This program is similar to Example 7-5.
sbit MYBIT = 0x95; // P1^5
36
Example 7-17 (sbit)
Write an 8051 C program to turn bit P1.5 on and off 50,000 times.
Solution (a):
void main(void) {
unsigned int z;
for (z=0;z<50000;z++) {
MYBIT=1;
MYBIT=0;
} }
This program is similar to Example 7-5.
#include <reg51.h>
sbit MYBIT=P1^0;
sbit MYBIT = 0x95; // P1^5
37
Access Bit-addressable RAM
• You can use bit to access one bit of bit-
addressable section of the data RAM space
20H-2FH.
#include <reg51.h>
sbit inbit = P1^0;
bit membit; //C compiler assign a RAM space for mybit
membit = inbit; //Read P1^0 to RAM
• See Example 7-18.
38
Example 7-18 (bit)
Write an 8051 C program to get the status of bit P1.0,
save it, and send it to P2.7 continuously.
Solution:
#include <reg51.h>
sbit inbit = P1^0;
sbit outbit = P2^7;
bit membit;
void main(void) {
while(1) { //repeat forever
membit = inbit;
outbit = membit }}
39
Section 7.3
Logic operations in 8051 C
40
Bit-wise Operations in C
• AND &
0x35 & 0x0F = 0x05
• OR |
0x04 | 0x68 = 0x6C
• Exclusive-OR ^
0x54 ^ 0x78 = 0x2C
• Inverter ~
~0x55 = 0xAA
0011 0101
AND 0000 1111
0000 0101
0000 0100
OR 0110 1000
0110 1100
0101 0100
XOR 0111 1000
0010 1100NOT 0101 0101
0000 0101
41
Bit-wise Shift Operation in C
• Shift Right >>
0x9A >> 3 = 0x13
shift right 3 times
0x77 >> 4 = 0x07
shift right 4 times
• Shift Left <<
0x96 << 4 = 0x60
shift left 4 times
1001 1010  0001
0011
0111 0111  0000
0111
1001 0110  0110
0000
42
Table 7-3. Bit-wise Logic Operations for C
43
Example 7-19
Run the following program on your simulator and examine the
results.
Solution:
#include <reg51.h>
void main(void) {
P0=0x35 & 0x0F;
P0=0x04 | 0x68;
P0=0x54 ^ 0x78;
P0=~0x35;
P0=0x9A >> 3;
P0=0x77 >> 4;
P0=0x96 << 4; }
44
Section 7.4
Data conversion programs in 8051 C
45
Data Conversion
• Packed BCD to ASCII conversion
– See Example 7-24
• ASCII to packed BCD conversion
– See Example 7-25
• Checksum byte in ROM
– See Example 7-26, 27, 28 (using +)
• Binary to decimal and ASCII conversion in C
– See Example 7-29( using /, %)
46
Table 7-4. ASCII Code for Digits 0-9
Packed BCD to ASCII conversion
Example 7-24
Write an 8051 C program to convert packed BCD 0x29 to
ASCII and display the bytes on P1 and P2.
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;}
47
ASCII to packed BCD conversion
See Example 7-25
Write an 8051 C program to convert ASCII digits of ‘4’ and ‘7’ to
packed BCD and display them on P1.
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;} 48
Binary to decimal and ASCII conversion in C
See Example 7-29( using /, %)
Write an 8051 C program to convert 11111101 (FD hex) to
decimaland 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;} 49
50
Section 7.5
Accessing code ROM space in 8051 C
51
To Store Data
• We have three spaces to store data:
1. the 128 bytes RAM space with address range 00-7FH
• If you declare variables (ex: char) to store data, C compiler
will allocate a RAM space for these variable.
2. Use code space
• External code memory (64K) + on-chip ROM (64K)
• Data is embedded to code or is separated as a data section
3. External data memory for data
• RAM or ROM
• see Chapter 14
52
Data Stored in RAM
• Where are the variables assigned by C compiler?
• 8051 C compiler allocates RAM locations for
variables in the following order:
– Bank 0: addresses 0-7
– Individual variables: addresses 08H and beyond
– Array elements: addresses right after variables
• Array size is limited in 128 bytes RAM.
– Stack: address right after array elements
• Check your simulation tool
53
8052 RAM Space
• 8052 has 256 bytes RAM.
• Based on 8052 architecture, you should
– Use the reg52.h header file.
– Choose the 8052 option when compiling the program.
54
Using ROM to Store Data
• To make C compiler use the code space (on-chip
ROM) instead of RAM space, we can put the
keyword “code” in front of the variable
declaration.
unsigned char mydata[] = “HELLO”
• HELLO is saved in RAM.
code unsigned char mydata[] = “HELLO”
• HELLO is saved in ROM.
• See Example 7-33
55
Example 7-33 (1/3)
Compare and contrast the following programs and discuss the
advantages and disadvantages of each one.
Solution (a):
#include <reg51.h>
void main(void) {
P1=“H”; P1=“E”;
P1=“L”; P1=“L”; P1=“O”;
}
Data is embedded into code.
Simple, short, not flexible.
56
Example 7-33 (2/3)
Solution (b):
#include <reg51.h>
void main(void) {
unsigned char mydata[]=“HELLO”;
unsigned char z;
for (z=0; z<5; z++)
P1 = mydata[z];
}
Data is stored in RAM and does not occupied ROM.
57
Example 7-33 (3/3)
Solution (c):
#include <reg51.h>
void main(void) {
code unsigned char mydata[]=“HELLO”;
unsigned char z;
for (z=0; z<5; z++)
P1 = mydata[z];
}
Data is stored in ROM. However, data and code are separate.
58
Section 7.6
Data serialization using 8051 C
59
Serializing Data
• Two ways to transfer a byte of data:
1. Using the serial port.
• For PC. See Chapter 10.
2. To transfer data one bit a time and control the sequence
of data and spaces in between them.
• For LCD, ADC, ROM
• Example 7-34
60
Example 7-34
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.
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; }
}
ACC P1.0
D7 D0
61
Thank you
Ad

More Related Content

What's hot (20)

8051 programming in c
8051 programming in c8051 programming in c
8051 programming in c
Dr. Ritula Thakur
 
TRAFFIC LIGHT CONTROL SYSTEM USING 8085 MICROPROCESSOR
TRAFFIC LIGHT CONTROL SYSTEM USING 8085 MICROPROCESSORTRAFFIC LIGHT CONTROL SYSTEM USING 8085 MICROPROCESSOR
TRAFFIC LIGHT CONTROL SYSTEM USING 8085 MICROPROCESSOR
Subash Sambath Kumar
 
8085 instruction set
8085 instruction set8085 instruction set
8085 instruction set
Velalar College of Engineering and Technology
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C
Aman Sharma
 
Stacks & subroutines 1
Stacks & subroutines 1Stacks & subroutines 1
Stacks & subroutines 1
deval patel
 
Serial communication in 8085
Serial communication in 8085Serial communication in 8085
Serial communication in 8085
Nitin Ahire
 
Microprocessor Interfacing and 8155 Features
Microprocessor Interfacing and 8155 FeaturesMicroprocessor Interfacing and 8155 Features
Microprocessor Interfacing and 8155 Features
Srikrishna Thota
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontroller
Gaurav Verma
 
Microprocessor 8085 complete
Microprocessor 8085 completeMicroprocessor 8085 complete
Microprocessor 8085 complete
Shubham Singh
 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085
ShivamSood22
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
Mahalakshmiv11
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / Counters
Patricio Lima
 
Interfacing with peripherals: analog to digital converters and digital to ana...
Interfacing with peripherals: analog to digital converters and digital to ana...Interfacing with peripherals: analog to digital converters and digital to ana...
Interfacing with peripherals: analog to digital converters and digital to ana...
NimeshSingh27
 
Shift Registers
Shift RegistersShift Registers
Shift Registers
Abhilash Nair
 
Introduction to Microcontroller
Introduction to MicrocontrollerIntroduction to Microcontroller
Introduction to Microcontroller
Pantech ProLabs India Pvt Ltd
 
Programming with 8085
Programming with 8085Programming with 8085
Programming with 8085
Shehrevar Davierwala
 
I o ports and timers of 8051
I o ports and timers of 8051I o ports and timers of 8051
I o ports and timers of 8051
SARITHA REDDY
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051
Dr. AISHWARYA N
 
Unit II Arm7 Thumb Instruction
Unit II Arm7 Thumb InstructionUnit II Arm7 Thumb Instruction
Unit II Arm7 Thumb Instruction
Dr. Pankaj Zope
 
Seven segment interfacing with 8051.pdf
Seven segment interfacing with 8051.pdfSeven segment interfacing with 8051.pdf
Seven segment interfacing with 8051.pdf
Srikrishna Thota
 
TRAFFIC LIGHT CONTROL SYSTEM USING 8085 MICROPROCESSOR
TRAFFIC LIGHT CONTROL SYSTEM USING 8085 MICROPROCESSORTRAFFIC LIGHT CONTROL SYSTEM USING 8085 MICROPROCESSOR
TRAFFIC LIGHT CONTROL SYSTEM USING 8085 MICROPROCESSOR
Subash Sambath Kumar
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C
Aman Sharma
 
Stacks & subroutines 1
Stacks & subroutines 1Stacks & subroutines 1
Stacks & subroutines 1
deval patel
 
Serial communication in 8085
Serial communication in 8085Serial communication in 8085
Serial communication in 8085
Nitin Ahire
 
Microprocessor Interfacing and 8155 Features
Microprocessor Interfacing and 8155 FeaturesMicroprocessor Interfacing and 8155 Features
Microprocessor Interfacing and 8155 Features
Srikrishna Thota
 
Embedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontrollerEmbedded C programming based on 8051 microcontroller
Embedded C programming based on 8051 microcontroller
Gaurav Verma
 
Microprocessor 8085 complete
Microprocessor 8085 completeMicroprocessor 8085 complete
Microprocessor 8085 complete
Shubham Singh
 
Addressing modes 8085
Addressing modes 8085Addressing modes 8085
Addressing modes 8085
ShivamSood22
 
Instruction sets of 8086
Instruction sets of 8086Instruction sets of 8086
Instruction sets of 8086
Mahalakshmiv11
 
8051 Timers / Counters
8051 Timers / Counters8051 Timers / Counters
8051 Timers / Counters
Patricio Lima
 
Interfacing with peripherals: analog to digital converters and digital to ana...
Interfacing with peripherals: analog to digital converters and digital to ana...Interfacing with peripherals: analog to digital converters and digital to ana...
Interfacing with peripherals: analog to digital converters and digital to ana...
NimeshSingh27
 
I o ports and timers of 8051
I o ports and timers of 8051I o ports and timers of 8051
I o ports and timers of 8051
SARITHA REDDY
 
Addressing modes of 8051
Addressing modes of 8051Addressing modes of 8051
Addressing modes of 8051
Dr. AISHWARYA N
 
Unit II Arm7 Thumb Instruction
Unit II Arm7 Thumb InstructionUnit II Arm7 Thumb Instruction
Unit II Arm7 Thumb Instruction
Dr. Pankaj Zope
 
Seven segment interfacing with 8051.pdf
Seven segment interfacing with 8051.pdfSeven segment interfacing with 8051.pdf
Seven segment interfacing with 8051.pdf
Srikrishna Thota
 

Viewers also liked (7)

Switches and LEDs interface to the 8051 microcontroller
Switches and LEDs interface to the 8051 microcontrollerSwitches and LEDs interface to the 8051 microcontroller
Switches and LEDs interface to the 8051 microcontroller
University of Technology - Iraq
 
Lcd
LcdLcd
Lcd
deepak281292
 
8051 full ppt
8051 full ppt8051 full ppt
8051 full ppt
noor mohamed s Snoormohamedcbe
 
Embedded c
Embedded cEmbedded c
Embedded c
Ami Prakash
 
Interfacing LCD with 8051 Microcontroller
Interfacing LCD with 8051 MicrocontrollerInterfacing LCD with 8051 Microcontroller
Interfacing LCD with 8051 Microcontroller
Pantech ProLabs India Pvt Ltd
 
Microcontroller 8051 and its interfacing
Microcontroller 8051 and its interfacingMicrocontroller 8051 and its interfacing
Microcontroller 8051 and its interfacing
Ankur Mahajan
 
UART
UARTUART
UART
Naveen Kumar
 
Ad

Similar to Chapter 7 8051 programming in c (20)

Arithmetic and logic operations in c
Arithmetic and logic operations in cArithmetic and logic operations in c
Arithmetic and logic operations in c
Vikas Dongre
 
Arithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded CArithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded C
Vikas Dongre
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
PRADEEP
 
26. 8255 control word programming
26. 8255 control word programming26. 8255 control word programming
26. 8255 control word programming
sandip das
 
Microprocessor and Microcontroller Lab Manual!
Microprocessor and Microcontroller Lab Manual!Microprocessor and Microcontroller Lab Manual!
Microprocessor and Microcontroller Lab Manual!
PRABHAHARAN429
 
Embedded system (Chapter 5) part 1
Embedded system (Chapter 5) part 1Embedded system (Chapter 5) part 1
Embedded system (Chapter 5) part 1
Ikhwan_Fakrudin
 
Embedded system (Chapter )
Embedded system (Chapter )Embedded system (Chapter )
Embedded system (Chapter )
Ikhwan_Fakrudin
 
Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.ppt
steffydean
 
LCD_Example.pptx
LCD_Example.pptxLCD_Example.pptx
LCD_Example.pptx
julioalexanderaguila
 
Real Time Embedded System
Real Time Embedded SystemReal Time Embedded System
Real Time Embedded System
Vrushali Lanjewar
 
Lecture07(DHDNBK)-Behavior-Modelling.pdf
Lecture07(DHDNBK)-Behavior-Modelling.pdfLecture07(DHDNBK)-Behavior-Modelling.pdf
Lecture07(DHDNBK)-Behavior-Modelling.pdf
thanhfacebook123dn
 
Analog to Digital Converter
Analog to Digital ConverterAnalog to Digital Converter
Analog to Digital Converter
Ariel Tonatiuh Espindola
 
DIGITAL TO ANALOG CONVERTERDAC-ppt.pptx
DIGITAL  TO ANALOG CONVERTERDAC-ppt.pptxDIGITAL  TO ANALOG CONVERTERDAC-ppt.pptx
DIGITAL TO ANALOG CONVERTERDAC-ppt.pptx
sharadakori1
 
DIGITAL TO ANALOG CONVERTERDAC-ppt.pptx
DIGITAL  TO ANALOG CONVERTERDAC-ppt.pptxDIGITAL  TO ANALOG CONVERTERDAC-ppt.pptx
DIGITAL TO ANALOG CONVERTERDAC-ppt.pptx
sharadakori1
 
Digital System Design Lab Report - VHDL ECE
Digital System Design Lab Report - VHDL ECEDigital System Design Lab Report - VHDL ECE
Digital System Design Lab Report - VHDL ECE
Ramesh Naik Bhukya
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
EasyStudy3
 
8051 -5
8051 -58051 -5
8051 -5
Ranjan Horkeri
 
17443 microprocessor
17443   microprocessor17443   microprocessor
17443 microprocessor
soni_nits
 
Chapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 InstructionsChapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 Instructions
cmkandemir
 
Microprocessor lab manual
Microprocessor lab manualMicroprocessor lab manual
Microprocessor lab manual
Dhaval Shukla
 
Arithmetic and logic operations in c
Arithmetic and logic operations in cArithmetic and logic operations in c
Arithmetic and logic operations in c
Vikas Dongre
 
Arithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded CArithmetic and Logic instructions in Embedded C
Arithmetic and Logic instructions in Embedded C
Vikas Dongre
 
EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5EMBEDDED SYSTEMS 4&5
EMBEDDED SYSTEMS 4&5
PRADEEP
 
26. 8255 control word programming
26. 8255 control word programming26. 8255 control word programming
26. 8255 control word programming
sandip das
 
Microprocessor and Microcontroller Lab Manual!
Microprocessor and Microcontroller Lab Manual!Microprocessor and Microcontroller Lab Manual!
Microprocessor and Microcontroller Lab Manual!
PRABHAHARAN429
 
Embedded system (Chapter 5) part 1
Embedded system (Chapter 5) part 1Embedded system (Chapter 5) part 1
Embedded system (Chapter 5) part 1
Ikhwan_Fakrudin
 
Embedded system (Chapter )
Embedded system (Chapter )Embedded system (Chapter )
Embedded system (Chapter )
Ikhwan_Fakrudin
 
Microcontroller 8051- soft.ppt
Microcontroller 8051- soft.pptMicrocontroller 8051- soft.ppt
Microcontroller 8051- soft.ppt
steffydean
 
Lecture07(DHDNBK)-Behavior-Modelling.pdf
Lecture07(DHDNBK)-Behavior-Modelling.pdfLecture07(DHDNBK)-Behavior-Modelling.pdf
Lecture07(DHDNBK)-Behavior-Modelling.pdf
thanhfacebook123dn
 
DIGITAL TO ANALOG CONVERTERDAC-ppt.pptx
DIGITAL  TO ANALOG CONVERTERDAC-ppt.pptxDIGITAL  TO ANALOG CONVERTERDAC-ppt.pptx
DIGITAL TO ANALOG CONVERTERDAC-ppt.pptx
sharadakori1
 
DIGITAL TO ANALOG CONVERTERDAC-ppt.pptx
DIGITAL  TO ANALOG CONVERTERDAC-ppt.pptxDIGITAL  TO ANALOG CONVERTERDAC-ppt.pptx
DIGITAL TO ANALOG CONVERTERDAC-ppt.pptx
sharadakori1
 
Digital System Design Lab Report - VHDL ECE
Digital System Design Lab Report - VHDL ECEDigital System Design Lab Report - VHDL ECE
Digital System Design Lab Report - VHDL ECE
Ramesh Naik Bhukya
 
17443 microprocessor
17443   microprocessor17443   microprocessor
17443 microprocessor
soni_nits
 
Chapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 InstructionsChapter 6 - Introduction to 8085 Instructions
Chapter 6 - Introduction to 8085 Instructions
cmkandemir
 
Microprocessor lab manual
Microprocessor lab manualMicroprocessor lab manual
Microprocessor lab manual
Dhaval Shukla
 
Ad

Recently uploaded (20)

acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
22PCOAM16_MACHINE_LEARNING_UNIT_IV_NOTES_with_QB
Guru Nanak Technical Institutions
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
Jimmy Lai
 
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic AlgorithmDesign Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Journal of Soft Computing in Civil Engineering
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Construction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.pptConstruction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.ppt
ssuser2ffcbc
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Physical and Physic-Chemical Based Optimization Methods: A Review
Physical and Physic-Chemical Based Optimization Methods: A ReviewPhysical and Physic-Chemical Based Optimization Methods: A Review
Physical and Physic-Chemical Based Optimization Methods: A Review
Journal of Soft Computing in Civil Engineering
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
[PyCon US 2025] Scaling the Mountain_ A Framework for Tackling Large-Scale Te...
Jimmy Lai
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Construction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.pptConstruction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.ppt
ssuser2ffcbc
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Artificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptxArtificial intelligence and machine learning.pptx
Artificial intelligence and machine learning.pptx
rakshanatarajan005
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
Slide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptxSlide share PPT of SOx control technologies.pptx
Slide share PPT of SOx control technologies.pptx
vvsasane
 
Personal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.pptPersonal Protective Efsgfgsffquipment.ppt
Personal Protective Efsgfgsffquipment.ppt
ganjangbegu579
 
Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025Transport modelling at SBB, presentation at EPFL in 2025
Transport modelling at SBB, presentation at EPFL in 2025
Antonin Danalet
 

Chapter 7 8051 programming in c

  • 2. 2 Sections 7.1 Data types and time delay in 8051 C 7.2 I/O programming in 8051 C 7.3 Logic operations in 8051 C 7.4 Data conversion programs in 8051 C 7.5 Accessing code ROM space in 8051 C 7.6 Data serialization using 8051 C
  • 3. 3 Why Program the 8051 in C • It is easier and less time consuming to write 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 microcontrollers with little or no modification.
  • 5. 1. if statement (selection) Structure 5
  • 6. 6 2. switch statement (selection) Structure
  • 7. 3. while Looping (Repetition) Structure 7
  • 8. 4. for Looping (Repetition) Structure 8
  • 9. 9 Translation between C and Assembly • A loop in Assembly MOV R2,#255 ABC:MOV P1,R2 DJNZ R2,ABC • A for-loop in C for (int z=255; z>0; z--) P1=z;
  • 10. 10 Section 7.1 Data Types and Time Delay in 8051 C
  • 11. 11 Re-examine C Data Type • Understanding the data types of C can help programmers to write an efficient code. • We will focus on (See Table7-1) – unsigned char, signed char – unsigned int, signed int, – Single bit: sbit (sfr) , bit (bit-addressable RAM) – Special function register: sfr • C compiler will allocate a RAM space for your variables (char, int & bit).
  • 12. 12 Table 7-1. Some Widely Used Data Types for 8051 C
  • 13. 13 Unsigned Char • The most widely used data types for the 8051 • 8-bit data type • The range of unsigned char: 0-255 (00-FFH) • When do you use unsigned char? – To set counter value (Example 7-1) – The string of ASCII character (Example 7-2) – For toggling ports (Example 7-3)
  • 14. 14 Signed Char • 8-bit data type • 2’s complement representation • The range of unsigned char: -128:+127 (00-FFH) • When do you use signed char? – To present a given quantity such as temperature
  • 15. 15 Example 7-1 (unsigned char) Write an 8051 C program to send values 00-FF to port P1. Solution: #include <reg51.h> void main(void) { unsigned char z; for (z=0; z<=255; z++) P1=z; }
  • 16. 16 Example 7-2 (unsigned char) 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[]=“012345ABCD”; unsigned char z; for (z=0; z<10; z++) P1=mynum[z]; } Note: “012345ABCD” is stored in RAM and can be changed.
  • 17. 17 Example 7-3 (unsigned char) Write an 8051 C program to toggle all the bits of P1 continuously. Solution: #include <reg51.h> void main(void) { for ( ; ; ) { //repeat forever P1=0x55; //0x: in hex (binary) P1=0xAA; } }
  • 18. 18 Example 7-4 (signed char) Write an 8051 C program to send values of -4 to 4 to port P1. Solution: #include <reg51.h> void main(void) { char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4}; unsigned char z; for (z=0; z<8; z++) P1=mynum[z]; }
  • 19. 19 Integer • 16-bit data type – The range of unsigned int: 0-65535 – The range of signed int: -32,768-32,767 • Since the 8051 is an 8-bit microcontroller and the int data type takes two bytes of RAM, we must not used the int data type unless we have to. • You should try to use unsigned char instead on int.
  • 20. 20 Example 7-5 (unsigned int, sbit) Write an 8051 C program to toggle bit D0 of P1 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; } }
  • 21. 21 Time Delay • Three factors that can affect the accuracy of the time delay: – Crystal frequency of 8051 system – 8051 machine cycle timing – Compiler used for 8051 C
  • 22. 22 Example 7-6 (time delay) 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 (;;) { P1=0x55; for (x=0;x<40000;x++); //time delay P1=0xAA; for (x=0;x<40000;x++); }}
  • 23. 23 Example 7-7 (1/2) Write an 8051 C program to toggle bits of P1 continuously with a 250 ms delay. Solution: #include <reg51.h> void MSDelay(unsigned int); void main(void) { while(1) { //repeat forever P1=0x55; MSDelay(250); //time delay P1=0xAA; MSDelay(250) }} Assume the program is tested for the DS89C420 with XTML=11.0592MHz. 90ns X 1275 = 114750ns = 1ms void MSDelay(unsigned int itime) { unsigned int i,j; for (i=0; i<itime; i++) for (j=0; j<1275; j++); // 1ms delay}
  • 24. 24 Section 7.2 I/O programming in 8051 C • Way to access SFR and single bit of SFR • Access Bit-addressable RAM
  • 25. 25 Access SFR • Way to access SFR Use name of SFR and reg51.h #include <reg51.h> P1=0x55; //P1=55H • reg51. h is necessary. • See Example 7-1. 2. Use the sfr data type and declare by yourself sfr P1 = 0x90; P1=0x55; //P1=55H • reg51. h is not necessary.
  • 28. 28 Access SFR • Way to access SFR 1. Use name of SFR and reg51.h #include <reg51.h> P1=0x55; //P1=55H • reg51. h is necessary. • See Example 7-1. 2. Use the sfr data type and declare by yourself sfr P1 = 0x90; P1=0x55; //P1=55H • reg51. h is not necessary.
  • 29. 29 Example 7-16 Use the sfr data type andeclare by yourself Write an 8051 C program to toggle all the bit of P0, P1 and P2 continuously with a 250 ms time dealy. Solution (a): sfr P0 = 0x80; // declare by yourself sfr P1 = 0x90; // declare by yourself sfr P2 = 0xA0; // declare by yourself void MSDelay(unsigned int); // MSDelay is as same as one in // Example7-1. Ignored it here. void main(void) { unsigned int z; while(1) { //repeat forever P0=0x55; P1=0x55; P2=0x55; MSDelay(250); //time delay P0=0xAA; P1=0xAA; P2=0xAA; MSDelay(250); //time delay } }
  • 30. 30 Example 7-16 Use name of SFR and reg51.h Write an 8051 C program to toggle all the bit of P0, P1 and P2 continuously with a 250 ms time dealy. Solution (a): void MSDelay(unsigned int); // MSDelay is as same as one in // Example7-1. Ignored it here. void main(void) { unsigned int z; while(1) { //repeat forever P0=0x55; P1=0x55; P2=0x55; MSDelay(250); //time delay P0=0xAA; P1=0xAA; P2=0xAA; MSDelay(250); //time delay } } sfr P0 = 0x80; // declare by yourself sfr P1 = 0x90; // declare by yourself sfr P2 = 0xA0; // declare by yourself
  • 31. 31 Example 7-16 Use name of SFR and reg51.h Write an 8051 C program to toggle all the bit of P0, P1 and P2 continuously with a 250 ms time dealy. Solution (a): void MSDelay(unsigned int); // MSDelay is as same as one in // Example7-1. Ignored it here. void main(void) { unsigned int z; while(1) { //repeat forever P0=0x55; P1=0x55; P2=0x55; MSDelay(250); //time delay P0=0xAA; P1=0xAA; P2=0xAA; MSDelay(250); //time delay } } #include <reg51.h>
  • 32. 32 sfr P0 = 0x80; // declare by yourself sfr P1 = 0x90; // declare by yourself sfr P2 = 0xA0; // declare by yourself #include <reg51.h>?
  • 33. 33
  • 34. 34 Access Single Bit of SFR • Way to access a single bit of SFR – Use sbit and name of SFR #include <reg51.h> sbit MYBIT = P1^5; //D5 of P1 • See Example 7-5. – Use sbit to declare the bit of SFR and declare by yourself sbit MYBIT = 0x95; //D5 of P1 • reg51. h is not necessary. • See Example 7-17.
  • 35. 35 Example 7-17 (sbit) Write an 8051 C program to turn bit P1.5 on and off 50,000 times. Solution (a): void main(void) { unsigned int z; for (z=0;z<50000;z++) { MYBIT=1; MYBIT=0; } } This program is similar to Example 7-5. sbit MYBIT = 0x95; // P1^5
  • 36. 36 Example 7-17 (sbit) Write an 8051 C program to turn bit P1.5 on and off 50,000 times. Solution (a): void main(void) { unsigned int z; for (z=0;z<50000;z++) { MYBIT=1; MYBIT=0; } } This program is similar to Example 7-5. #include <reg51.h> sbit MYBIT=P1^0; sbit MYBIT = 0x95; // P1^5
  • 37. 37 Access Bit-addressable RAM • You can use bit to access one bit of bit- addressable section of the data RAM space 20H-2FH. #include <reg51.h> sbit inbit = P1^0; bit membit; //C compiler assign a RAM space for mybit membit = inbit; //Read P1^0 to RAM • See Example 7-18.
  • 38. 38 Example 7-18 (bit) Write an 8051 C program to get the status of bit P1.0, save it, and send it to P2.7 continuously. Solution: #include <reg51.h> sbit inbit = P1^0; sbit outbit = P2^7; bit membit; void main(void) { while(1) { //repeat forever membit = inbit; outbit = membit }}
  • 40. 40 Bit-wise Operations in C • AND & 0x35 & 0x0F = 0x05 • OR | 0x04 | 0x68 = 0x6C • Exclusive-OR ^ 0x54 ^ 0x78 = 0x2C • Inverter ~ ~0x55 = 0xAA 0011 0101 AND 0000 1111 0000 0101 0000 0100 OR 0110 1000 0110 1100 0101 0100 XOR 0111 1000 0010 1100NOT 0101 0101 0000 0101
  • 41. 41 Bit-wise Shift Operation in C • Shift Right >> 0x9A >> 3 = 0x13 shift right 3 times 0x77 >> 4 = 0x07 shift right 4 times • Shift Left << 0x96 << 4 = 0x60 shift left 4 times 1001 1010  0001 0011 0111 0111  0000 0111 1001 0110  0110 0000
  • 42. 42 Table 7-3. Bit-wise Logic Operations for C
  • 43. 43 Example 7-19 Run the following program on your simulator and examine the results. Solution: #include <reg51.h> void main(void) { P0=0x35 & 0x0F; P0=0x04 | 0x68; P0=0x54 ^ 0x78; P0=~0x35; P0=0x9A >> 3; P0=0x77 >> 4; P0=0x96 << 4; }
  • 44. 44 Section 7.4 Data conversion programs in 8051 C
  • 45. 45 Data Conversion • Packed BCD to ASCII conversion – See Example 7-24 • ASCII to packed BCD conversion – See Example 7-25 • Checksum byte in ROM – See Example 7-26, 27, 28 (using +) • Binary to decimal and ASCII conversion in C – See Example 7-29( using /, %)
  • 46. 46 Table 7-4. ASCII Code for Digits 0-9
  • 47. Packed BCD to ASCII conversion Example 7-24 Write an 8051 C program to convert packed BCD 0x29 to ASCII and display the bytes on P1 and P2. 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;} 47
  • 48. ASCII to packed BCD conversion See Example 7-25 Write an 8051 C program to convert ASCII digits of ‘4’ and ‘7’ to packed BCD and display them on P1. 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;} 48
  • 49. Binary to decimal and ASCII conversion in C See Example 7-29( using /, %) Write an 8051 C program to convert 11111101 (FD hex) to decimaland 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;} 49
  • 50. 50 Section 7.5 Accessing code ROM space in 8051 C
  • 51. 51 To Store Data • We have three spaces to store data: 1. the 128 bytes RAM space with address range 00-7FH • If you declare variables (ex: char) to store data, C compiler will allocate a RAM space for these variable. 2. Use code space • External code memory (64K) + on-chip ROM (64K) • Data is embedded to code or is separated as a data section 3. External data memory for data • RAM or ROM • see Chapter 14
  • 52. 52 Data Stored in RAM • Where are the variables assigned by C compiler? • 8051 C compiler allocates RAM locations for variables in the following order: – Bank 0: addresses 0-7 – Individual variables: addresses 08H and beyond – Array elements: addresses right after variables • Array size is limited in 128 bytes RAM. – Stack: address right after array elements • Check your simulation tool
  • 53. 53 8052 RAM Space • 8052 has 256 bytes RAM. • Based on 8052 architecture, you should – Use the reg52.h header file. – Choose the 8052 option when compiling the program.
  • 54. 54 Using ROM to Store Data • To make C compiler use the code space (on-chip ROM) instead of RAM space, we can put the keyword “code” in front of the variable declaration. unsigned char mydata[] = “HELLO” • HELLO is saved in RAM. code unsigned char mydata[] = “HELLO” • HELLO is saved in ROM. • See Example 7-33
  • 55. 55 Example 7-33 (1/3) Compare and contrast the following programs and discuss the advantages and disadvantages of each one. Solution (a): #include <reg51.h> void main(void) { P1=“H”; P1=“E”; P1=“L”; P1=“L”; P1=“O”; } Data is embedded into code. Simple, short, not flexible.
  • 56. 56 Example 7-33 (2/3) Solution (b): #include <reg51.h> void main(void) { unsigned char mydata[]=“HELLO”; unsigned char z; for (z=0; z<5; z++) P1 = mydata[z]; } Data is stored in RAM and does not occupied ROM.
  • 57. 57 Example 7-33 (3/3) Solution (c): #include <reg51.h> void main(void) { code unsigned char mydata[]=“HELLO”; unsigned char z; for (z=0; z<5; z++) P1 = mydata[z]; } Data is stored in ROM. However, data and code are separate.
  • 59. 59 Serializing Data • Two ways to transfer a byte of data: 1. Using the serial port. • For PC. See Chapter 10. 2. To transfer data one bit a time and control the sequence of data and spaces in between them. • For LCD, ADC, ROM • Example 7-34
  • 60. 60 Example 7-34 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. 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; } } ACC P1.0 D7 D0
  翻译: