Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

writing efficient code - PIC microcontrollers

Status
Not open for further replies.

gehan_s

Member
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 fast. I am using MikroC pr for PIC.

Best regards
 
It's hard to tell without knowing your level of experience. PIC16 doesn't have neither multiplication nor division, both need to be simulated. PIC18 has multiplication, but no division. PIC24 have both, but division is still much slower than multiplication. Which PIC do you use?

If you post your code, it'll be easy to tell if you can speed it up somewhat.
 
Time critical is very specific to the application.. Microcontrollers today have many timers/counters that you can use to execute your timely actions. Don't worry too much about the code. Think about solving the problem. Usually well structured code is better than obscured code.
 
When using mathematics in embedded systems, you need to think about precision... What can you get away with..
There is a compromise, there always is.... I use fixed point maths and as I only need 2 decimal places speed isn't really an issue... All my trig is held in small lookup tables so I can get my two decimal place precision without having size or speed penalties... I can execute a cosine rule calculation quicker than the software floating point unit can calculate division...

If we knew what you were needing the speed for we could help you optimize your code, and you could see what needs to be done..

Optimization is an art form... That's why people who make compilers can charge so much for them...
 
If you're going for timing critical and writing tight code I would look into learning assembly first off. If assembly is already your preferred language then you're a step ahead of the game.

Use loops and counter loops wherever possible. Make all blocks of code that will be executed repeatedly into subroutines.

PIC16F can add & subtract. They can also multiply and divide by 2 with a left or a right shift respectively (rlf and rrf instructions).

As you get used to assembly and your micro's instruction set, you will naturally figure out ways of making it tight and efficient.
 
If you're going for timing critical and writing tight code I would look into learning assembly first off.

Writing assembly is the last thing to do. Learn your hardware and how to get the most out of it. Be creative. If assembly is "the thing" then we would still be using some pic 16f84 controllers. Today microcontrollers are designed for more advanced tools.
 
Last edited:
Optimize your UC's peripherals, espcially look at the enhanced midrange chips like a 16F1826 that offers hardware ECCP event triggering etc.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top