Comparitive speed of comparing IF

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Comparitive speed of comparing IF

Post by Francis » Sun Jan 03, 2010 2:08 pm

Is there a faster way to do a comparison than this?
The IF bit I mean.

My 2010 brain has not worked very well after New Years Eve.

Code: Select all


' X = random byte value between 10 and 255

For I=1 to 255
   If X<>0 Then
      Dec(X)
      OutPin = 1
   Else
      OutPin = 0
   EndIf
Next
It simply runs in a fixed loop. The value of X is random between 10 and 255 and whilst X>0 then the outpin is high. As soon as it hits zero the Outpin is switched off.
'Outpin' is declared elsewhere, its simply any old usable I/O pin as an ouput (e.g. portA.1)
If poss. I'd like to speed up the IF section. All variables are bytes.
Suggestions very welcome. Thanks.

rmteo
Posts: 237
Joined: Fri Feb 29, 2008 7:02 pm
Location: Colorado, USA

Post by rmteo » Sun Jan 03, 2010 3:47 pm

Try this?

Code: Select all

' X = random byte value between 10 and 255 

OutPin = 1
while X > 0
   dec(X)
wend
OutPin = 0

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Sun Jan 03, 2010 4:03 pm

Thanks. I'll give that a go on the 'scope.

Actually, sorry, I didn't explain myself very well at all.
I was trying to produce a rough single duty cycle.

The For-Next loop is approx the total cycle period and the value X is essentially the duty-On bit. Super accuracy not needed, but it 'scopes well.
I was really after a faster comparison/decrement part to reduce the total period. (If you see what I mean). I wanted to avoid timer interrupts as they are going to be used elsewhere.

I can dig out Coreldraw if I've confused the issue :)

Oh, darn my typing; I didn't mean X= range of 10 to 255, I meant 0 (zero) to 255. Oops. i.e. the 'duty' can be anywhere between 0 and 255 / 0-100%.

rmteo
Posts: 237
Joined: Fri Feb 29, 2008 7:02 pm
Location: Colorado, USA

Post by rmteo » Mon Jan 04, 2010 1:59 am

How about using one of these functions:

Code: Select all

delayms(X)
' or
delayus(X)
DelayMS

sub delayms(expression as word)
The delayms subroutine suspends program execution for up to 65535 milliseconds (ms).


DelayUS

sub delayus(expression as word)
The delayus subroutine suspends program execution for up to 65535 microseconds (μs).

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Mon Jan 04, 2010 6:32 am

Thanks for replying.

This is what I'm trying to achieve.
The For-Next Value defines the total period.
The random value X defines the Duty-On time.

Image

I realise I can reduce the For-Next Value but that will also reduce my resolution. e.g. For-Next=20 only gives me 20 X steps obv.
If I can do the IF bit quicker then that is what I'm after.

Super-dooper timing accuracy isn't required but a fast-as-possible loop speed would be ideal. So no delays wanted. And remember, in some cases (i.e. X=Zero) the output will be off for the whole period.
It does it fairly quickly as it is but if there is a simple way to improve it then that would be really good.

rmteo
Posts: 237
Joined: Fri Feb 29, 2008 7:02 pm
Location: Colorado, USA

Post by rmteo » Mon Jan 04, 2010 3:10 pm

I don't understand. Executing a for-next loop is no different from a delay. Both delay functions allow up to 16-bit values - way more than the 8-bit that you need.

In any event, the best way to do this would be to use the COMPARE module. If you need a continuous train of pulses, then use the PWM module.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Mon Jan 04, 2010 4:17 pm

True-ish.
The For-Next loop gives an overall period.
The value X specifies the Duty-ON time within that period.

I'm using the PWM module for something else.
I hadn't considered the Compare module.
It certainly looks like I can use Compare and PWM independently.
Good idea. Thanks.

rmteo
Posts: 237
Joined: Fri Feb 29, 2008 7:02 pm
Location: Colorado, USA

Post by rmteo » Tue Jan 05, 2010 5:09 pm

Which PIC18 are you using? A lot of them have 2 PWM modules.

Francis
Registered User
Registered User
Posts: 314
Joined: Sun Mar 25, 2007 9:40 am
Location: Devon

Post by Francis » Wed Jan 06, 2010 7:30 am

18FK20 . I've nearly got it sussed now.

Post Reply