/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.2c AlmostFULL
Automatic Program Generator
© Copyright 1998-2004 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.ro
e-mail:office@hpinfotech.ro

Project : Tachometer
Version : 1.0
Date    : 5/18/2006
Author  : Ashish V. Deshpande
Company : 
Comments: 
This program is for measuring propeller speed
of aeroplane.


Chip type           : ATmega16
Program type        : Application
Clock frequency     : 8.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/

#include <delay.h>
#include <mega16.h>
#include<bcd.h>
#include<math.h>
#define plus ~PIND.0
#define minus ~PIND.1
#define select ~PIND.2
#define exit ~PIND.3
#define prop 0
#define motor_speed 1
#define interval 2
#define bounce_delay delay_ms(200);
#define comp_dis ACSR=ACSR | 0x80
#define comp_en ACSR=ACSR & 0x7F
#define tmr1_stp TCCR1B=TCCR1B&0xF8 
#define tmr1_strt TCCR1B=0x0C
unsigned int count=0;
float temp;
unsigned char menu_no=prop,no_prop=1,spd=8,intvl=1,x,time=0,m1,m2,m3,m4,m5;
bit measure=0;
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>

void stop()
{
        comp_dis;
        count=0;
        temp=0;
        time=0;
        TCNT1H=0x00;
        TCNT1L=0x00;
        tmr1_stp;
}
void start()
{
        count=0;
        temp=0;
        time=0;
        TCNT1H=0x00;
        TCNT1L=0x00;
        comp_en;
        tmr1_strt;
}
void cl_dis()
{ 
  lcd_clear();
  _lcd_ready(); 
  lcd_gotoxy(0,0);
  lcd_putsf("RPM Speed is:");
  lcd_gotoxy(0,1);
  lcd_putsf("      rev/min   ");
  lcd_gotoxy(0,1);
  temp=temp*(60/intvl)/no_prop;
  (char)m5=floor(temp/10000);
  temp=fmod(temp,10000);
  _lcd_ready();
  lcd_putchar(m5+48);
  (char)m4=floor(temp/1000);
  temp=fmod(temp,1000);
  _lcd_ready();
  lcd_putchar(m4+48);
  (char)m3=floor(temp/100);
  temp=fmod(temp,100);
  _lcd_ready();
  lcd_putchar(m3+48);
  (char)m2=floor(temp/10);
  temp=fmod(temp,10);
  _lcd_ready();
  lcd_putchar(m2+48);
  (char)m1=floor(temp/1);
  temp=fmod(temp,1);
  _lcd_ready();
  lcd_putchar(m1+48);
  if(m5>9)
  {
        lcd_clear();
        lcd_gotoxy(0,0);
        lcd_putsf("Error!! Speed");
        lcd_gotoxy(0,1);
        lcd_putsf("Limit Exceeded");
  }
  stop();
}
// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here
PORTC=~PORTC;
TCNT1H=0x00;
TCNT1L=0x00;
time=time+1;
if(time==intvl)
{
(float)temp=count;
time=0;
count=0;
if(measure==1)
cl_dis();
}
}

// Analog Comparator interrupt service routine
interrupt [ANA_COMP] void ana_comp_isr(void)
{
// Place your code here
    count=count+1;
}

// Declare your global variables here
void display_menu()
{
	lcd_gotoxy(0,1);
	if(menu_no==0)
	{       
		_lcd_ready();
		lcd_putsf("Propellers      ");
	}
	if(menu_no==1)
	{       
		_lcd_ready();
		lcd_putsf("Motor Speed     ");
	}
	if(menu_no==2)
	{       
		_lcd_ready();
		lcd_putsf("Interval        ");
	}
}
void entr(char t)
{                                                     
   	if(t==prop)
	{       
		lcd_gotoxy(0,1);
	 	_lcd_ready();
	 	lcd_putsf("                ");
		while(~exit)
		{
		lcd_gotoxy(0,0);
		_lcd_ready();
	 	lcd_putsf("No of Propellers");
	   	_lcd_ready();
	  	lcd_gotoxy(0,1);
	  	lcd_putchar(no_prop+48);
	 	if(plus)
	 	{
	 		bounce_delay;
	 		if(no_prop!=8)
	 		{no_prop++;}
	 	}
	 	if(minus)
	 	{
	 		bounce_delay;
	 		if(no_prop!=1)
	 		{no_prop--;}
	 	}
	 	}
	}
	if(t==motor_speed)
	{       
		lcd_gotoxy(0,1);
		lcd_putsf("                ");
		while(~exit)
		{
		lcd_gotoxy(0,0);
		_lcd_ready();
	 	lcd_putsf("  Motor Speed   ");
	 	_lcd_ready();
	  	lcd_gotoxy(0,1);
	  	lcd_putchar(spd+48);
	 	if(plus)
	 	{
	 		bounce_delay;
	 		if(spd!=8)
	 		{spd++;}
	 	}
	 	if(minus)
	 	{
	 		bounce_delay;
	 		if(spd!=0)
	 		{spd--;}
	 	}
	 	}
	 	if(spd!=8)
	 	{OCR2=32*spd;}
	 	else
	 	OCR2=255;
	 }
	if(t==interval)
	{       
		lcd_gotoxy(0,1);
		lcd_putsf("    Seconds     ");
		while(~exit)
		{
		lcd_gotoxy(0,0);
		_lcd_ready();
	 	lcd_putsf(" Measuring Time");
	 	_lcd_ready();
	  	lcd_gotoxy(0,1);
	  	x=(floor(intvl/10))+48;
	  	lcd_putchar(x);
	  	lcd_gotoxy(1,1);
	  	x=(fmod(intvl,10))+48;
	  	lcd_putchar(x);
	 	if(plus)
	 	{
	 		bounce_delay;
	 		if(intvl!=64)
	 		{intvl=intvl*2;}
	 	}
	 	if(minus)
	 	{
	 		bounce_delay;
	 		if(intvl!=1)
	 		{intvl=intvl/2;}
	 	}
	 	}
}
}
void menu()
{
stop();
measure=0;       
while(~exit)
{
	lcd_gotoxy(0,0);
 	_lcd_ready();
	lcd_putsf("     *MENU*     ");
	display_menu();
       	 	if(plus)
 	 	{
 	 		bounce_delay;
 	 		if(menu_no!=2)
 	 		{menu_no++;}
 	 		lcd_gotoxy(0,0);
 	 		_lcd_ready();
			lcd_putsf("     *MENU*     ");
			display_menu();
		}
 	 	if(minus)
 	 	{
 	 		bounce_delay
 	 		if(menu_no!=0)
 	 		{menu_no--;}
 	 		lcd_gotoxy(0,0);
 	 		_lcd_ready();
			lcd_putsf("     *MENU*     ");
			display_menu();
 	 	}
 	       	if(select)
 	 	{
 	 		entr(menu_no);
 	 		bounce_delay;
 	 	}
 	}
 measure=1;
 start();
 lcd_clear();
 _lcd_ready();
 lcd_gotoxy(0,0);
 lcd_putsf("Measuring....");       
}

void main(void)
{ 
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0 
PORTC=0x00;
DDRC=0xFF;

// Port D initialization
// Func7=Out Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In 
// State7=0 State6=P State5=1 State4=1 State3=P State2=P State1=P State0=P 
PORTD=0x7F;
DDRD=0xB0;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0xF0;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 31.250 kHz
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x0C;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x7A;
OCR1AL=0x12;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Phase correct PWM top=FFh
// OC2 output: Non-Inverted PWM
ASSR=0x00;
TCCR2=0x71;                 
TCNT2=0x00;
OCR2=0xF0;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x10;

// Analog Comparator initialization
// Analog Comparator: On
// Interrupt on Rising Output Edge
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: On
ACSR=0x2B;
SFIOR=0x00;

// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")
_lcd_ready();
lcd_gotoxy(0,0);
lcd_putsf("   TACHOMETER   ");
lcd_gotoxy(0,1);
lcd_putsf("By Ashish");
delay_ms(100);
while(1)
{
menu();
while(~select);
}
}
