embedded training report(mcs 51)

83
RADIANT INSTITUTE OF ENGINEERING AND TECHNOLOGY ABOHAR SUBMITTED BY: NAME: Gurwinder Singh Branch : ECE Roll No.: 100930420230 1

Upload: gurwinder-singh

Post on 06-May-2015

1.703 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Embedded training report(mcs 51)

RADIANT INSTITUTE OF ENGINEERING AND

TECHNOLOGY

ABOHAR

SUBMITTED BY:

NAME: Gurwinder Singh

Branch : ECE

Roll No.: 100930420230

1

Page 2: Embedded training report(mcs 51)

2

Page 3: Embedded training report(mcs 51)

First of all I would like to thank almighty GOD who has given this wonderful gift of life to us.

He is the one who is guiding us in right direction to follow noble path of humanity. In my Six

weeks industrial training it is a wonderful experience to be a part of Beri Institute of

3

Page 4: Embedded training report(mcs 51)

Technology. Where I got the opportunity to work under brilliant minds. I owe my deep regards

for the supporting and kind staff authorities who are helping me in my lean patches during these

six weeks. The knowledge I am gaining through out my studies have the practical

implementation during this period. I am grateful to all the staff of BIT and for their timely

support and sharing of their experience with me. I would like to express my heartiest concern for

Er. Bikram Beri for his able guidance and for his inspiring attitude, praiseworthy attitude and

honest support. Not to forget the pain staking efforts of our college training and placement cell

and specially my training and placement officer Mr………….. Last but not the least I would

express my utmost regards for the ECE department of our Institute.

Beri Institute of Technologies(BIT) is an organization which is established in the field of

Computer hardware sporte, Network training and Embedded systems. We provide Support and

training in the field of networking solutions (LINUX) and embedded systems (Micro controller

based design, Electronics system design).

BIT also provide Technical Research & Development support and consultancy to some

Electronics companies.

THEIR TEAM

4

Page 5: Embedded training report(mcs 51)

Presently they have a strong technical team of certified professionals for catering to these

solutions and have presence in Abohar and Punjab. They have skilled team of engineers who are

experienced in design, programming.

Support Area (network solutions)

a) LINUX / UNIX networks

b) Radio Links

c) Security Solutions

Design Services (Embedded systems)

a) AVR family

b) MCS 51

c) ELECTRONIC SYSTEM DESIGN

5

Page 6: Embedded training report(mcs 51)

6

Page 7: Embedded training report(mcs 51)

1) Difference between microprocessors and microcontrollers

2) Disadvantages of processors over controllers

2) Embedded System using microcontrollers

Embedded System

Characteristics

History of 8051

Architecture

Pin configuration

AT89s52

Block Diagram

Features

7

Page 8: Embedded training report(mcs 51)

Pin description

On chip peripherals

Interrupts

Serial communication

Timer and counter

Advantages of C over Assembly language

Off chip peripherals

LCD (JHD162A)

RTC(DS12887)

ADC(ADC 0804)

8255PPI

Interfacing LCD to AT89s52

Serial communication B/W AT89s52 & PC

Interfacing of ADC(0804) with AT89s52 Microcontroller

Interfacing of RTC with AT89s52 Microcontroller

Interfacing of 8255 with AT89s52 Microcontroller

8

Page 9: Embedded training report(mcs 51)

DIFFERENCE BETWEEN MICROPROCESSORS AND

MICROCONTROLLERS

A Microprocessor is a general purpose digital computer central

processing unit(C.P.U) popularly known as CPU on the chip. The

Microprocessors contain no RAM, no ROM, and no I/P O/P ports on

the chip itself.

On the other hand a Microcontroller has a C.P.U(microprocessor) in

addition to a fixed amount of RAM, ROM, I/O ports and a timer all on

a single chip.

In order to make a Microprocessor functional we must add RAM,

ROM, I/O Ports and timers externally to them,ie any amount of

external memory can be added to it.

But in controllers there is a fixed amount of memory which makes

them ideal for many applications.

9

Page 10: Embedded training report(mcs 51)

The Microprocessors have many operational codes(opcodes) for

moving data from external memory to the C.P.U

Whereas Microcontrollers may have one or two operational codes.

DISADVANTAGES OF PROCESSORS OVER CONTROLLERS

System designed using Microprocessors are bulky

They are expensive than Microcontrollers

We need to add some external devices such as PPI chip, Memory,

Timer/counter chip, Interrupt controller chip,etc. to make it functional.

10

Page 11: Embedded training report(mcs 51)

EMBEDDED SYSTEMS

An embedded system:

Employs a combination of software & hardware to perform a specific function.

Is a part of a larger system which may not be a “computer”.

Works in a reactive & time constrained environment.

CHARACTERISTICS:

Single functioned

Executes a single program, repeatedly.

Tightly-constrained

Low power, low cost, small, fast etc.

Reactive & real time

Continually reacts to the changes in the system’s environment.

Must compute certain result in real-time without delay

Tools Used for Embedded System:

For Assembly Language:-

11

Page 12: Embedded training report(mcs 51)

8051 Assembler cum Simulator

ISP-Flash Programmer Version 3.0a -Hex File Downloader (Machine code gets burned in

controller) -

For C Language:-

Programming Environment- Programmers Notepad 2

Small Device C Compiler

ISP-Flash Programmer Version 3.0a -Hex File Downloader (Machine code gets burned in

controller)

Embedded System Applications:-

Consumer electronics, e.g., cameras, cell phones etc.

Consumer products, e.g. washers, microwave ovens etc.

Automobiles (anti-lock braking, engine control etc.)

Industrial process controller & defense applications.

Computer/Communication products, e.g. printers, FAX machines etc.

Medical Equipments.

ATMs

Aircrafts

elevators

MICROCONTROLLERS

History of 8051

Intel Corporation introduced an 8-bit microcontroller called 8051 in 1981 this controller had 128

bytes of RAM, 4k bytes of on chip ROM, two timers, one serial port, and four ports all are on

single chip. The 8051 is an 8 bit processor, meaning that the CPU can work on only 8 bit data at

a time. Data larger than 8 bits broken into 8 bit pieces to be processed by CPU. It has for I/O 8 bit

wide.

12

Page 13: Embedded training report(mcs 51)

Features of the 8051:-

Feature Quantity

