Microcontroller vs12

38
8/3/2019 Microcontroller vs12 http://slidepdf.com/reader/full/microcontroller-vs12 1/38 1

Transcript of Microcontroller vs12

Page 1: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 1/38

1

Page 2: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 2/38

Introduction

The project ‘Contact-Less Digital Tachometer’ is a device for measuring the Revolution per 

minute of a rotating shaft using the 8051 microcontroller and a proximity sensor.

This device is built on an AT89c51 microcontroller, an alpha-numeric LCD module and a

 proximity sensor to detect the rotation of the shaft whose speed is being measured.

The microcontroller is used to count the pulses coming from a sensor. In this tachometer, the

counted pulses are coming from the proximity sensor, which will detect any reflective element

 passing in front of it, and thus, would give an output pulse for each and every rotation of the

shaft. Those pulses would be fed to the microcontroller and counted.

LCD has been used to take the data from the microcontroller and display on its screen.

2

Page 3: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 3/38

Circuit Component Quantity

1. At89C51 microcontroller 1

2. 16*2 alpha-numeric display 1

3. IR LED 1

4. Photodiode 1

5. Capacitor 

Electrolytic capacitor 1(10uF)

Ceramic capacitor 2(22pF)

6. Resistor 100, 1k, 10k, 33k 

68k, 100k 

7. Crystal oscillator 1(11.0592MHz)

8. IC 7805 1

9. Battery 2

10. Op-Amp 358 1

11. Rotating Fan 1

12. Potentiometer 1(10k)

3

Page 4: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 4/38

Proximity sensor

This device is used to avoid the physical contact of the tachometer with the rotating shaft. The

contact with the rotating shaft is avoided with an optical sensing mechanism that uses an infrared

(IR) light emitting diode and a photo detecting diode. The IR LED transmits an infrared light

towards the rotating disc and the photo detecting diode receives the reflected light beam. This

special arrangement of sensors is placed at about an inch away and facing towards the rotating

disc. If the surface of the disc is rough and dark, the reflected IR light will be negligible. A tiny

 piece of white paper glued to the rotating disc is just enough to reflect the incident IR light when

it passes in front of the sensor, which happens once per rotation.

V C C

V C C

D 1

P H O T O D I O D E

1 K 

1 0 0 K

V C C

3 3 K

V C C

9 C 5 2

2 93 0

2 12 22 32 42 52 62 72 8

1 01 11 21 31 41 51 61 7

T

L 2L 1

P S E NA L E / P R O G

/ V P P

. 0 / T 2

. 1 / T 2 - E X

. 2

. 3

. 4

. 5

. 6

. 7

P 2 . 0 / A 8P 2 . 1 / A 9

P 2 . 2 / A 1 0P 2 . 3 / A 1 1P 2 . 4 / A 1 2P 2 . 5 / A 1 3P 2 . 6 / A 1 4P 2 . 7 / A 1 5

P 3 . 0 / R X DP 3 . 1 / T X D

P 3 . 2 / I N T 0P 3 . 3 / I N T 1

P 3 . 4 / T 0P 3 . 5 / T 1

P 3 . 6 / W R

P 3 . 7 / R D

. 0 / A D 0

. 1 / A D 1

. 2 / A D 2

. 3 / A D 3

. 4 / A D 4

. 5 / A D 5

. 6 / A D 6

. 7 / A D 7

D 2

I R L E

6 8 K

-

+

L M 3 5 83

21

           8

           4

4

Page 5: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 5/38

Microcontroller vs. General- Purpose Microprocessor

Microcontroller and microprocessors are electronic computing device which perform their task according to a pre-defined program stored in their memory. But microcontrollers differ from

microprocessors in several ways. The main difference is that microprocessor do not have built in

memory, input or output functions such as parallel ports or serial ports. Thus a microprocessor is

known as general purpose device whereas microcontroller is known as specific function device.

