Rising edge counter using Timer 3

I am running a program that will count the amount of ticks (Timer 3) during period of a square wave.

I have two Orangutan SVP 1284p’s.

The code works on one Orangutan SVP 1284p but, does not work the other.

I spoke with a Pololu tech about the problem he confirmed that both USB PORTs had the same firmware.

Any clue why it works on one but not the other. I am using the PB5 (MOSI) as the input.

The code is below:

// F_CPU tells util/delay.h our clock frequency
#define F_CPU 20000000UL // Clock frequency (20MHz)
#include <avr/io.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include <util/delay.h>

//Global Varibles holding counts
unsigned int old = 0;
unsigned int next = 0;
unsigned int period = 0;

void delayms( uint16_t millis ) {
 while ( millis ) {
  _delay_ms( 1 );
  millis--;
 }
}

ISR(TIMER3_CAPT_vect)
{
 next = ICR3;

 period = next - old;

 old = next;

 PORTD &= ~( 1 << PORTD1 );  // LED on (driving PD1 low on Orangutan SVP turns it on)
 delayms( 200 );             // delay 100 ms
 PORTD |= 1 << PORTD1;       // LED off
 delayms( 2000 );
 
}

int main( void ) {
 
 TCCR3B |= 1 << CS31 | 1 << CS30 ;  // Timer3 set to use /64 prescaler

 PINB |= 1 << DDB5; // set PINB5 to input
 
 TCCR3B |= 1 <<ICES3; // Look for a rising edge

 DDRD |= 1 << DDD1;

 TIMSK3 |= 1 << ICIE3; // Enable input capture interrupts

 sei();
 
 while ( 1 ) {
              
 }
 return 0;

}

Hello.

I spoke to you on the phone about this.

One thing that occurs to me is that the Orangutan SVP-324 does NOT have Timer 3. Is it possible one of your boards is actually an SVP-324 with an ATmega324PA instead of ATmega1284P? I would look on the bottom of the board to see which box is checked, and try to look at the AVR itself to see if I can read the label, and also read the signature of the device, which can be done in AVR Studio or AVRDUDE.

You’re programming both boards using the exact same AVR Studio project, right? If not, differences in your project configuration might cause problems.

Could you try reading the AVR’s contents as a HEX file from both boards and making sure they are the same? If you don’t have a file comparison tool handy you can just post the two files here.

What is connected to these Orangutan SVPs? How are you generating the signal on pin 3?

I noticed that this line is probably not correct:

PINB |= 1 << DDB5; // set PINB5 to input

I think it should be:

DDRB &= ~(1 << DDB5); // set PINB5 to input

–David

I have confirmed by inspection and by signature that both boards are SVP-1284. Both boards are programmed in the same project. I have connected a function generator that is generating a square wave with a 2ms period. The signal is on PB5 and they (Micro and Generator) share a common ground.

I’ve attached the Hex files.

- Chris P.

TheMach4.hex (360 KB)
TheMach8.hex (360 KB)

I compared your files using “diff” and they are the same.

How are you powering the SVPs? Did you attach the jumper that lets you power it from USB or do you have something connected to GND and VIN?

There are three things to check on the SVP that you’re having trouble with to narrow down the problem:

  1. Is your code actually running? At the very beginning of the program, I would add some code to blink the green LED a few times to make sure.

  2. Does the red LED actually work? I would write simple program that just blinks the red LED (e.g. BlinkLED) and see if that works.

  3. Does the PB5 pin actually work as an input? I would write a simple program that just reads PB5 and sets the red LED to be on or off.

PB5 is MOSI, which is connected to the auxiliary processor.

I’d be interested in seeing some photos of your setup so I can check that your wire actually goes to PB5 (which is MOSI on the ISP header) and also check for anything else unusual.

–David