Avr Project - Atmega8 Based RPM Meter
Avr Project - Atmega8 Based RPM Meter
Avr Project - Atmega8 Based RPM Meter
RPM Meter
By Avinash
In AVR Projects
Hello All,
Today I will show you how you can make a simple RPM Meter using AVR ATmega8. The
RPM meter we will be making is a contact less type, i.e. it measures the RPM of a
rotating object without actually making any contact with it. An IR reflectance sensor will
be used to sense the speed. You have to attach a white reflective object (like a white
paper sticker) at one point in the periphery of rotation . Then you need to place the
reflectance sensor such that the white reflector comes just above it once per rotation. In
this way the sensor will give one falling edge to the MCU per rotation, we will measure
number of such pulse in one second to get the revolution per second, multiplying this
with 60 we get RPM.
For this project I will use a ATmega8 MCU connected to a 162 LCD Module for showing
the RPM.
that means TIMER1 is clocked at 1/1024 of system clock. As our system clock is
1000000, timer frequency is 1000000/1024=976 Hz. To get exact 1 sec time base we
use the comparator unit of timer, we set compare value to 976. So as soon as TIMER1
reaches 976 it is cleared to 0 (CTC mode) and an interrupt called output compare 1A (1
because it is timer1 and A because timer1 has 2 comparator A and B)
In the main loop of the program we just print RPM and RPS.
#include <util/delay.h>
#include "lcd.h"
void Wait()
{
uint8_t i;
for(i=0;i<2;i++)
{
_delay_loop_2(0);
}
}
void main()
{
LCDInit(LS_NONE);
LCDWriteString("RPM Meter");
LCDWriteStringXY(0,1,"- by avinash");
Wait();
Wait();
Wait();
Wait();
//Init INT0
MCUCR|=(1<<ISC01); //Falling edge on INT0 triggers interrupt.
TCCR1B|=((1<<WGM12)|(1<<CS12)|(1<<CS10));
//Compare value=976
OCR1A=976;
LCDClear();
LCDWriteStringXY(0,0,"RPM =");
LCDWriteStringXY(0,1,"RPS =");
while(1)
{
LCDWriteIntXY(6,0,rpm,5);
LCDWriteIntXY(6,1,rps,5);
{
PORTB&=(~(1<<PB1));
Wait();
}
ISR(INT0_vect)
{
//CPU Jumps here automatically when INT0 pin detect a falling edge
count++;
}
ISR(TIMER1_COMPA_vect)
{
//CPU Jumps here every 1 sec exactly!
rps=count;
rpm=rps*60;
count=0;
}
The above code can be compiled using avr-gcc (WinAVR distro) and AVR Studio. Details
about downloading, installing and using the software is described in the following
tutorial.
The code depends on the LCD Library for displaying textual/numeric data on a
standard 162 LCDModule. The file "lcd.c" must be copied to the project folder and
added to the project using AVR Studio. See LCD Tutorial for more info.
Compatible lcd.c and lcd.h files are provided at the bottom of this article.
After compiling the code, burn it to the MCU using an AVR Programmer. The Fuse bits
must be set as follows.
HIGH=D9
LOW=E1
Please note that the above is default for ATmega8, so if you purchased a new chip then
you do not need to change them. But if you have purchased xBoard MINI v2.0 then you
need to write the above fuse bytes, as xBoard MINI v2.0 used different
configuration.
Downloads