After a few queries about code used for tachometer in my video:
I decided to publish it. It's much easire than sending code individually ;) It's written for atmega8 controller running on 1 MHz internal clock.
Code:
Code:
#include <avr/io.h>I also used a bit enhanced version of code above for my outboard motor multimeter (multi 'cause it measures rpm, gasoline flow and tank level). Take a look:
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
volatile uint16_t count=0; //revolution counter for INT0
volatile uint16_t count1=0; //revolution counter for INT1
volatile uint16_t rpm=0; //Revolution per minute
volatile uint16_t rps=0; //Revolution per second
volatile uint16_t rpm1=0;
volatile uint16_t rps1=0;
void main()
{
char str[15]; //polje stringova za lcd
lcd_init();lcd_clrscr();
_delay_ms(1000); //pricekati stabilizaciju
//Init INT0
MCUCR|=(1<<ISC01); //Falling edge on INT0 triggers interrupt.
GICR|=(1<<INT0); //Enable INT0 interrupt/ General Interrupt control register
//Init INT1
MCUCR|=(1<<ISC11); //Falling edge on INT1 triggers interrupt.
GICR|=(1<<INT1); //Enable INT1 interrupt
//Timer1 is used as 1 sec time base
//Timer Clock = 1/1024 of sys clock
//Mode = CTC (Clear Timer On Compare)
TCCR1B|=((1<<WGM12)|(1<<CS12)|(1<<CS10));
//Compare value=976
OCR1A=976;
TIMSK|=(1<<OCIE1A); //Output compare 1A interrupt enable
//Enable interrupts globaly
sei();
lcd_goto(0x00);lcd_puts("rpm:");
lcd_goto(0x40);lcd_puts("Hz:");
while(1)
{
rpm=rps*60;rpm1=rps1*60; //da se skrati vrijeme ISR, prebačeno iz ISR
lcd_goto(0x04);itoa(rpm,str,10);lcd_puts(str);lcd_puts(" ");
lcd_goto(0x44);itoa(rps,str,10);lcd_puts(str);lcd_puts(" ");
lcd_goto(0x09);itoa(rpm1,str,10);lcd_puts(str);lcd_puts(" ");
lcd_goto(0x49);itoa(rps1,str,10);lcd_puts(str);lcd_puts(" ");
_delay_ms(300); //display update
}
}
ISR(INT0_vect)
{
count++;
}
ISR(INT1_vect)
{
count1++;
}
ISR(TIMER1_COMPA_vect)
{
rps=count;rps1=count1;
count=0;count1=0;
}
Which is this sensor of RPM? How it is mounted?
ReplyDeleteThey are both rpm meters! Connected to different inputs, they are both measured simultaneously!
ReplyDeleteSensor is not mounted, they are both fed into controller by two square wave generators...