/* rpm measurements using the Orangutan Atmega8 version, last update 6/4/2007 FOSC=8MHz sjames_remington at yahoo dot com */ #include #include #include #include #define F_CPU 8000000UL /* * Function prototypes * Use home-rolled delays in main */ void Delay_us ( unsigned int microseconds ); void Delay_ms ( unsigned int milliseconds ); void LCDInit(void); void LCDSendData(unsigned char data); void LCDSendCommand(unsigned char command); void LCDPrintString(const unsigned char *string); void LCDGotoXY(unsigned char x,unsigned char y); extern int lcd_putchar(char c, FILE *stream); // globals volatile unsigned long ticks; volatile int secs; FILE lcd_str = FDEV_SETUP_STREAM(lcd_putchar, NULL, _FDEV_SETUP_WRITE); #include "lcd.c" int lcd_putchar(char c, FILE *stream) { /* * Send character c to the LCD for use with printf */ LCDSendData(c); return 0; } /* * Timer0 overflow interrupt handler. Counts system clock/8/250 -- provides global ticks at 4 kHz */ ISR(TIMER0_OVF_vect) { static unsigned int pcount = 4000; //4000 ticks per second TCNT0 = 6; ticks++; //reload counter; 250 to overflow if (pcount-- == 0) { //decrement tick counter and update runtime seconds secs++; pcount=4000; } } void timer_init(void) { // set up Timer0 to provide 0.25 ms ticks TCCR0 = (1<>2); //print period in milliseconds } //end while(1) //end of main } /* * Delay_us * * wait in a loop for the specified number of microseconds. * */ void Delay_us( unsigned int microseconds ) { register unsigned int loop_count; /* 8mhz clock, 4 instructions per loop_count */ loop_count = microseconds<<1; __asm__ volatile ( "1: sbiw %0,1" "\n\t" "brne 1b" : "=w" ( loop_count ) : "0" ( loop_count ) ); } /* Delay_ms * * wait in a loop for the specified number of milliseconds. * */ void Delay_ms( unsigned int milliseconds ) { uint16_t i; for ( i = 0; i < milliseconds; ++i ) { Delay_us( 1000 ); } }