
SliceKit GPIO Example Applications 8/24
le d_ va lu e = l ed _v al ue & 0 x0 F ;
if ( l ed _v a lu e = = 1 5)
{
le d_ va lu e =0 x 0E ;
}
}
if ( b u tt o n_ p re s s_ 1 = = B UT T ON _ PR ES S_ V AL U E - 1) // B ut to n 2 i s p re s se d
{
data1 [ 0]=0; d ata1 [1 ]=0;
i2 c_m ast er _r x (0 x28 , data1 , 2 , i 2cO ne ) ; / / Re ad ADC va lu e u si ng I2C
>read
pr in tst rl n ( " Re ad ing T emp er at ur e valu e .... " );
dat a1 [ 0] = d at a1 [ 0] &0 x 0F ;
adc _v al ue =( d ata1 [0] < <6) |( d ata 1 [1] > >2) ;
pri nt st r (" T emp er at ur e is :" );
pri nt in tl n ( lin ear _i nte rp ola tio n ( ad c_ va lu e ));
}
button=1;
brea k ;
}
After recognising the valid push then it checks if Button 1 is pressed or Button 2 is pressed. IF
Button 1 is pressed then, the application reads the status of LEDs and shift the position of the
LEDs to left by 1. If Button 2 is pressed, then the applciation reads the contents of ADC register
using I2C read instruction and input the ADC value to linear interpolation function as shown
below.
int l in ea r_ in t er p ol at io n ( in t a dc _v alu e )
{
int i=0 ,x1 , y1 , x2 , y2 , te mper ;
whil e ( adc _va lue < T EM PE RA TU RE _LU T [i ] [1] )
{
i ++;
}
//:: Fo r mul a start
// C a lc u la t in g L in ea r i nt e rp ol a ti o n us in g t he f or m ul a y = y1 + (x - x1 ) * ( y2 - y 1 ) /( x2 - x1 )
//::Formula
x1 = TE MPE RA TU RE_ LU T [i - 1][ 1];
y1 = TE MPE RA TU RE_ LU T [i - 1][ 0];
x2 = TE MPE RA TU RE_ LU T [i ][ 1 ];
y2 = TE MPE RA TU RE_ LU T [i ][ 0 ];
t em pe r = y 1 +(( ( a dc _v al ue - x 1 ) *( y2 - y1 ) ) /( x 2 - x1 ) ) ;
retu rn te m per ;// Re turn Tem per atu re v alue
}
The linear intepolation function calculates the linear interpolation value using the following
formula and returns the temperature value from temperature look up table.
// C a lc u la t in g L in ea r i nt e rp ol a ti o n us in g t he f or m ul a y = y1 + (x - x1 ) * ( y2 - y 1 ) /( x2 - x1 )
int TEM PE RAT UR E_ L UT [][2 ]= // T emp era tu re Look up t able
{
{ -10 ,845} ,{ -5 ,80 8 } ,{0 ,765} ,{5 ,718} , {1 0 ,6 68 } ,{ 15 , 61 4} ,{20 ,559} ,{25 ,5 04 } ,{ 30 , 45 0} , {3 5 ,399} ,
>
{40 ,352} , {45 ,308} ,{ 50 ,269} ,{5 5 ,233} ,{60 , 2 02}
REV A