Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 29, 2024, 06:15:41 06:15


Login with username, password and session length


Pages: [1]
Print
Author Topic: PBP snippets for use in your programs  (Read 13509 times)
0 Members and 1 Guest are viewing this topic.
pic_maniac
Newbie
*
 Warned
Offline Offline

Posts: 33

Thank You
-Given: 261
-Receive: 66


« 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
Logged
giappolo
Active Member
***
Offline Offline

Posts: 147

Thank You
-Given: 1487
-Receive: 601


« Reply #1 on: May 15, 2019, 08:26:20 20:26 »

Hi, in BASIC you must use Xor and not ^
Logged
TucoRamirez
Senior Member
****
Offline Offline

Posts: 307

Thank You
-Given: 257
-Receive: 115


Tuco ... dead or Alive


« Reply #2 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
Logged

Whoever double crosses me and leaves me alive... he understands nothing about Tuco.
PICker
Active Member
***
Offline Offline

Posts: 162

Thank You
-Given: 207
-Receive: 109


« Reply #3 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/
Logged
pic_maniac
Newbie
*
 Warned
Offline Offline

Posts: 33

Thank You
-Given: 261
-Receive: 66


« Reply #4 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
Logged
PICker
Active Member
***
Offline Offline

Posts: 162

Thank You
-Given: 207
-Receive: 109


« Reply #5 on: May 16, 2019, 08:47:56 20:47 »

you are right @pic_maniac, PBP uses the ^ operator as Bitwise Exclusive OR.
Logged
giappolo
Active Member
***
Offline Offline

Posts: 147

Thank You
-Given: 1487
-Receive: 601


« Reply #6 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
Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #7 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))
Logged
pic_maniac
Newbie
*
 Warned
Offline Offline

Posts: 33

Thank You
-Given: 261
-Receive: 66


« Reply #8 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
Logged
giappolo
Active Member
***
Offline Offline

Posts: 147

Thank You
-Given: 1487
-Receive: 601


« Reply #9 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
Logged
pic_maniac
Newbie
*
 Warned
Offline Offline

Posts: 33

Thank You
-Given: 261
-Receive: 66


« Reply #10 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.
Logged
pic_maniac
Newbie
*
 Warned
Offline Offline

Posts: 33

Thank You
-Given: 261
-Receive: 66


« Reply #11 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
Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #12 on: August 05, 2019, 03:44:29 03:44 »

man we are in 2019, use another compiler, people who wrote similar snippets are dead..
Logged
pic_maniac
Newbie
*
 Warned
Offline Offline

Posts: 33

Thank You
-Given: 261
-Receive: 66


« Reply #13 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...
« Last Edit: August 05, 2019, 02:15:19 14:15 by pic_maniac » Logged
TucoRamirez
Senior Member
****
Offline Offline

Posts: 307

Thank You
-Given: 257
-Receive: 115


Tuco ... dead or Alive


« Reply #14 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
Logged

Whoever double crosses me and leaves me alive... he understands nothing about Tuco.
pic_maniac
Newbie
*
 Warned
Offline Offline

Posts: 33

Thank You
-Given: 261
-Receive: 66


« Reply #15 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
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC