Learner PIC

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Jayadev
Posts: 17
Joined: Fri Jan 30, 2015 12:06 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Learner PIC

Postby Jayadev » Fri Feb 19, 2016 2:35 pm

Thanks again Medelec,

I missed the " " quotes hence it wasn't working,I thought it was only needed in C.


DisplayVolt = ADC_Value * 2;
volt[0] = DisplayVolt/1000 + 48;
volt[1] = (DisplayVolt/100)%10 + 48;
volt[3] = (DisplayVolt/10)%10 + 48;


What does the Percent sign mean,From internet I realize it is modulus which means the result is the final remainder,But what exactly is happening with above calculation.
It is a code from a volt meter.

Jayadev
Posts: 17
Joined: Fri Jan 30, 2015 12:06 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Learner PIC

Postby Jayadev » Fri Feb 26, 2016 3:53 pm

How do we send MSB or LSB of a Data first.
How is this done through Flowcode?

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8825
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2561 times
Been thanked: 3719 times
Contact:

Re: Learner PIC

Postby medelec35 » Fri Feb 26, 2016 9:47 pm

Jayadev wrote:Thanks again Medelec

No problem.
Jayadev wrote:What does the Percent sign mean,From internet I realize it is modulus which means the result is the final remainder,But what exactly is happening with above calculation.
It is a code from a volt meter.

the % as you state is the modulus.
if you have a variable called DisplayVolt = 1234 then

Code: Select all

volt[0] = (DisplayVolt / 1000) % 10  = 1
volt[1] = (DisplayVolt / 100) % 10 = 2
volt[2] = (DisplayVolt / 10) % =3
Volt[3] = DisplayVolt % 10 = 4

In effect max digit value = %value - 1 = 10 - 1 = 9
So the mod is separating th, h,t,u
The +48 is convting to an ascii number.
if we take result of 2 then + 48 = 50.
If you look up 50 on an ASCII table you will see its = 2

So the calculation

Code: Select all

DisplayVolt = ADC_Value * 2;
volt[0] = DisplayVolt/1000 + 48;
volt[1] = (DisplayVolt/100)%10 + 48;
volt[3] = (DisplayVolt/10)%10 + 48;

Since ; and +48 is used for each line then you are looking a C code rather than a flowchart.
You will not require + 48 if using LCD in flowcode.

Jayadev wrote:How do we send MSB or LSB of a Data first.
How is this done through Flowcode?

For what reason?
Can you give more details on why you need to do this?
These users thanked the author medelec35 for the post (total 2):
Jayadev (Sat Feb 27, 2016 10:42 am) • Benj (Mon Feb 29, 2016 12:06 pm)
Rating: 10%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

User avatar
Benj
Matrix Staff
Posts: 14794
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4755 times
Been thanked: 4361 times
Contact:

Re: Learner PIC

Postby Benj » Mon Feb 29, 2016 12:09 pm

Further to Martin's post.

What does the Percent sign mean,From internet I realize it is modulus which means the result is the final remainder,But what exactly is happening with above calculation.


If you take this.

Result = 123 % 10

Then the Result is 3.

What is actually happening is the following.

TempResult1 = 123 / 10 = 12
TempResult2 = TempResult1 * 10 = 120
Result = 123 - TempResult2 = 3

So the modulus or % is actually quite a complicated process involving divides, multiplications and subtractions. Therefore use it sparingly :D

Note that the compiler may have a more optimised way of doing the maths but it's still a fairly complicated calculation.
These users thanked the author Benj for the post (total 2):
medelec35 (Mon Feb 29, 2016 12:11 pm) • Jayadev (Mon Feb 29, 2016 3:54 pm)
Rating: 10%
 

Jayadev
Posts: 17
Joined: Fri Jan 30, 2015 12:06 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Learner PIC

Postby Jayadev » Mon Feb 29, 2016 4:31 pm

Hi Medelec,

Jayadev wrote:
How do we send MSB or LSB of a Data first.
How is this done through Flowcode?

For what reason?
Can you give more details on why you need to do this?


I have noticed in some Datasheets, the i2c communication some Manufacturers specifically stating LSB or MSB to be sent first .

If you take this.

Result = 123 % 10

Then the Result is 3
What is actually happening is the following.

TempResult1 = 123 / 10 = 12
TempResult2 = TempResult1 * 10 = 120
Result = 123 - TempResult2 = 3


Hi Benj,

I am little bit confused again.
The above example is about how compiler calculate the 123%10 or is it a general example as you stated Tempresult1 and Tempresult2.

I am trying to understand Modulus .I downloaded Modulus calculator App in my phone to try it out.

3%10=3
4%10=4
45%10 = 5
450%10=0
4500%10=0
4523%10=3

Apparently ,It is easiest way to extract the unit base digit from a number.That is what I understood.

volt[0] = DisplayVolt/1000 + 48;
volt[1] = (DisplayVolt/100)%10 + 48;


What happens if Displayvolt = 800.

Means, 0.8+48 = ?

------------------
How does the Compiler recognise the Number as ASCII is it by Default or is there a pointer?

User avatar
Benj
Matrix Staff
Posts: 14794
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4755 times
Been thanked: 4361 times
Contact:

Re: Learner PIC

Postby Benj » Mon Feb 29, 2016 6:18 pm

Hello,

You can think of modulus as counting to the number and then resetting to 0 when it reaches it.

so 15 % 10 we count up from 0 until we reach 10, at that point we reset our counter to 0 again and continue counting. The end result being 5.

0 = 0
1 = 1
2 = 2
3 = 3
4 = 4
5 = 5
6 = 6
7 = 7
8 = 8
9 = 9
10 = 0
11 = 1
12 = 2
13 = 3
14 = 4
15 = 5
Result = 5

On the other hand 15 % 2 = 1 because 15 is an odd number.

0 = 0
1 = 1
2 = 0
3 = 1
...

7 % 3 = 1 because we reset the counter to 0 every time the counter reaches 3.

0 = 0
1 = 1
2 = 2
3 = 0
4 = 1
5 = 2
6 = 0
7 = 1

Code: Select all

The above example is about how compiler calculate the 123%10 or is it a general example as you stated Tempresult1 and Tempresult2.


The compiler does all the heavy lifting for you behind the scenes, I was just highlighting what it does for you and hence why the % or MOD function is sometimes frowned upon on time critical applications.

For example 16 % 2 is far less efficient then doing 16 & 1 but both produce the same result. Of course not all calculations can be reduced to simple binary AND functions.

Here are some examples that do work and will be a lot more efficient.
x % 2 is the same as x & 1
x % 4 is the same as x & 3
x % 8 is the same as x & 7
x % 16 is the same as x & 15


On a microcontroller real numbers (numbers with a decimal point) are generally special case and have to be done using special Floating point variables.

800 / 1000 in integer maths is 0, 0 + 48 = '0' or 48 e.g. the ASCII value for 0.

http://www.asciitable.com/

Having a single ASCII character between single quotes like that is the same as writing the decimal number. e.g. '1' = 49.

How does the Compiler recognise the Number as ASCII is it by Default or is there a pointer?


To the compiler a number is a number there is no difference. '0' = 48 = 0x30 = 0b00110000. They are all treated in exactly the same way. 0x stands for hexadecimal and 0b stands for binary.

To prove this try passing the different values above to the PrintASCII LCD function. They should all end up showing you an ASCII '0'.


I have noticed in some Datasheets, the i2c communication some Manufacturers specifically stating LSB or MSB to be sent first .


A INT type variable is made up of two bytes, a high byte and a low byte. To split the INT into bytes you would do this.

MSB = INT >> 8
LSB = INT

You can then choose which order to send the bytes.

To recombine the bytes into an INT you would do something like this.

INT = (MSB << 8 ) + LSB

More info and worked examples on this here.
http://www.matrixtsl.com/article.php?a=366


Lots of complicated stuff there, took me a while to grasp it all :D
These users thanked the author Benj for the post (total 3):
Jayadev (Mon Feb 29, 2016 10:49 pm) • medelec35 (Tue Mar 01, 2016 9:02 am) • Steve001 (Sat Apr 23, 2016 9:13 pm)
Rating: 15%
 

Jayadev
Posts: 17
Joined: Fri Jan 30, 2015 12:06 pm
Has thanked: 15 times
Been thanked: 1 time
Contact:

Re: Learner PIC

Postby Jayadev » Tue Mar 01, 2016 8:18 am

Hi Benj,

Thank you so Much ,That was highly informative and helpful.Finally I understood Modulus.

Rgds
These users thanked the author Jayadev for the post:
Benj (Tue Mar 01, 2016 10:46 am)
Rating: 5%