Running a microcontroller in car - power supply aspects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Curious.George
    Badcaps Legend
    • Nov 2011
    • 2305
    • Unknown

    #141
    Re: Running a microcontroller in car - power supply aspects

    Code:
    8<
    
     GIMSK &= (0 << PCIE); // no more Pin Change interrupts; mask them off
    
    [B]What do you think THIS ^ does?[/B]
    
    8<
    
     GIMSK |= (1 << PCIE); // unmask Pin Change interrupts; Interrupt source
    
    [B]This suggests PCIE is a small integer[/B] (I don't use those processors)
    Last edited by Curious.George; 05-23-2020, 05:45 PM.

    Comment

    • redwire
      Badcaps Legend
      • Dec 2010
      • 3900
      • Canada

      #142
      Re: Running a microcontroller in car - power supply aspects

      Those are bit-set and bit-clear instructions directly to MCU registers. What you are doing is reading the register, OR'ing 1 with the bit you want to (set) to a 1 (or AND'ing 0 with the bit you want to (clear) to a 0), and then writing that result back to the register. So you leave all other bits in the register alone, just bump the one you are interested in.

      The Arduino IDE has all the MCU registers (for ATmega328 or ATtiny85 etc.) defined internally I think in <avr/io.h>. All capital letters are traditionally used for constants.

      For the ATtiny85:
      GIMSK is the MCU General Interrupt Mask Register at address 0x3B, and within it is
      PCIE is the 5th bit position of GIMSK, "pin change interrupt enable bit"
      PCMSK is the MCU Pin Change Mask Register, selects which pin(s) will generate a pin change interrupt.
      sorta explained here: https://thewanderingengineer.com/201...s-on-attiny85/

      Doing the same with the ATmega328:
      PCICR is the MCU Pin Change Interrupt Control Register, choose the port A,B or C.
      PCMSK2 is the MCU Pin Change Mask Register, choose which pin(s) will generate a pin change interrupt.
      example here: https://thewanderingengineer.com/201...ge-interrupts/

      In a nutshell, just turning on pin-change interrupts for the pin, the port, the CPU. So when the MCU is asleep, it gets woken up by the Ignition pin going from low-> high (DannyX is opto inverted so high->low).
      Last edited by redwire; 05-23-2020, 08:41 PM.

      Comment

      • Curious.George
        Badcaps Legend
        • Nov 2011
        • 2305
        • Unknown

        #143
        Re: Running a microcontroller in car - power supply aspects

        Originally posted by redwire
        Those are bit-set and bit-clear instructions directly to MCU registers. What you are doing is reading the register, OR'ing 1 with the bit you want to (set) to a 1 (or AND'ing 0 with the bit you want to (clear) to a 0), and then writing that result back to the register. So you leave all other bits in the register alone, just bump the one you are interested in.
        I understand that's what you THOUGHT you were doing. But, in practice, you're NOT!

        PCIE is the 5th bit position of GIMSK, "pin change interrupt enable bit"
        If PCIE is actually defined to be "5" (and not 00100000r2 = 0x20), then:
        GIMSK &= (0 << PCIE)
        is actually:
        GIMSK = GIMSK & (0 << 5)
        which is:
        GIMSK = GIMSK & 0
        // 0 shifted left 5 times is still 0
        which is:
        GIMSK = 0
        (i.e., each bit in GIMSK is anded with a 0, yielding a 0 in that bit position)

        Again, assuming PCIE is "5", the second quoted line is correct:
        GIMSK |= (1 << PCIE)
        is actually:
        GIMSK = GIMSK | (1 << 5)
        which is:
        GIMSK = GIMSK | 0x20
        which ensures "bit 5" is set in GIMSK regardless of whether or not it was set (or clear!) previously.

        The FIRST line of code should have been:
        GIMSK &= ~(1 << PCIE)
        which is:
        GIMSK = GIMSK & ~(1 << 5)
        which is:
        GIMSK = GIMSK & ~(0x20)
        // 0x20 is 00100000r2
        which is:
        GIMSK = GIMSK & 0xDF
        // 0xDF = 0x11011111

        In other words, "ensure bit 5 is clear regardless of whether it was set (or clear) previously."

        You can test this with a trivial program:

        // ensure ALL bits are set just so we can see which one(s) change
        foo = 0xFF;
        foo &= (0 << 5);
        printf("Foo is %d.\n", foo);

        foo = 0xFF;
        foo &= ~(1 << 5);
        printf("Second foo is %d.\n", foo);
        Last edited by Curious.George; 05-23-2020, 09:17 PM.

        Comment

        • redwire
          Badcaps Legend
          • Dec 2010
          • 3900
          • Canada

          #144
          Re: Running a microcontroller in car - power supply aspects

          I think you're right, I looked at the code and it would never shut off the pin-change interrupt on the Ignition switch input. It's been in the car for 2 years with no fatal troubles (i.e. dead car battery). The only thing I've noticed is if the MCU (accessory timer) is running and then I start the car, sometimes the stereo will cut out and then reboot- which I attributed to my old car battery dropping too low during cranking. But that might be an ignition off->on->off (during cranking)->on (engine running). Three pin change interrupts.

          If I am (wrongly) globally shutting off interrupts, the Arduino libraries must overwrite that, as I get serial I/O fine after waking up from sleep.

          Comment

          • Curious.George
            Badcaps Legend
            • Nov 2011
            • 2305
            • Unknown

            #145
            Re: Running a microcontroller in car - power supply aspects

            Originally posted by redwire
            If I am (wrongly) globally shutting off interrupts, the Arduino libraries must overwrite that, as I get serial I/O fine after waking up from sleep.
            Dunno as I have no experience with that hardware nor the "system" software.

            If you have access to ALL of the sources, you might try to search for "GIMSK" anywhere in them and manually examine each such reference to see when it may be taking effect.

            [One problem with MOST software is there's never a useful "roadmap" that lets you figure out WHERE to look when you have a problem.]

            The other thing you can try (brute force) is littering YOUR code with tests to check the value of GIMSK at convenient places. And, whenever it "looks significant", emit a message, blink a light, HALT the processor (so you can figure out WHERE the test came back "positive").

            [I don't know what sort of debug environment you have available]

            Comment

            • Dannyx
              CertifiedAxhole
              • Aug 2016
              • 3912
              • Romania

              #146
              Re: Running a microcontroller in car - power supply aspects

              Yeah, I've got some massive learning to do...that went way over my head really quickly Bits and registers and stuff...

              I may also have to learn something about PICs in the future, having come across one in a control panel for one of those pump systems I talked about in another thread...we know nothing about what's on there and how it works yet, so that will be my job...
              Last edited by Dannyx; 05-24-2020, 03:20 AM.
              Wattevah...

              Comment

              • Curious.George
                Badcaps Legend
                • Nov 2011
                • 2305
                • Unknown

                #147
                Re: Running a microcontroller in car - power supply aspects

                Originally posted by Dannyx
                Yeah, I've got some massive learning to do...that went way over my head really quickly Bits and registers and stuff...

                I may also have to learn something about PICs in the future, having come across one in a control panel for one of those pump systems I talked about in another thread...we know nothing about what's on there and how it works yet, so that will be my job...
                PICs are a lousy place to start as their architecture is really quirky.

                If you have NO experience with CPUs/software, your best bet is to pick one of the "classic" processors (1970-80 vintage) with more regular instruction sets and programmer models to "cut your teeth on".

                If you could find a copy of the 6800 Microprocessor Applications Manual (that's 6800 not 68000!) it can almost act as a textbook to expose you to the issues you'd need to know. It's long but is an easy read.

                Caxton Foster's (poorly named) Real-Time Programming is another quick read.

                Comment

                • stj
                  Great Sage 齊天大聖
                  • Dec 2009
                  • 30963
                  • Albion

                  #148
                  Re: Running a microcontroller in car - power supply aspects

                  hell no,
                  start with 6502 - get yourself a commodore64.

                  Comment

                  • Dannyx
                    CertifiedAxhole
                    • Aug 2016
                    • 3912
                    • Romania

                    #149
                    Re: Running a microcontroller in car - power supply aspects

                    Originally posted by Curious.George
                    PICs are a lousy place to start as their architecture is really quirky.
                    It's not about "starting" anything - it's already in place and I have to (at least) somehow back up what's on it, if at all possible, should the whole box fail and need replacing....at which point it'd probably be easier and cheaper to rebuild it from scratch using something I'm more familiar with...
                    Wattevah...

                    Comment

                    • Curious.George
                      Badcaps Legend
                      • Nov 2011
                      • 2305
                      • Unknown

                      #150
                      Re: Running a microcontroller in car - power supply aspects

                      Originally posted by stj
                      hell no,
                      start with 6502 - get yourself a commodore64.
                      You may as well push for a 2A03! <frown>

                      The 6502 is almost as quirky as a PIC and omits many of the features that are now commonplace in MPUs and MCUs.

                      While the dual accumulator 6800 is a throwback to the past, the SINGLE accumulator 6502 is even worse! EVERYTHING had to go through the accumulator.

                      You also lost the advantage of the index registers (now commonly used for structure member references as well as accessing the stack frame).

                      And, of course, the 6502 had a crippled stack (8 bit SP).

                      The 6502 belongs cleanly in the past, having failed to anticipate any of the more modern features in processors (micro- or otherwise)

                      Comment

                      • Curious.George
                        Badcaps Legend
                        • Nov 2011
                        • 2305
                        • Unknown

                        #151
                        Re: Running a microcontroller in car - power supply aspects

                        Originally posted by Dannyx
                        It's not about "starting" anything - it's already in place and I have to (at least) somehow back up what's on it, if at all possible, should the whole box fail and need replacing....at which point it'd probably be easier and cheaper to rebuild it from scratch using something I'm more familiar with...
                        Did you note the sentence that followed: "If you have NO experience with CPUs/software, your best bet is ..."? If you "start" your education on a PIC, you will find "real" processors to be confusing in their more orthogonal designs. The same is true if you start with a single-accumulator design (even if it has TWO accumulators) and then move to a register-file design. There are very different ways of thinking about solutions based on the programmer model you're faced with.

                        Comment

                        • Dannyx
                          CertifiedAxhole
                          • Aug 2016
                          • 3912
                          • Romania

                          #152
                          Re: Running a microcontroller in car - power supply aspects

                          Not wanting to give up just yet and also to hopefully get at least close to understanding stuff like in post #143, I briefly looked into registers and binary and stuff like that, since I know nothing about that, though I only did it strictly concerning this project for now, nothing too in-depth.

                          From what I gather, putting an Atmega328p to sleep involves setting the bits in SMCR (Sleep Mode Control Register) in a certain way, depending on how "deep" you want it to sleep and how many features you need disabled to save power. The datasheet talks about this on page 37. I'm not sure HOW you actually do that just yet. The info on Arduino registers although better than nothing, is a bit brief for my taste and mentions nothing of this SMCR.

                          Waking it up: what I'm trying to achieve is put the board to sleep after it sits idle for a while, then wake it up when a pin like the pins listening for the lock/unlock commands changes state, which if I understand some of these tutorials correctly is called an "external interrupt" which wakes it up once it's put to sleep by setting that SMCR. On the Arduino Mini pins 2 and 3 support this feature....again, that is if I understand this right...
                          Wattevah...

                          Comment

                          • Dannyx
                            CertifiedAxhole
                            • Aug 2016
                            • 3912
                            • Romania

                            #153
                            Re: Running a microcontroller in car - power supply aspects

                            UPDATE: I managed to learn a little bit more about this sleep business, enough to put my board to sleep successfully. Indeed it works: the current consumption drops down by a lot, though I noticed it varies quite a lot between two different projects I tried loading on it, despite the same sleep command is used. I think it has something to do with how many pins are defined as inputs vs how many as outputs, because I saw a video where the guy suggests setting all the unused pins as outputs to cut down the current consumption for some reason...
                            Wattevah...

                            Comment

                            • stj
                              Great Sage 齊天大聖
                              • Dec 2009
                              • 30963
                              • Albion

                              #154
                              Re: Running a microcontroller in car - power supply aspects

                              probably because of the pullup resistors

                              Comment

                              • redwire
                                Badcaps Legend
                                • Dec 2010
                                • 3900
                                • Canada

                                #155
                                Re: Running a microcontroller in car - power supply aspects

                                Are you are using this mode:
                                Code:
                                set_sleep_mode(SLEEP_MODE_PWR_DOWN);
                                Make sure you turn off the A/D, brown out detection, maybe watchdog timer to get really low current draw under 0.5mA when you go to sleep. Sometimes MCU input pins will float not at 5V or 0V and waste current. I don't think your circuit has any issues there because there are pullup resistors on all I/O. You have no outputs high all the time, which can also waste power.
                                There are many sleep tutorials, not all correct but they try explain the '328 sleep modes.

                                When the IGN key is off, I would wait 30 seconds before going to sleep. It's nice to be able to close the car windows without the key back in.

                                Comment

                                • Dannyx
                                  CertifiedAxhole
                                  • Aug 2016
                                  • 3912
                                  • Romania

                                  #156
                                  Re: Running a microcontroller in car - power supply aspects

                                  Originally posted by redwire
                                  Are you are using this mode:
                                  Code:
                                  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
                                  Yes, that along with sleep_cpu() as per THIS tutorial.
                                  I also got the hang of waking it back up using interrupts 0 and 1 on pins 2 and 3 respectively. I made it so that when you lock the car, the windows roll up and the sleep command is run after the windows have closed. Interrupt 1 is connected to the unlock signal - it wakes the MCU when you unlock the car. Interrupt 0 is the IGN ("ACC", accessory, as I call it) connection - it also wakes the MCU ready to roll some windows when you hop in The reason I use the unlock signal as an interrupt as well is because I made it so that if you double-click the unlock button, so the "unlock" pin (3) on the Mini gets pulled down twice in succession, the windows roll DOWN, say for those hot sunny days, so the car gets some air before you hop in. The same happens for the "lock" pin (4) - click it twice and the windows go up, then the MCU goes to sleep......it works, but I admit the code is a mess
                                  Last edited by Dannyx; 06-01-2020, 01:12 PM.
                                  Wattevah...

                                  Comment

                                  • sam_sam_sam
                                    Badcaps Legend
                                    • Jul 2011
                                    • 6030
                                    • USA

                                    #157
                                    Re: Running a microcontroller in car - power supply aspects

                                    Originally posted by Dannyx
                                    I also got the hang of waking it back up using interrupts 0 and 1 on pins 2 and 3 respectively. I made it so that when you lock the car, the windows roll up and the sleep command is run after the windows have closed. Interrupt 1 is connected to the unlock signal - it wakes the MCU when you unlock the car. Interrupt 0 is the IGN ("ACC", accessory, as I call it) connection - it also wakes the MCU ready to roll some windows when you hop in The reason I use the unlock signal as an interrupt as well is because I made it so that if you double-click the unlock button, so the "unlock" pin (3) on the Mini gets pulled down twice in succession, the windows roll DOWN, say for those hot sunny days, so the car gets some air before you hop in. The same happens for the "lock" pin (4) - click it twice and the windows go up, then the MCU goes to sleep......it works, but I admit the code is a mess

                                    Nice work I like your approach to this project keep up the good work
                                    When you finally finish this project could you show some pictures of the the setup

                                    Comment

                                    • Dannyx
                                      CertifiedAxhole
                                      • Aug 2016
                                      • 3912
                                      • Romania

                                      #158
                                      Re: Running a microcontroller in car - power supply aspects

                                      Alright guys, here's an update on this project and some pics of the board which is almost ready, component-wise at least. Yes, it actually took me this long to work on it some more - go ahead and laugh

                                      I added the two current sensing boards in the lower-left today, but haven't done any testing because some personal stuff came up just as I was getting ready to plop the Arduino on the headers and upload the code.

                                      Your boy Danny also made a little screw-up: I ordered the wrong relays I ordered relays with 5v coils, which was the original plan, since I was planning to drive them directly with the Arduino, but then I thought 12v relays and transistors might be better, yet my brain must've been stuck on the 5v idea and hence I ordered 4 of those - fail.
                                      To make matters worse, the first set of 4 I ordered had a knackered one among them which had something rattling inside, so I had no choice but order ANOTHER set of 2 (the minimum quantity the seller offered), so now I have 6 big and blocky 5v relays......

                                      Ok, so what do we do now ? I was just about ready to start over and order a set of the correct 12v relays but that would take a couple of extra months to arrive and with autumn just around the corner here, my time is pretty limited to finish this project and install it (it's already taken a laughable amount of time), plus I don't want to get stuck with 6 hunks of plastic I paid good money for, so I'm thinking of using a resistor in series with them to power them off the battery and still use the transistors to interface with the arduino - haven't done the math yet, but the current on them seems to be around 0.17mA.
                                      Attached Files
                                      Wattevah...

                                      Comment

                                      • redwire
                                        Badcaps Legend
                                        • Dec 2010
                                        • 3900
                                        • Canada

                                        #159
                                        Re: Running a microcontroller in car - power supply aspects

                                        How many ohms are the 5V relay coils? It looks like 1-4 of them could be on at once, worst case. A big 6V zener?

                                        Comment

                                        • Dannyx
                                          CertifiedAxhole
                                          • Aug 2016
                                          • 3912
                                          • Romania

                                          #160
                                          Re: Running a microcontroller in car - power supply aspects

                                          Originally posted by redwire
                                          How many ohms are the 5V relay coils? It looks like 1-4 of them could be on at once, worst case. A big 6V zener?
                                          Since the relay pulls 0.17a at 5v, I think it's possible to work out the resistance to be around 30 ohms, though this is on paper - haven't measured directly with a meter. Since it's a coil and has some inductance, there's probably other factors which influence it, but I think it's in the ballpark.

                                          I was thinking of simply adding a resistor in series with the coil to run it off 12v...
                                          Wattevah...

                                          Comment

                                          Related Topics

                                          Collapse

                                          • CG2
                                            Meaco Arete One / Deye Dehumidifier not working
                                            by CG2
                                            I've got a dehumidifier that has stopped working. Plug it in, press the power switch, and it beeps 5 times. Nothing showing in the display (which is just two 7 segment displays) apart from the two decimal points flashing. The model is a MeacoDry Arete One 18L, which seems to be a rebadged Deye (all the boards are marked Deye). Here's what I've got so far (details in case anyone else in future should need them).

                                            Starting with the power board. Mains power comes in at the red connector top left. The compressor is powered by the orange connector to the right of that and is switched by...
                                            08-17-2024, 07:47 AM
                                          • Kambi13
                                            Samsung NP370E4K No backlight running on battery
                                            by Kambi13
                                            Hi all, need some help. I have a Samsung NP370E4K-KD2BR, which as a 5th Gen Intel CPU that works fine when running with the charger plugged in, but has soon as I remove it and it's running on battery the screen backlight goes off.

                                            Tried different battery - no change
                                            Tried different screen - no change

                                            Removed the plastic film from the screen inverter and looked for pwm signal which as about 3.3v when running with the charger plugged in, when I take it off the voltage drops down to 2.232v, so my best guess is that the EC is not providing the correct voltage....
                                            09-04-2023, 11:06 AM
                                          • Dannyx
                                            "Kinetic" 433Mhz transmitter/receiver discussion
                                            by Dannyx
                                            Good day folks.

                                            TL;DR: has anyone tried pairing one of THESE "kinetic" battery-less switches with THESE generic remote modules ?

                                            Details: I recently got hit by this craze of wanting to add remote controls to the lights around my house, so I went with those generic 433Mhz remote relay modules found everywhere, which are really basic, easy to use and work well, since I used them before in other projects. What I like most about them is they accept all kinds of 433Mhz remotes that float around Aliexpress, since they're all the same under the hood, save for the...
                                            03-08-2022, 11:01 AM
                                          • Skgod
                                            Asus Rog strix g15 g513ih hn0865 and it's having display issues while running on igpu
                                            by Skgod
                                            Hey I am having Asus Rog strix g15 g513ih hn0865 and it's having display issues while running on igpu. Works fine when I open any game or whatsapp windows application. The issues are :
                                            Black screen, freeze
                                            The weird thing is that when as long as the laptop is running on igpu the problem keeps happening. But as soon as I open any game or whatsapp windows application, the laptop works completely fine.
                                            When the screen freeze or black screen I used to move the display lid up and down and it usually works like that.
                                            So can anyone tell me what would be the possible cause?...
                                            10-16-2024, 08:11 PM
                                          • FALKLAN
                                            Running Line Voltage and Integrating LED Pods - Automotive
                                            by FALKLAN
                                            Howdy everyone! I hope all is well where ever you are!

                                            I'm attempting to integrate a couple of LED pods into an automotive reverse lamp circuit. The problem being, while the vehicle is running there is a 8.5v current present on the reverse lamp power wire. Needless to say is that all LED pods and lights dimly illuminate while the 8.5v is present, so the LEDs are always powered on while the vehicle is running. When the vehicle is shifted into reverse, the voltage changes to 12 volts.

                                            I am uncertain how to convert this line into a switched 12v output line for the LED pods....
                                            02-05-2025, 12:30 AM
                                          • Loading...
                                          • No more items.
                                          Working...