ROM 4K bytes

RAM 128 bytes

Timer 2

I/O pins 32

Serial port 1

Interrupt sources 6

8051 Architecture Overview

The 8051 family is one of the most common microcontroller architectures used worldwide.8051

based microcontrollers are offered in hundreds of variants from many different silicon

manufacturers.

The 8051 is based on an 8-bit CISC core with Harvard architecture. It's an 8-bit CPU, optimized

for control applications with extensive Boolean processing (single-bit logic capabilities), 64K

program and data memory address space and various on-chip peripherals.

The 8051 microcontroller family offers developers a wide variety of high-integration and

Cost-effective solutions for virtually every basic embedded control application. From traffic

control equipment to input devices and computer networking products, 8051 microcontrollers

deliver high performance together with a choice of configurations and options matched to the

special needs of each application. Whether it's low power operation, higher frequency

performance, expanded on-chip RAM, or an application-specific requirement, there's a version of

the 8051 microcontroller that's right for the job.

When it's time to upgrade product features and functionality, the 8051 architecture puts you on

the first step of a smooth and cost-effective upgrade path - to the enhanced performance of the

151 and 251 microcontrollers

13

Page 14: Embedded training report(mcs 51)

14

Page 15: Embedded training report(mcs 51)

15

Page 16: Embedded training report(mcs 51)

Pin configuration of 8051

16

Page 17: Embedded training report(mcs 51)

Description of ports

There are four ports P0, P1, P2 and P3 each use 8 pins, making them 8-bit ports. All the ports

upon RESET are configured as output, ready to be used as output ports. To use any of these ports

as an input port, it must be programmed.

Port 0:- Port 0 occupies a total of 8 pins (pins 32-39) .It can be used for input or output. To use

the pins of port 0 as both input and output ports, each pin must be connected externally to a 10K

ohm pull-up resistor. This is due to the fact that P0 is an open drain, unlike P1, P2, and P3.Open

drain is a term used for MOS chips in the same way that open collector is used for TTL chips.

With external pull-up resistors connected upon reset, port 0 is configured as an output port. For

example, the following code will continuously send out to port 0 the alternating values 55H and

AAH.

MOV A, #55H

BACK: MOV P0, A

ACALL DELAY

CAPL A

SJMP BACK

Port 0 as input: - With resistors connected to port 0, in order to make it an input, the port must

be programmed by writing 1 to all the bits. In the following code, port 0 is configured first as an

input port by writing 1's to it, and then data is received from the port and sent to P1.                   

                       

17

Page 18: Embedded training report(mcs 51)

                     

MOV A, #0FFH         ; A = FF hex

                        MOV P0, A                ; make P0 an input port

                BACK: MOV A, P0     ; get data from P0 

                        MOV P1, A                ; send it to port 1

                         SJMP BACK 

Dual Role of Port 0:-Port 0 is also designated as AD0-AD7, allowing it to be used for both

address and data. When connecting an 8051/31 to an external memory, port 0 provides both

address and data. The 8051 multiplexes address and data through port 0 to save pins. ALE

indicates if P0 has address or data. When ALE = 0, it provides data D0-D7, but when ALE =1 it

has address and data with the help of a 74LS373 latch.

Port 1:- Port 1 occupies a total of 8 pins (pins 1 through 8). It can be used as input or output. In

contrast to port 0, this port does not need any pull-up resistors since it already has pull-up

resistors internally. Upon reset, Port 1 is configured as an output port. For example, the following

code will continuously send out to port1 the alternating values

18

Page 19: Embedded training report(mcs 51)

55h & AAh

MOV A, #55H; A = 55 hex

BACK: MOV P1, A; send it to Port 1

ACALL DELAY ; call delay routine

CPL A; make A=0

SJMP BACK

Port 1 as input:-To make port1 an input port, it must programmed as such by writing 1 to all its

bits. In the following code port1 is configured first as an input port by writing 1’s to it, then data

is received from the port and saved in R7 ,R6 & R5.

MOV A, #0FFH   ; A=FF HEX

MOV P1, A         ; make P1 an input port by writing all 1’s to it

MOV A, P1           ; get data from P1

MOV R7, A         ; save it in register R7

ACALL DELAY    ; wait

MOV A, P1         ; get another data from P1

MOV R6, A          ; save it in register R6

ACALL DELAY     ; wait

MOV A, P1          ; get another data from

MOV R5, A           ; save it in register R5                   

19

Page 20: Embedded training report(mcs 51)

Port 2:-Port 2 occupies a total of 8 pins (pins 21- 28). It can be used as input or output. Just like

P1, P2 does not need any pull-up resistors since it already has pull-up resistors internally. Upon

reset, Port 2 is configured as an output port. For example, the following code will send out

continuously to port 2 the alternating values 55h and AAH. That is all the bits of port 2 toggle

continuously.

     MOV A, #55H          ; A = 55 hex

        BACK: MOV P2, A                     ; send it to Port 2

ACALL DELAY        ; call delay routine   

  CPL A                              ; make A=0

                    SJMP BACK                           

Port 2 as input: - To make port 2 an input, it must programmed as such by writing 1 to all its

bits. In the following code, port 2 is configured first as an input port by writing 1’s to it. Then

data is received from that port and is sent to P1 continuously.

    MOV A, #0FFH     ; A=FF hex

MOV P2, A           ; make P2 an input port by writing all 1’s to it

  BACK: MOV A, P2          ; get data from P2

  MOV P1, A          ; send it to Port1

                SJMP BACK         ; keep doing that

Dual role of port 2:- In systems based on the 8751, 8951, and DS5000, P2 is used as simple I/O.

However, in 8031-based systems, port 2 must be used along with P0 to provide the 16-bit address

for the external memory. As shown in pin configuration 8051, port 2 is also designed as A8-A15,

indicating the dual function. Since an 8031 is capable of accessing 64K bytes of external

memory, it needs a path for the 16 bits of the address. While P0 provides the lower 8 bits via A0-

A7, it is the job of P2 to provide bits A8-A15 of the address. In other words, when 8031 is

20

Page 21: Embedded training report(mcs 51)

connected to external memory, P2 is used for the upper 8 bits of the 16 bit address, and it cannot

be used for I/O.

Port 3:- port 3 occupies a total of 8 pins, pins 10 through 17. It can be used as input or output.

