kmdineshece
Member level 1
- Joined
- Dec 17, 2013
- Messages
- 36
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 6
- Activity points
- 384
Hi everyone!...
8-magenetic sensor , 2-LM324 (8-operation-amplifiers), 8:1 Multiplexer (74HC4051), ATmega88PA
I want to measure RPM of 8-Motors continuously, I have written a code, but the controller sometimes reads previous sensor output and update into succeed sensor(next, eg: resultant of sensor one is update into second)
Conditions:if RPM less than 9800 update as Zero,
if RPM above 22000 update as old,
pls help me!...
8-magenetic sensor , 2-LM324 (8-operation-amplifiers), 8:1 Multiplexer (74HC4051), ATmega88PA
I want to measure RPM of 8-Motors continuously, I have written a code, but the controller sometimes reads previous sensor output and update into succeed sensor(next, eg: resultant of sensor one is update into second)
Conditions:if RPM less than 9800 update as Zero,
if RPM above 22000 update as old,
pls help me!...
Code:
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 uint16_t count=0,flag=0,flag1=0,sensor=0,m=1,z=1,breake,data=0; //Main revolution counter
volatile uint16_t newpulse=0; //Revolution per second
volatile uint16_t old=0; //Revolution per second
volatile unsigned int result,rps,rpm,rpmold,rpm_old[25],led[25],rps0,rps1,rps2,rps3,rpm_rsm[25]; //Revolution per second
volatile uint16_t capture=0,captureold; //Revolution per second
volatile uint16_t period1; //Main revolution counter
volatile uint16_t period2,period3;
#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];
#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);
}
}
pulse()
{
OCR0B = 30; //24
DDRD |= (1<<PORTD5); //OC0B output
TCCR0A = (1<<COM0B0)|(1<<WGM01); //toggle, CTC
TCCR0B = (1<<CS01); //div1
//OCR0A = F_CPU/8/2/40000 - 1; //set CTC freq
OCR0A=30;
}
address (unsigned int i)
{
if(i==1)
{
PORTB &= ~(1 << PINB2); //MUX-1
PORTB |= (1 << PINB3)|(1 << PINB4);
PORTD &= ~(1 << PIND2); //000 Address
PORTD &= ~(1 << PIND3);
PORTD &= ~(1 << PIND4);
}
else if(i==2)
{
PORTB &= ~(1 << PINB2); //MUX-1
PORTD &= ~(1 << PIND4); //001 Address
PORTD &= ~(1 << PIND3);
PORTD |= (1 << PIND2);
}
else if(i==3)
{
PORTB &= ~(1 << PINB2); //MUX-1
PORTD &= ~(1 << PIND2); //010 Address
PORTD &= ~(1 << PIND4);
PORTD |= (1 << PIND3);
}
else if(i==4)
{
PORTB &= ~(1 << PINB2); //MUX-1
PORTD &= ~(1 << PIND4); //011 Address
PORTD |= (1 << PIND2)|(1 << PIND3);
}
else if(i==5)
{
PORTB &= ~(1 << PINB2); //MUX-1
PORTD &= ~(1 << PIND3); //100 Address
PORTD &= ~(1 << PIND2);
PORTD |= (1 << PIND4);
}
else if(i==6)
{
PORTB &= ~(1 << PINB2); //MUX-1
PORTD &= ~(1 << PIND3); //101 Address
PORTD |= (1 << PIND4)|(1 << PIND2);
}
else if(i==7)
{
PORTB &= ~(1 << PINB2); //MUX-1
PORTD &= ~(1 << PIND2); //110 Address
PORTD |= (1 << PIND3)|(1 << PIND4);
}
else if(i==8)
{
PORTB &= ~(1 << PINB2); //MUX-1
PORTD |= (1 << PIND3)|(1 << PIND4)|(1 << PIND2); //111 Address
}
}
store_data(int x, unsigned int y)
{
if(x==1)
{
rpm_old[1]=rpm;
}
else if(x==2)
{
rpm_old[2]=rpm;
}
else if(x==3)
{
rpm_old[3]=rpm;
}
else if(x==4)
{
rpm_old[4]=rpm;
}
else if(x==5)
{
rpm_old[5]=rpm;
}
else if(x==6)
{
rpm_old[6]=rpm;
}
else if(x==7)
{
rpm_old[7]=rpm;
}
else if(x==8)
{
rpm_old[8]=rpm;
}
}
void main()
{
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
TCCR0A=0;
TCCR1B |= (1<<ICES1)|(1<<CS12);
TIMSK1 |= (1<<ICIE1)|(1<<TOIE1); // (00100001) Input capture and overflow interupts enabled
TCNT1 =0; // start from 0
sei();
uint8_t u8TempData;
PORTB &= ~(1 << PINB2); //MUX-1
PORTB |= (1 << PINB3)|(1 << PINB4);
PORTD &= ~(1 << PIND4); //000 Address
PORTD &= ~(1 << PIND2);
PORTD &= ~(1 << PIND3);
USART0Init();
while(1)
{
if(flag1==1)
{
TIMSK1 &=~(1<<ICES1);
TIMSK1 &=~(1<<TOIE1);
if(m==1)
{
address(2);
if((9800<=rpm)&&(rpm<=22000))
{
store_data(m,rpm);
sprintf(RPMa_arr,"RPM1:%5u\n\r",rpm);
USART_puts(RPMa_arr);
//sprintf(RPMa_arr,"RPMC1:%5u\n\r",data);
//USART_puts(RPMa_arr);
}
else if(rpm<9800)
{
breake=0;
sprintf(RPMa_arr,"RPM1:%5u\n\r",breake);
USART_puts(RPMa_arr);
}
else if(rpm>22000)
{
sprintf(RPMa_arr,"RPM1:%5u\n\r",rpm_old[m]);
USART_puts(RPMa_arr);
//sprintf(RPMa_arr,"RPMC1:%5u\n\r",data);
//USART_puts(RPMa_arr);
}
m!=1;
rpm=0;
flag1=0;
}
else if(m==2)
{
address(3);
if((9800<=rpm)&&(rpm<=22000))
{
store_data(m,rpm);
sprintf(RPMa_arr,"RPM2:%5u\n\r",rpm);
//sprintf(RPM_arr,"%5d",RPM1);
USART_puts(RPMa_arr);
// sprintf(RPMa_arr,"RPMC2:%5u\n\r",data);
//USART_puts(RPMa_arr);
}
else if(rpm<9800)
{
breake=0;
sprintf(RPMa_arr,"RPM2:%5u\n\r",breake);
USART_puts(RPMa_arr);
}
else if(rpm>22000)
{
sprintf(RPMa_arr,"RPM2:%5u\n\r",rpm_old[m]);
USART_puts(RPMa_arr);
// sprintf(RPMa_arr,"RPMC1:%5u\n\r",data);
// USART_puts(RPMa_arr);
}
//PORTB &= ~(1 << PINB2); //MUX-1
//PORTD &= ~((1 << PIND2)|(1 << PIND3)); //010 Address
//PORTD |= (1 << PIND4);
m!=2;
rpm=0;
data=0;
flag1=0;
}
else if(m==3)
{
address(4);
if((9800<=rpm)&&(rpm<=22000))
{
store_data(m,rpm);
sprintf(RPMa_arr,"RPM3:%5u\n\r",rpm);
//sprintf(RPM_arr,"%5d",RPM1);
USART_puts(RPMa_arr);
//sprintf(RPMa_arr,"RPMC3:%5u\n\r",data);
//USART_puts(RPMa_arr);
}
else if(rpm<9800)
{
breake=0;
sprintf(RPMa_arr,"RPM3:%5u\n\r",breake);
USART_puts(RPMa_arr);
}
else if(rpm>22000)
{
sprintf(RPMa_arr,"RPM3:%5u\n\r",rpm_old[m]);
USART_puts(RPMa_arr);
//sprintf(RPMa_arr,"RPMC3:%5u\n\r",data);
//USART_puts(RPMa_arr);
}
rpm=0;
data=0;
//PORTB &= ~(1 << PINB2); //MUX-1
//PORTD &= ~((1 << PIND4)|(1 << PIND3)); //011 Address
//PORTD |= (1 << PIND2);
//sensor=3;
m!=3;
flag1=0;
}
else if(m==4)
{
address(5);
if((9800<=rpm)&&(rpm<=22000))
{
store_data(m,rpm);
sprintf(RPMa_arr,"RPM4:%5u\n\r",rpm);
//sprintf(RPM_arr,"%5d",RPM1);
USART_puts(RPMa_arr);
//sprintf(RPMa_arr,"RPMC4:%5u\n\r",data);
//USART_puts(RPMa_arr);
}
else if(rpm<9800)
{
breake=0;
sprintf(RPMa_arr,"RPM4:%5u\n\r",breake);
USART_puts(RPMa_arr);
}
else if(rpm>22000)
{
sprintf(RPMa_arr,"RPM4:%5u\n\r",rpm_old[m]);
USART_puts(RPMa_arr);
//sprintf(RPMa_arr,"RPMC4:%5u\n\r",data);
//USART_puts(RPMa_arr);
}
rpm=0;
data=0;
// PORTB &= ~(1 << PINB2); //MUX-1
//PORTD &= ~((1 << PIND4)|(1 << PIND2)); //100 Address
//PORTD |= (1 << PIND3);
//sensor=4;
m!=4;
flag1=0;
}
else if(m==5)
{
address(6);
if((9800<=rpm)&&(rpm<=22000))
{
store_data(m,rpm);
sprintf(RPMa_arr,"RPM5:%5u\n\r",rpm);
//sprintf(RPM_arr,"%5d",RPM1);
USART_puts(RPMa_arr);
}
else if(rpm<9800)
{
breake=0;
sprintf(RPMa_arr,"RPM5:%5u\n\r",breake);
USART_puts(RPMa_arr);
}
else if(rpm>22000)
{
sprintf(RPMa_arr,"RPM5:%5u\n\r",rpm_old[m]);
USART_puts(RPMa_arr);
//sprintf(RPMa_arr,"RPMC5:%5u\n\r",data);
//USART_puts(RPMa_arr);
}
rpm=0;
data=0;
//PORTB &= ~(1 << PINB2); //MUX-1
//PORTD &= ~(1 << PIND4); //101 Address
//PORTD |= (1 << PIND3)|(1 << PIND2);
//sensor=5;
m!=5;
flag1=0;
}
else if(m==6)
{
address(7);
if((9800<=rpm)&&(rpm<=22000))
{
store_data(m,rpm);
sprintf(RPMa_arr,"RPM6:%5u\n\r",rpm);
//sprintf(RPM_arr,"%5d",RPM1);
USART_puts(RPMa_arr);
}
else if(rpm<9800)
{
breake=0;
sprintf(RPMa_arr,"RPM6:%5u\n\r",breake);
USART_puts(RPMa_arr);
}
else if(rpm>22000)
{
sprintf(RPMa_arr,"RPM6:%5u\n\r",rpm_old[m]);
USART_puts(RPMa_arr);
}
rpm=0;
data=0;
//PORTB &= ~(1 << PINB2); //MUX-1
//PORTD &= ~(1 << PIND2); //110 Address
//PORTD |= (1 << PIND3)|(1 << PIND4);
//sensor=6;
m!=6;
flag1=0;
}
else if(m==7)
{
address(8);
if((9800<=rpm)&&(rpm<=22000))
{
store_data(m,rpm);
sprintf(RPMa_arr,"RPM7:%5u\n\r",rpm);
//sprintf(RPM_arr,"%5d",RPM1);
USART_puts(RPMa_arr);
}
else if(rpm<9800)
{
breake=0;
sprintf(RPMa_arr,"RPM7:%5u\n\r",breake);
USART_puts(RPMa_arr);
}
else if(rpm>22000)
{
sprintf(RPMa_arr,"RPM7:%5u\n\r",rpm_old[m]);
USART_puts(RPMa_arr);
}
rpm=0;
data=0;
//PORTB &= ~(1 << PINB2); //MUX-1
//PORTD |= (1 << PIND3)|(1 << PIND4)|(1 << PIND2); //111 Address
//sensor=7;
m!=7;
flag1=0;
}
else if(m==8)
{
address(1);
if((9800<=rpm)&&(rpm<=22000))
{
store_data(m,rpm);
sprintf(RPMa_arr,"RPM8:%5u\n\r",rpm);
//sprintf(RPM_arr,"%5d",RPM1);
USART_puts(RPMa_arr);
}
else if(rpm<9800)
{
breake=0;
sprintf(RPMa_arr,"RPM8:%5u\n\r",breake);
USART_puts(RPMa_arr);
}
else if(rpm>22000)
{
sprintf(RPMa_arr,"RPM8:%5u\n\r",rpm_old[m]);
USART_puts(RPMa_arr);
}
rpm=0;
data=0;
//PORTB &= ~(1 << PINB3); //MUX-2
//PORTB |= (1 << PINB2)|(1 << PINB4);
//PORTD &= ~((1 << PIND4)|(1 << PIND3)|(1 << PIND2)); //000
Address
//sensor=8;
m=0;
flag1=0;
}
/*
else if(sensor==8)
{
if((9500<=rpm)&&(rpm<=25000))
{
sprintf(RPMa_arr,"RPM9:%5u\n\r",rpm);
//sprintf(RPM_arr,"%5d",RPM1);
USART_puts(RPMa_arr);
}
else if(rpm<9500)
{
sprintf(RPMa_arr,"RPM9:%5u\n\r",rpm);
USART_puts(RPMa_arr);
}
else if(rpm>25000)
{
sprintf(RPMa_arr,"RPM9:%5u\n\r",rpm);
USART_puts(RPMa_arr);
}
rpm=0;
PORTB &= ~(1 << PINB3); //MUX-2
PORTD &= ~((1 << PIND4)|(1 << PIND3)); //001 Address
PORTD |= (1 << PIND2);
sensor=9;
flag1=0;
}
TCCR1B |= (1<<ICES1)|(1<<CS12);
}
}
ISR(TIMER1_CAPT_vect)
{
//CPU Jumps here automatically when INT0 pin detect a falling edge
period2=ICR1;
result=period2-period1;
period1=period2;
rps=(1000000/(result*128/10)*60);
}
ISR(TIMER1_OVF_vect)
{
//CPU Jumps here every 1 sec exactly!
TIMSK1 &= ~(1<<ICIE1);
rpm=rps;
capture=result;
m=m+1;
result=0;
period1=0;
period2=0;
rps=0;
flag1=1;
//TCNT1=0;
}