Author Topic: Is this enough uC for this task?  (Read 1958 times)

0 Members and 1 Guest are viewing this topic.

Offline JesterTopic starter

  • Frequent Contributor
  • **
  • Posts: 859
  • Country: ca
Is this enough uC for this task?
« on: August 17, 2018, 04:33:38 pm »
I'm more of a hardware guy, looking for comments from those that write embedded C code, does this part have enough FLASH and RAM?

Simple program:
1) continuously measure the voltages on either side of the current sense resistor and average the difference (the measured motor current), probably best to do this in an IRQ
2) Set the PWM level based on the measured current
3) Momentary latched on/off

Motor is used for a small water pump that can experience air at times so the PWM needs to be reduced when air present and then ramped up when only water.

Feel free to critique.

If anyone wants to write the code, please PM me.

 

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 3719
  • Country: us
Re: Is this enough uC for this task?
« Reply #1 on: August 17, 2018, 05:41:38 pm »
I'm more of a hardware guy, looking for comments from those that write embedded C code, does this part have enough FLASH and RAM?

Simple program:
1) continuously measure the voltages on either side of the current sense resistor and average the difference (the measured motor current), probably best to do this in an IRQ
2) Set the PWM level based on the measured current
3) Momentary latched on/off

Motor is used for a small water pump that can experience air at times so the PWM needs to be reduced when air present and then ramped up when only water.

Feel free to critique.

If anyone wants to write the code, please PM me.

Yes, any microcontroller can do this task.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14472
  • Country: fr
Re: Is this enough uC for this task?
« Reply #2 on: August 17, 2018, 05:43:58 pm »
This PIC12F is most likely adequate for the task, but you're not mentioning what kind of algorithm you're going to use to adjust PWM. Would this be a PID? If so, it may not fit in this small amouint of memory. Also, what language are you going to use? Writing this in C will need very tight coding or you'll reach the flash memory limit pretty quickly.

 
The following users thanked this post: Jester

Offline JesterTopic starter

  • Frequent Contributor
  • **
  • Posts: 859
  • Country: ca
Re: Is this enough uC for this task?
« Reply #3 on: August 17, 2018, 06:15:21 pm »
This PIC12F is most likely adequate for the task, but you're not mentioning what kind of algorithm you're going to use to adjust PWM. Would this be a PID? If so, it may not fit in this small amouint of memory. Also, what language are you going to use? Writing this in C will need very tight coding or you'll reach the flash memory limit pretty quickly.


  • C language
  • algorithm can be really simple, switch statement with desired PWM levels for measured current, perhaps 5 levels at most




Yes, any microcontroller can do this task.

Considered the PIC10F and I don't think it has enough program memory size, (256), might do that in assembler, but seems iffy at best so perhaps not any uC.
« Last Edit: August 17, 2018, 06:18:09 pm by Jester »
 

Offline retrolefty

  • Super Contributor
  • ***
  • Posts: 1648
  • Country: us
  • measurement changes behavior
Re: Is this enough uC for this task?
« Reply #4 on: August 17, 2018, 07:14:04 pm »
A cheap $2 asian arduino pro-mini board would be more than enough and you could not build cheaper then that.

 
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26906
  • Country: nl
    • NCT Developments
Re: Is this enough uC for this task?
« Reply #5 on: August 17, 2018, 07:24:27 pm »
What I'm missing is the budget and power requirements. AFAIK the commercial PIC development tools cost around $1k to start with. Also choosing a very minimalistic microcontroller may end up costing more in engineering costs because of needed optimisations. I'd go for an ARM based controller. This will have no problems at all with delivering the processing power if it turns out the algorithm isn't that simple after all. Also be really sure to check a PIC can run at 1.8V and it is not the minimal voltage. To run at 1.8V the minimum voltage must be around 1.65V to allow for variations in supply voltages.
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline JesterTopic starter

  • Frequent Contributor
  • **
  • Posts: 859
  • Country: ca
Re: Is this enough uC for this task?
« Reply #6 on: August 17, 2018, 08:14:12 pm »
A cheap $2 asian arduino pro-mini board would be more than enough and you could not build cheaper then that.

True on the price, however this will be produced in volume and has to be small <10mm in one direction, so I can't rely on a Asian Arduino pro-mini board as it's way too big.


