Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

RPM measurement -ATmega88PA, 74HC4051, Magnetic Sensor

Status
Not open for further replies.

kmdineshece

Member level 1
Joined
Dec 17, 2013
Messages
36
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
384
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include<avr/pgmspace.h>
#include<math.h>
#include<stdlib.h>
#include <util/delay.h>
#include<string.h>
#include<stdio.h>
volatile unsigned int count=0,flag=0,m;
volatile unsigned int flag1=0;
volatile unsigned int update=0;
volatile unsigned int sensor=0; //Main revolution counter
volatile unsigned int breake=0;
volatile unsigned int newpulse=0; //Revolution per second

volatile unsigned int result;
volatile unsigned int rpm[25],rps[25];
volatile unsigned int rpmold,i,j;

volatile unsigned int capture=0;
volatile unsigned int captureold; //Revolution per second
volatile unsigned int period1; //Main revolution counter
volatile unsigned int period2;
#define F_CPU 20000000UL


//uint32_t T;
//unsigned int count=0;
float RPM_float;
char RPM_arr[6];
float RPMa_float;
char RPMa_arr[6];
float RPMb_float;
char RPMb_arr[6];

float RPMd_float;
char RPMd_arr[6];
//uint16_t RPM0,RPM1,RPM2,RPM3,RPM4,RPM5,RPM6,RPM7,RPM8;    // Revolutions per minute
#define USART_BAUDRATE 19200
#define UBRR_VALUE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)


void USART0Init(void)
{
	// Set baud rate
	UBRR0H = (uint8_t)(UBRR_VALUE>>8);
	UBRR0L = (uint8_t)UBRR_VALUE;
	// Set frame format to 8 data bits, no parity, 1 stop bit
	UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);
	//enable transmission and reception
	UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
}
void USART0SendByte(unsigned char u8Data)
{
	//wait while previous byte is completed
	while(!(UCSR0A&(1<<UDRE0))){};
	// Transmit data
	UDR0 = u8Data;
}
uint8_t USART0ReceiveByte()
{
	// Wait for byte to be received
	while(!(UCSR0A&(1<<RXC0))){};
	// Return received data
	return UDR0;
}

void USART_puts(char *s)
{
	while(*s)
	{
		USART0SendByte(*s);
		s++;
	}
}

void Wait()
{
	uint8_t i;
	for(i=0;i<2;i++)
	{
		_delay_loop_2(0);
	}
}


int main()
{
		Wait();
	Wait();
	
	PORTB|=(1<<PINB0);     //pullup enabled
	DDRB&=~(1<<PINB0);     //ICR1 as input
	DDRB |= (1 << PINB2)| (1 << PINB3)| (1 << PINB4); 
DDRD |= (1 << PIND2)| (1 << PIND3)| (1 << PIND4)| (1 << PIND5)| (1 << PIND6)| (1 << PIND7); 
	TCCR1A =0;      // normal mode
	TCCR1B |= (1<<ICNC1)|(1<<ICES1)|(1<<CS12);     // (01000100) Rising edge trigger, Timer = CPU Clock/256 -or- 14745600/256 -or- 57.6KHz
	
	TIMSK1 |= (1<<ICIE1)|(1<<TOIE1);     // (00100001) Input capture and overflow interupts enabled

	TCNT1 =0;       // start from 0
	
	//Enable interrupts globaly
	sei();
	//pulse();
	
	//uint8_t u8TempData;
	
	//Initialize USART0
	USART0Init();
	
	while(1)
	{
		if(flag1==1)
		{
			for(m=1;m<=4;m++)
			{
				
			
			if((9500<=rpm[m])&&(rpm[m]<=25000))
			{
				
				
				
				sprintf(RPMa_arr,"RPM%d\n\r",m);
				USART_puts(RPMa_arr);
				sprintf(RPMa_arr,"%5u\n\r",rpm[m]);
				USART_puts(RPMa_arr);
				
				
			}
			else if(rpm[m]<9500)
			{
				breake=0;
				
				sprintf(RPMa_arr,"RPM%d\n\r",m);
				USART_puts(RPMa_arr);
				sprintf(RPMa_arr,"%5u\n\r",breake);
				USART_puts(RPMa_arr);
				
			}
			else if(rpm[m]>25000)
			{
				//Do Nothing
			}
			//update=1;
		}		
			
         	                   }

		}
                                     }




ISR(TIMER1_CAPT_vect)
{
	//CPU Jumps here automatically when INT0 pin detect a falling edge
	if(sensor==0)
	{
		//MUX-1-LOW,MUX-2-HIGH,MUX-3-HIGH
		//MUX-1-000 CHANNEL
	   	PORTD &= ~((1 << PIND0)|(1 << PIND1)|(1 << PIND2));		//000 Address
		
	    period2=ICR1;
	    result=period2-period1;
	    period1=period2;
	    rps[1]=(result*128/10);
	    rps[1]=1000000/rps[1];
		PORTD &= ~((1 << PIND1)|(1 << PIND2));		//001 Address
		PORTD |= (1 << PIND0);	
		sensor=1;
		}
		
	else if(sensor==1)
	{
		//MUX-1-LOW,MUX-2-HIGH,MUX-3-HIGH
		//MUX-1-001 CHANNEL
		period2=ICR1;
		result=period2-period1;
		period1=period2;
		rps[2]=(result*128/10);
		rps[2]=1000000/rps[2];
			PORTD &= ~((1 << PIND0)|(1 << PIND1));		//010 Address
			PORTD |= (1 << PIND2);
		sensor=2;
	}
	else if(sensor==2)
	{
		//MUX-1-LOW,MUX-2-HIGH,MUX-3-HIGH
		//MUX-1-010 CHANNEL
		period2=ICR1;
		result=period2-period1;
		period1=period2;
		rps[3]=(result*128/10);
		rps[3]=1000000/rps[3];
			PORTD &= ~(1 << PIND2);		//011 Address
			PORTD |= (1 << PIND1)|(1 << PIND0);
		sensor=3;
	}
	else if(sensor==3)
	{
		//MUX-1-LOW,MUX-2-HIGH,MUX-3-HIGH
		//MUX-1-101 CHANNEL
		period2=ICR1;
		result=period2-period1;
		period1=period2;
		rps[4]=(result*128/10);
		rps[4]=1000000/rps[4];
			PORTD &= ~((1 << PIND1)|(1 << PIND0));		//100 Address
			PORTD |= (1 << PIND2);
		sensor=0;
	}
	}
ISR(TIMER1_OVF_vect)
{
	//CPU Jumps here every  838 ms exactly!
	for(i=1;i<=4;i++)
	//rps=count;6
	{
		rpm[i]=rps[i]*60;
		rps[i]=0;
		
	}
	
			
	flag1=1;
	
}
mux.jpg
is the code right?, how to read multiple sensor using mux (74hc4051)address lines and input capture pin?
 
Last edited by a moderator:

Hi,

It's hard to read foreign code. Then finding an error, when one does not know where or what to look for, is even harder.

Did you test the code?
What is happening?
Did you encounter any problems?

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top