Become a leader in the IoT community!

New DevHeads get a 320-point leaderboard boost when joining the DevHeads IoT Integration Community. In addition to learning and advising, active community leaders are rewarded with community recognition and free tech stuff. Start your Legendary Collaboration now!

Step 1 of 5

CREATE YOUR PROFILE *Required

Change Email
OR
Step 2 of 5

WHAT BRINGS YOU TO DEVHEADS? *Choose 1 or more

Collaboration & Work 🤝
Learn & Grow 📚
Contribute Experience & Expertise 🔧
Step 3 of 5

WHAT'S YOUR INTEREST OR EXPERTISE? *Choose 1 or more

Hardware & Design 💡
Embedded Software 💻
Edge Networking
Step 4 of 5

Personalize your profile

Step 5 of 5

Read & agree to our COMMUNITY RULES

  1. We want this server to be a welcoming space! Treat everyone with respect. Absolutely no harassment, witch hunting, sexism, racism, or hate speech will be tolerated.
  2. If you see something against the rules or something that makes you feel unsafe, let staff know by messaging @admin in the "support-tickets" tab in the Live DevChat menu.
  3. No age-restricted, obscene or NSFW content. This includes text, images, or links featuring nudity, sex, hard violence, or other graphically disturbing content.
  4. No spam. This includes DMing fellow members.
  5. You must be over the age of 18 years old to participate in our community.
  6. Our community uses Answer Overflow to index content on the web. By posting in this channel your messages will be indexed on the worldwide web to help others find answers.
  7. You agree to our Terms of Service (https://www.devheads.io/terms-of-service/) and Privacy Policy (https://www.devheads.io/privacy-policy)
By clicking "Finish", you have read and agreed to the our Terms of Service and Privacy Policy.

How to Optimize Energy Calculations for STM32 Energy Meter Without Overloading Interrupts?

Hello team, so I have been working on an energy meter for some time using stm32f103c8t6 as my controller, zmct103 current transformer for measuring AC current and then using LM324 op-amp in differential mode to measure ac voltage. I have been able to get data with ac load, but my readings(adc readings) and calculations(energy calculation) are done inside interupt. I know the interrupt is meant to be short, is there a better way to handle calculations? I will appreciate your insights. here is the link to the calculations in the interrupt: https://github.com/Afuevu/dev_iot/blob/main/energy_meter

  1. ZacckOsiemo#0000

    @afuevu_ you know you can share a line number, Impressive too! I would do most of this work not in the handler, basically the handler should just get information and hand it over t a task, or some function if you are not using an OS

  2. Afuevu#0000

    Thanks man. I’ll look into it and see what I can do, I’m not using an OS so i just go with functions.
    The calculations start off from line 20. If needed i could work you through the lines so you better understand my thought processes as to why i decided to put it in interrupt

  3. ZacckOsiemo#0000

    yeah we can do that but most of this work should not be in the handler it should go into some queue or something that you check in your idle or main loop

  4. Marvee Amasi#0000

    the interrupt can be kept short and the data safe if u try the flag system wher yo have the interrupt set a flag and store ADC values in a buffer, then you wil now let the main loop handle the processing based on that flag

  5. Afuevu#0000

    I am using DMA for the ADC. The timer 2 interrupt is called every 50uS. Line 20 and 23, i am updating the variable to store the values of ADC read using DMA. There is a counter variable that increments every time the interrupt is called which is on line 3, the counter variable is value1. On line 23 the when value1 counter gets to 400, the counter is true
    at that point and i have gotten 20ms( 50uS*400= 20ms) = 50hz, meaning every 50hz or 20ms, i would have gotten 400 samples. From line 30 to 41, is where i get the main calculation for my voltage and current after taking many samples. For a detailed breakdown and understanding of how i came up with the voltage reading, this link explains more on how to use op-amp to measure ac voltagehttps://microcontrollerslab.com/ac-voltage-measurement-using-microcontroller/. For the measurement of the current, this link explains how to go about it https://microcontrollerslab.com/alternating-current-measurement-using-pic-micocontroller/#:~:text=To%20measure%20AC%20current%20with,drop%20across%20the%20shunt%20resistor.
    Then on line 47 there is a variable timer2_count that increments every 20ms. Then on line 51, when the timer2_count variable gets up to 50, the condition is true and that block of code is called, inside the block i calculate my energy consumed every second.

  6. ZacckOsiemo#0000

    all the work after this https://github.com/Afuevu/dev_iot/blob/7055fff9c2fa26d6aed8bd3fadba1d5951001c7e/energy_meter#L25 should be done in your main loop, not in your ISR

  7. ZacckOsiemo#0000

    well except resetting the Interrupt

  8. Afuevu#0000

    Yes chief I will work on that

  9. Afuevu#0000

    Thanks chief, i will implement it using flags

  10. ZacckOsiemo#0000

    I wouldn’t mind seeing the whole project as well though

  11. Afuevu#0000

    For sure, i will drop the link.

  12. Afuevu#0000
  13. Afuevu#0000

    Hello everyone, Good day. I have been able to make the adjustment @superbike_z and @marveeamasi suggested that I moved the calculations from the interrupt to the while loop and just used flags to do the checks in the ISR. I have made a commit on the repo, here is the link https://github.com/DevHeadsCommunity/AC_CURRENT_MEASUREMENT/tree/master. I intend to save data on the EEPROM every 8 hours and 24 hours. I am considering using the internal RTC of the stm32 or a counter that keeps incrementing once the system turns on. What are our thoughts, have anyone used the internal RTC of the stm32f103c8t6 before?

  14. ZacckOsiemo#0000

    i have used the internal RTC, If i remember correctly its on SPI 1 and that connection was hard to get going.

  15. ZacckOsiemo#0000

    could you share the line of code where you put in the change?

  16. Afuevu#0000

    Line 30 to line 82

  17. Afuevu#0000

    what do you mean RTC is on SPI 1, please? I think RTC is a stand-alone peripheral on the STM32

  18. melta101#0000

    they share pins

  19. Afuevu#0000

    Oh i see

  20. Afuevu#0000

    Actually i am not using its pin, I am just reading the date and time after i have written to it

  21. ZacckOsiemo#0000

    it turns out I was mistaken, i was talking about the MEMs sensor but I have used thr RTC as well, I faced some issues too but I don’t remember which means i fixed them.

  22. ZacckOsiemo#0000

    I can share code if you want @afuevu_

CONTRIBUTE TO THIS THREAD

Browse other questions tagged