/**
 * \brief Application entry point.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	int32_t ul_vol;
	int32_t ul_temp;

	/* Initialize the SAM system. */
	sysclk_init();
	board_init();

	configure_console();

	/* Output example information. */
	puts(STRING_HEADER);

	adc_enable();

	struct adc_config adc_cfg;

	adc_get_config_defaults(&adc_cfg);

	adc_init(ADC, &adc_cfg);
	adc_channel_enable(ADC,ADC_TEMPERATURE_SENSOR);

	adc_set_trigger(ADC, ADC_TRIG_SW);

	struct adc_temp_sensor_config adc_temp_sensor_cfg;

	adc_temp_sensor_get_config_defaults(&adc_temp_sensor_cfg);
	
	adc_temp_sensor_set_config(ADC, &adc_temp_sensor_cfg);

	adc_set_callback(ADC, ADC_INTERRUPT_EOC_16,
			adc_temp_sensor_end_conversion, 1);

	adc_start_software_conversion(ADC);

	while (1) {
		if (is_conversion_done == true) {
			is_conversion_done = false;
			ul_vol = g_ul_value * VOLT_REF / MAX_DIGITAL;
			/*
			* According to datasheet, The output voltage VT = 1.44V at 27C
			* and the temperature slope dVT/dT = 4.7 mV/C
			*/
			ul_temp = (ul_vol - 1440)  * 100 / 470 + 27;

			printf("-- Temperature is: %4d\r\n", (int)ul_temp);
			delay_ms(1000);
			adc_start_software_conversion(ADC);
		}
	}
}
Beispiel #2
0
OSStatus platform_adc_take_sample( const platform_adc_t* adc, uint16_t* output )
{
  OSStatus    err = kNoErr;
 
  platform_mcu_powersave_disable();
  
  require_action_quiet( adc != NULL, exit, err = kParamErr);
  
  channel_num = adc->channel;
  
  adc_channel_enable(ADC, adc->channel);
    
  adc_set_callback(ADC, adc->interrupt, adc_end_conversion, 1);
  
  /* Start conversion */
  adc_start_software_conversion(ADC);
  adc_start_calibration(ADC);
  
  while (adc_get_interrupt_status(ADC) & (1 << adc->channel));
  
  *output = adc_channel_get_value(ADC, adc->channel);	
  mico_thread_msleep(1);
  adc_channel_disable(ADC, adc->channel);
  
exit:
  platform_mcu_powersave_enable();
  return err;  
}
Beispiel #3
0
/**
 * \brief Application entry point for adcife example.
 *
 * \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	uint32_t uc_key = 0;

	/* Initialize the SAM system */
	sysclk_init();
	board_init();

	/* Initialize the UART console */
	configure_console();

	/* Output example information */
	puts(STRING_HEADER);

	/* Set default ADCIFE test mode. */
	g_adc_test_mode.uc_trigger_mode = TRIGGER_MODE_SOFTWARE;
	g_adc_test_mode.uc_pdc_en = 1;
	g_adc_test_mode.uc_gain_en = 0;

	display_menu();

	start_dac();

	start_adc();

	while (1) {
		/* ADCIFE software trigger per 1s */
		if (g_adc_test_mode.uc_trigger_mode == TRIGGER_MODE_SOFTWARE) {
			adc_start_software_conversion(&g_adc_inst);
		}
		if (!usart_read(CONF_UART, &uc_key)) {
			adc_disable_interrupt(&g_adc_inst, ADC_SEQ_SEOC);
			display_menu();
			set_adc_test_mode();
			start_adc();
			puts("Press any key to display configuration menu.\r");
		}
		delay_ms(1000);
		if (g_uc_condone_flag == 1) {
			if(g_adc_test_mode.uc_pdc_en == 0) {
				printf("Internal DAC Voltage = %4d mv  \r\n",
						(int)(g_adc_sample_data[0] * VOLT_REF / MAX_DIGITAL));
			} else {
				printf("Internal DAC Voltage = %4d mv  \r\n",
						(int)(g_adc_sample_data[0] * VOLT_REF /
							MAX_DIGITAL));
				printf("Scaled VCC Voltage = %4d mv  \r\n",
						(int)(g_adc_sample_data[1] * VOLT_REF /
							MAX_DIGITAL));
			}
			g_uc_condone_flag = 0;
		}
	}
}
Beispiel #4
0
/**
 * \brief The main application.
 */
int main(void)
{
	uint8_t i;
	uint8_t temperature[BUFFER_SIZE];
	uint8_t light[BUFFER_SIZE];
	char value_disp[5];
	uint32_t adc_value;
	uint32_t light_value;
	double temp;

	/* Initialize clocks. */
	sysclk_init();

	/* Initialize GPIO states. */
	board_init();

	/* Configure ADC for light sensor. */
	configure_adc();

	/* Initialize at30tse. */
	at30tse_init();

	/* Configure IO1 buttons. */
	configure_buttons();

	/* Initialize SPI and SSD1306 controller. */
	ssd1306_init();
	ssd1306_clear();

	/* Clear internal buffers. */
	for (i = 0; i < BUFFER_SIZE; ++i) {
		temperature[i] = 0;
		light[i] = 0;
	}

	/* Show the start info. */
	multi_language_show_start_info();

	/* Wait 3 seconds to show the above message. */
	delay_s(3);

	/* Check for valid firmware in SD card. */
	check_valid_firmware();

	while (true) {
		/* Set the trigger and jump to bootloader */
		if (reset_flag) {
			jump_to_bootloader();
		}

		/* Refresh page title only if necessary. */
		if (app_mode_switch > 0) {
			app_mode = (app_mode + 1) % 3;

			/* Clear screen. */
			ssd1306_clear();
			ssd1306_set_page_address(0);
			ssd1306_set_column_address(0);

			if (app_mode == 0) {
				/* Temperature mode. */
				ioport_set_pin_level(OLED1_LED1_PIN, OLED1_LED1_ACTIVE);
				ioport_set_pin_level(OLED1_LED2_PIN, !OLED1_LED2_ACTIVE);
				ioport_set_pin_level(OLED1_LED3_PIN, !OLED1_LED3_ACTIVE);
				multi_language_show_temperature_info();
			} else if (app_mode == 1) {
				/* Light mode. */
				ioport_set_pin_level(OLED1_LED2_PIN, OLED1_LED2_ACTIVE);
				ioport_set_pin_level(OLED1_LED1_PIN, !OLED1_LED1_ACTIVE);
				ioport_set_pin_level(OLED1_LED3_PIN, !OLED1_LED3_ACTIVE);
				multi_language_show_light_info();
			} else {
				/* SD mode. */
				ioport_set_pin_level(OLED1_LED3_PIN, OLED1_LED3_ACTIVE);
				ioport_set_pin_level(OLED1_LED1_PIN, !OLED1_LED1_ACTIVE);
				ioport_set_pin_level(OLED1_LED2_PIN, !OLED1_LED2_ACTIVE);

				sd_listing_pos = 0;
				/* Show SD card info. */
				display_sd_info();
			}

			app_mode_switch = 0;
		}

		/* Shift graph buffers. */
		for (i = 0; i < (BUFFER_SIZE - 1); ++i) {
			temperature[i] = temperature[i + 1];
			light[i] = light[i + 1];
		}

		/* Get temperature. */
		if (at30tse_read_temperature(&temp) == TWI_SUCCESS) {
			/* Don't care about negative temperature. */
			if (temp < 0) {
				temp = 0;
			}

			/* Update temperature for display. */
			/* Note: rescale to 0~24 for better rendering. */
			if (temp > 40) {
				temperature[BUFFER_SIZE - 1] = 24;
			} else {
				temperature[BUFFER_SIZE - 1] = (uint8_t)temp * 24 / 40;
			}
		} else {
			/* Error print zero values. */
			temperature[BUFFER_SIZE - 1] = 0;
		}

		/* Get light sensor information. */
		/* Rescale to 0~24 for better rendering. */
		adc_start_software_conversion(ADC);
		adc_value = adc_channel_get_value(ADC, ADC_CHANNEL_0);
		light[BUFFER_SIZE - 1] = 24 - adc_value * 24 / 1024;

		if (app_mode == 0) {
			/* Display temperature in text format. */
			sprintf(value_disp, "%d", (uint8_t)temp);
			ssd1306_set_column_address(98);
			ssd1306_write_command(SSD1306_CMD_SET_PAGE_START_ADDRESS(0));
			ssd1306_write_text(" ");
			/* Avoid character overlapping. */
			if (temp < 10) {
				ssd1306_clear_char();
			}

			ssd1306_write_text(value_disp);
			/* Display degree symbol. */
			ssd1306_write_data(0x06);
			ssd1306_write_data(0x06);
			ssd1306_write_text("c");

			/* Refresh graph. */
			ssd1306_draw_graph(0, 2, BUFFER_SIZE, 2, temperature);
		} else if (app_mode == 1) {
			light_value = 100 - (adc_value * 100 / 1024);
			sprintf(value_disp, "%lu", light_value);
			ssd1306_set_column_address(98);
			ssd1306_write_command(SSD1306_CMD_SET_PAGE_START_ADDRESS(0));
			ssd1306_write_text(" ");
			/* Avoid character overlapping. */
			if (light_value < 10) {
				ssd1306_clear_char();
			}

			ssd1306_write_text(value_disp);
			ssd1306_write_text("%");
			/* Avoid character overlapping. */
			if (light_value < 100) {
				ssd1306_clear_char();
			}

			/* Refresh graph. */
			ssd1306_draw_graph(0, 2, BUFFER_SIZE, 2, light);
		} else {
			/**
			 * Refresh screen if card was inserted/removed or
			 * browsing content.
			 */
			if (sd_update == 1) {
				/* Clear screen. */
				ssd1306_clear();
				ssd1306_set_page_address(0);
				ssd1306_set_column_address(0);

				if (sd_listing_pos == 0) {
					/* Show SD card info. */
					display_sd_info();
				} else {
					/* List SD card files. */
					display_sd_files_unicode();
				}

				sd_update = 0;
			}
		}

		/* Wait and stop screen flickers. */
		delay_ms(150);

		if (app_mode_switch == 0) {
			pio_enable_interrupt(OLED1_PIN_PUSHBUTTON_1_PIO,
					OLED1_PIN_PUSHBUTTON_1_MASK);
		}
		if (sd_update == 0) {
			pio_enable_interrupt(OLED1_PIN_PUSHBUTTON_2_PIO,
					OLED1_PIN_PUSHBUTTON_2_MASK);
			pio_enable_interrupt(OLED1_PIN_PUSHBUTTON_3_PIO,
					OLED1_PIN_PUSHBUTTON_3_MASK);
		}

	}
}