// Menu file for I2C sensor demonstration board for TU480X272C // Updated 12-01-11 AG /////////// //Setup// ////////// SETUP( SYSTEM ) { wdog = 5000; // Watchdog timeout in millisecs (0 is off) bled = 100; // Backlight LED (0 = off, 100 = on) encode = S; // File is in ASCII format } SETUP( ADC ) { active = 12; //Set ADC 1 and ADC 2 as acive calib1 = .2; //Set value of calibration for ADC1 calib2 = .2; //Set value of calibration for ADC2 avg1 = 16; //Set number of samples to be taken and averaged for ADC1 avg2 = 16; //Set number of samples to be taken and averaged for ADC2 } SETUP( I2C ) { active = M; //Set I2C to be in master mode end = \\FF; //Set the byte to return at the end of the I2C data speed = 100; //Set the tansmit speed in kilobits/sec encode = sd; //Set the encode to sd for raw ascii data rxi = Y; //Set the recieve buffer interface as active txi = Y; //Set the transmit buffer interface as active } ///////// //Files// ///////// LIB(fnt_16,"SDHC/asc_16b.fnt"); //Load the ascii size 16 font LIB(fnt_24,"SDHC/asc_24.fnt"); //Load the ascii size 32 font LIB(fnt_72,"SDHC/asc_72.fnt"); //Load the ascii size 72 font /////////// //Styles// /////////// STYLE( myPage, Page ) { back = \\333333; } //Set up the page style STYLE( txtstylet, Text) { font=fnt_24; col=white; curRel=CC; maxLen=32; maxRows=1; } //Set up font style for size 24 STYLE( txtstyles, Text) { font=fnt_16; col=\\cccccc; curRel=CC; maxLen=32; maxRows=1; } //Set up font style for size 16 STYLE( txtstyles2, Text) { font=fnt_16; col=\\cccccc; curRel=RC; maxLen=32; maxRows=1; } //Set up font style for size 16 right aligned STYLE( txtstylel, Text) { font=fnt_72; col=white; curRel=CC; maxLen=32; maxRows=1; } //Set up font style for size 72 STYLE( boxstyle, Draw) { type=Box; col=White; back=Black; width=1; curRel=CC; maxX=480; maxY=272; } //Set up the white box style STYLE( accstyle, Draw) { type=Circle; col=White; back=Blue; width=1; curRel=CC; maxX=480; maxY=272; } //Set up the tilt output circle STYLE( barstyle, Draw) { type=Box; col=Magenta; back=Magenta; width=1; curRel=BC; maxX=10; maxY=200; } //Set up the ADC bar /////////////// //Variables// ////////////// VAR(tuvar,0,U8); //Variable to state which unit of temperature is shown VAR(luvar,0,U8); //variable to state which unit of light is shown VAR(tilt_highX, 0, U8); //variable to store the highest tilt value in X axis VAR(tilt_lowX, 0, U8); //variable to store the lowest tilt value in X axis VAR(tilt_highY, 0, U8); //variable to store the highest tilt value in Y axis VAR(tilt_lowY, 0, U8); //variable to store the lowest tilt value in Y axis VAR(temp_high, 0, U8); //variable to store high temperature value VAR(temp_low, 0, U8); //variable to store low temperature value VAR(light_final, 0.0, FLT4); //variable to store decimal final value of the ligt VAR(light_final_int, 0, U32); //variable to store the integer of final value of light VAR(light_high, 0, U8); //variable to store high light value VAR(light_low, 0, U8); //variable to store low light value VAR(final_tiltX, 0, S16); //variable to store X Tilt value while doing calculations to make it suitable for display VAR(final_tiltY, 0, S16); //variable to store Y Tilt value while doing calculations to make it suitable for display VAR(adr_temp,72,U8); //variable of temperature address VAR(adr_light,35,U8); //variable of light address VAR(adr_tilt,16,U8); //variable of tilt address VAR(tiltX,0,S16); //variable to store final value of tilt in X axis VAR(tiltY,0,S16); //variable to store final value of tilt in Y axis VAR(num_temp,2,U8); //Number of bytes to read for temperature VAR(num_light,2,U8); //Number of bytes to read for light VAR(num_tilt,4,U8); //Number of bytes to read for tilt VAR(adc1val,0,U16); //variable to store ADC1 value into VAR(adc2val,0,U16); //variable to store ADC2 value into VAR(null,0,U8); //Null variable ////////// //Page// ///////// PAGE( mainPage, myPage ) { POSN(240, 12); TEXT(title, "I\\B2C Sensor Demonstration", txtstylet); //Title text POSN(26, 146); DRAW(ad2box,34,230,boxstyle); //Box for ADC2 input POSN(25, 40); TEXT(titlead2, "A2", txtstyles); //title for ADC2 POSN(26, 252); DRAW(bar2,10,200,barstyle); //Bar for ADC2 POSN(68, 146); DRAW(ad1box,34,230,boxstyle); //Box for ADC1 input POSN(65, 40); TEXT(titlead1, "A1", txtstyles); //title for ADC1 POSN(68, 252); DRAW(bar1,10,200,barstyle); //Bar for ADC1 POSN(163, 86); DRAW(tempbox,140,110,boxstyle); //Temperature box KEY(tempunitkey,tukey,120,90,TOUCH); //Convert temperature Key POSN(118, 40); TEXT(titletemp, "Temp", txtstyles); //temperature title POSN(160, 82); TEXT(tempval, "0", txtstylel); //Temperature value POSN(226, 128); TEXT(tempunit, "\\B0C", txtstyles2); //Temperature Units POSN(163, 206); DRAW(lightbox,140,110,boxstyle); //Light box KEY(lightunitkey,lukey,120,90,TOUCH); //Convert Light Key POSN(116, 160); TEXT(titlelight, "Light", txtstyles); //light title POSN(160, 202);TEXT(lightval, "0", txtstylel); //Light value POSN(226, 248); TEXT(lightunit, "Lux", txtstyles2); //Light Units POSN(356, 146); DRAW(tiltbox,230,230,boxstyle); //Tilt box POSN(256, 40); TEXT(titletilt, "Tilt", txtstyles); //Tilt title POSN(356, 146); DRAW(tiltpos,16,16,accstyle); //Tilt Circle // initialise light sensor LOAD(I2C,adr_light,null,1); WAIT(10); // initialise tilt sensor LOAD(I2C,adr_tilt,null,0,0); WAIT(100); // ------------------------------------------------------------------------------ // Main loop - read sensor data and update display // ------------------------------------------------------------------------------ LOOP(myloop,FOREVER) { // fetch and show ADC values LOAD(adc2val, ADC2); //Load the adc2 value into the variable IF(adc2val>200?[LOAD(adc2val,200);]); //set a limit on the adc2val (200 being the max height of bar) CALC(adc2val,200,adc2val,"-"); //Calculate the adc2val by taking it away from 200 (200 being the max height of bar) DRAW(bar2,10,adc2val); //Draw the adc2 bar with the height of adc2val LOAD(adc1val, ADC1); //Load the adc1 value into the varible IF(adc1val>200?[LOAD(adc1val,200);]); //set a limit on the asc1val (200 being the max height of bar) CALC(adc1val,200,adc1val,"-"); //calculate the adc1val by taking away from 200 (200 being the max height of bar) DRAW(bar1,10,adc1val); //draw the adc1 bar with the height of adc1val // measure light LOAD(I2C,adr_light,null,16); //adr_light variable has \\35 for light sensor I2C address. Command 16 is sent with no bytes read. WAIT(2); LOAD(I2C,adr_light,num_light); // read 2 bytes of data into buffer WAIT(2); LOAD(light_high, I2C); //Load the first byte of the I2C value into light high vairable LOAD(light_low, I2C); //Load the next byte of the I¶C value into light low vatriable LOAD(light_final, light_high); //Load the light_high variable into the light_final before calculations undertaken CALC(light_final, light_final, 256, "*"); //Calculations to convert the data to the reading required CALC(light_final, light_final, light_low, "+"); //Calculations to convert the data to the reading required CALC(light_final, light_final, 1.2, "/"); //Calculations to convert the data to the reading required LOAD(light_final_int, light_final); //Load the decimal place light value into the interger varoibale IF(luvar=1?convertl); //If the key is pressed convert the light value between Lux and Lumens TEXT(lightval, light_final_int); //Display the light value in the light box // measure temp LOAD(I2C,adr_temp,null,0); //adr_temp variable has \\72 for temperature sensor I2C address. Command 0 is sent with no bytes read. WAIT(2); LOAD(I2C,adr_temp,num_temp); //read 2 bytes of data into buffer WAIT(2); LOAD(temp_high, I2C); //Load the first byte of the I2C value into the temperature high variable LOAD(temp_low, I2C); //Load the nezt next byte of the I2C value into the temperature low variable IF(tuvar=1?convertt); //If the key is presse convert the temperature units between °C °F TEXT(tempval, temp_high); //Display the temperature value in the temperature box // measure tilt LOAD(I2C,adr_tilt,null,1); //adr_tilt variable has \\16 for tilt sensor I2C address. Command 1 is sent with no bytes read. WAIT(2); LOAD(I2C,adr_tilt,num_tilt); // read 4 bytes of data into buffer WAIT(2); LOAD(tilt_highX, I2C); //Load the first byte of the I2C value into tilt_highX LOAD(tilt_lowX, I2C); //Load the next byte of the I2C value into tilt_lowX LOAD(tilt_highY, I2C); //Load the next byte of the I2C value into tilt_highY LOAD(tilt_lowY, I2C); //Load the next byte of the I2C value into tilt_lowY CALC(final_tiltX,tilt_highX,256,"*");CALC(final_tiltX,final_tiltX,tilt_lowX,"+"); //Calculation to get one value in the X direction CALC(final_tiltY,tilt_highY,256,"*");CALC(final_tiltY,final_tiltY,tilt_lowY,"+"); //Calculation to get one value in the Y direction CALC(final_tiltX,final_tiltX,2048,"-"); //Calculations to get the X tilt value suitable for the containing box CALC(final_tiltX,final_tiltX,5,"/"); //Calculations to get the X tilt value suitable for the containing box CALC(tiltX,final_tiltX,356,"+"); //Calculations to get the X tilt value suitable for the containing box CALC(final_tiltY,final_tiltY,2048,"-"); //Calculations to get the Y tilt value suitable for the containing box CALC(final_tiltY,final_tiltY,5,"/"); //Calculations to get the Y tilt value suitable for the containing box CALC(tiltY,146,final_tiltY,"-"); //Calculations to get the Y tilt value suitable for the containing box POSN(tiltX, tiltY, tiltpos);; //Display the circle in the new position and refresh the page } } /////////////// //Functions// /////////////// // ------------------------------------------------------------------------------ // Convert degC to degF // ------------------------------------------------------------------------------ FUNC(convertt) { CALC(temp_high,temp_high,2,"*"); //Convert the temperature from °C to °F by multiplying by 2 and adding 32 CALC(temp_high,temp_high,32,"+"); IF(temp_low=128?[CALC(temp_high,temp_high,1,"+");]); } // ------------------------------------------------------------------------------ // Convert Lux to Lumens // ------------------------------------------------------------------------------ FUNC(convertl) { CALC(light_final_int,light_final_int,10,"/"); //convert the light from Lux to Lumens by dividing by 10 } // ------------------------------------------------------------------------------ // Toggle temp units depending on the value of tuvar which is changed by pressing the temperature key // ------------------------------------------------------------------------------ FUNC(tukey) { IF(tuvar=0?[TEXT(tempunit,"\\B0F");LOAD(tuvar,1);]:[TEXT(tempunit,"\\B0C");LOAD(tuvar,0);]);; } // ------------------------------------------------------------------------------ // Toggle light units depending on the value of luvar which is changed by pressing the light key // ------------------------------------------------------------------------------ FUNC(lukey) { IF(luvar=0?[TEXT(lightunit,"Lumens");LOAD(luvar,1);]:[TEXT(lightunit,"Lux");LOAD(luvar,0);]);; } // ------------------------------------------------------------------------------ // Show page // ------------------------------------------------------------------------------ SHOW(mainPage);