// Displays the temperature, in C or F. void temp_test() { static int display_c = 0; // 0 for F, 1 for C int temperature; if(display_c) temperature = read_temperature_c(); else temperature = read_temperature_f(); // display temperature; it is in tenths of a degree print_long(temperature/10); print("."); print_long(temperature%10); // character 0xDF is the degree symbol print_character('\xdf'); if(display_c) print_character('C'); else print_character('F'); // allow the user to switch between modes if(button_is_pressed(BUTTON_A)) display_c = 1; if(button_is_pressed(BUTTON_C)) display_c = 0; delay_ms(100); }
void test() { unsigned char button; clear(); delay(200); print("Orangutn"); // print to the top line of the LCD delay_ms(400); // delay 200 ms lcd_goto_xy(0, 1); // go to the start of the second LCD line #if defined __AVR_ATmega328P__ print(" LV-328"); // print to the bottom line of the LCD #elif defined __AVR_ATmega168__ print(" LV-168"); // print to the bottom line of the LCD #else #error "Unrecognized device type" #endif delay_ms(1000); // delay 700 ms clear(); // clear the LCD, move cursor to start of top line print(" Temp."); do { // Perform 10-bit analog-to-digital conversions on ADC channel 6. // Average ten readings and return the result, which will be one // third of the battery voltage when the "ADC6 = VBAT/3" solder // bridge is in place on the bottom of the Orangutan PCB int Tf = read_temperature_f(); // read temp sensor on ADC6 in 0.1°F lcd_goto_xy(1, 1); // second character of the second LCD line print_long(Tf/10); // display temperature in °F print("."); // print the decimal point print_long(Tf - 10*(Tf/10)); // display the tenths digit print_character(223); // display the degree symbol character (°) print("F "); // display the units delay_ms(50); // delay for 50 ms button = button_is_pressed(ALL_BUTTONS); // check for button press } while (button == 0); // loop if no buttons are being pressed // *** MAIN LOOP *** while (1) // loop forever { if (button & TOP_BUTTON) // if the top button is pressed button = melodyTest(); // this func. loops until next button press else if (button & MIDDLE_BUTTON) // if the middle button is pressed button = IOTest(); // this func. loops until next button press else if (button & BOTTOM_BUTTON) // if the bottom button is pressed button = motorTest(); // this func. loops until next button press } }
int main() { set_analog_mode(MODE_10_BIT); // 10-bit analog-to-digital conversions while(1) // run over and over again { lcd_goto_xy(0,0); // LCD cursor to home position (upper-left) print_long(to_millivolts(read_trimpot())); // trimpot output in mV print(" mV "); // added spaces are to overwrite left over chars lcd_goto_xy(0, 1); // LCD cursor to start of the second line unsigned int temp = read_temperature_f(); // get temp in tenths of a degree F print_long(temp/10); // get the whole number of degrees print_character('.'); // print the decimal point print_long(temp - (temp/10)*10); // print the tenths digit print_character(223); // print a degree symbol print("F "); // added spaces are to overwrite left over chars delay_ms(100); // wait for 100 ms (otherwise LCD flickers too much) } }
void test_analog() { // test that set/get mode works set_analog_mode(MODE_8_BIT); printf("\nGet8BIT"); assert(MODE_8_BIT == get_analog_mode()); set_analog_mode(MODE_10_BIT); printf("\nGet10BIT"); assert(MODE_10_BIT == get_analog_mode()); // read the trimpot in 10 bit mode and compare it to 8 bit mode int x1 = analog_read(7); set_analog_mode(MODE_8_BIT); delay_ms(1); // required for readings to stabilize int x2 = analog_read(7); printf("\n8BIT10BIT %d %d",x1,x2); assert( abs((x1>>2) - x2) < 10 ); // make sure that the average reading is more stable than individual readings set_analog_mode(MODE_10_BIT); unsigned char i; int min = 1023, max = 0, avg_min = 1023, avg_max = 0; for(i=0;i<10;i++) { int x1 = analog_read(7); int x2 = analog_read_average(7,256); if(x1 > max) max = x1; if(x1 < min) min = x1; if(x2 > avg_max) avg_max = x2; if(x2 < avg_min) avg_min = x2; printf("\nAvgComp %03x %03x", x1, x2); assert( abs(x1-x2) < 10); } printf("\nAB%03x%03x%03x%03x",max,min,avg_max,avg_min); assert( max - min >= avg_max - avg_min); // check that temp C and F return appropriate values in 10bit mode set_analog_mode(MODE_10_BIT); x1 = analog_read_average(6,100); int expect_temp_f = (((int)(analog_read_average_millivolts(TEMP_SENSOR, 20)) * 12) - 634) / 13; int expect_temp_c = (((int)(analog_read_average_millivolts(TEMP_SENSOR, 20) * 20)) - 7982) / 39; int temp_f = read_temperature_f(); int temp_c = read_temperature_c(); printf("\nTF10 %d %d", expect_temp_f, temp_f); assert( expect_temp_f/5 == temp_f/5 ); printf("\nTC10 %d %d", expect_temp_c, temp_c); assert( expect_temp_c/5 == temp_c/5 ); // try temp in 8bit mode set_analog_mode(MODE_8_BIT); delay_ms(1); // required for readings to stabilize? temp_f = read_temperature_f(); temp_c = read_temperature_c(); printf("\nTF8 %d %d", expect_temp_f, temp_f); assert( (expect_temp_f - temp_f) <= 20 ); printf("\nTC8 %d %d", expect_temp_c, temp_c); assert( abs(expect_temp_c - temp_c) <= 20 ); // test background conversion set_analog_mode(MODE_10_BIT); delay_ms(1); // required for readings to stabilize x1 = analog_read_average(6,100); start_analog_conversion(6); while(analog_is_converting()) printf("\nConvert"); x2 = analog_conversion_result(); printf("%d %d", x1, x2); assert( abs(x1 - x2) < 10 ); // make sure to_millivolts works in 8 and 10 bit mode set_analog_mode(MODE_10_BIT); x1 = 5000; x2 = to_millivolts(1023); printf("\nmV1 %d %d",x1,x2); assert( x1 == x2 ); x1 = 2498; x2 = to_millivolts(511); printf("\nmV2 %d %d",x1,x2); assert( x1 == x2 ); x1 = 0; x2 = to_millivolts(0); printf("\nmV3 %d %d",x1,x2); assert( x1 == x2 ); set_analog_mode(MODE_8_BIT); x1 = 5000; x2 = to_millivolts(255); printf("\nmV4 %d %d",x1,x2); assert( x1 == x2 ); x1 = 2490; x2 = to_millivolts(127); printf("\nmV5 %d %d",x1,x2); assert( x1 == x2 ); x1 = 0; x2 = to_millivolts(0); printf("\nmV6 %d %d",x1,x2); assert( x1 == x2 ); }