2 stroke ignition on atmega328


hi all,
i implemented code posted nick gammon of electronic engine ignition.
i'm looking commanding output pin pb1 directly com1a1 , com1a0 config , without using digitalwrite command.

this original code of nick

code: [select]


// timer isr example
// author: nick gammon
// date: 13th january 2012
// modified: 19 october 2014

#include <digitalwritefast.h>

const byte fire_sensor = 2;  // note interrupt 0
const byte sparkplug = 9;

volatile unsigned int sparkdelaytime = 500;   // microseconds
volatile unsigned int sparkontime = 2000;     // microseconds

// allow time taken enter isr (determine empirically)
const unsigned int isrdelayfactor = 4;        // microseconds

// spark on?
volatile boolean sparkon;

void activateinterrupt0 ()
  {
  eicra &= ~(bit(isc00) | bit (isc01));  // clear existing flags
  eicra |=  bit (isc01);    // set wanted flags (falling level interrupt)
  eifr   =  bit (intf0);    // clear flag interrupt 0
  eimsk |=  bit (int0);     // enable it
  }  // end of activateinterrupt0

void deactivateinterrupt0 ()
  {
  eimsk &= ~bit (int0);     // disable it
  }  // end of deactivateinterrupt0

// interrupt when time turn spark on off again
isr (timer1_compa_vect)
  {

  // if on, turn off
  if (sparkon)
    {
    digitalwritefast (sparkplug, low);  // spark off
    tccr1b = 0;                         // stop timer
    timsk1 = 0;                         // cancel timer interrupt
    activateinterrupt0 ();              // re-instate interrupts firing time
    }
  else
    // hold-off time must up
    {
    digitalwritefast (sparkplug, high); // spark on
    tccr1b = 0;                         // stop timer
    tcnt1 = 0;                          // count zero
    tccr1b = bit(wgm12) | bit(cs11);    // ctc, scale clock / 8
    // time before timer fires (zero relative)
    // multiply 2 because on prescaler of 8
    ocr1a = (sparkontime * 2) - (isrdelayfactor * 2) - 1;     
    }
   
  sparkon = !sparkon;                  // toggle

  }  // end of timer1_compa_vect

// isr when fire
isr (int0_vect)
  {
  sparkon = false;                  // make sure flag off in case

  // set timer 1
  tccr1a = 0;                       // normal mode
  tcnt1 = 0;                        // count zero
  tccr1b = bit(wgm12) | bit(cs11);  // ctc, scale clock / 8
  // time before timer fires - 0 relative
  // multiply 2 because on prescaler of 8
  ocr1a = (sparkdelaytime * 2) - (isrdelayfactor * 2) - 1;
  timsk1 = bit (ocie1a);            // interrupt on compare match

  deactivateinterrupt0 ();          // no more interrupts yet
  } // end of isr (int0_vect)

void setup()
  {
  tccr1a = 0;  // normal mode
  tccr1b = 0;  // stop timer
  timsk1 = 0;  // cancel timer interrupt

  pinmode (sparkplug, output);
  pinmode (fire_sensor, input_pullup);
 
  activateinterrupt0 ();
  }  // end of setup

void loop()
  {
  // read sensors, compute time fire spark
 
  if (false)  // if need change time, insert condition here ...
    {
    nointerrupts ();        // atomic change of time amount
    sparkdelaytime = 500;   // delay before spark in microseconds
    sparkontime = 2000;     // spark on time in microseconds
    interrupts ();
    }
  }  // end of loop


i tried differend ways no complete success , don't want post code because understand solution , because don't want waste time did in other forums gave ultra-explained details of code , haven't got solution.

thanks
 :)

i don't think there substitute learning how, in case, timer1 works, , looking @ data sheet atmega328p.
the pin want control, pb1, referred in timer1 documentation oc1a. relevant control register tccr1a.


Arduino Forum > Using Arduino > Programming Questions > 2 stroke ignition on atmega328


arduino

Comments

Popular posts from this blog

DHT11 Time out error using v0.4.1library

Sketch upload fails with Java error (___REMOVE___/bin/avrdude)!

Arduino Uno + KTY81/210 temperature sensor