writing efficient code PIC microcontrollers

Discussion in 'General Electronics Chat' started by gehan_s, Aug 9, 2014.

Thread Status:
Not open for further replies.
  1. gehan_s

    Thread Starter Member

    Sep 17, 2012
    38
    0
    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
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,387
    497
    Learn Assembly for PIC.
     
    vinnnie and MaxHeadRoom like this.
  3. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    +1 ;)
    Max.
     
  4. Polymorphism

    New Member

    Aug 10, 2014
    30
    2
    Oh rubbish.

    Mikro C is an beautiful language.

    Post some of your code and people will advise.
     
  5. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    Didn't say it wasn't:p
    And I didn't originate the comment, just endorsed the answer.
    Max.
     
  6. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    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.
     
  7. Brownout

    Well-Known Member

    Jan 10, 2012
    2,375
    998
    Guilt by association.

    @ the OP, you can write good efficient code without having to use assembly. It just takes practice.
     
  8. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,648
    763
    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.
     
  9. Polymorphism

    New Member

    Aug 10, 2014
    30
    2
    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.
     
  10. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,678
    2,737
    I write entire applications in .asm, including complex algorithms and IEEE floating point math. My last piece of code was 12,527 lines long.

    What is my IQ?
     
    Eric007 likes this.
  11. Polymorphism

    New Member

    Aug 10, 2014
    30
    2
    30-years experience and you formally studied it?
     
  12. Polymorphism

    New Member

    Aug 10, 2014
    30
    2
    Could be 166. I dunno mate.
     
  13. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Well if it was just a BIT higher...

    You would have used C for that. ;)

    :D:D:D
     
  14. Sensacell

    Well-Known Member

    Jun 19, 2012
    1,130
    266
    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.
     
  15. Polymorphism

    New Member

    Aug 10, 2014
    30
    2
    I used to know a bloke that did some assembly for the PC.

    He wasn't all there.
     
  16. Polymorphism

    New Member

    Aug 10, 2014
    30
    2
    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!
     
  17. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,388
    1,605
    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.
     
  18. mitko89

    Member

    Sep 20, 2012
    123
    19
    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;
     
  19. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,678
    2,737
    I appreciate the nice comments, Ernie. But 10% ain't enough:

    Code ( (Unknown Language)):
    1.  
    2. Program Memory Bytes Used: 16274
    3. Program Memory Bytes Free:   102
    4.  
    :D
     
  20. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    Here we go again...... the monthly slanging match. Assy Vs C :rolleyes:
    Max.
     
Loading...
Thread Status:
Not open for further replies.