What I'm missing is the budget and power requirements. AFAIK the commercial PIC development tools cost around $1k to start with. Also choosing a very minimalistic microcontroller may end up costing more in engineering costs because of needed optimisations. I'd go for an ARM based controller. This will have no problems at all with delivering the processing power if it turns out the algorithm isn't that simple after all. Also be really sure to check a PIC can run at 1.8V and it is not the minimal voltage. To run at 1.8V the minimum voltage must be around 1.65V to allow for variations in supply voltages.

Budget as cheap as possible.

Power consumption, runs off an AA, very intermittently so the motor current is orders of magnitude > uC current which will be asleep when its not on.

For a development kit:  https://www.digikey.com/products/en?keywords=Q7442559%20  at $35 seems pretty cheap, it's been a while but the last PIC I programmed was with a free IDE from Microchip has that changed?

Looks like its specified to operate at 1.8V, I'm using a boost converter so that should be okay.

PIC12 is $0.89 so pretty cheap.



« Last Edit: August 17, 2018, 08:22:26 pm by Jester »
 

Offline jaromir

  • Supporter
  • ****
  • Posts: 338
  • Country: sk
Re: Is this enough uC for this task?
« Reply #7 on: August 17, 2018, 11:46:17 pm »
For a development kit:  https://www.digikey.com/products/en?keywords=Q7442559%20  at $35 seems pretty cheap, it's been a while but the last PIC I programmed was with a free IDE from Microchip has that changed?

MPLABX + XC8 compiler suitable also for commercial applications costs $0 and is available on http://www.microchip.com/mplab/mplab-x-ide
You may use that devkit as a programmer (if you bring out needed GND/VDD/MCLR/PGC/PDG pins to your target board).
 
The following users thanked this post: Jester

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14472
  • Country: fr
Re: Is this enough uC for this task?
« Reply #8 on: August 18, 2018, 12:53:23 am »
  • C language
  • algorithm can be really simple, switch statement with desired PWM levels for measured current, perhaps 5 levels at most

Should be doable without any problem then.
A good fit for those small MCUs. I like simple parts for simple tasks. Besides, they draw very little power which is a nice bonus.

 
The following users thanked this post: Jester

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: Is this enough uC for this task?
« Reply #9 on: August 18, 2018, 01:39:33 am »
Best to simplify early on,

Just about every micro's ADC can operate in differential mode, e.g. the difference between 2 ADC inputs. So apart from the boilerplate to get the ADC set up, your only dealing with 1 measurement.

Next up, PWM, pick a suitable pin for the method of PWM you want, likely using a 16 bit timer to allow finer control. (these are baked in to just about every 8 bit micro out there), this is just 1 output, the timer compare register. and using the timer will mean it runs on its own, independent of what the processor is doing.

And finally the switch, seeing as your not doing much, you can just sit there polling it in the time between ADC measurements, when it changes state, you use an 8 bit timer, and if it stays in that state until your debounce period has elapsed you treat it as valid and do whatever you where planning

All that leaves is the averaging, now in reality I would just size my variable to take all the summed values and work off that rather than averaging, as it gives much finer granularity,

E.g. you have a 10 bit ADC, you can sum up to 32 readings and be confident it will fit in a 16 bit value.

