/**************************************************************************** * * Copyright (c) 2005 Dave Hylands * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * ****************************************************************************/ #include #include #include #include #include #include "a2d.h" #include "Hardware.h" #include "Delay.h" #include "Tach.h" #include "UART.h" #define SAMPLE_SIZE 4 uint8_t gADC[ 8 ]; typedef struct { uint16_t dummyLocZero; uint16_t cyclesPerRev; uint8_t ocr2; } EE_Data_t; EE_Data_t __attribute__((section (".eeprom"))) eeprom = { 0, 1, 255 }; EE_Data_t eeRam; char gLine[ 20 ]; int main(void) { int i; uint32_t prevEndTime = 0; char *spinner = "/-\\|"; int spinnerIdx = 0; InitHardware(); // The first handle opened for read goes to stdin, and the first handle // opened for write goes to stdout. So u0 is stdin, stdout, and stderr #if defined( __AVR_LIBC_VERSION__ ) fdevopen( UART0_PutCharStdio, UART0_GetCharStdio ); #else fdevopen( UART0_PutCharStdio, UART0_GetCharStdio, 0 ); #endif printf( "*****\n" ); printf( "***** Tachometer/Motor Controller program\n" ); printf( "*****\n" ); // Read in the data from EEPROM eeprom_read_block( &eeRam, &eeprom, sizeof( eeprom )); if (( eeRam.cyclesPerRev == 0 ) || ( eeRam.cyclesPerRev == 0xFFFF )) { eeRam.cyclesPerRev = 1; } printf( "Cycles/Rec = %d\n", eeRam.cyclesPerRev ); printf( "ocr2 = %d\n", eeRam.ocr2 ); OCR2 = eeRam.ocr2; while( 1 ) { // Use the Red LED like a heartbeat LED_TOGGLE( RED ); if ( CBUF_Len( TACH_gBuffer ) > SAMPLE_SIZE ) { uint32_t startTime; uint32_t endTime; uint32_t deltaTime; uint32_t rpm; cli(); startTime = CBUF_GetEnd( TACH_gBuffer, SAMPLE_SIZE ); endTime = CBUF_GetEnd( TACH_gBuffer, 0 ); sei(); deltaTime = endTime - startTime; if ( deltaTime > 0 ) { if ( endTime == prevEndTime ) { printf( "%c RPM: no signal\r", spinner[ spinnerIdx ]); } else { rpm = SAMPLE_SIZE * TIMER3_FREQ * 60 / ( deltaTime * eeRam.cyclesPerRev ); printf( "%c RPM: %5ld \r", spinner[ spinnerIdx ], rpm ); } } else { printf( "deltaTime = %ld\n", deltaTime ); } spinnerIdx++; spinnerIdx &= 0x03; prevEndTime = endTime; } #if 0 // Read the ADC for ( i = 0; i < 8; i++ ) { gADC[ i ] = a2d_8( i ); printf( "%02x ", gADC[ i ]); } #endif // Tick rate is 100/sec so waiting for 25 waits for 1/4 sec for ( i = 0; i < 25; i++ ) { WaitForTimer0Rollover(); if ( UART0_IsCharAvailable() ) { char ch = getchar(); if (( ch == 'c' ) || ( ch == 'C' )) { printf( "\nCycles/Rev = %d\n", eeRam.cyclesPerRev ); printf( "Enter new cycles/rev: " ); if ( fgets( gLine, sizeof( gLine ), stdin ) != NULL ) { eeRam.cyclesPerRev = atoi( gLine ); if (( eeRam.cyclesPerRev == 0 ) || ( eeRam.cyclesPerRev == 0xFFFF )) { eeRam.cyclesPerRev = 1; } eeprom_write_word( &eeprom.cyclesPerRev, eeRam.cyclesPerRev ); } } if (( ch == 'o' ) || ( ch == 'O' )) { printf( "\nOCR2 = %d\n", eeRam.ocr2 ); printf( "Enter OCR2: " ); if ( fgets( gLine, sizeof( gLine ), stdin ) != NULL ) { eeRam.ocr2 = atoi( gLine ); OCR2 = eeRam.ocr2; eeprom_write_byte( &eeprom.ocr2, eeRam.ocr2 ); } } } } } return 0; }