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

4 TH

Uploaded by

Vishva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

4 TH

Uploaded by

Vishva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Department of Computer and Communication Engineering

UNIT IV 8051 PROGRAMMING IN C

Cross compiler C -programming structure, Data types, memory models, infinite loops and
handling interrupts in C. Intel Hex file format. C - Programming for LED, LCD display,
temperature sensor with ADC, Measuring pulse width and frequency
,,

Compilers:
Compilers produce hex files that we download into the ROM of the
microcontroller. The size of the hex file produced by the compiler is
one of the main concerns of microcontroller programmers, for two
reasons:
 Microcontrollers have limited on-chip ROM.
 The code space for the 8051 is limited to 64K bytes.
How does the choice of programming language affect the
compiled pro- gram size? While Assembly language produces a
hex file that is much smaller than C, programming in Assembly
language is tedious and time consuming. C programming, on the
other hand, is less time consuming and much easier to write, but
the hex file size produced is much larger than if we used Assembly
language. The following are some of the major reasons for writing
programs inC instead of Assembly:
 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.

Compilers produce hex files that is


downloaded to ROM of micro controller
 The size of hex file is the main concern
 Microcontrollers have limited on-chip ROM
 Code space for 8051 is limited to 64K bytes
 C programming is less time consuming,but has larger hex file size

The reasons for writing programs in C :


 It is easier and less time consuming to write in C than Assembly
 C is easier to modify and update
 You can use code available in function libraries
 C code is portable to other microcontroller with little or no
modification

8051 PROGRAMMING IN
1
C data types for the 8051
Since one of the goals of 8051 C programmers is to create
smaller hex files, it is worthwhile to reexamine C data types for
the 8051. In other words, a good understanding of C data types for
the 8051 can help programmers to create smaller hex files. In this
section, we focus on the specific C data types that are most useful
and widely used for the 8051 microcontroller.

DATA TYPES
 Unsigned char
 Signed char
 Unsigned int
 Signed int
 Sbit (single bit)
 Bit and sfr

Unsigned char
 Since the 8051 is an 8-bit microcontroller, the character data type
is themost natural choice for many applications.
 The unsigned char is an 8-bit data type that takes a value in the
range of 0–255 (00–FFH).
 It is one of the most widelyused data types for the 8051. Counter value
ASCII characters
 C compilers use the signed char as the default if we do not put the keyword unsigned

Example 1
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;
}

Run the above program on your simulator to see how P1 displays values 00–FFHin binary.

8051 PROGRAMMING IN 165


,,

Example 2

Solution:
// Toggle P1 forever #include <reg51.h> void main(void)

for(;;) //repeat forever

P1=0x55;//0x indicates the data is in hex (binary)


P1=0xAA;

8051 PROGRAMMING IN
1
Signed char
 The signed char is an 8-bit data type that uses the most
significant bit(D7 of D7–D0) to represent the – or + value.
 The magnitude of the signed number, giving us values from –
128 to +127.
 If we do not use the keyword unsigned, the default is the signed
value. For that reason,we should stick with the unsigned char
unless the data needs to be represented as signed numbers. See
Example 4.

Example 4

Solution:
//sign numbers #include
<reg51.h>void main(void)
Example 3

Exa mple

Solution:

8051 PROGRAMMING IN
1
,,

Unsigned int
 Takes a value in the range of 0 to 65535(0000 -FFFFH)
 Define 16-bit variables such as memoryaddresses
 Set counter values of more than 256
 Since registers and memory accesses arein 8-bit chunks, the misuse of int variableswill
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,767.

Sbit (single bit)


The sbit keyword is a widely used 8051 C data type designed specifically to
access single-bit addressable registers. It allows access to the single bits of the SFR
registers.

Example 5

Solution:
#include <reg51.h> sbit MYBIT = P1^0;
//notice that sbit is
//declared outside of main
void main(void)
for (z=0;z<=50000; z++) MYBITMYBIT
= 0;

8051 PROGRAMMING IN
1
Bit and sfr
The bit data type allows access to single bits of bit-
addressable memory spaces 20–2FH.To access the byte-size SFR
registers, we use the sfr data type. We will see the use of sbit, bit,
and sfr data types in the next section. See Table 1.

Time delay
There are two ways to create a time delay in 8051 C:
1. Using a simple for loop
2. Using the 8051 timers

The 8051 design:


 The number of machine cycles and the number of clock periods
per machine cycle vary among different versions of the 8051/52
micro controller.
 The crystal frequency connected to the X1–X2 input pins:
 Compiler choice In the case of C programs, it is the C compiler that converts
the C statements and functions to Assembly language instructions.
 As a result, differ-ent compilers produce different code.

Table 1. Some Widely Used Data Types for 8051 C

Data Type Size in Bits Data Range/Usage


unsigned char 8-bit 0 to 255
char (signed) 8-bit −128 to +127

unsigned int 16-bit 0 to 65535


int (signed) 16-bit −32,768 to +32,767

sbit 1-bit SFR bit-addressable only


bit 1-bit RAM bit-addressable only
sfr 8-bit RAM addresses 80–FFH only

8051 PROGRAMMING IN
1
,,

Solution:
Example 6
Solution:

// 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++); //delay size unknown

P1=0xAA; For(x=0;x<40000;x++);
}

8051 PROGRAMMING IN
1
I/O PROGRAMMING IN 8051 C
In this section, we look at C programming of the I/O ports for the 8051.
We look at both byte and bit I/O programming.

Byte size I/O

In 8051, I/O operations are done using four Input/output ports P0, P1, P2, and P3,
each port is 8-bit port having 8 pins each. During RESET, all the ports are used as
input ports. When the port gets first 0, then it becomes an output port.

 Port 0 (Pin No 32 – Pin No 39)


 Port 1 (Pin 1 through 8)
 Port 2 (Pins 21 through 28)
 Port 3 (Pins 10 through 17)

Example 9
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.
Solution:

#include <reg51.h>

#define LED P2 //notice how we can define P2


Void main(void)
{
P1=00; //clear P1
LED=0; //clear P2
For(;;) //repeat forever
{
P1++; //increment P1
LED++; //increment P2
}
}

8051 PROGRAMMING IN
1
,,

Bit-addressable I/O programming


The I/O ports of P0–P3 are bit-addressable. We can access a
single bit without disturbing the rest of the port. We use the sbit
data type to access a single bit of P0–P3.In Px^y format where x
is the port 0, 1, 2, or 3, and y is the bit 0–7 of that port to include
the reg51.h file.
Example 12

Write an 8051 C program to toggle only bit P2.4 continuously without disturbing
The rest of the bits of P2.

Solution:

//toggling an individual bit

#include <reg51.h>

Sbit mybit = P2^4; //notice the way single bit is declared

Void main(void)

While(1)

Mybit=1; //turn on P2.4

Mybit=0; //turn off P2.4

8051 PROGRAMMING IN
1
,,

Accessing SFR addresses 80–FFH


Another way to access the SFR RAM space 80–FFH is to
use the sfr data type. We can also access a single bit of any SFR if
we specify the bit address. Both the bit and byte addresses for the
P0–P3 ports are given in Table 2.

Example 16
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.
Solution:
// Accessing Ports as SFRs using the sfr data type
sfr P0 = 0x80; //declaring P0 using sfr data
type sfr P1 = 0x90;
sfr P2 = 0xA0;
void MSDelay(unsigned int);
void main(void)
{
while(1) //do it forever
{
P0=0x55;
P1=0x55;
P2=0x55;
MSDelay(250); //250 ms delay
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++);
}

8051 PROGRAMMING IN
1
Using bit data type for bit-addressable RAM
The sbit data type is used for bit-addressable SFR registers
only. Sometimes we need to store some data in a bit- addressable
section of the data RAM space 20–2FH. To do that, we use the bit
data type.

LOGIC OPERATIONS IN 8051 C


One of the most important and powerful features of the C
language is its ability to perform bit manipulation. This section
describes the action of bit- wise logic operators and provides some
examples of how they are used.

Logical operators : AND (&&), OR (||), and NOT (!)


Bit-wise operators : AND (&), OR (|), EX-OR (^), Inverter (~), Shift Right (>>), and
Shift Left (<<)
Example:
SUM OF SERIES OF OF N BYTES OF DATA
ORG 0000H
SJMP 30H
ORG 30H
MOV R0,#30H
MOV R1,#05H
MOV R3,#00H
MOV A,@R0
L1: INC R0
ADD A,@R0
MOV R2,A
JNC L2
INC R2
L2: DJNZ R1, L1
END

8051 HARDWARE CONNECTION AND INTEL HEX FILE


1
,,

EXPLAINING THE INTEL HEX FILE

The Intel HEX file format is a text-based file format used to represent
binary data, particularly in the context of programming microcontrollers and other
programmable devices. It is widely used in the embedded systems and
microcontroller programming communities. The format is designed to be human-
readable while encoding binary data in a compact, efficient, and checksum-verified
manner.

Here's a brief overview of the structure and common features of an Intel HEX
file

 Record Types: An Intel HEX file consists of multiple records, each


starting with a colon ':' character. The most common record types are

 Data Record (Type 00): Contains data bytes and the starting address
for that data. The data is encoded in hexadecimal form.

 End of File Record (Type 01): Marks the end of the HEX file.

 Length Byte: The first byte following the colon (':') specifies the number
of bytes (n) in the record.

 Address Field: The next two to four bytes (depending on the HEX file
variant) represent the memory address where the data should be stored.
This field is used to specify where in the memory the data should be
loaded.

 Record Type: The next two bytes indicate the record type. The most
common types are 00 (Data Record) and 01 (End of File Record).

 Data Bytes: Following the record type, the next 2n bytes contain the
binary data. These bytes are typically represented in hexadecimal format
(two characters per byte).

 Checksum: The last two characters represent a checksum. It is calculated


from the length byte, address bytes, record type, and data bytes. It is used
to verify It is used to verify the integrity of the data.

EXAMPLE:

10 0100 00 214601360121470136007EFE09D21901 40

The colon (:) at the beginning indicates the start of a record.

The 10 following the colon represents the length of the data (16 bytes).

8051 HARDWARE CONNECTION AND INTEL HEX FILE


1
,,

0100 represents the address where this data should be stored. 00

is the record type (Data Record).

The following 16 characters are the data bytes in hexadecimal form.

40 at the end is the checksum.

Program list file for test program

The list file for the test program is given in Fig.8

LOC OBJ LINE


0000 1 ORG 0H
0000 758055 2 MAIN: MOV P0, #55H
0003 759055 3 MOV P1, #55H
0006 75A055 4 MOV P2, #55H
0009 7DFA 5 MOV R5, #250
000B 111C 6 ACALL MSDELAY
000D 7580AA 7 MOV P0, #0AAH
0010 7590AA 8 MOV P1, #0AAH
0013 75A0AA 9 MOV P2, #0AAH
0016 7DFA 10 MOV R5, #250
0018 111C 11 ACALL MSDELAY
001A 80E4 12 SJMP MAIN
13 ;--- THE 250 MILLISECOND DELAY.
14 MSDELAY:
001C 7C23 15 HERE3: MOV R4, #35
001E 7B4F 16 HERE2: MOV R3,#79
0020 DBFE 17 HERE1: DJNZ R3, HERE1
0022 DCFA 18 DJNZ R4, HERE2
0024 DDF6 19 DJNZ R5, HERE3
0026 22 20 RET
21 END

Analyzing Intel hex file


Figure 9 shows the Intel hex file for the test program whose list file is given in Figure 8.
Since the ROM burner (loader) uses the hex file to down- load the opcode into ROM, the hex
file must provide the following:
(1) the number of bytes of information to be loaded,
(2) the information itself, and
(3) the starting address where the

8051 HARDWARE CONNECTION AND INTEL HEX FILE


1
,,

Figure 9. Intel Hex File Test Program as Provided by the Assembler

Each line of the hex file consists of six parts. In Figure 9, we have
separated the parts to make it easier to analyze.

The following describes each part.

1. “:” Each line starts with a colon.


2. CC, the count byte. This tells the loader how many bytes are in the line.
3. AAAA is for the address. This is a 16-bit address.
4. TT is for type. This field is either 00 or 01.
5. DD....D is the real information (data or code). There is a maximum of
16 bytes in this part.
6. SS is a single byte. This last byte is the checksum byte is used for error
checking.

8051 HARDWARE CONNECTION AND INTEL HEX FILE


1
,,

INTERRUPT PROGRAMMING IN C

So far all the programs in this chapter have been written in Assembly.
In this section, we show how to program the 8051/52’s interrupts in 8051 C
language. In reading this section, it is assumed that you already know the
material in the first two sections of this chapter.

8051 C interrupt numbers


The 8051 C compilers have extensive support for the 8051 interrupts
with two major features as follows:
1. They assign a unique number to each of the 8051 interrupts, as shown in
Table 4.
2. It can also assign a register bank to an ISR. This avoids code overhead due
to the pushes and pops of the R0–R7 registers.
Example 14 shows how a simple interrupt is written in 8051 C. See also
Examples 15 through 17.
Table 4. 8051/52 Interrupt Numbers in C

Interrupt Name Numbers Used by 8051 C


External Interrupt 0 (INT0) 0
Timer Interrupt 0 (TF0) 1
External Interrupt 1 (INT1) 2
Timer Interrupt 1 (TF1) 3
Serial Communication (RI + TI) 4
Timer 2 (8052 only) (TF2) 5

INTERRUPTS PROGRAMMING IN ASSEMBLY AND C


3
,,

Example 14
Write a C program that continuously gets a single bit of data from P1.7 and sends it
to P1.0, while simultaneously creating a square wave of 200 µs period on pin P2.5.
Use timer 0 to create the square wave. Assume that XTAL = 11.0592 MHz.
Solution:
We will use Timer 0 in mode 2 (auto-reload). One half of the period is 100 µs,
100/1.085 µs = 92, and TH0 = 256 – 92 = 164 or A4H

#include <reg51.h>

sbit SW = P1^7;
sbit IND = P1^0;
sbit WAVE = P2^5;

void timer0(void) interrupt 1


{
WAVE = ~WAVE; //toggle pin
}

void main()
{
SW = 1; //make switch input
TMOD = 0x02;
TH0 = 0xA4; //TH0 = -92
IE = 0x82; //enable interrupts for timer 0
while(1)
{
IND = SW; //send switch to LED
}
}

200 µs / 2 = 100 µs

100 µs / 1.085 µs = 92

8051

Switch

INTERRUPTS PROGRAMMING IN ASSEMBLY AND C


3
,,

Example 17
Write a C program using interrupts to do the following:
(a) Generate a 10000 Hz frequency on P2.1 using T0 8-bit auto-reload.
(b) Use Timer 1 as an event counter to count up a 1-Hz pulse and display it on P0.
The pulse is connected to EX1.
Assume that XTAL = 11.0592 MHz. Set the baud rate at 9600.
Solution:
#include <reg51.h>

sbit WAVE = P2^1;


unsigned char cnt;

void timer0() interrupt 1


{
WAVE = ~WAVE; //toggle pin
}
void timer1() interrupt 3
{
cnt++; //increment counter
P0 = cnt; //display value on pins
}

void main()
{
cnt = 0; //set counter to zero
TMOD = 0x42;
TH0 = 0x-46; //10000 Hz
IE = 0x86; //enable interrupts
TR0 = 1; //start timer 0
TR1 = 1; //start timer 1
while(1); //wait until interrupted
}

1/10000 Hz = 100 µs

100 µs/2 = 50 µs

50 µs/1.085 µs = 46
//

/
from

INTERRUPTS PROGRAMMING IN ASSEMBLY AND C


3
,,

LCD INTERFACING

This section describes the operation modes of LCDs, then describes


how to program and interface an LCD to an 8051 using Assembly and C.

LCD operation
In recent years, the LCD is finding widespread use replacing LEDs (seven-
segment LEDs or other multisegment LEDs). This is due to the follow- ing reasons:
1. The declining prices of LCDs.
2. The ability to display numbers, characters, and graphics. This is in
contrast to LEDs, which are limited to numbers and a few characters.
3. Incorporation of a refreshing controller into the LCD, thereby relieving
the CPU of the task of refreshing the LCD. By contrast, the LED must be
refreshed by the CPU (or in some other
way) to keep displaying the data. Table 1. Pin Descriptions for LCD
4. Ease of programming for characters and Pin Symbol I/O Description
graphics.
1 Vss -- Ground
LCD pin descriptions 2 Vcc -- +5 V power supply
3
The LCD discussed in this section has Vee -- Power supply
14 pins. The function of each pin is given in to control contrast
4
Table 1. Figure 1 shows the pin positions for RS I RS = 0 to select
various LCDs. command register,
RS = 1 to select
VCC, VSS, and VEE data register
While VCC and VSS provide +5 V and 5
R/W I R/W = 0 for write,
ground, respectively, VEE is used for control- ling R/W = 1 for read
LCD contrast. 6
E I Enable
7
RS, register select DB0 I/O The 8-bit data bus
8
There are two very important registers DB1 I/O The 8-bit data bus
9
inside the LCD. The RS pin is used for their DB2 I/O The 8-bit data bus
selection as follows. 10
DB3 I/O The 8-bit data bus
11
If RS = 0,code register is selected, DB4 I/O The 8-bit data bus
allowing the user to clear display. 12
DB5 I/O The 8-bit data bus
13
If RS = 1, the data register is DB6 I/O The 8-bit data bus
selected, allowing the user to send data. 14
DB7 I/O The 8-bit data bus

LCD AND KEYBOARD


3
,,

R/W, read/write
Table 2. LCD Command Codes Code R/W input allows the user to
Command to LCD Instruction (Hex) Register write information to the LCD or read
1 Clear display screen information from it. R/W = 1 when
reading; R/W = 0 when writing.
2
Return home E, enable
The enable pin is used by the
4 Decrement cursor (shift cursor to left) LCD to latch information presented to its
6 Increment cursor (shift cursor to right) data pins. When data is supplied to data
pins, a high-to-low pulse must be applied
5 Shift display right
to this pin in order for the LCD to latch
7 in the data present at the data pins. This
Shift display left pulse must be a minimum of 450 ns
wide.
8 Display off, cursor off D0–D7
A Display off, cursor on
The 8-bit data pins, D0–D7, are
C Display on, cursor off used to send information to the LCD or
E Display on, cursor blinking off read the contents of the LCD’s internal
F Display on, cursor blinking registers. To display letters and
10 Shift cursor position to left numbers,
we send ASCII codes for the letters A–Z,
14 Shift cursor position to right a–z, and numbers 0–9 to these pins while
18 Shift the entire display to the left making RS = 1.
1C Shift the entire display to the right
80 Force cursor to beginning of 1st line
C0 Force cursor to beginning of 2nd line
38 2 lines and 5x7 matrix

LCD
8051 +5 V
P1.0 D0 Vcc

Vee 10 K POT
P1.7 D7
RS R/W E Vss

P2.0 P2.1 P2.2

Figure 2. LCD Connections

3
L
LCD AND KEYBOARD
3
,,

Sending commands and data to LCDs with a time delay


To send any of the commands from Table 2 to the LCD, make pin
RS = 0. For data, make RS = 1. Then send a high-to-low pulse to the E pin to
enable the internal latch of the LCD. This is shown in Program 1. See Figure 2
for LCD connections.

;calls a time delay before sending next data/command


; P1.0-P1.7 are connected to LCD data pins D0-D7
; P2.0 is connected to RS pin of LCD
; P2.1 is connected to R/W pin of LCD
; P2.2 is connected to E pin of LCD
ORG 0H
MOV A,#38H ;init. LCD 2 lines,5x7 matrix
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#0EH ;display on, cursor on
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#01 ;clear LCD
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#06H ;shift cursor right
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#84H ;cursor at line 1,pos. 4
ACALL COMNWRT ;call command subroutine
ACALL DELAY ;give LCD some time
MOV A,#'N' ;display letter N
ACALL DATAWRT ;call display subroutine
ACALL DELAY ;give LCD some time
MOV A,#'O' ;display letter O
ACALL DATAWRT ;call display subroutine
AGAIN: SJMP AGAIN ;stay here
COMNWRT: ;send command to LCD
MOV P1,A ;copy reg A to port1
CLR P2.0 ;RS=0 for command
CLR P2.1 ;R/W=0 for write
SETB P2.2 ;E=1 for high pulse
ACALL DELAY ;give LCD some time
CLR P2.2 ;E=0 for H-to-L pulse
RET
DATAWRT: ;write data to LCD
MOV P1,A ;copy reg A to port1
SETB P2.0 ;RS=1 for data
CLR P2.1 ;R/W=0 for write

LCD AND KEYBOARD


3
// The delay function will generate a delay of 1mS.

PROGRAM FOR BLINKING THE LED


#include<reg51.h>
#define ON 1
#define OFF 0
sbit led=P2^0;
unsigned int i=0;
void delay();
void delay()
{
TMOD = 0x01; // Timer0 mode1
TH0 = 0xFC; //initial value for 1ms
TL0 = 0x66;
TR0 = 1; // timer start
while(TF0==0); // check overflow condition
TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
}
void main()
{
while(1)
{
led = ON; for(i=0;i<1000;i+
+)
{
delay();
}
led = OFF; for(i=0;i<1000;i+
+)
{
delay();
}}}

LCD AND KEYBOARD


3
KEYBOARD INTERFACING

Keyboards and LCDs are the most widely used input/output devices of the
8051, and a basic understanding of them is essential. In this section, we first
discuss keyboard fundamentals, along with key press and key detection
mechanisms. Then we show how a keyboard is interfaced to an 8051.

Interfacing the keyboard to the 8051


 At the lowest level, keyboards are organized in a matrix of rows and columns.
 The CPU accesses both rows and columns through ports; therefore, with two
8-bit ports, an 8 x 8 matrix of keys can be connected to a microprocessor.
 When a key is pressed, a row and a column make a contact; otherwise,
thereis no connection between rows and columns.
 In IBM PC keyboards, a single microcontroller (consisting of a
microprocessor, RAM and EPROM, and sev- eral ports all on a single chip)
takes care of hardware and software interfacing of the keyboard.
 In such systems, it is the function of programs stored in the EPROM of the
microcontroller to scan the keys continuously, identify which one has been
activated, and present it to the motherboard.
 In this section, we look at the mechanism by which the 8051 scans and
identifies the key.

LCD AND KEYBOARD


3
Temperature sensors
 Transducers convert physical data such as temperature, light intensity, flow, and speed to
electrical signals.
 Depending on the transducer, the output produced is in the form of voltage, current,
resistance, or capacitance.
 For example, temperature is converted to electrical signals using a transducer called a
thermistor.
 A thermistor responds to temperature change by changing resistance, but its response is not
linear, as seen in
 Simple and widely used linear temperature sensors include the LM34 and LM35
series from National Semiconductor Corp..

LM34 and LM35 temperature sensors

The sensors of the LM34 series are precision integrated-circuit temper-ature sensors
whose output voltage is linearly proportional to the Fahrenheit temperature.

Table 9. LM34 Temperature Sensor Series Selection Guide


Signal conditioning and interfacing the LM35 to the 8051

 Transducers convert physical data such as temperature, light intensity, flow,


and speed to electrical signals. Depending on the transducer, the output
produced is in the form of voltage, current, resistance, or capacitance.
 Signal conditioning is widely used in the world of data acquisition.
The most common transducers produce an output in the form of voltage,
current, charge, capacitance, and resistance.
 However, we need to convert these signals to voltage in order to send input
to an A-to-D converter.
SeeThis conversion (modification) is commonly called signal conditioning.
Signal conditioning can be a current-to-voltage conversion or a signal
amplification.For example, the thermistor changes resistance with temperature.
 The change of resistance must be translated into voltages in order to be of
any use to an ADC.

Signal conditioning and interfacing

ADC, DAC, AND SENSOR INTERFACING


3
,,.,;.

LC

D0 D7
RS R/W E
Vcc 10
+5 Vss
V P2.0 P2.1 P2.2 Vcc
+5 V

10
P1.7 D7
P1.7

Figure 21. 8051 Connection to ADC0848 and Temperature Sensor

PROGRAM TO MONITOR ROOM TEMPERATURE


float temp;
void setup()
{
Serial.begin(9600);
}
Void loop()
{
Temp=analogRead(A0);
Temp=temp*0.48828125;
Serial.print(“TEMPERATURE: “);
Serial.print(temp);
Serial.print(“*C”);
Serial.println();
Delay(1000);
}

ADC, DAC, AND SENSOR INTERFACING


3
Reading and displaying temperature
The following two programs show code for displaying temperature in
both Assembly and C. The programs correspond to Figure 21.

;Program 1
;Assembly code to read temperature, convert it,
;and put it on P0 with some delay
RD BIT P2.5 ; RD
WR BIT P2.6 ; WR (start conversion)
INTR BIT P2.7; end-of-conversion
MYDATA EQU P1 ;P1.0-P1.7=D0-D7 of the ADC0848
MOV P1,#0FFH ;make P1 = input
SETB INTR
BACK: CLR WR ;WR=0
SETB WR ;WR=1 L-to-H to start conversion
HERE: JB INTR,HERE ;wait for end of conversion
CLR RD ;conversion finished,enable RD
MOV A,MYDATA ;read the data from ADC0848
ACALL CONVERSION ;hex-to-ASCII conversion
ACALL DATA_DISPLAY ;display the data
SETB RD ;make RD=1 for next round
SJMP BACK
CONVERSION:
MOV B,#10
DIV AB
MOV R7,B ;least significant byte
MOV B,#10
DIV AB
MOV R6,B
MOV R5,A ;most significant byte
RET
DATA_DISPLAY
MOV P0,R7
ACALL DELAY
MOV P0,R6
ACALL DELAY
MOV P0,R5
ACALL DELAY
RET

//Program 2
//C code to read temp from ADC0848, convert it to
//decimal, and put it on P0 with some delay
#include <reg51.h>
sbit RD = P2^5;

ADC, DAC, AND SENSOR INTERFACING


3
sbit WR = P2^6;
sbit INTR = P2^7;
sfr MYDATA = P1; //P1 connected to D0-D7 of ‘848
void ConvertAndDisplay(unsigned char value);
void MSDelay(unsigned int value);
void main()
{
MYDATA = 0xFF; //make P1 and input
INTR = 1; //make INTR and input
RD = 1; //set RD high
WR = 1; //set WR high
while(1)
{
WR = 0; //send WR pulse
WR = 1;
while(INTR == 1); //wait for EOC
RD = 0; //send RD pulse
value = MYDATA; //read value from ADC0848
ConvertAndDisplay(value);
RD = 1;
}
}
void ConvertAndDisplay(unsigned char value)
{
unsigned char x,d1,d2,d3;
x=value/10;
d1=value%10;
d3=x/10
P0=d1; //LSByte
MSDelay(250);
P0=d2;
MSDelay(250);
P0=d3; //MSByte
MSDelay(250);
}
void MSDelay(unsigned int value)
{
unsigned char x,y;
for(x=0;x<value;x++)
for(y=0;y<1275;y++);
}

ADC, DAC, AND SENSOR INTERFACING


3
ADC, DAC, AND SENSOR INTERFACING
3
,,.
,,.

You might also like