Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 16, 2019, 06:23:30 18:23


Login with username, password and session length


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

Posts: 15

Thank You
-Given: 67
-Receive: 9


« 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: 125

Thank You
-Given: 533
-Receive: 502


« 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: 296

Thank You
-Given: 230
-Receive: 107


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
***
Online Online

Posts: 146

Thank You
-Given: 149
-Receive: 91


« 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: 15

Thank You
-Given: 67
-Receive: 9


« 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
***
Online Online

Posts: 146

Thank You
-Given: 149
-Receive: 91


« 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: 125

Thank You
-Given: 533
-Receive: 502


« 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: 2230

Thank You
-Given: 699
-Receive: 570


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: 15

Thank You
-Given: 67
-Receive: 9


« 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: 125

Thank You
-Given: 533
-Receive: 502


« 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: 15

Thank You
-Given: 67
-Receive: 9


« 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
pic_maniac
Newbie
*
Offline Offline

Posts: 15

Thank You
-Given: 67
-Receive: 9


« Reply #11 on: August 04, 2019, 09:54:40 09: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: 2230

Thank You
-Given: 699
-Receive: 570


Top Topic Starter


« Reply #12 on: August 04, 2019, 09:44:29 21:44 »

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

Posts: 15

Thank You
-Given: 67
-Receive: 9


« Reply #13 on: August 05, 2019, 08:12:25 08: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, 08:15:19 08:15 by pic_maniac » Logged
TucoRamirez
Senior Member
****
Offline Offline

Posts: 296

Thank You
-Given: 230
-Receive: 107


Tuco ... dead or Alive


« Reply #14 on: August 05, 2019, 12:11:35 12: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
*
Offline Offline

Posts: 15

Thank You
-Given: 67
-Receive: 9


« Reply #15 on: August 06, 2019, 09:26:55 09: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