or if you really want a rolling average, then you need 16 bit chunks of memory to hold each sample. you keep track of a pointer in that memory area, each new sample you add the new sample to the total, then subtract the old and overwrite the old samples location with the new one in RAM. (lets say 32 bytes for 16 samples, plus 1 byte for the pointer, and 2 bytes for the Total

Finally control, you have some desired current, so you subtact your average (total) from your desired value (ADC value for current * number of averaged samples) and sum it to the PWM output, you can divide down this difference by powers of 2 to reduce the gain of the corrections, but as the motor has a mass, the micro will be making corrections way faster than the mass can respond, so integral only control works out beautifully.
 
The following users thanked this post: Jester

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26906
  • Country: nl
    • NCT Developments
Re: Is this enough uC for this task?
« Reply #10 on: August 18, 2018, 08:21:49 am »
What I'm missing is the budget and power requirements. AFAIK the commercial PIC development tools cost around $1k to start with. Also choosing a very minimalistic microcontroller may end up costing more in engineering costs because of needed optimisations. I'd go for an ARM based controller. This will have no problems at all with delivering the processing power if it turns out the algorithm isn't that simple after all. Also be really sure to check a PIC can run at 1.8V and it is not the minimal voltage. To run at 1.8V the minimum voltage must be around 1.65V to allow for variations in supply voltages.
Budget as cheap as possible.

Power consumption, runs off an AA, very intermittently so the motor current is orders of magnitude > uC current which will be asleep when its not on.

For a development kit:  https://www.digikey.com/products/en?keywords=Q7442559%20  at $35 seems pretty cheap, it's been a while but the last PIC I programmed was with a free IDE from Microchip has that changed?

Looks like its specified to operate at 1.8V, I'm using a boost converter so that should be okay.

PIC12 is $0.89 so pretty cheap.
Last time I looked the free PIC development tools didn't include full C optimisation which you might need to get better performance and/or small size. Anyway, I'd also suggest to look at TI's MSP430 series. A couple of years ago I redesigned a PIC based design using an MSP430 and the power consumption of the MSP430 was over 2 times less. Also the MSP430 is likely to have better analog peripherals. Analog circuits are a weak point of Microchip which is why I never use Microchip parts myself.

@Rerouter: averaging 10 bit ADC results into 16 bits is not going to work. If it was possible then the ADC would have been advertised as 16 bit. If you test it, you'll likely see 10 bit resolution. Maybe 11 bits if you have a lot of noise.
« Last Edit: August 18, 2018, 08:31:10 am by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 
The following users thanked this post: Jester

Offline Rerouter

  • Super Contributor
  • ***
  • Posts: 4694
  • Country: au
  • Question Everything... Except This Statement
Re: Is this enough uC for this task?
« Reply #11 on: August 18, 2018, 08:58:23 am »
To clarify, I did not mean he would get more resolution, he said he wanted averaging

So if you want to sum those 10 bit numbers up, you need a variable to hold them in, being an 8 bit micro is likely what he will end up with, and I cannot imagine a large number of averaged samples for a feedback system, so felt 32 samples would cover it, the summation of 32 x 10 bit samples can be held in a 16 bit variable without a risk of overflow, To the OP, in almost all cases you can only really trust about 8 to 9 bits of a 10 Bit micros ADC result.

Again, this is an average, without the divisor, just by scaling up the rest of his math, gives better granularity for the control loop, e.g. by dividing the result of the integral gives a nice bit of attenuation in the control so it takes a bigger difference to cause a change to the final PWM output, with the least additional computation steps I can see,
« Last Edit: August 18, 2018, 08:59:57 am by Rerouter »
 
The following users thanked this post: Jester

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8172
  • Country: fi
Re: Is this enough uC for this task?
« Reply #12 on: August 18, 2018, 09:27:34 am »
Almost any MCU will do, except the smallest $0.10 <6-leg guys without RAM, ADC or PWM outputs at all, but you know this already.

The one you have chosen already has quite a bit of extra room for some feature bloat, and leeway for non-optimal coding practices.

Note that you are creating a motor controller, and if the motor is fairly big with high stall current, controlling that is more than just averaging current on a slow ADC and controlling PWM setpoint on software. Hardware design may also require extra consideration - power layout, freewheeling, snubbing, DC link caps, etc.

I have done large DC motor controllers on tiny, cheap MCUs as well. An analog comparator input can trig cycle-by-cycle current limit in a fast ISR no problem.
 
The following users thanked this post: Jester

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11633
  • Country: my
  • reassessing directives...
Re: Is this enough uC for this task?
« Reply #13 on: August 18, 2018, 09:55:06 am »
2) Set the PWM level based on the measured current
Motor is used for a small water pump that can experience air at times so the PWM needs to be reduced when air present and then ramped up when only water.
Feel free to critique.
the tiniest in my arsenal (and my favorite too) is attiny13, its a 20 cents mcu and can do 2 motors regulation that you required, due to pin count limitation, and from processing power having no problem regulating 100s of the motors i rough-stimate it.
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 
The following users thanked this post: Jester

