PIC32 Maths

Maths

sqrt
sin, asin
cos, acos
tan, atan
pi
e
mod
bcd2dec
dec2bcd

Mathematical functions in general return 64 bit floating point values unless otherwise stated.

sqrt()
sqrt(expression)
a#=sqrt(j#+27.3)

Returns the square root of the expression. If the expression is less than 0 then an error will be registered with ‘error()’

sin(), asin()
sin(expression)
a#=sin(j#+27.3)
b#=asin(20)

sin’ Returns the sine of an angle in radians.

‘asin’ returns the arc of the sine of the expression.

cos(), acos()
cos(expression)
a#=cos(j#+27.3)
b#=acos(20)

‘cos’ Returns the cosine of an angle in radians.

‘acos’ returns the arc of the cosine of the expression.

tab(), atan()
tan(expression)
a#=tan(j#+27.3)
b#=atan(20)

‘tan’ Returns the tangent of an angle in radians.

‘atan’ returns the arc of the tangent of the expression.

log(), log10()
log(expression)
a#=log(j#+27.3)
b#=log10(20)

‘log’ returns the natural logarithm of the expression and ‘log10’ return the logarithm to base 10.

mod()
mod(value, divisor)
a=mod(5,2)
b#=mod(12.7,2.6)

Returns the remainder of a division. Primarily intended for integer values and so ‘a’ will have a value of 1. This also works on floating point values and so ‘b#’ will have a value of 2.3.

bcd2dec(), dec2bcd()
bcd2dec(<value>, <places>)
a=bcd2dec(0x19, 2)
a%=dec2bcd(19,2)

Conversion to and from binary coded decimal. This is a representation of decimal where each nibble of a byte represents a decimal value. In the first example above the hex value (actually a binary value of 25) has a high nibble value of 1 and a low nibble value of 9. Given the 2 places, the high value represents 10 and the low value 9, thus the value is decimal 19. In the second example 0x19 will be returned to a%.

BCD (Binary Coded Decimal) is used in some peripheral IC’s such as real time clocks and temperature sensors.

Note the contents 'pi' and 'e' are represented as upper case constants and so must be used as PI and E.