Hi,

I am working on some code to read the Parallax Ping sensor from a board
(robostix) with an ATMega128 microcontroller.  Unfortunately, I am getting
some strange tick counts from my functions.  For example, in my Sensor.c
file, I start the counter, then call a function to delay for 5
microseconds, then read the counter value.  I have repeated this process
several times, and I generally see about 350 ticks after my routine --
this converts to about a 20 microsecond delay.  Am I missing anything
pretty obvious in my Timer.h and Timer.c files?

Thanks,
Daniel
/* Copyright (c) 2007 The Information and Telecommunication Technology Center 
 * (ITTC) at the University of Kansas
 * ALL RIGHTS RESERVED
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2 as
 *   published by the Free Software Foundation.
 *
 *   Alternatively, this software may be distributed under the terms of BSD
 *   license.
 *
 *
 * Function definitions for reading data from the Parallax Sensors
 *
 * %Notes
 * 
 * Author: Daniel Fokum, 20070529
 */
 /**
*
*   @file   Sensors.c
*
*   @brief  Function implementations for reading Parallax Sensors.
*
****************************************************************************/

/* ---- Include Files ---------------------------------------------------- */

#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>

#include "Hardware.h"
#include "Timer.h"
#include "Delay.h"
#include "Sensors.h"

/* ---- Public Variables -------------------------------------------------- */

/* ---- Private Constants and Types --------------------------------------- */

/* ---- Private Variables ------------------------------------------------- */

/* ---- Private Function Prototypes --------------------------------------- */

/* ---- Functions --------------------------------------------------------- */


//***************************************************************************
/**
 *  Reads data from the Parallax Ping Sensor
*/
uint8_t ReadPingSensor( uint8_t SensorIdx, uint16_t	Dist[] )
{
	unsigned int ticks=0;
	SENSOR_ON( PING1 );
	_delay_loop_1(26);		//Pause for 5 microseconds
	SENSOR_OFF( PING1 );
	DDRC = DDRC & (~(1<<PING1_SENSOR_PIN));	//Change data direction bit to input
	
	_delay_loop_2(2900);	//Pause for 725 microseconds
	/* Read port pins */
	while ((PINC & PING1_SENSOR_MASK) == 0x00)
		;
	TIM16_StartTCNT1();		//Start timer
	while ((PINC & PING1_SENSOR_MASK) == PING1_SENSOR_MASK)
		;
	TIM16_StopTCNT1();
	ticks = TIM16_ReadTCNT1();
	DDRC = DDRC | (1<<PING1_SENSOR_PIN);	//Change data direction bit to output
	
	// The next 6 lines are for debugging purposes.
	TIM16_StartTCNT1();
	_delay_loop_1(26);
	TIM16_StopTCNT1();
	ticks = TIM16_ReadTCNT1();
	Dist[SensorIdx] = ticks;
	
//	Dist[SensorIdx] = ticks*3/20;
	if (ticks==0)
		return 1;
	else
		return 0;
	/*
		//To return distance in inches do
			return(ticks*3/51);
		//To return distance in tenths of inches do
			return(ticks*3/5);
		//To return distance in centimeters do
			return(ticks*3/20);
		//To return distance in millimeters do
			return(ticks*3/2);	
	*/
}

//***************************************************************************
/**
 *  Reads data from the Parallax PIR (motion) Sensor
*/
uint8_t ReadPIRSensor( uint8_t SensorIdx, uint8_t Motion[] )
{
	//	Data direction bit is set to input in the InitHardware() function.
	LED_ON(YELLOW);
	if ((PINC & PIR1_SENSOR_MASK) == PIR1_SENSOR_MASK)
		Motion[SensorIdx] = 1;		//motion detected
	else
		Motion[SensorIdx] = 0;		//No motion detected
	LED_OFF(YELLOW);
	return 0;
}

/*
    To set bit n in a register to 1, do the following:
	DDRC = DDRC | (1<<n);
    To set bit n in a register to 0, do the following:
	DDRC = DDRC & (~(1<<n));
*/
/* Copyright (c) 2007 The Information and Telecommunication Technology Center 
 * (ITTC) at the University of Kansas
 * ALL RIGHTS RESERVED
 *
 *   This file is built on an original file written by Dave Hylands.
 *   	2006 Dave Hylands     <[EMAIL PROTECTED]>
 *   This version was modified by Daniel Fokum on 20070524
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2 as
 *   published by the Free Software Foundation.
 *
 *   Alternatively, this software may be distributed under the terms of BSD
 *   license.
 *
 *   See README and COPYING for more details.
 *
****************************************************************************/
/**
*
*   @file    Timer.c
*
*   @brief   Routines for dealing with Timer related functions.
*
*****************************************************************************/
/*
*
*   Configurable options
*
*   Option Name         Default Description
*   ------------------  ------- ----------------------------------------------
*   CFG_TIMER_MS_TICK       0   Add a 32 bit millisecond timer
*   CFG_TIMER_MICRO_TICK    1   Add a counter to make the tick occur once every 10 msec
*   CFG_TIMER_8_BIT_TICK    0   Should gTickCount be 8 bit or 16 bit?
*   CFG_TIMER0_INCLUDE          Name of a header file to #include (useful in conjunction with CFG_TIMER0_MS_TICK)
*   CFG_TIMER0_MS_TICK          Code to execute every tick
*
*****************************************************************************/

// ---- Include Files -------------------------------------------------------

#include "Config.h"
#include "Timer.h"

#include <avr/interrupt.h>
#include <avr/io.h>
#if !defined( __AVR_LIBC_VERSION__ )
#include <avr/signal.h>
#endif

// By #defining CFG_TIMER0_INCLUDE to be the name of a header file containing
// a prototype, and CFG_TIMER0_MS_TICK to be some code to execute, you can
// get some code to execute every millisecond.

#if defined( CFG_TIMER0_INCLUDE )
#include    CFG_TIMER0_INCLUDE
#endif

// ---- Public Variables ----------------------------------------------------

volatile tick_t     gTickCount = 0;

#if CFG_TIMER_MS_TICK
volatile msTick_t   gMsTickCount = 0;
#endif

#if !defined( CFG_TIMER_MICRO_TICK )
#   define  CFG_TIMER_MICRO_TICK    1
#endif

// ---- Private Constants and Types -----------------------------------------
// ---- Private Function Prototypes -----------------------------------------
// ---- Functions -----------------------------------------------------------

/***************************************************************************/
/**
*  Timer 0 interrupt handler
*/

SIGNAL(SIG_OVERFLOW0)        /* signal handler for tcnt0 overflow interrupt */
{

    // With CFG_TIMER_MICRO_TICK set to 1, gTickCount will increment once
    // every 10 msec. Otherwise, it will increment every millisecond.

#if CFG_TIMER_MICRO_TICK
    static  uint8_t microTick = 0;
    if ( ++microTick >= 10 )
    {
        microTick = 0;
        gTickCount++;
    }
#else
    gTickCount++;
#endif

#if CFG_TIMER_MS_TICK
    gMsTickCount++;
#endif

    // We want our timer tick to interrupt once a millisecond.
    // If we use 16 MHz/64 then we get 250000 counts/second. So setting
    // things up to overflow after 250 will give us 1000 overflows/second
    //
    // For CFG_CPU_CLOCK = 16 MHz, 16,000,000 / 64 / 1000 = 250
    // For 
    // 256 - 250 = 6

#define OVERFLOW_COUNT  ( CFG_CPU_CLOCK / 1000 / 64 )

    TCNT0 = (uint8_t) -OVERFLOW_COUNT;

#if defined( CFG_TIMER0_MS_TICK )
    CFG_TIMER0_MS_TICK;
#endif

} // Timer 0 Overflow

/***************************************************************************/
/**
*  InitTimer
*/

void InitTimer( void )
{
#if defined( TCCR0B )
    TCCR0B = TIMER0_CLOCK_SEL_DIV_64; // Divide by 64
#else
    TCCR0  = TIMER0_CLOCK_SEL_DIV_64; // Divide by 64
#endif
    TCNT0 = 0;

    // Enable Timer 0 interrupt

#if defined( TIMSK )
    TIMSK = ( 1 << TOIE0 );
#endif
#if defined( TIMSK0 )
    TIMSK0 = ( 1 << TOIE0 );
#endif

    sei();
} // InitTimer

/***************************************************************************/
/**
*  Wait for Timer 0 to rollover
*/

#if !defined( CFG_TIMER_WAITFORTIMER0ROLLOVER )
#define CFG_TIMER_WAITFORTIMER0ROLLOVER 1
#endif

#if CFG_TIMER_WAITFORTIMER0ROLLOVER
void WaitForTimer0Rollover( void )
{
    tick_t prevCount = gTickCount;
    while ( gTickCount == prevCount )
        ;
} // WaitForTimer0Rollover
#endif

//***************************************************************************
/**
    Disables interrupts, and returns the value of the Timer/Counter Register.
    Based on code from the ATmega128 datasheet.
*/
unsigned int TIM16_ReadTCNT1( void )
{
	unsigned char sreg;
	unsigned int i;
	sreg = SREG;		/* Save global interrupt enable */
	cli();			/* Disable interrupts */
	i = TCNT1;	  	/* Read TCNT1 into i */
	SREG = sreg;	  	/* Restore global interrupt enable */
	return i;
} // TIM16_ReadTCNT1

//***************************************************************************
/**
    Disables interrupts, and returns the value of the Timer/Counter Register.
    Based on code from the ATmega128 datasheet.
*/
unsigned int TIM16_ReadTCNT3( void )
{
	unsigned char sreg;
	unsigned int i;
	sreg = SREG;		/* Save global interrupt enable */
	cli();			/* Disable interrupts */
	i = TCNT3;	  	/* Read TCNT3 into i */
	SREG = sreg;	  	/* Restore global interrupt enable */
	return i;
} // TIM16_ReadTCNT3

//***************************************************************************
/**
    Writes a value to the Timer/Counter Register.  Based on code in the
    ATmega128 datasheet.
*/
void TIM16_WriteTCNT1( unsigned int i )
{
	unsigned char sreg;
	sreg = SREG;  	/* Save global interrupt flag */
	cli();			/* Disable interrupts */
	TCNT1 = i;  		/* Set TCNT1 to i */
	SREG = sreg;  	/* Restore global interrupt flag */
} // TIM16_WriteTCNT1

//***************************************************************************
/**
    Writes a value to the Timer/Counter Register.  Based on code in the
    ATmega128 datasheet.
*/
void TIM16_WriteTCNT3( unsigned int i )
{
	unsigned char sreg;
	sreg = SREG;  	/* Save global interrupt flag */
	cli();			/* Disable interrupts */
	TCNT3 = i;  		/* Set TCNTn to i */
	SREG = sreg;  	/* Restore global interrupt flag */
} // TIM16_WriteTCNT3

/* Copyright (c) 2007 The Information and Telecommunication Technology Center 
 * (ITTC) at the University of Kansas
 * ALL RIGHTS RESERVED
 *
 *   This file is built on an original file written by Dave Hylands.
 *   	2006 Dave Hylands     <[EMAIL PROTECTED]>
 *   This version was modified by Daniel Fokum on 20070524
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License version 2 as
 *   published by the Free Software Foundation.
 *
 *   Alternatively, this software may be distributed under the terms of BSD
 *   license.
 *
 *   See README and COPYING for more details.
 *
****************************************************************************/
/**
*
*   @file   Timer.h
*
*   @brief  Defines all of the hardware definitions for the chip.
*
****************************************************************************/

#if !defined( TIMER_H )
#define TIMER_H             /**< Include Guard                             */

#include "Config.h"
#include <avr/io.h>

/* ---- Include Files ---------------------------------------------------- */

/* ---- Constants and Types ---------------------------------------------- */

#define TIMER0_CLOCK_SEL_MASK       (( 1 << CS02 ) | ( 1 << CS01 ) | ( 1 << CS00 ))
#define TIMER1_CLOCK_SEL_MASK       (( 1 << CS12 ) | ( 1 << CS11 ) | ( 1 << CS10 ))
#define TIMER2_CLOCK_SEL_MASK       (( 1 << CS22 ) | ( 1 << CS21 ) | ( 1 << CS20 ))
#define TIMER3_CLOCK_SEL_MASK       (( 1 << CS32 ) | ( 1 << CS31 ) | ( 1 << CS30 ))

#define TIMER1_CLOCK_SEL_NONE       (( 0 << CS12 ) | ( 0 << CS11 ) | ( 0 << CS10 ))
#define TIMER1_CLOCK_SEL_DIV_1      (( 0 << CS12 ) | ( 0 << CS11 ) | ( 1 << CS10 ))
#define TIMER1_CLOCK_SEL_DIV_8      (( 0 << CS12 ) | ( 1 << CS11 ) | ( 0 << CS10 ))
#define TIMER1_CLOCK_SEL_DIV_64     (( 0 << CS12 ) | ( 1 << CS11 ) | ( 1 << CS10 ))
#define TIMER1_CLOCK_SEL_DIV_256    (( 1 << CS12 ) | ( 0 << CS11 ) | ( 0 << CS10 ))
#define TIMER1_CLOCK_SEL_DIV_1024   (( 1 << CS12 ) | ( 0 << CS11 ) | ( 1 << CS10 ))
#define TIMER1_CLOCK_SEL_T1_FALLING (( 1 << CS12 ) | ( 1 << CS11 ) | ( 0 << CS10 ))
#define TIMER1_CLOCK_SEL_T1_RISING  (( 1 << CS12 ) | ( 1 << CS11 ) | ( 1 << CS10 ))

#if defined (__AVR_ATmega8__) \
 || defined (__AVR_ATmega16__) \
 || defined (__AVR_ATmega32__) \
 || defined (__AVR_ATmega48__) \
 || defined (__AVR_ATmega88__) \
 || defined (__AVR_ATmega168__) \

#define TIMER0_CLOCK_SEL_NONE       (( 0 << CS02 ) | ( 0 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_1      (( 0 << CS02 ) | ( 0 << CS01 ) | ( 1 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_8      (( 0 << CS02 ) | ( 1 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_64     (( 0 << CS02 ) | ( 1 << CS01 ) | ( 1 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_256    (( 1 << CS02 ) | ( 0 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_1024   (( 1 << CS02 ) | ( 0 << CS01 ) | ( 1 << CS00 ))
#define TIMER0_CLOCK_SEL_T0_FALLING (( 1 << CS02 ) | ( 1 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_T0_RISING  (( 1 << CS02 ) | ( 1 << CS01 ) | ( 1 << CS00 ))

#elif defined (__AVR_ATmega644__)

#define TIMER0_CLOCK_SEL_NONE       (( 0 << CS02 ) | ( 0 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_1      (( 0 << CS02 ) | ( 0 << CS01 ) | ( 1 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_8      (( 0 << CS02 ) | ( 1 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_64     (( 0 << CS02 ) | ( 1 << CS01 ) | ( 1 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_256    (( 1 << CS02 ) | ( 0 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_1024   (( 1 << CS02 ) | ( 0 << CS01 ) | ( 1 << CS00 ))
#define TIMER0_CLOCK_SEL_T0_FALLING (( 1 << CS02 ) | ( 1 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_T0_RISING  (( 1 << CS02 ) | ( 1 << CS01 ) | ( 1 << CS00 ))

#define TIMER2_CLOCK_SEL_NONE       (( 0 << CS22 ) | ( 0 << CS21 ) | ( 0 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_1      (( 0 << CS22 ) | ( 0 << CS21 ) | ( 1 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_8      (( 0 << CS22 ) | ( 1 << CS21 ) | ( 0 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_32     (( 0 << CS22 ) | ( 1 << CS21 ) | ( 1 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_64     (( 1 << CS22 ) | ( 0 << CS21 ) | ( 0 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_128    (( 1 << CS22 ) | ( 0 << CS21 ) | ( 1 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_256    (( 1 << CS22 ) | ( 1 << CS21 ) | ( 0 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_1024   (( 1 << CS22 ) | ( 1 << CS21 ) | ( 1 << CS20 ))

#elif defined (__AVR_ATmega64__) \
   || defined (__AVR_ATmega128__)

#define TIMER0_CLOCK_SEL_NONE       (( 0 << CS02 ) | ( 0 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_1      (( 0 << CS02 ) | ( 0 << CS01 ) | ( 1 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_8      (( 0 << CS02 ) | ( 1 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_32     (( 0 << CS02 ) | ( 1 << CS01 ) | ( 1 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_64     (( 1 << CS02 ) | ( 0 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_128    (( 1 << CS02 ) | ( 0 << CS01 ) | ( 1 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_256    (( 1 << CS02 ) | ( 1 << CS01 ) | ( 0 << CS00 ))
#define TIMER0_CLOCK_SEL_DIV_1024   (( 1 << CS02 ) | ( 1 << CS01 ) | ( 1 << CS00 ))

#define TIMER2_CLOCK_SEL_NONE       (( 0 << CS22 ) | ( 0 << CS21 ) | ( 0 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_1      (( 0 << CS22 ) | ( 0 << CS21 ) | ( 1 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_8      (( 0 << CS22 ) | ( 1 << CS21 ) | ( 0 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_64     (( 0 << CS22 ) | ( 1 << CS21 ) | ( 1 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_256    (( 1 << CS22 ) | ( 0 << CS21 ) | ( 0 << CS20 ))
#define TIMER2_CLOCK_SEL_DIV_1024   (( 1 << CS22 ) | ( 0 << CS21 ) | ( 1 << CS20 ))
#define TIMER2_CLOCK_SEL_T2_FALLING (( 1 << CS22 ) | ( 1 << CS21 ) | ( 0 << CS20 ))
#define TIMER2_CLOCK_SEL_T2_RISING  (( 1 << CS22 ) | ( 1 << CS21 ) | ( 1 << CS20 ))

#define TIMER3_CLOCK_SEL_NONE       (( 0 << CS32 ) | ( 0 << CS31 ) | ( 0 << CS30 ))
#define TIMER3_CLOCK_SEL_DIV_1      (( 0 << CS32 ) | ( 0 << CS31 ) | ( 1 << CS30 ))
#define TIMER3_CLOCK_SEL_DIV_8      (( 0 << CS32 ) | ( 1 << CS31 ) | ( 0 << CS30 ))
#define TIMER3_CLOCK_SEL_DIV_64     (( 0 << CS32 ) | ( 1 << CS31 ) | ( 1 << CS30 ))
#define TIMER3_CLOCK_SEL_DIV_256    (( 1 << CS32 ) | ( 0 << CS31 ) | ( 0 << CS30 ))
#define TIMER3_CLOCK_SEL_DIV_1024   (( 1 << CS32 ) | ( 0 << CS31 ) | ( 1 << CS30 ))
#define TIMER3_CLOCK_SEL_T3_FALLING (( 1 << CS32 ) | ( 1 << CS31 ) | ( 0 << CS30 ))
#define TIMER3_CLOCK_SEL_T3_RISING  (( 1 << CS32 ) | ( 1 << CS31 ) | ( 1 << CS30 ))

#define TIMER_1A_PIN     5
#define TIMER_1A_MASK    ( 1 << 5 )
#define TIMER_1A_PORT    PORTB
#define TIMER_1A_DDR     DDRB
#define TIMER_1A_OCR     OCR1A
#define TIMER_1A_COM_1   COM1A1
#define TIMER_1A_COM0    COM1A0
#define TIMER_1A_TCCRA   TCCR1A

#define TIMER_1B_PIN     6
#define TIMER_1B_MASK    ( 1 << 6 )
#define TIMER_1B_PORT    PORTB
#define TIMER_1B_DDR     DDRB
#define TIMER_1B_OCR     OCR1B
#define TIMER_1B_COM_1   COM1B1
#define TIMER_1B_COM0    COM1B0
#define TIMER_1B_TCCRA   TCCR1A

#define TIMER_1C_PIN     7
#define TIMER_1C_MASK    ( 1 << 7 )
#define TIMER_1C_PORT    PORTB
#define TIMER_1C_DDR     DDRB
#define TIMER_1C_OCR     OCR1C
#define TIMER_1C_COM_1   COM1C1
#define TIMER_1C_COM0    COM1C0
#define TIMER_1C_TCCRA   TCCR1A

#define TIMER_3A_PIN     3
#define TIMER_3A_MASK    ( 1 << 3 )
#define TIMER_3A_PORT    PORTE
#define TIMER_3A_DDR     DDRE
#define TIMER_3A_OCR     OCR3A
#define TIMER_3A_COM_1   COM3A1
#define TIMER_3A_COM0    COM3A0
#define TIMER_3A_TCCRA   TCCR3A

#define TIMER_3B_PIN     4
#define TIMER_3B_MASK    ( 1 << 4 )
#define TIMER_3B_PORT    PORTE
#define TIMER_3B_DDR     DDRE
#define TIMER_3B_OCR     OCR3B
#define TIMER_3B_COM_1   COM3B1
#define TIMER_3B_COM0    COM3B0
#define TIMER_3B_TCCRA   TCCR3A

#define TIMER_3C_PIN     5
#define TIMER_3C_MASK    ( 1 << 5 )
#define TIMER_3C_PORT    PORTE
#define TIMER_3C_DDR     DDRE
#define TIMER_3C_OCR     OCR3C
#define TIMER_3C_COM_1   COM3C1
#define TIMER_3C_COM0    COM3C0
#define TIMER_3C_TCCRA   TCCR3A

#else
#   error   Common/avr/Timer.h Processor not supported
#endif

/* ---- Variable Externs ------------------------------------------------- */

#if CFG_TIMER_8_BIT_TICK
typedef uint8_t     tick_t;     // Counts once per 10 msec
#else
typedef uint16_t    tick_t;     // Counts once per 10 msec
#endif

extern volatile tick_t      gTickCount;

#if CFG_TIMER_MS_TICK

typedef uint32_t    msTick_t;   // Counts once per millisecond

extern volatile msTick_t    gMsTickCount;

#endif

/* ---- Function Prototypes ---------------------------------------------- */

void InitTimer( void );
void WaitForTimer0Rollover( void );
static inline void TIM16_StartTCNT1( void ) __attribute__((always_inline));
static inline void TIM16_StartTCNT3( void ) __attribute__((always_inline));
static inline void TIM16_StopTCNT1( void ) __attribute__((always_inline));
static inline void TIM16_StopTCNT3( void ) __attribute__((always_inline));
unsigned int TIM16_ReadTCNT1( void );
unsigned int TIM16_ReadTCNT3( void );
void TIM16_WriteTCNT1( unsigned int i );
void TIM16_WriteTCNT3( unsigned int i );

// ---- Functions -----------------------------------------------------------

//***************************************************************************
/**
    Stops the Timer/Counter Register.
*/
void TIM16_StopTCNT1( void )
{
	TCCR1B = TIMER1_CLOCK_SEL_NONE;	
	TIMSK&=~(1<<TOIE1);  // stop interrupts	
}

//***************************************************************************
/**
    Stops the Timer/Counter Register.
*/
void TIM16_StopTCNT3( void )
{
	TCCR3B = TIMER3_CLOCK_SEL_NONE;
}

//***************************************************************************
/**
    Starts the Timer/Counter Register.
*/
void TIM16_StartTCNT1( void )
{
	TCNT1 = 0;
	TCCR1B = TIMER1_CLOCK_SEL_DIV_1;
	TIMSK = ( 1 << TOIE1 );		//Enable Timer1 interrupts
}

//***************************************************************************
/**
    Starts the Timer/Counter Register.
*/
void TIM16_StartTCNT3( void )
{
	TCNT3 = 0;
	TCCR3B = TIMER3_CLOCK_SEL_DIV_1;
}

#endif  // TIMER_H
/* Copyright (c) 2007 The Information and Telecommunication Technology Center 
 * (ITTC) at the University of Kansas
 * ALL RIGHTS RESERVED
 *
 * Function definitions for reading data from the Parallax Sensors
 *
 *
 * %Notes
 * 
 * Author: Daniel Fokum, 20070529
 */
 /**
*
*   @file   Sensors.h
*
*   @brief  Function prototypes for reading Parallax Sensors.
*
****************************************************************************/

#if !defined( SENSORS_H )
#define SENSORS_H         /**< Include Guard                             */

/* ---- Include Files ---------------------------------------------------- */

#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>

#include "Hardware.h"
#include "Timer.h"
#include "Delay.h"
//#include "UART.h"

//#define F_CPU 16000000UL  // 16 MHz

/* ---- Constants and Types ---------------------------------------------- */

/* ---- Variable Externs ------------------------------------------------- */

/* ---- Function Prototypes ---------------------------------------------- */

uint8_t ReadPingSensor( uint8_t SensorIdx, uint16_t	Dist[] );
uint8_t ReadPIRSensor( uint8_t SensorIdx, uint8_t Motion[] );

#endif /* SENSORS_H */
_______________________________________________
AVR-GCC-list mailing list
[email protected]
http://lists.nongnu.org/mailman/listinfo/avr-gcc-list

Reply via email to