Thus main differences are:-

 

S No. COMPONENTS MICROPROCESSOR MICROCONTROLLER  1. IN-BUILT RAM NO YES

2. IN-BUILT ROM NO YES

3. I/O PORTS NO YES4. TIMER NO YES

Thus microcontroller is

MICROCONTROLLER 

5

Page 6: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 6/38

Microcontroller for Embedded system

An embedded system is a computer system designed to do one or a few dedicated and/or 

specific functions. Embedded systems are controlled by one or more main pro-cessing cores that

are typically either microcontrollers or digital signal processors(DSP).

Since the embedded system is dedicated to specific tasks, design engineers can optimize it to

reduce the size and cost of the product and increase the reliability and performance.

CHARACTERISTICS

1. Embedded systems are designed to do some specific task, rather than be a general-purpose

computer for multiple tasks.

2. Some systems have real-time  performance constraints that must be met, for reasons such as

safety and usability; others may have low or no performance requirements, allowing the system

hardware to be simplified to reduce costs.

3. Embedded systems are not always standalone devices. Many embedded systems consist of 

small, computerized parts within a larger device that serves a more general purpose. Like an

embedded system in an automobile  provides a specific function as a subsystem of the car itself.

4. The program instructions written for embedded systems are referred to as firmware, and are

stored in read-only memory or Flash memory chips. They run with limited computer hardware

resources: little memory, small or non-existent keyboard and/or screen. 

6

Page 7: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 7/38

ARCHITECTURE OF 8051 MICROCONTROLER 

The 8051 is a 8-bit microcontroller with 40 pin DIP shown in figure.

The 8051 microcontroller has

1. Supply input and external clock frequency

• 8051 microcontroller works on +5V which is connected between VCC (pin 40)

and GND (pin 20).

• The 8051 has an on-chip oscillator but requires an external clock to run it.

A quartz crystal oscillator is connected to inputs XTAL1 (pin19) and XTAL2(pin18)

• The 8051 have clock frequencies range of up to 100 MHz.

• The speed of 8051 depends upon the oscillator frequency connected to XTAL

7

Page 8: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 8/38

1. Four bi-directional ports for I/O operation (P0, P1, P2, and P3).

• The four 8-bit I/O ports P0, P1, P2 and P3 each use 8 pins.

• All the ports upon RESET are configured as output, ready to be used as input

 ports

➢ Port P0

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

outp0ut.

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

address and data

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

is then demultiplexed using ALE signal.

When ALE=0, it provides data D0-D7

When ALE=1, it has address A0-A7

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

8

Page 9: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 9/38

➢ P1 and P2

Port 1 occupies a total of 8 pins (pin 1-8). It can be used for input or 

output.

Port 2 occupies a total of 8 pins (pin 21-28) and these can be also used for 

input or output.

Port 2 must be used along with P0 to provide the 16-bit address for the

external memory. This is the dual role of the Port 2

P0 provides the lower 8 bits via A0 – A7 & P2 is used for the upper 8

 bits of the 16-bit address, designated as A8 – A15, and it cannot be used

for I/O

Unlike port P0, P1 & P2 do not require any external pull up resistor.

➢ Port P3

So far we have three ports, P0, P1, P2 for I/O. This should be enough for 

most microcontroller applications. That leaves port 3 for interrupts as

well as for other signals.

Port 3 can also be used as input or output.

Like P1 & P2 Port 3 does not need any pull-up resistors

Port 3 has the additional function of providing some extremely important

Signals.

9

Page 10: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 10/38

1. Timers (TIMER0, TIMER1).

• 8051 has three timers named

Timer0

Timer1

• All of these are 16-bit timers and can also be used as counter.

Modes of operation of TIMER0 & TIMER1 can be controlled by TMOD register 

10

Page 11: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 11/38

LCD Interfacing

LCD is finding widespread use replacing LEDs

• The declining prices of LCD

