# MPU support in MiscEl

 Back to MiscEl main page Write a message to author

MiscEl contains lots of different calculations and some of them are very usefull when working with microprocessors. This page will high light these functions and possibilities.

### Doing integer math

MiscEl has a special page for help optimizing Integer math. This page can help converting floating point expressions to integer expressions, without loosing much precision. This function is called "Real to Mult/Div", it basically takes a floating point multiplication and tries to make the best match with integers. While doing this it also keeps track of allowable range in different word sizes. Four different approaches are possible: Multiply and divide with any integer, restricting multiply or divide to 2^n (i.e. shift opretation) or use the upper word from the multiplier.

Examples:

To multiply with PI, select the page Integer math function "Real to Mult/Div", then write PI in the value field:
355/113 is within 8.49e-8 of the correct value. When using 16 bit operations the input range is 0..366 (with signed value: -183 to 183). I.e. to multiply with rougly PI your can multiply with 355 and divide with 113 and then your do not need to include a floating point package in your code (At least for this operation).

To multiply with PI and keep high performance, select the page Integer math function "Real to Mult/Div with x/2^n", then write PI in the value field:
201/64 is within 308ppm of the correct value. When using 16 bit operations the input range is 0..326 (with signed value: -163 to 163). I.e. to multiply with rougly PI your can multiply with 201 and shift the result 6 bit right

To divide a value with (rougly) 23.5, select the page Integer math function "Real to Mult/Div with x/2^wordsize", then write 1/23.5 in the value field:
Result on 8 bit MPU: Multiply by 11 and use the upper word of the result.
Result on 16 bit MPU: Multiply by 2789 and use the upper word of the result.

If your just need to known how many bits are required for a number, this page also has the function for that, this is called "Bit size".
Getting from number of bits to possible contents in these bits, check "Word size".

### Converting from ADC values to calibrated values or how to linearizing a sensor readout

Curve fit are very usefull for this kind of jobs. It can generate formulas or tables that will convert values from a ADC to desired range. For this to work follow these steps:
2. Fill these values into the curve fit input table.*
3. Select the fit algorithm that gives the best curve.
4. Save a table or formula from the page
5. Paste the table or formula into your program
* For some thermo sensors these steps has been integrated into MiscEl, your just need to enter the sensor data and select the range. MiscEl will then generate the data.

### Simplifying complex formula

If your have a very complex function with one parameter, it can be converted to a table with the Curve fit function. Here is an example of generating a sinus table with 100 steps:
1. In Calculator select "Curve/Table setup" and enter in X: "t*100/2/pi" and in Y1: "sin(t)*127"
2. Fill in the "Table" parameters Start: "0", End: "2*pi", Steps: "101" and press "Fill"
3. In the table, right click and select "Table to clibboard X,Y"
4. Goto Curve fit and paste the table into the data input
5. Delete first line (The one with "X Y1")
6. Select Fit format: "Spline", Data format: "x <tab> f(x)" and Fit steps: "100"
7. Select Save format: "Y (uint8, ofs)" (This will offset all negative values) and press "Save"
Now the clipboard containts a sinus table that can be copied directly to a 8 bit DAC. The last line in the table is redundant and can be removed.

### Setting up timers

On microprocessors with only 8 bit timers, it can sometimes be difficult to fit multiple real time tasks together. The MPU Timers page tries to do this. Your enter the clock frequence and the desired task schedules and MiscEl tries to combines all schedules to the best timer and counter settings.

### Serial communication (Software UART)

When building software UARTS it can be very usefull to have exact curves to look at, the page Serial communication can generate these curves. All standard baudrates and communication settings are supported.
It is possible to enter any data and get listing of the exact timing for each bit.

### Remote controls

MiscEl has a database of different remote control and what curves they generates. This includes exact timing and the possibility to generate curves for any data in the communication frame. See Serial communication for this.

### CRC

When communicating between computers and when storing data a crc is often used to verify that the data has not changed. But crc are not always calculated the same way, different parameters (Polynomial, word size, initial value, reflection) are used and this can make it difficult to get the correct crc.
MiscEl can help with this, the CRC and checksum page can be used to analyze data for what crc parameters are used or it can calculate a crc for some data, making it possible to verify your crc calculations.
When using table driven crc routines, MiscEl can also generate the tables.
The help page CRC calculations show code for all the different kind of crc algorithms that are used.

### Calculations and conversions

The Calculator is a general purpose calculator, and it support a number of functions that are usefull for working with bits.

#### Entering bit and hex values

Terminate number with b or B, i.e. 0110101b or 0110101B are binary values
Terminate number with h or H, i.e. 7fh or 0e67H are hexadecimal values (Note: They must start with a digit)
Prefixing a number with \$ of 0x, i.e. \$ef or 0xf567 are hexadecimal values

#### Displaying bit and hex values

Use Bin(Value) to display value as binary, i.e. Bin(13) will return a string with 00000000000000000000000000001101
Use Hex(Value) to display value as hexadecimal, i.e. hex(13) will return a string with 0000000D

#### Boolean operations

These boolean operations are bitwise:
a BitAnd b
a BitOr b
a BitXor b

#### Bit functions

Some functions are also designed to work with bits:
SHL(Value,shift) // Shift left
SHR(Value,shift) // Shift right
BitExtract(Value,FirstBit,Bits) // Extract a number of bits
BitReplace(Value,NewBits,FirstBit,Bits) // Replace a number of bits
BitSet(Value,Bit) // Set specified bit, i.e. BitSet(0,4) is 10000B
BitReset(Value,Bit) // Reset specified bit, i.e. BitReset(11111111B,4) is 11101111B
BitInvert(Value,Bit) // Invert specified bit, i.e. BitInvert(0,4) is 10000B
```Nipple="Low nipple: "+BitExtract(Param,0,4)+"        High nipple: "+BitExtract(Param,4,4)