Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 22, 2019, 10:20:51 22:20


Login with username, password and session length


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

Posts: 7

Thank You
-Given: 10
-Receive: 4


« on: May 15, 2019, 01:03:17 13: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: 104

Thank You
-Given: 469
-Receive: 377


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

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

Posts: 289

Thank You
-Given: 227
-Receive: 106


Tuco ... dead or Alive


« Reply #2 on: May 16, 2019, 02:58:03 02: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: 141

Thank You
-Given: 135
-Receive: 86


« Reply #3 on: May 16, 2019, 03:07:58 03: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
*
Offline Offline

Posts: 7

Thank You
-Given: 10
-Receive: 4


« Reply #4 on: May 16, 2019, 12:56:05 12: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: 141

Thank You
-Given: 135
-Receive: 86


« Reply #5 on: May 16, 2019, 02:47:56 14:47 »

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

Posts: 104

Thank You
-Given: 469
-Receive: 377


« Reply #6 on: May 17, 2019, 09:50:17 21: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: 2212

Thank You
-Given: 673
-Receive: 567


Top Topic Starter


« Reply #7 on: May 18, 2019, 07:19:06 07: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
*
Offline Offline

Posts: 7

Thank You
-Given: 10
-Receive: 4


« Reply #8 on: May 18, 2019, 01:04:10 13: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: 104

Thank You
-Given: 469
-Receive: 377


« Reply #9 on: May 18, 2019, 03:35:51 15: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
*
Offline Offline

Posts: 7

Thank You
-Given: 10
-Receive: 4


« Reply #10 on: July 01, 2019, 03:53:05 15: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
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