P3 does not need any pull-up resistors, the same as P1 and P2 did not. Although port 3 is

configured as an output port upon reset. Port 3 has the additional function of providing some

extremely important signals such as interrupts. This information applies both 8051 and 8031

chips. There functions are as follows:-

        P3.0 and P3.1 are used for the RxD and TxD serial communications signals. Bits P3.2 and

P3.3 are set aside for external interrupts. Bits P3.4 and P3.5 are used for timers 0 and 1. Finally

P3.6 and P3.7 are used to provide the WR and RD signals of external memories connected in

8031 based systems.    

 

21

Page 22: Embedded training report(mcs 51)

Single bit addressability of ports:-

There are times that we need to access only 1 or 2 bits of the port instead of the entire 8 bits. A

powerful feature of 8051 I/O ports is their capability to access individual bits of the port without

altering the rest of the bits in that port.

For example, the following code toggles the bit p1.2 continuously.

  BACK:  CPL P1.2                   ; complement p1.2 only

  ACALL DELAY

       SJMP BACK

      Notice that P1.2 is the third bit of P1, since the first bit is P1.0, the second bit is P1.1, and so

on. Notices in example of those unused portions of port1 are undisturbed. Table bellow shows

the bits of 8051 I/O ports. This single bit addressability of I/O ports is one of the features of the

8051 microcontroller.

 

22

Page 23: Embedded training report(mcs 51)

AT89s52

AT89S52 is an ATMEL controller with the core of Intel MCS-51. It has same pin

configuration as give above.

The AT89S52 is a low-power, high-performance CMOS 8-bit microcomputer with 8K

bytes of Downloadable Flash programmable and erasable read only memory and 2K

bytes of EEPROM. The device is manufactured using Atmel’s high density nonvolatile

memory technology and is compatible with the industry standard 80C51 instruction set

and pin out. The on-chip Downloadable Flash allows the program memory to be

reprogrammed in-system through an SPI serial interface or by a conventional

nonvolatile memory programmer. By combining a versatile 8-bit CPU with Downloadable

Flash on a monolithic chip, the Atmel AT89S52 is a powerful microcomputer which

provides a highly flexible and cost effective solution to many embedded control 23

Page 24: Embedded training report(mcs 51)

applications. The AT89S52 provides the following standard features: 8K bytes of

Downloadable Flash, 2K bytes of EEPROM, 256 bytes of RAM, 32 I/O lines,

programmable watchdog timer, two Data Pointers, three 16-bit timer/counters, a six-

vector two-level interrupt, a full duplex serial port, on-chip oscillator, and clock circuitry.

In addition, the AT89S52 is designed with static logic for operation down to zero

frequency and supports two software selectable power saving modes. The Idle Mode

stops the CPU while allowing the RAM, timer/counters, serial port, and interrupt system

to continue functioning. The Power down Mode saves the RAM contents but freezes the

oscillator, disabling all other chip functions until the next interrupt or hardware reset.

The Downloadable Flash can be changed a single byte at a time and is accessible

through the SPI serial interface. Holding RESET active forces the SPI

bus into a serial programming interface and allows the program memory to be written to

or read from unless Lock Bit 2 has been activated.

Features• Compatible with MCS-51™Products

• 8K bytes of In-System Reprogrammable Downloadable Flash Memory

- SPI Serial Interface for Program Downloading

- Endurance: 1,000 Write/Erase Cycles

• 4.0V to 5.5V Operating Range

• Fully Static Operation: 0 Hz to 33 MHz

• Three-Level Program Memory Lock

• 256 x 8 bit Internal RAM

• 32 Programmable I/O Lines

• Three 16 bit Timer/Counters

• Eight Interrupt Sources

• Full Duplex UART Serial Channel

• Low Power Idle and Power Down Modes

• Interrupt Recovery From Power Down Mode

• Watchdog Timer

• Dual Data Pointer

• Power off Flag

RST24

Page 25: Embedded training report(mcs 51)

Reset input. A high on this pin for two machine cycles while the oscillator is running

resets the device.

ALE/PROG

Address Latch Enable is an output pulse for latching the low byte of the address during

accesses to external memory. This pin is also the program pulse input (PROG) during

Flash programming. In normal operation, ALE is emitted at a constant rate of 1/ 6 the

oscillator frequency and may be used for external timing or clocking purposes. Note,

however, that one ALE pulse is skipped during each access to external data memory. If

desired, ALE operation can be disabled by setting bit 0 of SFR location 8EH. With the bit

set, ALE is active only during a MOVX or MOVC instruction. Otherwise, the pin is

weakly pulled high. Setting the ALE-disable bit has no effect if the microcontroller is in

external execution mode.

PSEN

Program Store Enable is the read strobe to external program memory. When the

AT89S8252 is executing code from external program memory, PSEN is activated twice

each machine cycle, except that two PSEN activations are skipped during each access

to external data memory.

EA/VPP

External Access Enable. EA must be strapped to GND in order to enable the device to

fetch code from external program memory locations starting at 0000H up to FFFFH.

Note, however, that if lock bit 1 is programmed, EA will be internally latched on reset.

EA should be strapped to VCC for internal program executions. This pin also receives

the 12-volt programming enable voltage (VPP) during Flash programming when 12-volt

programming is selected

XTAL1

Input to the inverting oscillator amplifier and input to the internal clock operating circuit.

25

Page 26: Embedded training report(mcs 51)

XTAL2

Output from the inverting oscillator amplifier.

Circuit representing external crystal

Timer and counter description

26

Page 27: Embedded training report(mcs 51)

Timer 0 and 1:

Timer 0 and Timer 1 in the AT89S52 operate the same way as Timer 0 and Timer 1 in

the AT89C51, AT89C52 and “Timer/Counters.”

Timer 2:

Timer 2 is a 16 bit Timer/Counter that can operate as either a timer or an event counter.

The type of operation is selected by bit C/T2 in the SFR T2CON (shown in Table 2).

Timer 2 has three operating modes: capture, auto-reload (up or down counting), and

baud rate generator. The modes are selected by bits in T2CON, as shown in Table 8.

Timer 2 consists of two 8-bit registers, TH2 and TL2. In the Timer function, the TL2

register is incremented every machine cycle. Since a machine cycle consists of 12

oscillator periods, the count rate is 1/12 of the oscillator frequency. In the Counter

function, the register is incremented in response to a 1-to-0 transition at its

corresponding external input pin, T2. In this function, the external input is sampled

during S5P2 of every machine cycle. When the samples show a high in one cycle and a

low in the next cycle, the count is incremented. The new count value appears in the

register during S3P1 of the cycle following the one in which the transition was detected.

Since two machine cycles (24 oscillator periods) are required to recognize a 1-to-0

transition, the maximum count rate is 1/24 of the oscillator frequency. To ensure that a

given level is sampled at least once before it changes, the level should be held for at

least one full machine cycle

Interrupts:

27

Page 28: Embedded training report(mcs 51)

The AT89S52 has a total of six interrupt vectors: two external interrupts (INT0 and

INT1), three timer interrupts (Timers 0, 1, and 2), and the serial port interrupt. Each of

these interrupt sources can be individually enabled or disabled by setting or clearing a

bit in Special Function Register IE. IE also contains a global disable bit, EA, which

disables all interrupts at once. In the AT89C51, bit position IE.5 is also unimplemented.

User software should not write 1s to these bit positions, since they may be used in

future AT89 products. Timer 2 interrupt is generated by the logical OR of bits TF2 and

EXF2 in register T2CON. Neither of these flags is cleared by hardware

when the service routine is vectored to. In fact, the service routine may have to

determine whether it was TF2 or EXF2 that generated the interrupt, and that bit will have

to be cleared in software. The Timer 0 and Timer 1 flags, TF0 and TFI, are set at S5P2

of the cycle in which the timers overflow. The values are then polled by the circuitry in

28

Page 29: Embedded training report(mcs 51)

the next cycle. However, the Timer 2 flag, TF2, is set at S2P2 and is polled in the same

cycle in which the timer overflows.

Interrupt Registers:

The global interrupt enable bit and the individual interrupt enable bits are in the IE

register. In addition, the individual interrupt enable bit for the SPI is in the SPCR

register. Two priorities can be set for each of the six

interrupt sources in the IP register.

CODE FOR INTERRUPTS

#include<at89s8252.h>

void en_int(void);

void delay(unsigned int i);

void main (void)

{

P1=0x00;

INT0=0;

en_int();

}

void en_int(void)

{

EA=1;

EX0=1;

}

void isr_intr (void) interrupt 0

{

29

Page 30: Embedded training report(mcs 51)

if(INT0==0)

{

while(1)

{

P1_0=1;

delay(1000000);

delay(1000000);

delay(1000000);

delay(1000000);

P1_0=0;

delay(1000000);

delay(1000000);

delay(1000000);

delay(1000000);

}

}

}

void delay(unsigned int i)

{

while(i!=0)

{

i--;

}

}

Advantages of C over Assembly language programming

Knowledge of the processor instruction set is not required.

Details like register allocation and addressing of memory and data is managed by

the compiler.

30

Page 31: Embedded training report(mcs 51)

Programs get a formal structure and can be divided into separate functions.

Programming and program test time is drastically reduced, this increases

efficiency.

Keywords and operational functions can be used that come closer to how humans

think.

The supplied and supported C libraries contain many standard routines such as

numeric conversions.

Reusable code: Existing program parts can be more easily included into new

programs, because of the comfortable modular program construction techniques.

The C language based on the ANSI standard is very portable. Existing programs

can be quickly adapted to other processors as needed.

Interfacing to LCD Display

On most displays, the pins are numbered on the LCD’s printed circuit board, but if not, it

is quit easy to locate pin1. Since the pin is connected to ground, it often has a thicker

p.c.b. track connected to it, and it is generally connected to the metal work at some

point.

The function of each of the connections is shown in the table below:-

31

Page 32: Embedded training report(mcs 51)

Pins 1 & 2 are the power supply lines, Vss & Vdd. The Vdd pin should be connected to the

positive supply & Vss to the 0V supply or ground.

Although the LCD module data sheets specify 5V D.C. supply (at only a few milliamps),

supplies of 6V & 4.5V both work well, and even 3V is sufficient for some modules.

Consequently, these modules can be effectively and economically powered by batteries.

Pin 3 is a control pin, Vee, which is used to alter the contrast of the display. Ideally, these

pin should be connected to a variable voltage supply. A preset potentiometer connected

between the power supply lines, with its wiper connected to the contrast pin is suitable in

many cases, but be aware that some modules may require a

negative potential; as low as 7V in some cases. For absolute simplicity, connecting this pin

to 0V will often suffice.

Pin 4 is register select (RS) line.

PIN NO. NAME FUNCTION

1 Vss Ground

2 Vdd +ve supply

3 Vee contrast

4 RS Register select

5 R/W Read/Write

6 E Enable

7 D0 Data Bit 0

8 D1 Data Bit 1

9 D2 Data Bit 2

10 D3 Data Bit 3

11 D4 Data Bit 4

12 D5 Data Bit 5

32

Page 33: Embedded training report(mcs 51)

13 D6 Data Bit 6

14 D7 Data Bit 7

Three command control inputs. When this line is low, data bytes transferred to the display are treated as commands, and data bytes read from the display indicate its status. By setting the RS line high, character data can be transferred to and from the module.

Pin 5 is (R/W) line. This line is pulled low in order to write commands or character data to the module, or pulled high to read character data or status information from its registers.Pin 6 is Enable (E) line. This input is used to initiate the actual transfer of commands or character data between the module and the data lines. When writing to the display, data is transferred only on the high to low transition of this signal. However, when reading from the display, data will become available shortly after the low to high transition and remain available until the signal falls low again.

Pins 7 to 14 are the eight data bus lines (D0 to D7). Data can be transferred to and from the display, either as a single 8-bit byte or as two 4-bit “nibbles”. In the latter case, only the upper four data lines (D4 to D7) are used. This $-bit mode is beneficial when using a microcontroller, as fewer I/O lines are required.

33

Page 34: Embedded training report(mcs 51)

CODE FOR INTERFACING OF LCD WITH AT89S52 MICROCONTROLLER

#include<at89s8252.h>

#define lcdprt P0

#define rs P1_2

#define en P1_3

void delay(unsigned int i);

void lcd_cmd(unsigned char a);

void display(unsigned char b);34

Page 35: Embedded training report(mcs 51)

void wait(void);

void init_lcd(void);

void clear_lcd(void);

void cursor_position(unsigned char c);

void disp_hex(unsigned char digit);

void disp_dec(unsigned int digit);

code unsigned char

lkup_tb101[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

//LCD ROUTINES//

void lcd_cmd(unsigned char a)

{

wait();

lcdprt=a;

rs=0;

en=1; en=0;

}

void init_lcd(void)

{

lcd_cmd(0x3c);

lcd_cmd(0x0c);

lcd_cmd(0x06);

lcd_cmd(0x01);

}

void clear_lcd(void)

{

lcd_cmd(0x01);

}

void display(unsigned char b)

{

wait();

lcdprt=b;

rs=1;

35

Page 36: Embedded training report(mcs 51)

en=1;

en=0;

}

void wait(void)

{unsigned int count=300;

while(count!=0)

{count--;

}

}

void cursor_position(unsigned char c)

{lcd_cmd(c+0x80);

}

void disp_hex(unsigned char digit)

{unsigned char temp;

temp=digit>>4;

display(lkup_tb101[temp])

temp=(digit&0x0f);

display(lkup_tb101[temp]);

}

void disp_dec(unsigned int digit)

{unsigned int temp;

if (digit<100)

{temp=digit/10;

display(lkup_tb101[temp]);

temp=digit-temp*10;

display(lkup_tb101[temp]);

}

if(digit>99&&digit<1000)

{temp=digit/100;

display(lkup_tb101[temp]);

digit=digit-(temp*100);

temp=digit/10;

36

Page 37: Embedded training report(mcs 51)

display(lkup_tb101[temp]);

digit=digit-(temp*10);

temp=digit;

display(lkup_tb101[temp]);

}

if(digit>99&&digit<10000)

{temp=digit/100;

display(lkup_tb101[temp]);

digit=digit-(temp*1000);

temp=digit/100;

display(lkup_tb101[temp]);

digit=digit-(temp*100);

temp=digit/10;

display(lkup_tb101[temp]);

digit=digit-(temp*10);

temp=digit;

display(lkup_tb101[temp]);

}

if(digit>10000)

{temp=digit/10000;

display(lkup_tb101[temp]);

digit=digit-(temp*10000);

temp=digit/1000;

display(lkup_tb101[temp]);

digit=digit-(temp*1000);

temp=digit/100;

display(lkup_tb101[temp]);

digit=digit-(temp*100);

temp=digit/10;

display(lkup_tb101[temp]);

digit=digit-(temp*10);

temp=digit;

37

Page 38: Embedded training report(mcs 51)

display(lkup_tb101[temp]);

}}

void delay(unsigned int i)

{

while(i!=0)

{

i--;

}

}

void main(void)

{

while(1)

{init_lcd();

cursor_position(0x00);

display('h');

delay(0xffff);

cursor_position(0x01);

display('e');

delay(0xffff);

cursor_position(0x02);

display('l');

delay(0xffff);

cursor_position(0x03);

display('l');

delay(0xffff);

cursor_position(0x04);

display('o');

delay(0xffff);}

}

38

Page 39: Embedded training report(mcs 51)

CODE FOR SERIAL PORT COMMUNICATION IN TRANSMIT MODE

#include<at89s8252.h>

void init_sit(void)

{TMOD=0x00;

TMOD=0x20;

SCON=0x40;

TR1=1;

TH1=0xfd;

}

void transmit_serial(unsigned int a)

{int i;

TI=0;

39

Page 40: Embedded training report(mcs 51)

for(i=0xffff;i>=0;i--);

SBUF=a;

}

void main(void)

{init_sit();

transmit_serial(0x01);}

CODE FOR SERIAL PORT COMMUNICATION IN RECEIVE MODE

#include<at89s8252.h>

void init_sit(void)

{TMOD=0x00;

TMOD=0x20;

SCON=0x50;

TR1=1;

TH1=0xfd;

}

unsigned int receive(void)

{int i;

RI=0;

for(i=0xffff;i>=0;i--);

40

Page 41: Embedded training report(mcs 51)

return SBUF;

}

void main(void)

{int e;

init_lcd();

clear_lcd();

init_sit();

e=receive();

display(e);

}

82C55A FUNCTIONAL DESCRIPTION

The 82C55A is a programmable peripheral interface device designed for use

in Intel microcomputer systems.

Its function is that of a general purpose I/O component to interface peripheral

equipment to the microcomputer system bus. The functional configuration of

the 82C55A is programmed by the system software so that normally no

external logic is necessary to interface peripheral devices or structures

41

Page 42: Embedded training report(mcs 51)

Pin Diagram

Data Bus Buffer

This 3-state bidirectional 8-bit buffer is used to interface the 82C55A to the

system data bus. Data is transmitted or received by the buffer upon

execution of input or output instructions by the CPU. Control words and

status information are also transferred through the data bus buffer.

Read/Write and Control Logic

The function of this block is to manage all of the internal and external

transfers of both Data and Control or Status words. It accepts inputs from the

CPU Address and Control busses and in turn, issues commands to both of the

Control Groups.

Ports A, B, and C

42

Page 43: Embedded training report(mcs 51)

The 82C55A contains three 8-bit ports (A, B, and C). All can be configured in

a wide variety of functional characteristics by the system software but each

has its

Port A:

One 8-bit data output latch/buffer and one 8-bit input latch buffer. Both “pull-

up'' and “pull down'' buses hold devices are present on Port A.

Port B:

One 8-bit data input/output latch/buffer. Only “pull-up'' bus hold devices are

present on Port B.

Port C:

One 8-bit data output latch/buffer and one 8-bit data input buffer (no latch

for input). This port can be divided into two 4-bit ports under the mode

control. Each 4-bit port contains a 4-bit latch and it can be used for the

control signal outputs and status signal inputs in conjunction with ports A and

B. Only ``pull-up'' bus hold devices are present

43

Page 44: Embedded training report(mcs 51)

RESET is kept low to make all the ports the output ports. To do this, the pin is

connected to the pin of controller as shown and the pin is then made to 1 and then to 0.

PA, PB, PC become output ports.

It has four registers Pa, Pb, Pc, CW

00, 01, 10, 11

44

Page 45: Embedded training report(mcs 51)

ADC0804

8-Bit μP Compatible D/A Converters with 8-

ChannelThe ADC0804 family is CMOS 8-Bit, successive-approximation A/D converters which

use a modified potentiometric ladder and are designed to operate with the 8080A control

bus via three-state outputs. These converters appear to the processor as memory

locations or I/O ports, and hence no interfacing logic is required. The differential analog

voltage input has good common mode- rejection and permits offsetting the analog zero-

input voltage value. In addition, the voltage reference input can be adjusted to allow

encoding any smaller analog voltage span to the full 8 bits of resolution.

Features45

Page 46: Embedded training report(mcs 51)

• 80C48 and 80C80/85 Bus Compatible - No Interfacing Logic Required

• Conversion Time < 100s

• Easy Interface to Most Microprocessors

• Differential Analog Voltage Inputs

• TTL Compatible Inputs and Outputs

• On-Chip Clock Generator

• 0V to 5V Analog Voltage Input Range (Single + 5V Supply)

• No Zero-Adjust Required

PIN DIAGRAM

46

Page 47: Embedded training report(mcs 51)

When interfacing is being done then gets lowered then only it allows the controller

to read the data, otherwise controller can not read the data.

is always grounded.

is software controlled.

CODE FOR INTERFACING OF ADC(0804) WITH AT89S52

MICROCONTROLLER

          #include<at89s8252.h>

                 #define lcdprt P0

                 #define rs P1_2

                 #define en P1_3

                 unsigned char read_adc(void);

          unsigned char display_dec(unsigned char i);

                 void init_sit(void);

                 void transmit_serial(unsigned int a);

                 void delay(unsigned int i);

47

Page 48: Embedded training report(mcs 51)

                 void lcd_cmd(unsigned char a);

                 void display(unsigned char b);

                 void wait(void);

                 void init_lcd(void);

                 void clear_lcd(void);

                 void cursor_position(unsigned char c);

                 void disp_hex(unsigned char digit);

                 void disp_dec(unsigned int digit);

                 void shift(void);

                 code unsigned char

                 lkup_tb101[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

                 //LCD ROUTINES//

             void lcd_cmd(unsigned char a)

             {

                 wait();

                 lcdprt=a;

                 rs=0;

                 en=1; en=0;

                 }

                 void init_lcd(void)

          {

             lcd_cmd(0x3c);

                         lcd_cmd(0x0c);

                         lcd_cmd(0x06);

                         lcd_cmd(0x01);

                }

                   void clear_lcd(void)

                       {

                       lcd_cmd(0x01);

                       }

                       void display(unsigned char b)

48

Page 49: Embedded training report(mcs 51)

                       {

                        wait();

                        lcdprt=b;

                        rs=1;

                        en=1;

                        en=0;

                        }

                        void wait(void)

                        {unsigned int count=300;

                        while(count!=0)

                        {count--;

 

                

 }

                   

   }

                       void shift(void)

                       {

                       lcd_cmd(0x1c);

                       }

                       void cursor_position(unsigned char c)

                       {lcd_cmd(c+0x80);

                        }

                        void disp_hex(unsigned char digit)

                        {unsigned char temp;

                        temp=digit>>4;

                        display(lkup_tb101[temp]);

                        temp=(digit&0x0f);

                        display(lkup_tb101[temp]);

                        }

                        void disp_dec(unsigned int digit)

49

Page 50: Embedded training report(mcs 51)

                        {unsigned int temp,temp1,temp2;

                        if (digit<100)

                        {temp1=digit/10;

                        display(lkup_tb101[temp1]);

                       temp2=digit-temp1*10;

                        display(lkup_tb101[temp2]);

                        transmit_serial(temp1*10+temp2);}

                        if(digit>99&&digit<1000)

                        {temp=digit/100;

                     

display(lkup_tb101[temp]);

       digit=digit-(temp*100);

                        temp=digit/10;

                        display(lkup_tb101[temp]);

                        digit=digit-(temp*10);

                        temp=digit;

                        display(lkup_tb101[temp]);

                        }

                        if(digit>99&&digit<10000)

                        {temp=digit/100;

                        display(lkup_tb101[temp]);

                        digit=digit-(temp*1000);

                        temp=digit/100;

                        display(lkup_tb101[temp]);

                        digit=digit-(temp*100);

                        temp=digit/10;

                        display(lkup_tb101[temp]);

                         digit=digit-(temp*10);

                        temp=digit;

                        display(lkup_tb101[temp]);

50

Page 51: Embedded training report(mcs 51)

                        }

                        if(digit>10000)

                        {temp=digit/10000;

                        display(lkup_tb101[temp]);

                        digit=digit-(temp*10000);

  temp=digit/1000;

                        display(lkup_tb101[temp]);

                        digit=digit-(temp*1000);

                        temp=digit/100;

                        display(lkup_tb101[temp]);

                         digit=digit-(temp*100);

                        temp=digit/10;

                        display(lkup_tb101[temp]);

                        digit=digit-(temp*10);

                        temp=digit;

                        display(lkup_tb101[temp]);

                        }}

                        void delay(unsigned int i)

                        {

                        while(i!=0)

                        {

                        i--;

                        }

                        }

    void init_sit(void)

     {TMOD=0x00;

     TMOD=0x20;

     SCON=0x40;

51

Page 52: Embedded training report(mcs 51)

      TR1=1;

       TH1=0xfd;

   

   }

void transmit_serial(unsigned int a)

{int i;

TI=0;

for(i=0xffff;i>=0;i--);

SBUF=a;

}

void main(void)

{

unsigned int e;

P2=0xff;

init_lcd();

init_sit();

while(1)

{

e=read_adc();

cursor_position(0x00);

delay(0xffff);

disp_dec(e);

}

}

unsigned char read_adc(void)

{unsigned char n;

P1_0=0; // SOC=0

P1_0=1; // SOC=1

while(P1_1==1) //while( EOC==1)

{n=P2;

}

52

Page 53: Embedded training report(mcs 51)

return(n);

}

Real Time Clock

The DS12887 is real-time clocks (RTCs). The devices provide a real-time

clock/calendar, one time-of-day alarm, three maskable interrupts with a

common interrupt output, a programmable square wave, and 114 bytes of

battery backed static. The DS12887 integrates a quartz crystal and lithium

energy source into a 24-pin encapsulated DIP package. The DS12C887 adds

a century byte at address 32h. For all devices, the date at the end of the

month is automatically adjusted for months with fewer than 31 days,

including correction for leap years. The devices also operate in either 24-hour

or 12-hour format with an AM/PM indicator. A precision temperature-

compensated circuit monitors the status of Vcc. If a primary power failure is

53

Page 54: Embedded training report(mcs 51)

detected, the device automatically switches to a backup supply. A lithium

coin-cell battery can be connected to the VBAT input pin on the DS12885 to

maintain time and date operation when primary power is absent. The device

is accessed through a multiplexed byte-wide interface, which supports both

Intel and Motorola mode

Pin diagram

54

Page 55: Embedded training report(mcs 51)

Features

♦RTC Counts Seconds, Minutes, Hours, Day, Date, Month, and Year with

Leap Year Compensation Through 2099

♦Binary or BCD Time Representation

♦12-Hour or 24-Hour Clock with AM and PM in 12-Hour Mode

♦Daylight Saving Time Option

♦Interfaced with Software as 128 RAM Locations

♦14 Bytes of Clock and Control Registers

♦114 Bytes of General-Purpose, Battery-Backed RAM (113 Bytes in the

DS12C887 and DS12C887A)

♦Time-of-Day Alarm Once Per Second to Once Per Day

♦Periodic Rates from 122μs to 500ms

♦Programmable Square-Wave Output

♦Automatic Power-Fail Detect and Switch Circuitry

♦Optional 28-Pin PLCC Surface Mount Package or 32-Pin TQFP (DS12885

♦Optional Encapsulated DIP (EDIP) Package with Integrated Crystal and

Battery (DS12887, DS12887A, DS12C887, DS12C887A)

♦Optional Industrial Temperature Range Available

♦Underwriters Laboratory (UL) Recognized

FOR REGISTER ra (To control RTC functions for some settings)

To

initially start the oscillator.

ra=0x20

FOR REGISTER rb (To control RTC functions for some settings)

55

UIP DV2 DV1 DV0 RS3 RS2 RS1 RS00 0 1 0 0 0 0 0

Page 56: Embedded training report(mcs 51)

DM=0(BCD)

=1(HEXADECIMAL)

Square Wave

Alarm Interrupt Enable

Periodic Interrupt Enab

To Update Set Bit is 1

rb=0x80

56

SET PIE AIE UIE SQWE DM 24/12 DSE1 0 0 0 0 0 0 0

Page 57: Embedded training report(mcs 51)

CODE FOR INTERFACING OF RTC (DS12887) WITH ATs52 MICROCONTROLLER#include<at89s8252.h>#define LCDPRT P2#define RS P1_2#define EN P1_3void bcdconv(unsigned int mb);void T0M2delay_rtc(void);void init_rtc(void);void delay(unsigned int i);void lcd_cmd(unsigned char a);void display(unsigned char b);void wait(void);void Init_lcd(void);void clear_lcd(void);void cursor_position(unsigned char c);

struct rtc{ unsigned char second; unsigned char sa; unsigned char minute; unsigned char ma; unsigned char hours; unsigned char ha; unsigned char dow; unsigned char dom; unsigned char month; unsigned char years; unsigned char ra; unsigned char rb; unsigned char rc; unsigned char rd;

57

Page 58: Embedded training report(mcs 51)

};xdata at 0x0000 struct rtc ds12887;void main(void){unsigned int x;unsigned char sec;unsigned char hr;unsigned char minInit_lcd();for(x=0;x<10000;x++)T0M2delay_rtc();ds12887.ra=0x20;ds12887.rb=0x81;ds12887.second=0x55;ds12887.minute=0x25;ds12887.hours=0x09;ds12887.dom=0x19;ds12887.month=0x10;ds12887.years=0x06;ds12887.rb=0x03;

while(1){ sec=ds12887.second;cursor_position(0x00);bcdconv(sec);cursor_position(0x02);display(':');min=ds12887.minute;cursor_position(0x03);bcdconv(min);cursor_position(0x05);display(':'); hr=ds12887.hours; cursor_position(0x06); bcdconv(hr);cursor_position(0x40);display('s');cursor_position(0x42);display(':');cursor_position(0x43);display('m');cursor_position(0x45);display(':');cursor_position(0x46);display('h');

58

Page 59: Embedded training report(mcs 51)

}}void bcdconv(unsigned int mb)

{unsigned int x;unsigned int y;x=mb&0x0f;x=x|0x30;y=mb&0xf0;y=y>>4;y=y|0x30;display(y);display(x);}void T0M2delay_rtc(void){TMOD=TMOD&0xf0;TMOD=TMOD|0x02;TH0=0xEC; // 236 decimal value load in THOTR0=1;if(TF0==1){ TR0=0; TF0=0; }}void lcd_cmd(unsigned char a) { wait (); LCDPRT=a; RS=0; EN=1; EN=0; } void display(unsigned char b) { wait (); LCDPRT=b; RS=1; EN=1; EN=0; } void wait(void) { unsigned int count=300;

59

Page 60: Embedded training report(mcs 51)

while(count!=0) { count--; }

}

void Init_lcd(void) { lcd_cmd(0x3c); lcd_cmd(0x0c); lcd_cmd(0x06); lcd_cmd(0x01); } void clear_lcd(void) { lcd_cmd(0x01); }

void cursor_position(unsigned char c) { lcd_cmd(c+0x80); }

60

Page 61: Embedded training report(mcs 51)

61

Page 62: Embedded training report(mcs 51)

Detailed description of training project:

For easy understanding I have divide my project into four sections:

(I) POWER SUPPLY SECTION: Initial stage of every electronic circuit is power supply system which provides required power to drive the whole system. The specification of power supply depends on the power requirement and this requirement is determined by its rating. The main components used in supply system are:

(i) transformer(ii) rectifier(iii) input filter(iv) regulator(v) output filter(vi) output indication

(i) Transformer: The main source of power supply is a transformer. The maximum output power of power supply is dependent on maximum output power of transformer .We determine power

MicrocontrollerAT89S52

Regulated power supply

DC Motor drive

LCD display Conveyor belt application

62

Page 63: Embedded training report(mcs 51)

from its current and voltage rating. e.g.: if there is a transformer of 12V, 500mA then maximum power delivered by transformer is 6Watt.It means we can drive a load from this transformer up to 6w. In our project our maximum power requirement is 1watt. So to provide this power we use 12V/250mA transformer. The maximum output power of this transformer is 4watt.it means it can easily drive load up to 4 watt.

(ii) Rectifier: Rectifier is a circuit which is used to convert ac to dc. Every electronic circuit requires a dc power supply for rectification. We have used four diodes.

(iii) Input filter: After rectification we obtain dc supply from ac but it is not pure dc it may have some ac ripples .To reduce these ripples we use filters. It comprises of two filters –low frequency ripple filter and high frequency ripple filter. To reduce low frequency ripples we use electrolytic capacitor. The voltage rating of capacitor must be double from incoming dc supply. It blocks dc and passes ripples to ground.

(iv) Regulator: Regulator is a device which provides constant output voltage with varying input voltage. There are two types of regulators-(a) Fixed voltage regulator(b) Adjustable regulatorWe have used fixed voltage regulator LM78XX last two digits signify output voltage. The voltage for our system is 5V that is why we have used 7805 regulator which provides 5V from 12V dc.

(v) Output filter: It is used to filter out output ripple if any.

(vi) Output indication: We use LED to observe the functioning of our system. If the LED glows it confirms proper functioning of our supply.

(III) MICROCONTROLLER AND DISPLAY SECTION: We are all familiar with the term microcontroller and even have studied it as part of our curriculum but the question is why we use microcontroller?Well, we use microcontroller to control our hardware using programs which we make according to our requirement. We have used MCS51 family microcontroller AT89S52. We have interfaced LCD with microcontroller to display the working action of our drive. We have connected LCD on port1 and we control the drive action of motor from port2. RS (P3.3) and EN (P3.4) pin of LCD are connected to port 3. Detailed description of interfacing of LCD with microcontroller has been explained in earlier section in detail.

(IV) DC MOTOR DRIVE SECTION: We use dc motor drive to control direction of dc motor. it means at particular time it will move in forward motion and at another particular time it will move in backward direction. The timing of this circuit is controlled by microcontroller. To operate dc

63

Page 64: Embedded training report(mcs 51)

motor we require dc voltage. When we give positive voltage to positive terminal and negative voltage to negative terminal it moves clockwise i.e. forward direction and when we give positive voltage to negative terminal and negative voltage to positive terminal the motor moves in anticlockwise direction i.e. backward/reverse direction. This voltage is provided to the motor through drive section which comprises of transistors. The switching action of these transistors is controlled by microcontroller. We have used four NPN transistors (BC547). Since the out put power of BC547 that is the transistor is not sufficient to drive motor that is why we use a Darlington pair package TIP127 (PNP) and TIP122 (NPN) with BC547 to increase the incoming power from the emitter of BC547. Hence sufficient power from TIP122 and TIP127 is easily provided to motor. in our circuit transistor Q1,Q2,Q3,Q4 provide Vcc to motor and Q5,Q6,Q7,Q8 provide ground to motor.

WORKIN ACTION OF TRANSISTOR: When we give a base voltage to Q1 and Q8 they are set to on position. Q1 provide Vcc and Q8 provide ground to DC motor and in this case motor moves clock wise. Q4 and Q7 are in off position. Similarly when we set Q4 and Q7 in on position Q4 provide Vcc to motor and Q7 provide ground to motor. This configuration is opposite to previous configuration and in this case motor moves anti clock wise. since we have used silicon transistor the need minimum .7 volt dc to get on and we are providing 5volt as base voltage from microcontroller.

(IV) CONVEYOR BELT APPLICATION USING DC MOTOR: Our project is a small model which depicts the transportation of goods from one place to another in production plant in industry. C CODING FOR CONTROLLING DRIVE ACCORDING TO APPLICATION

# include<at89s8252.h>#define LCDPRT P1#define RS P3_3#define EN P3_4void lcd_cmd(unsigned char a);code unsigned char name_arry[]={"forward$"};code unsigned char name_arry1[]={"backward$"};code unsigned char name_arry2[]={"stop $"};

bit flag;unsigned char sec;

void drive(void);void display_string(unsigned char *sp);void display_string1(unsigned char *pp);void display_string2(unsigned char *tp);void delay(unsigned int i);void lcd_cmd(unsigned char a);void display(unsigned char b);void wait(void);void Init_lcd(void);void clear_lcd(void);

64

Page 65: Embedded training report(mcs 51)

void cursor_position(unsigned char c);

void main(void){P2=0x00;Init_lcd(); drive();

} void drive(void) { while(1) { P2=0x00; cursor_position(0x00); display_string2(&name_arry2); delay(0xffff); delay(0xffff); delay(0xffff); delay(0xffff);delay(0xffff);delay(0xffff);delay(0xffff);

P2=0x05; cursor_position(0x00); display_string(&name_arry); delay(0xffff); P2=0x00;

cursor_position(0x00); display_string2(&name_arry2); delay(0xffff); delay(0xffff); delay(0xffff); delay(0xffff); delay(0xffff); delay(0xffff); delay(0xffff); P2=0x0a; cursor_position(0x00); display_string1(&name_arry1);

65

Page 66: Embedded training report(mcs 51)

delay(0xffff); } } void display_string(unsigned char *sp) { while(*sp!='$') {

display(*sp);

sp=sp+1; } }

void display_string1(unsigned char *pp) { while(*pp!='$') {

display(*pp);

pp=pp+1; } }

void display_string2(unsigned char *tp) { while(*tp!='$') {

display(*tp);

tp=tp+1; } }

void lcd_cmd(unsigned char a) { wait (); LCDPRT=a;

66

Page 67: Embedded training report(mcs 51)

RS=0; EN=1; EN=0; } void display(unsigned char b) { wait (); LCDPRT=b; RS=1; EN=1; EN=0; } void wait(void) { unsigned int count=300; while(count!=0) { count--; }

} void Init_lcd(void) { lcd_cmd(0x3c); lcd_cmd(0x0c); lcd_cmd(0x06); lcd_cmd(0x01); } void clear_lcd(void) { lcd_cmd(0x01); } void delay (unsigned int i) { while (i!=0) { i--; }} void cursor_position(unsigned char c) { lcd_cmd(c+0x80); }

67

Page 68: Embedded training report(mcs 51)

68