You are on page 1of 38

1

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.

Circuit Component 1. At89C51 microcontroller 2. 16*2 alpha-numeric display 3. IR LED 4. Photodiode 5. Capacitor Electrolytic capacitor Ceramic capacitor 6. Resistor

Quantity 1 1 1 1

1(10uF) 2(22pF) 100, 1k, 10k, 33k 68k, 100k

7. Crystal oscillator 8. IC 7805 9. Battery 10. Op-Amp 358 11. Rotating Fan 12. Potentiometer

1(11.0592MHz) 1 2 1 1 1(10k)

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.

J 1 V L C D D I S P L A Y 1 6 * 2 3 3 3 3 3 3 3 3 1 2 3 4 5 6 7 8 1 9 1 8 1 1 .0 5 9 2 M H z 3 1 9 A + 9 8 7 6 5 4 3 2 U P P P P P P P P P P P P P P P P 7 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 . . . . . . . . . . . . . . . . 0 1 2 3 4 5 6 7 / / / / / / / / A PD A PD A P D2 A P D2 A P D2 A P D2 A P D2 A P D2 2 0. 2 1. .2 2 .3 3 .4 4 .5 5 .6 6 .7 7 0 1 2 / 2A / 2A A2 A2 A2 A2 A2 A 1 2 3 1 4 1 5 1 6 1 7 1 8 1 8 9 1 0 0 K C 1 C K

1 2 3 4 5 6 7 8 9 1 1 1 1 1 1 1

0 1 2 3 4 5 6

/ / / / / /

0 1 2 3 4 5

D P

1 H O T O D I O

D D I R

2 E L E D

1 0 0 / TP 2 3 . 0 / R 1 X D 1 1 / T P 2 3 - .E 1 X / T 1 X 2 D 2 P 3 . 2 / I 1N 3 T 0 3 P 3 . 3 / I 1N 4 T 1 4 P 3 . 4 /1T 50 5 P 3 . 5 /1T 61 6 P 3 . 6 / W 7 R 1 7 P 3 . 7 / R D 3 0 L A1 L E / P R2 9O L 2 P S E N P P G

L M 3 5 8 3 + 2

3 3 K 4

X T A X T A E R

6 8 K

0 p F

A / V S T T 8 9 C

5 2

0 p F +

8 . 2

+ C

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. 1. 2. 3. 4.

COMPONENTS IN-BUILT RAM IN-BUILT ROM I/O PORTS TIMER

MICROPROCESSOR NO NO NO NO

MICROCONTROLLER YES YES YES YES

Thus microcontroller is

MICROCONTROLLER

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.

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

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

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.

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

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 Symbol I/O VSS VCC VEE RS R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 BPL Gnd ---I I I/O I/O I/O I/O I/O I/O I/O I/O I/O Descriptions Ground +5V power supply Power supply to control contrast RS=0 to select command register, RS=1 to select data register R/W=0 for write, R/W=1 for read Enable The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus Back Plane Light +5V or Lower Ground Voltage

Pin 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

11

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

No. 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17

Instruction Function Set: 8-bit, 1 Line, 5x7 Dots Function Set: 8-bit, 2 Line, 5x7 Dots Function Set: 4-bit, 1 Line, 5x7 Dots Function Set: 4-bit, 2 Line, 5x7 Dots Entry Mode Display off Cursor off (clearing display without clearing DDRAM content) Display on Cursor on Display on Cursor off Display on Cursor blinking Shift entire display left Shift entire display right Move cursor left by one character Move cursor right by one character Clear Display (also clear DDRAM content) Set DDRAM address or coursor position on display Set CGRAM address or set pointer to CGRAM location

Hex 0x30 0x38 0x20 0x28 0x06 0x08 0x0E 0x0C 0x0F 0x18 0x1C 0x10 0x14 0x01 0x80+add* 0x40+add**

Decimal 48 56 32 40 6 8 14 12 15 24 30 16 20 1 128+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

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

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

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 mode data 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

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