• The ability to display numbers, characters, and graphics

• Incorporation of a refreshing controller  into the LCD, thereby relieving the CPU of the task 

of refreshing the LCD

• Ease of programming for characters and graphics

Pin Symbol I/O Descriptions

1 VSS -- Ground

2 VCC -- +5V power supply

3 VEE -- Power supply to control contrast

4 RS I RS=0 to select command register,

RS=1 to select data register 

5 R/W I R/W=0 for write,

R/W=1 for read

6 E I/O Enable

7 DB0 I/O The 8-bit data bus

8 DB1 I/O The 8-bit data bus

9 DB2 I/O The 8-bit data bus

10 DB3 I/O The 8-bit data bus

11 DB4 I/O The 8-bit data bus

12 DB5 I/O The 8-bit data bus

13 DB6 I/O The 8-bit data bus

14 DB7 I/O The 8-bit data bus

15 BPL Back Plane Light +5V or Lower  

16 Gnd Ground Voltage

11

Page 12: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 12/38

*Enable –used by the LCD to latch information presented to its Data Bus

No. Instruction Hex Decimal

1 Function Set: 8-bit, 1 Line, 5x7 Dots 0x30 482 Function Set: 8-bit, 2 Line, 5x7 Dots 0x38 56

3 Function Set: 4-bit, 1 Line, 5x7 Dots 0x20 32

4 Function Set: 4-bit, 2 Line, 5x7 Dots 0x28 40

5 Entry Mode 0x06 6

6

Display off Cursor off 

(clearing display without clearing DDRAM

content)

0x08 8

7 Display on Cursor on 0x0E 14

8 Display on Cursor off 0x0C 12

9 Display on Cursor blinking 0x0F 1510 Shift entire display left 0x18 24

12 Shift entire display right 0x1C 30

13 Move cursor left by one character 0x10 16

14 Move cursor right by one character 0x14 20

15 Clear Display (also clear DDRAM content) 0x01 1

16Set DDRAM address or coursor position on

display0x80+add* 128+add*

17Set CGRAM address or set pointer to

CGRAM location0x40+add** 64+add**

VCC, VSS and VEE 

While VCC and VSS provide +5V and ground, respectively, VEE is used for controlling the LCD

contrast.

RS, register select

There are two important registers inside the LCD. The RS pin is used for their selection as

follows if 

12

Page 13: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 13/38

RS=0 select command register 

RS=1 select data register 

Command register selection allows user to send command such as clear display, cursor at home

etc.

Data register selection allows user to send data to be displayed on LCD.

R/W, read/write

R/w input allows the user to read and write the information from the LCD. If 

R/W=0, write information to LCD

R/W=1, write information from LCD.

E, enable

The enable pin is used by LCD to latch information presented to its data pins. When data is

supplied to data pins, a high to low pulse must be applied to this pin in order for the LCD to latch

in the data pins. This pulse must be a minimum of 450ns wide.

There are two methods to send data to the LCD

1. Sending commands and data to LCD with a time delay

2. Sending commands and data to LCD with checking busy flag

LCD in 4-bit mode

 Now we are going to use LCD in 4-bit mode. There are many reasons why sometime we prefer 

to use LCD in 4-bit mode instead of 8-bit. One basic reason is lesser number of pins is needed to

interface LCD.

In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower 

nibble. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization

that tells the LCD controller that user has selected 4-bit mode of operation. We call this special

13

Page 14: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 14/38

sequence as resetting the LCD. Following is the reset sequence of LCD.

Wait for abour 20mS

Send the first init value (0x30)

Wait for about 10mS

Send second init value (0x30)

Wait for about 1mS

Send third init value (0x30)

Wait for 1mS

Select bus width (0x30 - for 8-bit and 0x20 for 4-bit)

Wait for 1mS

►LCD connections in 4-bit Mode

14

Page 15: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 15/38

