PID loop with Pic in Assembly

Discussion in 'Embedded Systems and Microcontrollers' started by MaxHeadRoom, Oct 8, 2015.

  1. MaxHeadRoom

    Thread Starter Expert

    Jul 18, 2013
    10,553
    2,375
    Anyone have and helpful links for PID programming and/or assembly routines for PIC preferably 18F series?.
    Just to use as a starting point.
    The AN937/AN964 are an outline starting point but most of the sample code found out there so far is in C.
    Max.
     
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,824
    364
    The difficulties you will experience in finding such a piece say a lot about your use of assembly for this task and assembly in general.
     
  3. MaxHeadRoom

    Thread Starter Expert

    Jul 18, 2013
    10,553
    2,375
    I have since found the assembly program for the AN964, I may just put the inverted pendulum together for the experience and heck of it!.
    I have most of the parts.
    Max.
     
  4. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    Danny, please stop ridiculing those who choose .asm over C. You are relatively new here, and we've had many debates on the subject in the past. Most of us have decided to agree to disagree and leave it at that.

    I realize you are a super duper expert in C programming, but there are good reasons to use .asm at times. I have been programming in .asm for 35 years, and C and C++ for 25. I still use .asm on all of my embedded designs, many millions of dollars worth of which are currently in the field. I use .asm because it is superior at getting the most out of a very low cost MCU on commodity products where margins make or break success.

    With that said, I have many times implemented PID loops in .asm that would have been absolutely impossible to perform in C, simply because the compiler overhead would have caused the resulting code to run too slow. We are talking 12 bit A/D -> PID -> 14 bit PWM at a 20khz rate & 20mhz clock on a PIC16 with dynamic set points and adaptive coefficients. This cannot be done in C no matter how hard you try.

    Stop with the ridicule. It's annoying and makes you look silly.
     
    Brevor, GopherT, MaxHeadRoom and 3 others like this.
  5. DickCappels

    Moderator

    Aug 21, 2008
    2,658
    632
    Nice application note. Thank you for sharing it.
     
  6. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    I roll my own...so I can't point you to anything specific. But, if you provide some additional details, I might be able to help guide you a bit.
     
  7. MaxHeadRoom

    Thread Starter Expert

    Jul 18, 2013
    10,553
    2,375
    Thanks, the eventual aim is for a PWM DC motor controller with 2pulse/rev feedback.
    I have most of the code tested including the control command via Modbus.
    It is just a side project I am doing for knowledge purposes, so there is no pressure on to get it done.
    I think I will see how far I get with the pendulum code and if I get stuck, I'll Be Back, as Arnold would say.
    Max.
     
  8. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    764
    Max,

    Not sure if you could find it today but when the 16C57 was THE micro and datasheets you could obtain them in books nicely printed by Microchip, there was an AN with an application implementing it. Found it simple to understand.

    Tomorrow I will look for the number.

    EDIT

    For the 16C57, AN531. Certainly old but well explained.

    /EDIT
     
    Last edited: Oct 9, 2015
  9. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,061
    3,823
    We did PID control on a line following robot but with several cooks taking care of the soup, the commenting protocol fell apart and I couldn't start to explain how that 3-year-old project worked today. The next year, it was done in C and easier to follow the code but slower - still won!
     
  10. MaxHeadRoom

    Thread Starter Expert

    Jul 18, 2013
    10,553
    2,375
    Thanks, I see that is listed here, http://pic-rosa.blogspot.ca/2007/09/source-code-for-pic.html
    The list is not up to date, but useful, I will take a look.
    AN964 (inverted pendulum) uses a 16F684, so if I get this one working I will have to translate it over to the 18F23k22.
    Max.
     
  11. dannyf

    Well-Known Member

    Sep 13, 2015
    1,824
    364
    Take it up a separate thread and let's see how others get it done right. Post your code and we will benchmark against it.

    I think you have proved quite convincingly that you couldn't get it done. Whether others can get it done remains to be seen.

    That was like last millennium.
     
  12. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    I have no idea who 'we' are, but you could be the first to accept my challenge.
     
    OBW0549 and GopherT like this.
  13. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,061
    3,823
    I think it is already clear that someone does not understand how many assembly steps it takes to create each of those seemingly efficient commands from the C program.
     
    OBW0549 likes this.
  14. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    That someone would be me. In fact, it seems that every different compiler I have seen takes a different approach, and that is before optimizations. So, no, I have absolutely no idea of the number of assembly instructions that are required for all but the most trivial C constructs.

    One would have to examine the .asm output to be sure, but then he'd have to know how to read (an obscure dialect of) .asm to begin with.
     
    GopherT likes this.
  15. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,061
    3,823
    It would be you if you are wearing the rose colored glasses that make you believe those lines of C code are efficiently converted to ASM code. It sounds more like you are the one who goes into the comparison with the (correct) assumption that it is inefficiently converted to ASM.

    My first attempt at C was completely frustrating as I was trying to convert a very simple project from ASM and write in C. I needed to calculate clock ticks to calculate a time delay. In C, my TIMER0 was rolling over multiple times, there was no way I could use the same, small and efficient code in C as I did in ASM and no way to match the speed of ASM and, no way to know exactly how long a subroutine would take in C without actually compiling it.
     
  16. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    See this long post and search for the word "babysit" in response to @THE_RB's comment about "C--".
     
  17. MaxHeadRoom

    Thread Starter Expert

    Jul 18, 2013
    10,553
    2,375
    I never really intended the post to fall into the age old battle of C vs Assembly.
    For better or worse I am pursuing the assembly method.
    One mention made in AN964 is that the the signed PID math in C code executes in 1.4ms and the assembly version in 215υs
    Max.
     
  18. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    Max, I would have held my peace except that @dannyf was indirectly calling you, me, and a bunch others fools across multiple threads.
     
    GopherT and MaxHeadRoom like this.
  19. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,061
    3,823
    I thought long and hard about that inverted pendulum project. I always wanted to build one. Second thing about that app note, I always wondered why they would post the application note in C rather than ASM before a free version of C was even available.
     
  20. Lourens

    New Member

    Jul 16, 2012
    14
    1
    Hi Max
    I have available 18F assembler code for 2 PID controllers. The code is part of the library for a visual programming system. The first uses the standard algorithm in independent gains form, uses 582 bytes program memory and executes in 143 uSec. The second also uses the standard form but in velocity format, uses 278 bytes program memory and executes in 152 uSec. Floating point calculation is used so this will skew those program memory figures. Give me a URL and I will send you the code.
    Lourens
     
Loading...