Sonsivri

Electronics => Pic Basic Languages => Topic started by: pic_maniac on May 15, 2019, 07:03:17 19:03



Title: PBP snippets for use in your programs
Post by: pic_maniac on May 15, 2019, 07:03:17 19:03
A quicker way to exchange variables without temp variable:

Code:
a=a^b
b=a^b
a=a^b

This code exchanges the variables a and b without use of third temp variable! Just magic!

pic_maniac


Title: Re: PBP snippets for use in your programs
Post by: giappolo on May 15, 2019, 08:26:20 20:26
Hi, in BASIC you must use Xor and not ^


Title: Re: PBP snippets for use in your programs
Post by: TucoRamirez on May 16, 2019, 08:58:03 08:58
Hi, in BASIC you must use Xor and not ^

I suppose :  http://melabs.com/resources/articles/boolean.htm (http://melabs.com/resources/articles/boolean.htm)


Title: Re: PBP snippets for use in your programs
Post by: PICker on May 16, 2019, 09:07:58 09:07
I agree, ^ is the Xor operator in C/C++.
you can use several methods for obtaining the same result... with same problems...
https://www.geeksforgeeks.org/swap-two-numbers-without-using-temporary-variable/


Title: Re: PBP snippets for use in your programs
Post by: pic_maniac on May 16, 2019, 06:56:05 18:56
1. As the Subject tilte implies, this is an example for PBP, that is Pic Basic Pro lanquage. Not C, C++ or other Basic.
2. I do not understand why one should swap x with x. Sure iti does not work, but this makes no sense either.
3. Other methods need too much MCU time, like multiplication or division.

Thanks
pic_maniac


Title: Re: PBP snippets for use in your programs
Post by: PICker on May 16, 2019, 08:47:56 20:47
you are right @pic_maniac, PBP uses the ^ operator as Bitwise Exclusive OR.


Title: Re: PBP snippets for use in your programs
Post by: giappolo on May 18, 2019, 03:50:17 03:50
It's easy how it working
At the first a=avalue and b=bvalue then
a=avalue ^ bvalue then
b=(avalue ^ bvalue) ^ bvalue, being (bvalue ^ bvalue) = 0  it follows that b=avalue ^ 0  that is b=avalue then
a= (avalue ^ bvalue) ^ avalue, being (avalue ^ avalue) = 0 it follows that a= bvalue ^ 0 that is a=bvalue


Title: Re: PBP snippets for use in your programs
Post by: metal on May 18, 2019, 01:19:06 13:19
Code:
#! /usr/bin/python3

from operator import xor

a = int('00001111',  2)
b = int('11110000',  2)

print("a=%s" % bin(a)[2:].zfill(8))
print("b=%s" % bin(b)[2:].zfill(8))

a = xor(a,b)
b = xor(a,b)
a = xor(a,b)

print("a=%s" % bin(a)[2:].zfill(8))
print("b=%s" % bin(b)[2:].zfill(8))


Title: PBP snippets for use in your programs-ADC Filter
Post by: pic_maniac on May 18, 2019, 07:04:10 19:04
Well, my intention was not to start a flame on this swap thing but to give ideas of snippets for use in PBP language environment (and hopefully others too with minor mod's).

OK, on with next useful code.

An ADC filter sub routine to Low Pass your data if they are noisy.

Code:
filter CON 16 'A constant to vary the speed of the filter (255 = no effect, 1 = Max filtering)
temp VAR WORD 'temporary variable
adc_value var byte 'the raw value from adc converter in 8-bit
out_value var byte 'the filtered output value

low_pass: '------------------- Low Pass Filter subroutine --------------------------
temp = temp - (temp */ filter) + adc_value
out_value = temp */ filter 'returned value that is filtered
RETURN

In the attached EXCEL file you can play with the filter constant and see how the out_value is affected and in what speed.

Also you can insert a disturbance while the filter output is stable for a few cycles and see how this changes the output.

pic_maniac


Title: Re: PBP snippets for use in your programs
Post by: giappolo on May 18, 2019, 09:35:51 21:35
What's flame? I am agreed with you
I knew my mistake and I dimostrated that
At first
a=avalue
b=bvalue

At end
a=bvalue
b=avalue

that's the switch


Title: Re: PBP snippets for use in your programs
Post by: pic_maniac on July 01, 2019, 09:53:05 21:53
Many times a random generator is needed but also a defined range too.

Say you need a range of 1-6 for an electronic dice.

In PicBasic this is done like this:

Code:
random my_variable
my_value=(word_variable//5)+1

my_value can be byte size while my_variable should be word size.

The random function is not really random and if you reset PIC, every time will start with exactly the same sequence, so you can predict the numbers coming.


Title: PBP snippets for use in your programs - */ οperator
Post by: pic_maniac on August 04, 2019, 03:54:40 15:54
There are times that a floating point multiplication (or division) is needed.

Problem: PBP does not support floating point.

Solution: Use */ operator

Example:

You want to display a value that comes from a 10-bit ADC (0-1023) and should been multiplied by a factor of 2.4438 (0-2500). Normally this cannot be calculated in plain integer math.

The */ operator does a multiplication by x and division by 256.

So, if our number is 2.4438 then the x is 2.4438*256 or 625.6128. Rounding to the closest integer 626.

Then in our adc_value we do this: adc_value */626 which corresponds to  adc_value*626/256.

If our adc_value is 1023, then 1023*626/256=2501, close enough.

pic_maniac


Title: Re: PBP snippets for use in your programs
Post by: metal on August 05, 2019, 03:44:29 03:44
man we are in 2019, use another compiler, people who wrote similar snippets are dead..


Title: Re: PBP snippets for use in your programs
Post by: pic_maniac on August 05, 2019, 02:12:25 14:12
  • Like which one?
  • No one is forced to use this one
  • Comparing the hex code produced is very compact and sometimes better than C compilers for the "same" program
  • OK, I am not that young, but not dead yet!
  • I can stop this posting any time if I am annoying...


Title: Re: PBP snippets for use in your programs
Post by: TucoRamirez on August 05, 2019, 06:11:35 18:11
There are times that a floating point multiplication (or division) is needed.

Problem: PBP does not support floating point.

http://melabs.com/resources/fp.htm


Title: Re: PBP snippets for use in your programs
Post by: pic_maniac on August 06, 2019, 03:26:55 15:26
But, this is not a generic support.

Also, please do compare the code produced by a single */ to the included subroutines of the fp libraries.

pic_maniac