Atmega8 ICP1 Help?

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all, been lurking for a while now (last year as SW Engineer major) and read over data sheet alot but a bit stuck.

I have successfully fused my atMEGA8-16pu with a 16MHz crystal and caps. And can do basic blinking leds however stuck with interrupts. 
This code will be the back bone structure of a crank trigger ignition, but for now using a LED in place for development.
Not sure why it is not working?
Code below
 

#define F_CPU 16000000UL //-- External Crystal <- PB6 (XTAL1), PB7 (XTAL2) 22pF Caps, Fuses <- L0xFF H0xC9

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

volatile uint16_t Tooth = 0; // Counter of the Crank Trigger
volatile uint16_t lastEdge = 0; // Last Captured Falling Edge Time
volatile uint16_t newEdge = 0; // Current Falling Edge Time Captured
volatile uint16_t widthEdge = 0; // Edge Pulse Width (comparing last and new)

#define Trigger PB0; // Hall Effect Sensor to ICP1 (PB0)
#define LED1 PC0; // Fires Coil Driver Circuit (Just an LED + Resistor for now)

int main(void)
{
	DDRC = 0x01; // Set Output (LED1 PC0)
	DDRB = 0x00; // Set Input (Trigger PB0 (ICP1) 
	TIMSK = 0x00; // Enable Input Capture Interrupt
	TIFR = 0b00100000; // ICF1 Input Capture Flag <- Auto clears after exe
	TCCR1B = 0b00011100; // Falling Edge Capture BIT6, WGM12(CTC1) BIT3, CS12(/256) BIT2
			
	sei(); // Allow Interrupts
	
	while (1)
	{
		PINB = 0x01; // Enable Trigger Wheel
		PINC = 0x01; // Enable LED1	
			
		if(Tooth == 11)  // Count 9 Hall effect pulses
		{	
			PINC = 0x01; // Turn on LED		
				_delay_ms(2500); // Delay for visual Inspection		
					PINC = 0x00; // Turn off LED
		}
		else if(Tooth >= 36) // Max number of trigger wheel 36-2						
		{
			Tooth = 0; // Reset Trigger - Wait to Input Capture Tooth=2
		}		
	}
}

ISR(TIMER1_CAPT_vect)
{	
	newEdge = ICR1; // Capture the new falling edge
	widthEdge = newEdge - lastEdge;	// Compare the time between edges
	lastEdge = newEdge; // set the edge to lastEdge	
	if (widthEdge > (lastEdge * 2))
	{		
		Tooth=2; // Reset for TDC
	}
	else
	{
		Tooth++; // Just increase counter for now
	}
}

Thanks in advance if you do figure where I am lacking

ATmega8 Noob

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Which interrupts did you enable?

 

TIMSK = 0x00; // Enable Input Capture Interrupt

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
	TIMSK = 0b00100000; // Enable Input Capture Interrupt TICIE1 BIT5

 

Sorry It should be bit 5 TICIE1 enabled correct?

ATmega8 Noob

Last Edited: Sun. Jun 28, 2020 - 01:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The test environment is at you. please try it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Will have to try in the morning, cheers for help though mate.

ATmega8 Noob

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try to avoid magic numbers.  Hard to decipher when reading code, and error-prone when writing code.

 

Instead of this:

TIFR = 0b00100000; // ICF1 Input Capture Flag <- Auto clears after exe

... this:

TIFR = (1 << ICF1); // Auto clears after exe

Note that it's also self-documenting.

 

Similarly:

    TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS12); // Falling Edge Capture BIT6, WGM12(CTC1) BIT3, CS12(/256) BIT2

 

I tend to prefer:

  const uint8_t com1a = 0;  // No OCA
  const uint8_t com1b = 0;  // No OCB
  const uint8_t wgm1  = 12; // CTC /w TOP=ICR1
  const uint8_t cs1   = 4;  // /256

  TCCR1A = (com1a << COM1A0) | (com1b << COM1B0) | ((wgm1 & 0x3) << WGM10);
  TCCR1B = ((wgm1 >> 2) << WGM12) | (cs1 << CS10);

With a bit of constraint:

  TCCR1A = ((com1a & 0x3) << COM1A0) | ((com1b & 0x3) << COM1B0) | ((wgm1 & 0x3) << WGM10);
  TCCR1B = (((wgm1 >> 2) & 0x3) << WGM12) | ((cs1 & 0x7) << CS10);

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I simulated your code.
Waveform Generation Mode must select Normal.

TCCR1B = 0b00011100;

 

And of course you need to enable TICIE1.

TIMSK = 0b00100000; // Enable Input Capture Interrupt TICIE1 BIT5

 

In addition, writing to the input pin does not toggle the output. This feature is implemented in newer chips.

PINB = 0x01; // Enable Trigger Wheel
PINC = 0x01; // Enable LED1