Above is the connection diagram of LCD in 4-bit mode, where we only need 6 pins to

interface an LCD. D4-D7 are the data pins connection and Enable and Register select are for 

LCD control pins. We are not using Read/Write (RW) Pin of the LCD, as we are only writing

on the LCD so we have made it grounded permanently. If we want to use it.. then we may

connect it on our controller but that will only increase another pin and does not make any big

difference. Potentiometer RV1 is used to control the LCD contrast. The unwanted data pins

of LCD i.e. D0-D3 are connected to ground.

►Sending data/command in 4-bit Mode

the common steps to send data/command to LCD when working in 4-bit mode. in 4-bit modedata is sent nibble by nibble, first we send higher nibble and then lower nibble. This means in both command and data sending function we need to separate the higher 4-bits and lower 4-bits.

The common steps are:

Mask lower 4-bits

Send to the LCD port

Send enable signal

Mask higher 4-bits

Send to LCD port

Send enable signal

Programming Timers

In the programming we have used both the timer of the microcontroller 8051.

We have used TMOD=0x51 in 16-bit timer mode.

It indicate that we have used the timer1 as a counter and timer0 as a timer 

We have initialized timer0 from

TH0=0x00;

TL0=0x00;

It indicates that the timer0 starts the counting of the pulse from zero to its maximum limit, and

during this counting it takes a time delay of 71ms.

We have initialized timer1 from

TH1=0x00;

15

Page 16: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 16/38

TL1=0x00;

It indicates that the timer0 starts the counting of the pulse from zero.

Initially we have started the timer0 and timer1 starts in overflow interrupt of timer0 i.e. when

TF0 is 1, the interrupt routine of the timer0 starts.

The 8051 has two timers/counters, they can be used either as

 Timers to generate a time delay or as

 Event counters to count events happening outside the microcontroller 

 Both Timer 0 and Timer 1 are 16 bits wide

 since 8051 has an 8-bit architecture, each 16-bits timer is accessed as two separate registers of 

low byte and high byte

 Accessed as low byte and high byte

 the low byte register is called TL0/TL1 and

 the high byte register is called TH0/TH1

 Accessed like any other register 

 Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the various

timer operation modes

 TMOD is a 8-bit register 

 The lower 4 bits are for Timer 0

 The upper 4 bits are for Timer 1

 In each case,

 The lower 2 bits are used to set the timer mode

 the upper 2 bits to specify the operations C/T M1 M0

16

Page 17: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 17/38

GATE C

/T M1

/

17

Page 18: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 18/38

Timers of 8051 do starting and stopping by either software or hardware control

In using software to start and stop the timer where GATE=0

The start and stop of the timer are controlled by way of software by the TR (timer start) bits TR0

and TR1

• The SETB instruction starts it, and it is stopped by the CLR instruction

• These instructions start and stop the timers as long as GATE=0 in the TMOD register 

The hardware way of starting and stopping the timer by an external source is achieved by making

GATE=1 in the TMOD register 

The following are the characteristics and operations of mode1:

1. It is a 16-bit timer; therefore, it allows value of 0000 to FFFFH to be loaded into the timer’s

register TL and TH

2. After TH and TL are loaded with a 16-bit initial value, the timer must be started

This is done by SETB TR0 for timer 0 and

SETB TR1 for timer 1

3. After the timer is started, it starts to count up

It counts up until it reaches its limit of FFFFH

When it rolls over from FFFFH to 0000, it sets high a flag bit called TF (timer flag)

– Each timer has its own timer flag: TF0 for timer 0, and TF1 for timer 1

– This timer flag can be monitored

When this timer flag is raised, one option would be to stop the timer with the instructions

CLR TR0 or CLR TR1, for timer 0 and timer 1, respectively

4. After the timer reaches its limit and rolls over, in order to repeat the process

TH and TL must be reloaded with the original value, and

TF must be reloaded to 0.