GATE C

/T M1 /
17

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 timers 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 This timer flag can be monitored When this timer flag is raised, one option CLR TR0 or CLR TR1, for timer 0 would be to stop the timer with the instructions and timer 1, respectively high a flag bit called TF (timer flag) Each timer has its own timer flag: TF0 for timer 0, and TF1 for timer 1

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

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

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 microcontrollers time by polling devices that do not need service

The advantage of interrupts is that the microcontroller can serve many devices (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

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

In the Program we have used IE=0x82 This indicates that we have used timer0 overflow interrupt. This statement enables timer0 overflow interrupt.

22

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

23

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 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. 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

Flow Chart of Main Program


STAR T

Set Input/output Port

LCD Initialization in 4bit mode

Initialize TMOD =0x51 Initialize Timer 0 Initialize Timer 1

Initialize IE=0x82

Flag=0

START Timer 0 START Timer 1

25

while (1)

STOP

LCD Initialization subroutine flowchart


Check RS RS=0 Command Register Initialize LCD in 4-bit mode RS=1 Data Register

Mask lower 4 bits

Mask lower 4 bits

Send higher nibble to LCD port Send enable signal to LCD Mask higher 4 bits Send lower nibble to Send enable signal to LCD port LCD

Send higher nibble to LCD port Send enable signal to LCD Mask higher 4 bits Send lower nibble to Send enable signal to LCD port LCD 26

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.
Flag== 0

Disable Interrupt (EA=0)

TRUE

FALSE

27

Countpulse= 0 Initialize counter Start counter Flag= 1 Delay ()

Stop Counter Count pulse= TH1 Countpulse= ((countpulse <<8) + TL1) Initialize counter

Enable Interrupt (EA=1)

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

//D7 P1.3 //EN P1.7 //RS P1.5 #include<AT89X51.H> #include<string.h> #include<stdio.h>

#define en LCD control pins #define rs #define lcd_port

0x80 0x20 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

P0=0x00; port for pwm P1=0x00; or LCD display P2=0x00; 2_5 to 7 as input for kbd P3=0xFF; port for (pwm)count pulse

//set port 0 as o/p //set port1 as o/p //set port2 as o/p port for pwm & //set port3 as i/p

Lcd_init(); TMOD = 0x51; counter in mode 16 bit timer in mode 16 bit TL0= 0x00; TH0= 0x00; TL1= 0x00; TH1=0x00; IE = 0x82; Flag =0; Lcd_cmd(0x01); Sprintf(str, TECHO-METER); Lcd_cmd(0x84); beginng of 1st line Display(str); Sprintf(str,RPM:); Lcd_mcd(0xc2); tobegin of 2nd line Display(str); // forse cursor // force cursor to // clear display //timer 1 as //timer2 as

30

TR0=1; TR1=1; Buffer[0] = buffer[1] = buffer[2]= buffer[3]= 0; while(1) { } } // function starts here void display(char *str) on LCD { unsigned int j; for(j=0 ; j<strlen(*str) ; j++) { lcd_data (str[j]); } } void delayms(unsigned int tme) { // fn to display

unsigned int k2, k1; for(k1=0 ; k1<tme ; k1++) { for(k2=0; k2<1; k2++); } } void lcd_cmd(char cmd)

31

{ 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

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); 5*7 font delayms(20); lcd_cmd(0x0C); blink delayms(20); lcd_cmd(0x06); no display shift delayms(20); lcd_cmd(0x01); offset 80h } void timer0(void) interrupt 1 // p3.5 pin15
33

// 4-bit mode 2 line

//display on cursor no

// automatic increment

// address DDRAM with 0

{ 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; msec interval buffer[ii++] = rpm1; if(ii == 4) ii=0; //1000

34

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

} 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

Conclusion
A digital tachometer based on an infrared light reflection technique has been demonstrated successfully. Its major advantage is that it doesnt 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

Circuit Diagram

38

You might also like