element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Ben Heck Featured Content
  • Challenges & Projects
  • element14 presents
  • element14's The Ben Heck Show
  • Ben Heck Featured Content
  • More
  • Cancel
Ben Heck Featured Content
Forum Engine Management
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Ben Heck Featured Content to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 63 replies
  • Subscribers 46 subscribers
  • Views 4146 views
  • Users 0 members are here
Related

Engine Management

jack.chaney56
jack.chaney56 over 9 years ago

Hi Ben,

I am a programmer with a very small amount of skill with circuits, and am looking to create a platform for an engine management system, using an Arduino Mega 2560. I had done a bit of the coding, when I ran into some timing issues with the built in Arduino manager, so I switched over and started using AVR Studio and a programmer to go directly to the chip itself.  The code looks like it should work ok, but now I need some additional circuits to handle the energy levels of coils and injectors (Something like IGBTs). Sensors are being run through simple dividers (no protection yet), and cam and crank inputs are through a simple comparitor

 

Let me know what you think,

Jack

  • Sign in to reply
  • Cancel

Top Replies

  • jack.chaney56
    jack.chaney56 over 7 years ago +2
    Back again... After a bit of time away seeking enlightenment (and a steady paycheck), I am ready to get back to work on my project. I have continued to play around with the code and a number of components…
  • jack.chaney56
    jack.chaney56 over 7 years ago +2
    I want to start this thing right, so the shopping list for people that want to play along at home: Raspberry Pi - version is not significant if you don't mind a slow response when using Eclipse, but 3B…
  • jack.chaney56
    jack.chaney56 over 7 years ago +2
    Start off with two things. First, I forgot (neglected) to provide instruction on how to get the compiled code onto the Nano. Fault of familiarity; having done the process so many times, I had shifted to…
