david90
Advanced Member level 1
photointerrupter mcu
Here is my finished code (to me :|) This code is for WINAVR GCC compiler and it is for atmel MCU at90s2313.
The code above is for my electric scooter. The motor is attached to a slotted wheel and that wheel spins thru a photointerrupter. The circuit above is made to count those pulses to calc distance traveled (not yet finish) and mph. When I connect the mcu interrupt pin to the photointerrupter and spins the wheel with my hand, it counts very well. When I turn on the motor, my display seems to go crazy and crashes. This happen even when the MCU is not connected to the photointerrupter. I think it has to do with the EMF from the motor. Anyway, my circuit seems to count even when it is not grounded to the scooter. Lots of noise??? Can somebody give me some advice?
Added after 3 minutes:
basically i'm having a lot of false triggering and my 7segment displays go crazy.
Here is my finished code (to me :|) This code is for WINAVR GCC compiler and it is for atmel MCU at90s2313.
Code:
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
void delay(void);
void display7seg(void);
volatile uint16_t pulses=0,mph=0,mph_result=0;
int hun_mi,ten_mi,one_mi,hun_mph,ten_mph,one_mph;
const char segment_table[10]=
{
0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x2, 0x78, 0, 0x10
};
int main (void)
{
DDRB = 0xff;// set PORTB as output
DDRD = 0x77;
TCCR0 = 0x03;// delay timer
TCNT1H=0xC9;
TCNT1L=0x85;
TCCR1B=0x5; // clk/1024 TCCR1B=0x5; // clk/1024
TIMSK=0x80;
MCUCR=0x08;
SREG=0x80;
GIMSK=0x80;
while (1) // Forever
{
display7seg();
}
return 0;
}
void delay(void) //Produce a delay of 65 ms at 4 MHz
{
TCNT0=0x7F;
while (!(TIFR&0x02));
// Wait for timer0 overflow flag to be set
TIFR = 0x02; // Clear overflow flag
}
void display7seg(void)
{
hun_mi=(pulses)/100;
ten_mi=((pulses)%100)/10;
one_mi=((pulses)%100)%10;
hun_mph=(mph_result)/100;
ten_mph=((mph_result)%100)/10;
one_mph=((mph_result)%100)%10;
PORTB=segment_table[hun_mi];
PORTD=0x7B;
delay();
PORTB=segment_table[ten_mi];
PORTD=0x7D;
delay();
PORTB=segment_table[one_mi];
PORTD=0x7E;
delay();
PORTB=segment_table[hun_mph];
PORTD=0x6F;
delay();
PORTB=segment_table[ten_mph];
PORTD=0x5F;
delay();
PORTB=segment_table[one_mph];
PORTD=0x3F;
delay();
}
INTERRUPT(SIG_INTERRUPT1)
{
if (++pulses==999)
{
pulses=0;
}
mph++;
}
INTERRUPT(SIG_OVERFLOW1)
{
mph_result=mph*.08863;
mph=0;
TCNT1H=0xC9;
TCNT1L=0x61;
}
The code above is for my electric scooter. The motor is attached to a slotted wheel and that wheel spins thru a photointerrupter. The circuit above is made to count those pulses to calc distance traveled (not yet finish) and mph. When I connect the mcu interrupt pin to the photointerrupter and spins the wheel with my hand, it counts very well. When I turn on the motor, my display seems to go crazy and crashes. This happen even when the MCU is not connected to the photointerrupter. I think it has to do with the EMF from the motor. Anyway, my circuit seems to count even when it is not grounded to the scooter. Lots of noise??? Can somebody give me some advice?
Added after 3 minutes:
basically i'm having a lot of false triggering and my 7segment displays go crazy.