writing efficient code PIC microcontrollers

Status
Not open for further replies.

jpanhalt

Joined Jan 18, 2008
11,087
I am not sure exactly what you are doing, since I don't do C, but here is come code to measure both high and low periods to determine duty cycle with a 16-bit counter:
Rich (BB code):
_Data                         ;TMR1 runs continuously                           |B0
     
     bcf       PIR1,CCP1IF    ;flag must be cleared after mode change           |B0
     btfss     PIR1,CCP1IF    ;test CCP1 interrupt flag (interrupt not enabled)
     goto      $-1
     movf      CCPR1H,w       ;start time high byte
     movwf     CCP_T1H        ;save value in shadow register
     movf      CCPR1L,w       ;start time low byte
     movwf     CCP_T1L        ;save value in shadow register
     bcf       CCP1CON,0      ;change interrupt flag to falling edge
     bcf       PIR1,CCP1IF    ;clear flag
     btfss     PIR1,CCP1IF    ;test CCP1 falling edge interrupt flag
     goto      $-1
     movf      CCPR1H,w       ;stop time high byte
     movwf     CCP_T2H        ;save value in shadow register
The measured frequency is 100 Hz. There is, of course, a wait for the IRQ flag, does your code do that? If not, how does it know when to start?

In this comparison, let's be sure to have equivalent starting and ending points.

John
 

jpanhalt

Joined Jan 18, 2008
11,087
What level of chip is it written for? Presumably, your C must use the same instruction set (plus Assembly instructions) as our Assembly code.

You posted in a language I can't read. So, in plain English, French, or German, what exactly does your posted code do?

John
 
What level of chip is it written for? Presumably, your C must use the same instruction set (plus Assembly instructions) as our Assembly code.

You posted in a language I can't read. So, in plain English, French, or German, what exactly does your posted code do?

John
It is not platform specific. That's the beauty of C.
 

NorthGuy

Joined Jun 28, 2014
611
Rich (BB code):
If (alpha == 1)
{
   delta += 1;
}
What would this look like in assembly?
Your point must be that C generates exactly the same code.

Rich (BB code):
decf alpha, w
btfsc STATUS,2 ; skip if not zero
incf delta, f
For comparison

Rich (BB code):
If (alpha != 1)
{
   delta += 1;
}
Rich (BB code):
decfsz alpha, w
incf delta, f
Or if we increase complexity:

Rich (BB code):
If (alpha == 1)
{
   delta += 1;
}
else
{
  delta -= 1;
}
Rich (BB code):
decfsz alpha, w
movlw 0xfe
incf WREG
addwf delta, f
 

takao21203

Joined Apr 28, 2012
3,702
Efficient these days translates to sell more + save time- using assembler will mean sell less and spend more time.

Show me a mobile phone or tablet device or modern laptop where any piece of the software has anything to do with assembler.

Assembler was dead already 15 years ago, mostly PIC users resurrect it all the time with the assistance of old tutorial homepages, not unlike a virus, they also self replicate.

All this 16f84 and 16f628 stuff is somewhat history.

But, google 16f84, and google 16F1824 or 16F1709 and look how many images. You'll see.

And google 16f54 too.
 
PIC 16F628a is dead?

Oh please don't say that! I love that micro and they're only a dollar now on eBay. (I have 100 pcs)

Mikro C goes down well with the 16F628a. You can snap up solutions for many projects in minutes.
 
Well, run these 3 examples through the C compiler, post what it does, and then we can compare to human-written assembler code and draw conclusions.
I would, but all I have is an smart phone for the internet.

Don't get me wrong. I do believe that an TOP assembly programmer would beat the compiler. But in general, for most of us, the C compiler will win.
 

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
OP wrote the above.

Quote: I am using MikroC pr for PIC.
quote: and hence have decided to learn how to write more efficient code.
 
Yeah, it really was!

You asked for assembly code so that you could "argue a point" then you posted a conclusion without argument (premises and proof) and you say you don't have the tools to argue the point. Isn't that called "trolling"?
Hey look I do know how to write an argument at University level but where is the ******* money to do it?
 
That excuse can't possibly be valid.

How much money are you making doing your "thing" here?

John
I haven't made a dollar since 2005.

I am not armed enough on this smart phone to be able to write an paper unless I go pen to paper to computer then to smart phone.

It would take me forever.
 
Status
Not open for further replies.
Top