Parents
  • jack.chaney56
    jack.chaney56 over 7 years ago

    Presenting two tricks.

     

    I hesitate to call them tricks, because someone once said they weren't interested in tricks that nobody else would know about. The truthful statement is a derived technique. The result of years of experience, and effort, providing a knowledge to distill a procedure to its most effective form. A clever trick. The first is from necessity, because to have both ignition and fueling, two pair of timer compare registers were needed. However, in the 328, only one timer set is 16 bit, the other set is only 8 bits. To compensate, the 8 bit timer is slowed so 250 tics equals 1mS.

     

    The difference between the two is the 16 bit timer runs 8 times faster than the 8 bit timer. Important information to note. All the equations for time will remain the same, and the only change is, for slow timers, the elapseTime and eventTime get shifted right by 3 (divide by 8). Part one solved, now there could be a problem with the limit of just a little over 1mS for the compare register. The problem isn't there with the 16 bit register which is big enough for about 32mS, meaning another technique would be necessary for extended times, and anything greater than 1mS.  I elected to just use the overflow interrupt to manage the countdown, and activate the timer compare when the overflow was a match to the upper portion of the effective time value. Once again using the tach value, but changing to use timer 2 compare registers. For the operation, everything stays the same in the scheduler portion. The change is in the ignTkHi and ignTkLo functions. Instead of checking to see if there is a long delay, it just uses the extended time value. The only math is to do the right shift three bits. There is also an active bit, otherwise it would require disabling the overflow counter which would put it out of sync with the tooth timer.

     

    I said I was going to provide full listings in the next installment, but I want to do a bunch of clean up first, so it will have to wait a bit longer.  The code for the slow timer goes like this. First change the ignTkHi and ignTkLo

    /**************************************************************************************************
    Generated TACH signal
    **************************************************************************************************/
    void ignTkHi(SLong et, SLong bt) {
        SLong ev = (et + bt + 4) >> 3;
    
        etTkHi = (UByte)(ev & 0xff);
        dlyTkHi = ev & 0x07ffff00;
        tkHi = 1;
    }
    void ignTkLo(SLong et, SLong bt) {
        SLong ev = (et + bt + 4) >> 3;
    
        etTkLo = (UByte)(ev & 0xff);
        dlyTkLo = ev & 0x07ffff00;
        tkLo = 1;
    }

     

    Then the timer overflow and compare

    ISR(TIMER2_COMPA_vect) { TIMSK2 &= ~(1 << OCIE2A); setTach(true); }
    ISR(TIMER2_COMPB_vect) { TIMSK2 &= ~(1 << OCIE2B); setTach(false); }
    ISR(TIMER2_OVF_vect) {
        ov2Tic = (ov2Tic +   256L) & 0x07ffff00;
        if ((ov2Tic == dlyTkHi) && (tkHi == 1)) { /* timer match and flag on */
          tkHi = 0;                               /* condition met, so turn off flag */
          if (etTkHi > 0) {                       /* special condition if low order portion is 0 */
            OCR2A = etTkHi; TIMSK2 |= (1 << OCIE2A); /* everything regular, so just like the 16 bit */
          } else {                                /* if short portion is 0 the event is now */
            setTach(true);                        /* tach on */
          }
        }
        if ((ov2Tic == dlyTkLo) && (tkLo == 1)) { /* timer match and flag on */
          tkLo = 0;                               /* condition met, so turn off flag */
          if (etTkLo > 0) {                       /* everything else the same */
            OCR2B = etTkHi; TIMSK2 |= (1 << OCIE2B);
          } else {
            setTach(false);                      /* tach on */
          }
        }
    }

     

    Really, I will provide full source, but the day is getting long, and I wanted to send out the tricks.

     

    The other trick is with hardware. Because the 328 is limited in the number of output lines available, I used a 74ls138 (3 to 8 decoder), tied to the Nano D4-D6 pins, then tied D7 to the enable pin to act as a switch. I routed the output line to an inverter (74ls04), then to the gate of a quad latch (74ls75). There is enough inverters in one chip to enable 6 of the quad latches. I then tied D8-D11 to the inputs of the latches. With a bit of simple code to drive the whole thing, 8 output lines becomes 24. Which is enough for 8 coils, 8 injectors, and a handful of idiot lights. All for pocket change.  The code for the latching operation is:

    /***************************************************************************************************
    
     History: [JAC] Original creation when the earth was cooling
    ***************************************************************************************************/
    #include "types.h"
    
    #define MAX_LATCH            32
    
    static const UByte lchMask[] = {
         0x01,0x02,0x04,0x08,0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x31,0x32,0x34,0x38
        ,0x41,0x42,0x44,0x48,0x51,0x52,0x54,0x58,0x61,0x62,0x64,0x68,0x71,0x72,0x74,0x78
    };
    static UByte lch[8];
    void initLatches(void) {
        UByte c;
        DDRB &= 0xf0; DDRD &= 0x0f;
        for (c = 0; c < 8; c++) { lch[c] = 0; PORTB = 0; PORTD = c; PORTD = (PIND | 0x80); }
    }
    void setLatch(UByte n, bool t) {
        UByte a, b, c;
    
        if (n < MAX_LATCH) {
            b = lchMask[n] & 0x0f; a = lchMask[n] & 0x70; c = n / 4;
            lch[c] = t ? (lch[c] | b) : (lch[c] & ~b); PORTB = lch[c];
            PORTD = (PIND & 0x0f) | a; PORTD = (PIND | 0x80);
        }
    }
    bool isLatchOn(UByte n) {
        return (n < MAX_LATCH ? ((lch[n>>2] & (lchMask[n] & 0x0f)) != 0) : false);
    }
    /* end of file */

    Thats it for now

    Jack

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • jack.chaney56
    jack.chaney56 over 7 years ago

    Presenting two tricks.

     

    I hesitate to call them tricks, because someone once said they weren't interested in tricks that nobody else would know about. The truthful statement is a derived technique. The result of years of experience, and effort, providing a knowledge to distill a procedure to its most effective form. A clever trick. The first is from necessity, because to have both ignition and fueling, two pair of timer compare registers were needed. However, in the 328, only one timer set is 16 bit, the other set is only 8 bits. To compensate, the 8 bit timer is slowed so 250 tics equals 1mS.

     

    The difference between the two is the 16 bit timer runs 8 times faster than the 8 bit timer. Important information to note. All the equations for time will remain the same, and the only change is, for slow timers, the elapseTime and eventTime get shifted right by 3 (divide by 8). Part one solved, now there could be a problem with the limit of just a little over 1mS for the compare register. The problem isn't there with the 16 bit register which is big enough for about 32mS, meaning another technique would be necessary for extended times, and anything greater than 1mS.  I elected to just use the overflow interrupt to manage the countdown, and activate the timer compare when the overflow was a match to the upper portion of the effective time value. Once again using the tach value, but changing to use timer 2 compare registers. For the operation, everything stays the same in the scheduler portion. The change is in the ignTkHi and ignTkLo functions. Instead of checking to see if there is a long delay, it just uses the extended time value. The only math is to do the right shift three bits. There is also an active bit, otherwise it would require disabling the overflow counter which would put it out of sync with the tooth timer.

     

    I said I was going to provide full listings in the next installment, but I want to do a bunch of clean up first, so it will have to wait a bit longer.  The code for the slow timer goes like this. First change the ignTkHi and ignTkLo

    /**************************************************************************************************
    Generated TACH signal
    **************************************************************************************************/
    void ignTkHi(SLong et, SLong bt) {
        SLong ev = (et + bt + 4) >> 3;
    
        etTkHi = (UByte)(ev & 0xff);
        dlyTkHi = ev & 0x07ffff00;
        tkHi = 1;
    }
    void ignTkLo(SLong et, SLong bt) {
        SLong ev = (et + bt + 4) >> 3;
    
        etTkLo = (UByte)(ev & 0xff);
        dlyTkLo = ev & 0x07ffff00;
        tkLo = 1;
    }

     

    Then the timer overflow and compare

    ISR(TIMER2_COMPA_vect) { TIMSK2 &= ~(1 << OCIE2A); setTach(true); }
    ISR(TIMER2_COMPB_vect) { TIMSK2 &= ~(1 << OCIE2B); setTach(false); }
    ISR(TIMER2_OVF_vect) {
        ov2Tic = (ov2Tic +   256L) & 0x07ffff00;
        if ((ov2Tic == dlyTkHi) && (tkHi == 1)) { /* timer match and flag on */
          tkHi = 0;                               /* condition met, so turn off flag */
          if (etTkHi > 0) {                       /* special condition if low order portion is 0 */
            OCR2A = etTkHi; TIMSK2 |= (1 << OCIE2A); /* everything regular, so just like the 16 bit */
          } else {                                /* if short portion is 0 the event is now */
            setTach(true);                        /* tach on */
          }
        }
        if ((ov2Tic == dlyTkLo) && (tkLo == 1)) { /* timer match and flag on */
          tkLo = 0;                               /* condition met, so turn off flag */
          if (etTkLo > 0) {                       /* everything else the same */
            OCR2B = etTkHi; TIMSK2 |= (1 << OCIE2B);
          } else {
            setTach(false);                      /* tach on */
          }
        }
    }

     

    Really, I will provide full source, but the day is getting long, and I wanted to send out the tricks.

     

    The other trick is with hardware. Because the 328 is limited in the number of output lines available, I used a 74ls138 (3 to 8 decoder), tied to the Nano D4-D6 pins, then tied D7 to the enable pin to act as a switch. I routed the output line to an inverter (74ls04), then to the gate of a quad latch (74ls75). There is enough inverters in one chip to enable 6 of the quad latches. I then tied D8-D11 to the inputs of the latches. With a bit of simple code to drive the whole thing, 8 output lines becomes 24. Which is enough for 8 coils, 8 injectors, and a handful of idiot lights. All for pocket change.  The code for the latching operation is:

    /***************************************************************************************************
    
     History: [JAC] Original creation when the earth was cooling
    ***************************************************************************************************/
    #include "types.h"
    
    #define MAX_LATCH            32
    
    static const UByte lchMask[] = {
         0x01,0x02,0x04,0x08,0x11,0x12,0x14,0x18,0x21,0x22,0x24,0x28,0x31,0x32,0x34,0x38
        ,0x41,0x42,0x44,0x48,0x51,0x52,0x54,0x58,0x61,0x62,0x64,0x68,0x71,0x72,0x74,0x78
    };
    static UByte lch[8];
    void initLatches(void) {
        UByte c;
        DDRB &= 0xf0; DDRD &= 0x0f;
        for (c = 0; c < 8; c++) { lch[c] = 0; PORTB = 0; PORTD = c; PORTD = (PIND | 0x80); }
    }
    void setLatch(UByte n, bool t) {
        UByte a, b, c;
    
        if (n < MAX_LATCH) {
            b = lchMask[n] & 0x0f; a = lchMask[n] & 0x70; c = n / 4;
            lch[c] = t ? (lch[c] | b) : (lch[c] & ~b); PORTB = lch[c];
            PORTD = (PIND & 0x0f) | a; PORTD = (PIND | 0x80);
        }
    }
    bool isLatchOn(UByte n) {
        return (n < MAX_LATCH ? ((lch[n>>2] & (lchMask[n] & 0x0f)) != 0) : false);
    }
    /* end of file */

    Thats it for now

    Jack

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube