writing efficient code PIC microcontrollers

Status
Not open for further replies.

Thread Starter

gehan_s

Joined Sep 17, 2012
38
Dear all,

Lately I have been working with some projects where timing is critical and hence have decided to learn how to write more efficient code.

Can you direct me some where or give some pointers regarding things like multiplication, division and ANY other ways of making my code run faster. I am using MikroC pr for PIC.

Best regards
 

takao21203

Joined Apr 28, 2012
3,702
Dear all,

Lately I have been working with some projects where timing is critical and hence have decided to learn how to write more efficient code.

Can you direct me some where or give some pointers regarding things like multiplication, division and ANY other ways of making my code run faster. I am using MikroC pr for PIC.

Best regards
Use a PIC with hardware multiplier.
Optimize the source codes with the help of examining the disassembly, you'd wonder what some C lines produce compared to others.

Use precomputation by the means of tables, if the computation result is not depending on some varying input.

You can often consider that the actual result of a calculation or part of it will only be within a limited number of results. Even if it is some 1000s of ressults, it can be faster to calculate them once and pack into FLASH or external serial memory.

Learn optimization techniques such as loop unrolling.
 

atferrari

Joined Jan 6, 2004
4,770
Why not to have an algorithm shown to see how it could be implemented?

Discussing in abstract brings you nowhere. Clock and required timing period would help to decide.
 

Polymorphism

Joined Aug 10, 2014
30
Guilt by association.

@ the OP, you can write good efficient code without having to use assembly. It just takes practice.
Assembly you need an IQ of 150+ to make an complex algorithm.

That's why they made C (to keep you from going insane)

Mikro C is an decade long project. So it is very mature.

I have done some pretty lengthy stuff in Mikro C.

It works.
 

Sensacell

Joined Jun 19, 2012
3,447
I just love assembly, I dunno, maybe it's a masochistic thing? There is something about being in 'total control of every stinking bit' that makes me feel good.
 
IQ is flawed anyway.

Lots of people with high IQs are a few short of an six pack.

When I buy an six pack of beer I expect six bottles, not four and a half!
 

ErnieM

Joined Apr 24, 2011
8,377
joeyd you are a very special case and someone I have nothing but respect for. I'd peg your IQ at "super genious" (the same as Wiley E. Coyote :D) .

I just don't think assembler is the best choice for someone who may not do this stuff day to day. I once worked exclusively in assembler, back in the bad old days when compilers were very expensive. I was quite happy when first inexpensive then free compilers became available and have never looked back (with minor exceptions).

Using a better algorithm or arranging tasks properly is 90% of the battle. I am content with leaving that last 10% on the table for guys like you to optimize.
 

mitko89

Joined Sep 20, 2012
127
1. Microchip/AVR have library functions that perfor most of the maths (implemented in inline assembly);
2. Get uC with hardware arithemtic modules;
3. Optimize your code by using more bit operations instead of whole expressions;
4. Use more efficient algoriths;
5. Use comilers with higher optimization;
 
Status
Not open for further replies.
Top