Timers can also be used as counters counting events happening outside the 8051

18

Page 19: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 19/38

When it is used as a counter, it is a pulse outside of the 8051 that increments TH, TL registers

TMOD and TH, TL registers are the same as for the timer discussed previously

Programming the timer in the last section also applies to programming it as a counter 

Except the source of the frequency

The C/T bit in the TMOD registers decides the source of the clock for the timer 

When C/T = 1, the timer is used as a counter and gets its pulses from outside the 8051

The counter counts up as pulses are fed from pins 14 and 15, these pins are called T0 (timer 0

input) and T1 (timer 1 input)

19

Page 20: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 20/38

Interrupt programming

An interrupt is an external or internal event that interrupts the microcontroller to inform it that a

device needs its service

A single microcontroller can serve several devices by two ways

Interrupts

Whenever any device needs its service, the device notifies the microcontroller by sending it an

interrupt signal

Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and serves

the device

The program which is associated with the interrupt is called the interrupt service routine (ISR) or 

interrupt handler 

Polling

The microcontroller continuously monitors the status of a given device

When the conditions met, it performs the service

After that, it moves on to monitor the next device until every one is serviced 

Polling can monitor the status of several devices and serve each of them as certain conditions are

met

The polling method is not efficient, since it wastes much of the microcontroller’s time by polling

devices that do not need service

The advantage of interrupts is that the microcontroller can serve manydevices (not all at the same time)

• Each device can get the attention of the microcontroller based on the assigned priority

• For the polling method, it is not possible to assign priority since it checks all devices in a

round-robin fashion

20

Page 21: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 21/38

• The microcontroller can also ignore (mask) a device request for service

• This is not possible for the polling method

Six interrupts are allocated as follows

• Reset – power-up reset

• Two interrupts are set aside for the timers: one for timer 0 and one for timer 1

• Two interrupts are set aside for hardware external interrupts (P3.2 and P3.3 are for the

external hardware interrupts INT0 (or EX1), and INT1 (or EX2))

• Serial communication has a single interrupt that belongs to both receive and transfer 

Enabling and Disabling an Interrupt

Upon reset, all interrupts are disabled (masked), meaning that none will be responded to by the

microcontroller if they are activated. The interrupts must be enabled by software in order for the

microcontroller to respond to them.

There is a register called IE (interrupt enable) that is responsible for enabling (unmasking) and

disabling (masking) the interrupts. 

21

Page 22: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 22/38

In the Program we have used IE=0x82

This indicates that we have used timer0 overflow interrupt. This statement enables timer0

overflow interrupt.

22

Page 23: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 23/38

The 8051 compiler have extensive support for the interrupts. They assign a unique number to

each of the 8051 interrupts

23

Page 24: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 24/38

Working

Tachometer is a device that measures the rotational speed of any shaft or disc. The unit of the

measurement is usually revolutions per minute or RPM. A digital tachometer is based on a

AT89C51 microcontroller that requires no physical contact with the rotating shaft to measure its

rotational speed. The contact with the rotating shaft is avoided with an optical sensingmechanism that uses an infrared (IR) light emitting diode and a photo detecting diode. The IR 

LED transmits an infrared light towards the rotating disc and the photo detecting diode receives

the reflected light beam. This special arrangement of sensors is placed at about an inch away and

facing towards the rotating disc. If the surface of the disc is rough and dark, the reflected IR light

will be negligible. A tiny piece of white paper glued to the rotating disc is just enough to reflect

the incident IR light when it passes in front of the sensor, which happens once per rotation. For 

our purpose, the Timer0 module will be configured as a 16-bit counter to count the number of 

 pulses arriving at P3.5/T1 input pin. The counter will be active for 1 sec and the number of 

 pulses arrived during this interval will be recorded, and later multiplied by 60 to get the RPM of 

the disc.

24

Page 25: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 25/38

Flow Chart of Main Program

25

STAR

 T

LCD Initialization in 4bitmode

Set Input/output Port

Initialize TMOD =0x51

Initialize Timer 0

Initialize Timer 1

Initialize IE=0x82

START Timer 0

START Timer 1

Flag=0

Page 26: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 26/38

LCD Initialization subroutine flowchart

26

while (1)

STOP

Check RS

Mask lower 4 bits Mask lower 4 bits

Send enable signal toSend lower nibble toLCD port

Mask higher 4 bits

Send enable signal to

Send higher nibble to

LCD port

Send enable signal toSend lower nibble toLCD port

Mask higher 4 bits

Send enable signal to

Send higher nibble to

LCD port

Initialize LCD in 4-bit

RS=0

Command Register 

RS=1

Data

Page 27: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 27/38

Timer 0 Interrupt 1 Flowchart

When timer interrupt 0 becomes 1 i.e. TF 0=1, these steps takes place in the

program.

In the Main program, we initialize flag =0.

27

Flag==

0 Disable Interrupt

 TRUE FALSE

Page 28: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 28/38

Coding

//program for contactless digital techometer

//in this whole 4-bit mode LCD is connected to

//D4 - P1.0

//D5 –P1.1

//D6 – P1.2

28

Countpulse= 0

Initialize counter

Start counter

Flag= 1

Delay ()

Enable Interrupt

Stop Counter

Count pulse= TH1

Countpulse= ((countpulse

<<8) + TL1)

Initialize counter

Page 29: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 29/38

//D7 –P1.3

//EN – P1.7

//RS – P1.5

#include<AT89X51.H>

#include<string.h>

#include<stdio.h>

#define en 0x80 //LCD control pins

#define rs 0x20

#define lcd_port P1

int inp(void);

void lcd_cmd(unsinghed char);

void lcd_data(unsingned char);

void display(char*);

void delayms(unsingned int);

void lcd_resset(void);

void lcd_init(void);

unsigned int countpulse, rpm=0, rpm1=0;

unsinged int i , j;

char str[16];

unsigned char flag = 0 , temp1=0 , temp2=0 , ii=0;

unsigned int buffer[4];

void main(void)

{

29

Page 30: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 30/38

P0=0x00; //set port 0 as o/pport for pwm

P1=0x00; //set port1 as o/por LCD display

P2=0x00; //set port2 as o/p port for pwm &2_5 to 7 as input for kbd

P3=0xFF; //set port3 as i/pport for (pwm)count pulse

 

Lcd_init();

TMOD = 0x51; //timer 1 ascounter in mode 16 bit

//timer2 astimer in mode 16 bit

TL0= 0x00;

TH0= 0x00;

TL1= 0x00;

TH1=0x00;

IE = 0x82;

Flag =0;

Lcd_cmd(0x01); // clear display

Sprintf(str, “TECHO-METER”);

Lcd_cmd(0x84); // force cursor tobeginng of 1st line

Display(str);

Sprintf(str,”RPM:”);

Lcd_mcd(0xc2); // forse cursortobegin of 2nd line

Display(str);

30

Page 31: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 31/38

TR0=1;

TR1=1;

Buffer[0] = buffer[1] = buffer[2]= buffer[3]= 0;

while(1)

{

}

}

// function starts here

void display(char *str) // fn to displayon LCD

{

unsigned int j;

for(j=0 ; j<strlen(*str) ; j++)

{

lcd_data (str[j]);

}

}void delayms(unsigned int tme)

{

unsigned int k2, k1;

for(k1=0 ; k1<tme ; k1++)

{

for(k2=0; k2<1; k2++);

}

}

void lcd_cmd(char cmd)

31

Page 32: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 32/38

{

lcd_port = ((cmd>>4) & 0X0F)| en;

lcd_port = ((cmd>>4) & 0X0F);

lcd_port = (cmd & 0X0F)| en;

lcd_port = (cmd & 0X0F);

}

void lcd_data(unsigned char dat)

{

lcd_port = (((dat>>4) & 0x0F)| en | rs);

delayms(2);

lcd_port = (((dat>>4) & 0x0F)| rs);

delayms(2);

lcd_port = ((dat & 0x0F)| en | rs);

delayms(2);

lcd_port = ((dat & 0x0F) | rs);

delayms(2);

}

void lcd_reset()

{

lcd_port = 0xFF;delayms(20);

lcd_port = 0x03+ en;

lcd_port = 0x03;

delayms(20);

32

Page 33: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 33/38

lcd_port = 0x03 + en;

lcd_port = 0x03;

delayms(20);

lcd_port = 0x03 + en;;

lcd_port = 0x03;

delayms(20);

lcd_port = 0x02 + en;;

lcd_port = 0x02;

delayms(20);

}

void lcd_init()

{

lcd_reset();

lcd_cmd(0x28);

delayms(20);

lcd_cmd(0x28); // 4-bit mode – 2 line –5*7 font

delayms(20);

lcd_cmd(0x0C); //display on cursor – noblink

delayms(20);

lcd_cmd(0x06); // automatic increment –no display shift

delayms(20);

lcd_cmd(0x01); // address DDRAM with 0offset 80h

}

void timer0(void) interrupt 1 // p3.5 pin15

33

Page 34: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 34/38

{

EA=0;

if (flag==0)

{

P2_0 = ~ p2_0;

countpulse =0;

TL1 =0;

TH1=0;

TR1=1;

flag = 1;

delayms(20030);

delayms(20030);

}

else

{

TR1 = 0;

flag = 0;

countpulse = TH1;

countpulse = ((countpulse << 8) + TL1);

TL1= 0x00;

TH1= 0x00;

rpm1 = countpulse*60; //1000msec interval

buffer[ii++] = rpm1;

if (ii == 4)

ii=0;

34

Page 35: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 35/38

rpm = buffer[0] + buffer[1] + buffer[2]+ buffer[3];

rpm= rpm >>2;

lcd_cmd(0xC6);

temp1 = rpm / 1000;

lcd_data(temp1 + 0x30);

rpm = rpm % 1000;

temp1= rpm/100;

lcd_data(temp1 + 0x30);

rpm = rpm % 100;

temp1= rpm /10;

lcd_data(temp1+0x30);

rpm = rpm %10;

temp1= rpm ;

lcd_data(temp1 + 0x30);

}

EA=1;

}

{

lcd_port = ((cmd >> 4) & 0x0F)|LCD_EN;

lcd_port = ((cmd >> 4) & 0x0F);

lcd_port = (cmd & 0x0F)|LCD_EN;

lcd_port = (cmd & 0x0F);

delayus(200);

delayus(200);

35

Page 36: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 36/38

}

void lcd_data (unsigned char dat)

{

lcd_port = (((dat >> 4) & 0x0F)|LCD_EN|LCD_RS);

lcd_port = (((dat >> 4) & 0x0F)|LCD_RS);

 

lcd_port = ((dat & 0x0F)|LCD_EN|LCD_RS);

lcd_port = ((dat & 0x0F)|LCD_RS);

delayus(200);

delayus(200);}

36

Page 37: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 37/38

Conclusion

A digital tachometer based on an infrared light reflection technique has been demonstrated

successfully. Its major advantage is that it doesn’t require any physical contact with the rotating

shaft to measure its speed. This project can be extended further by adding data logging feature to

it. This is required in certain applications where the RPM of a rotating shaft is needed to be

monitored. The data logger will keep the records of varying RPM over time, and those records

can be later transferred to a PC through the USB interface.

37

Page 38: Microcontroller vs12

8/3/2019 Microcontroller vs12

http://slidepdf.com/reader/full/microcontroller-vs12 38/38

Circuit Diagram