Is this enough uC for this task? - Page 1
Forum section: electronics / projects

author: Jester
author_rank: Frequent Contributor
timestamp: August 17, 2018, 04:33:38 pm
content: 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 IRQ2) Set the PWM level based on the measured current3) Momentary latched on/offMotor 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.

author: ejeffrey
author_rank: Super Contributor
timestamp: August 17, 2018, 05:41:38 pm
content: Quote from: Jester on August 17, 2018, 04:33:38 pmYes, any microcontroller can do this task.

author: SiliconWizard
author_rank: Super Contributor
timestamp: August 17, 2018, 05:43:58 pm
content: 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.

author: Jester
author_rank: Frequent Contributor
timestamp: August 17, 2018, 06:15:21 pm
content: Quote from: SiliconWizard on August 17, 2018, 05:43:58 pmC languagealgorithm can be really simple, switch statement with desired PWM levels for measured current, perhaps 5 levels at mostQuote from: ejeffrey on August 17, 2018, 05:41:38 pmConsidered 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.

author: retrolefty
author_rank: Super Contributor
timestamp: August 17, 2018, 07:14:04 pm
content: A cheap $2 asian arduino pro-mini board would be more than enough and you could not build cheaper then that.

author: nctnico
author_rank: Super Contributor
timestamp: August 17, 2018, 07:24:27 pm
content: 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.

author: Jester
author_rank: Frequent Contributor
timestamp: August 17, 2018, 08:14:12 pm
content: Quote from: retrolefty on August 17, 2018, 07:14:04 pmTrue 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.Quote from: nctnico on August 17, 2018, 07:24:27 pmBudget 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%20at $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.

author: jaromir
author_rank: Unknown
timestamp: August 17, 2018, 11:46:17 pm
content: Quote from: Jester on August 17, 2018, 08:14:12 pmMPLABX + XC8 compiler suitable also for commercial applications costs $0 and is available onhttp://www.microchip.com/mplab/mplab-x-ideYou may use that devkit as a programmer (if you bring out needed GND/VDD/MCLR/PGC/PDG pins to your target board).

author: SiliconWizard
author_rank: Super Contributor
timestamp: August 18, 2018, 12:53:23 am
content: Quote from: Jester on August 17, 2018, 06:15:21 pmShould 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.

author: Rerouter
author_rank: Super Contributor
timestamp: August 18, 2018, 01:39:33 am
content: 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 planningAll 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 TotalFinally 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.

author: nctnico
author_rank: Super Contributor
timestamp: August 18, 2018, 08:21:49 am
content: Quote from: Jester on August 17, 2018, 08:14:12 pmLast 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.

author: Rerouter
author_rank: Super Contributor
timestamp: August 18, 2018, 08:58:23 am
content: To clarify, I did not mean he would get more resolution, he said he wanted averagingSo 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,

author: Siwastaja
author_rank: Super Contributor
timestamp: August 18, 2018, 09:27:34 am
content: 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.

author: Mechatrommer
author_rank: Super Contributor
timestamp: August 18, 2018, 09:55:06 am
content: Quote from: Jester on August 17, 2018, 04:33:38 pmthe 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.

author: Jester
author_rank: Frequent Contributor
timestamp: August 18, 2018, 10:47:30 pm
content: Quote from: Rerouter on August 18, 2018, 01:39:33 amThanks 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.

author: bson
author_rank: Unknown
timestamp: August 18, 2018, 11:18:50 pm
content: Quote from: nctnico on August 18, 2018, 08:21:49 am^ thisThe 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.

author: Jester
author_rank: Frequent Contributor
timestamp: August 19, 2018, 01:09:40 am
content: Quote from: bson on August 18, 2018, 11:18:50 pmThis 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.