Offline JesterTopic starter

  • Frequent Contributor
  • **
  • Posts: 859
  • Country: ca
Re: Is this enough uC for this task?
« Reply #14 on: August 18, 2018, 10:47:30 pm »
Best to simplify early on,

Just about every micro's ADC can operate in differential mode, e.g. the difference between 2 ADC inputs. So apart from the boilerplate to get the ADC set up, your only dealing with 1 measurement.

Next up, PWM, pick a suitable pin for the method of PWM you want, likely using a 16 bit timer to allow finer control. (these are baked in to just about every 8 bit micro out there), this is just 1 output, the timer compare register. and using the timer will mean it runs on its own, independent of what the processor is doing.

And finally the switch, seeing as your not doing much, you can just sit there polling it in the time between ADC measurements, when it changes state, you use an 8 bit timer, and if it stays in that state until your debounce period has elapsed you treat it as valid and do whatever you where planning

All that leaves is the averaging, now in reality I would just size my variable to take all the summed values and work off that rather than averaging, as it gives much finer granularity,

E.g. you have a 10 bit ADC, you can sum up to 32 readings and be confident it will fit in a 16 bit value.

or if you really want a rolling average, then you need 16 bit chunks of memory to hold each sample. you keep track of a pointer in that memory area, each new sample you add the new sample to the total, then subtract the old and overwrite the old samples location with the new one in RAM. (lets say 32 bytes for 16 samples, plus 1 byte for the pointer, and 2 bytes for the Total

Finally control, you have some desired current, so you subtact your average (total) from your desired value (ADC value for current * number of averaged samples) and sum it to the PWM output, you can divide down this difference by powers of 2 to reduce the gain of the corrections, but as the motor has a mass, the micro will be making corrections way faster than the mass can respond, so integral only control works out beautifully.

Thanks for you advice, I'm going to order a Curiosity Development Board and write some code. I downloaded the free MPLAB X IDE and a couple of examples written in C,  and within minutes was able to get an example with A/D measurement driving the PWM compiled and it only uses 7% of ROM so far, so this is looking like a viable approach.

Thanks to everyone that chimed in, this forum has so many excellent members with such a wide scope of experience that are willing to share.
« Last Edit: August 18, 2018, 10:49:07 pm by Jester »
 

Offline bson

  • Supporter
  • ****
  • Posts: 2270
  • Country: us
Re: Is this enough uC for this task?
« Reply #15 on: August 18, 2018, 11:18:50 pm »
I'd also suggest to look at TI's MSP430 series. A couple of years ago I redesigned a PIC based design using an MSP430 and the power consumption of the MSP430 was over 2 times less. Also the MSP430 is likely to have better analog peripherals. Analog circuits are a weak point of Microchip which is why I never use Microchip parts myself.
^ this

The MSP430 is also 16 bit, so can atomically read volatile 16 bit data like ADC conversion registers (when > 8 bit) and timers/counters in a single cycle without worrying about rollover or carry in the middle.  Then perform operations on the data without churning cycles on 8 bit operations.  Fewer cycles = lower clock = lower power consumption.  The linear address space gets rid of banking overhead, it has a proper stack, and the TI compiler is quite good.
 

Offline JesterTopic starter

  • Frequent Contributor
  • **
  • Posts: 859
  • Country: ca
Re: Is this enough uC for this task?
« Reply #16 on: August 19, 2018, 01:09:40 am »
I'd also suggest to look at TI's MSP430 series. A couple of years ago I redesigned a PIC based design using an MSP430 and the power consumption of the MSP430 was over 2 times less. Also the MSP430 is likely to have better analog peripherals. Analog circuits are a weak point of Microchip which is why I never use Microchip parts myself.
^ this

The MSP430 is also 16 bit, so can atomically read volatile 16 bit data like ADC conversion registers (when > 8 bit) and timers/counters in a single cycle without worrying about rollover or carry in the middle.  Then perform operations on the data without churning cycles on 8 bit operations.  Fewer cycles = lower clock = lower power consumption.  The linear address space gets rid of banking overhead, it has a proper stack, and the TI compiler is quite good.

This may be a better choice than the PIC, and $0.41 - $0.51 reel price.

I already have CCS and have used it for many projects, I'm going to sleep on it.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf