PID loop with Pic in Assembly

Status
Not open for further replies.

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
30,659
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.
 

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
30,659
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.
 

joeyd999

Joined Jun 6, 2011
6,281
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.
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.
 

joeyd999

Joined Jun 6, 2011
6,281
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.
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.
 

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
30,659
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.
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.
 

atferrari

Joined Jan 6, 2004
5,012
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:

GopherT

Joined Nov 23, 2012
8,009
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.
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!
 

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
30,659
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.
For the 16C57, AN531. Certainly old but well explained.
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.
 

dannyf

Joined Sep 13, 2015
2,197
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.
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.

This cannot be done in C no matter how hard you try.
I think you have proved quite convincingly that you couldn't get it done. Whether others can get it done remains to be seen.

when the 16C57 was THE micro
That was like last millennium.
 

joeyd999

Joined Jun 6, 2011
6,281
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.
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

Joined Nov 23, 2012
8,009
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.
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.
 

joeyd999

Joined Jun 6, 2011
6,281
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.
See this long post and search for the word "babysit" in response to @THE_RB's comment about "C--".
 

Thread Starter

MaxHeadRoom

Joined Jul 18, 2013
30,659
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.
 

joeyd999

Joined Jun 6, 2011
6,281
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.
Max, I would have held my peace except that @dannyf was indirectly calling you, me, and a bunch others fools across multiple threads.
 

GopherT

Joined Nov 23, 2012
8,009
Max, I would have held my peace except that @dannyf was indirectly calling you, me, and a bunch others fools across multiple threads.
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.
 

Lourens

Joined Jul 16, 2012
15
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
 
Status
Not open for further replies.
Top