/** * \brief Measures and enables offset and gain corrections */ static void main_adc_correction(void) { /* ADC channel configuration structure */ struct adc_channel_config adcch_conf; uint16_t offset_correction; /* Expected value for gain correction at 1.9V * expected_value = Max. range * 1.9V / (VCC / 1.6) * Max. range = 12 bits signed = 11 bits unsigned */ const uint16_t expected_value = ((1 << 11) * 1900UL) / (3300L * 1000L / 1600L); /* Captured value for gain correction */ uint16_t captured_value; /* DAC Output 0 Volt */ main_dac_output(0); /* Capture value for 0 Volt */ adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); offset_correction = adc_get_unsigned_result(&ADCA, ADC_CH0); /* Enable offset correction */ adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf); adcch_enable_correction(&adcch_conf, offset_correction, 1, 1); adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf); /* DAC Output 1.9 Volts */ main_dac_output(1900); /* Capture value for 1.9 Volts */ adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); captured_value = adc_get_unsigned_result(&ADCA, ADC_CH0); /* Enable offset & gain correction */ adcch_enable_correction(&adcch_conf, offset_correction, expected_value, captured_value); adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf); printf("\n\rADC correction: "); printf("Offset correction %d, ", offset_correction); if (expected_value > captured_value) { printf("Gain correction 1.%03u\n\r\n\r", (uint16_t) ((((uint32_t)expected_value - captured_value) * 1000) / captured_value)); } else { printf("Gain correction 0.%03u\n\r\n\r", (uint16_t) (((uint32_t)expected_value * 1000) / captured_value)); } }
/** * \brief Reads a 16-bit analog value on ADC channel 0 and returns it. * * In this application CH0 is set up to read the analog voltage from * a simulated thermometer. * * \return Temperature of the induction element. */ uint16_t ovenctl_get_plate_temperature(void) { adc_start_conversion(&ADCA, ADC_CH0); adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); return adc_get_unsigned_result(&ADCA, ADC_CH0) / 4; }
/** * \brief Captures a values on ADC * * \return value on ADC pins (mV) */ static uint16_t main_adc_input(void) { uint16_t sample; adc_start_conversion(&ADCA, ADC_CH0); adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); sample = adc_get_unsigned_result(&ADCA, ADC_CH0); /* Conversion sample to mV : * mV = sample * (VCC / 1.6) / Max. range */ return ((uint32_t)sample * (3300L * 1000L / 1600L)) / (1 << 12); }
uint16_t adcReadX() { PORTA_DIR = 0b00110001; PORTA_OUT = 0b00010001; delay_ms(10); adc_start_conversion(&ADCA, 1); delay_ms(10); uint16_t pos = adc_get_unsigned_result(&ADCA, 1); PORTA_DIR = 0; return pos; }
int main(void) { const usart_serial_options_t usart_serial_options = { .baudrate = CONF_TEST_BAUDRATE, .charlength = CONF_TEST_CHARLENGTH, .paritytype = CONF_TEST_PARITY, .stopbits = CONF_TEST_STOPBITS, }; /* Usual initializations */ board_init(); sysclk_init(); sleepmgr_init(); irq_initialize_vectors(); cpu_irq_enable(); stdio_serial_init(CONF_TEST_USART, &usart_serial_options); printf("\x0C\n\r-- ADC Averaging Example --\n\r"); printf("-- Compiled: %s %s --\n\r\n\r", __DATE__, __TIME__); printf("Commands:\n\r"); printf("- key 'a' to enable averaging\n\r"); printf("- key 'd' to disable averaging\n\r"); /* ADC initialization */ main_adc_init(); main_adc_averaging_stop(); while (1) { if (usart_rx_is_complete(CONF_TEST_USART)) { char key = getchar(); if (key == 'a') { main_adc_averaging_start(); } if (key == 'd') { main_adc_averaging_stop(); } } /* Wait sample with or without average */ uint16_t sample; adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); sample = adc_get_unsigned_result(&ADCA, ADC_CH0); printf("ADC Value: %4u\r", sample); } }
bool adcCheck() { uint16_t threashold = 500; // around 200 is a sensible value PORTA_DIR = 0b11000001; PORTA_OUT = 0b11000001; // set pulldown on pin 4 PORTA.PIN4CTRL = PORT_OPC_PULLDOWN_gc; adc_start_conversion(&ADCA, 2); // measure if delay_ms(10); uint16_t value = adc_get_unsigned_result(&ADCA, 2); PORTA.PIN4CTRL = 0; if(value > threashold) return false; return true; }
int main(void) { const usart_serial_options_t usart_serial_options = { .baudrate = CONF_TEST_BAUDRATE, .charlength = CONF_TEST_CHARLENGTH, .paritytype = CONF_TEST_PARITY, .stopbits = CONF_TEST_STOPBITS, }; char binary[16 + 1]; uint8_t i; bool oversampling_is_enabled; /* Usual initializations */ board_init(); sysclk_init(); sleepmgr_init(); irq_initialize_vectors(); cpu_irq_enable(); stdio_serial_init(CONF_TEST_USART, &usart_serial_options); printf("\x0C\n\r-- ADC Over-sampling Example --\n\r"); printf("-- Compiled: %s %s --\n\r\n\r", __DATE__, __TIME__); printf("Commands:\n\r"); printf("- key 'o' to enable over-sampling\n\r"); printf("- key 'd' to disable over-sampling\n\r"); /* ADC initializations */ main_adc_init(); main_adc_oversampling_stop(); oversampling_is_enabled = false; while (1) { if (usart_rx_is_complete(CONF_TEST_USART)) { char key = getchar(); if (key == 'o') { main_adc_oversampling_start(); oversampling_is_enabled = true; } if (key == 'd') { main_adc_oversampling_stop(); oversampling_is_enabled = false; } } /* Wait sample with or without over-sampling */ uint16_t sample; adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); sample = adc_get_unsigned_result(&ADCA, ADC_CH0); if (!oversampling_is_enabled) { sample <<= 4; } i = 15; do { binary[i] = '0' + (sample & 1); sample >>= 1; } while (i--); binary[16] = 0; printf("ADC Value: %sb\r", binary); } }
/** * \brief Measures and enables offset and gain corrections */ static void main_adc_correction_start(void) { /* ADC channel configuration structure */ struct adc_channel_config adcch_conf; static bool correction_measures_done = false; static uint16_t offset_correction; /* Expected value for gain correction at 1.9V * expected_value = Max. range (12 bits unsigned) * 1.9V / (VCC / 1.6) */ const uint16_t expected_value = ((1 << 12) * 1900UL) / (3300L * 1000L / 1600L); /* Captured value for gain correction */ static uint16_t captured_value; if (correction_measures_done) { goto main_adc_correction_enable; } printf("\n\r*Measure offset correction\n\r"); printf("Set PA0 pin to GND and press a key to trigge measurement.\n\r"); printf("Warning on STK600: Remove AREF0 jumper to do it.\n\r"); getchar(); /* Capture value for 0 Volt */ adc_start_conversion(&ADCA, ADC_CH0); adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); offset_correction = adc_get_unsigned_result(&ADCA, ADC_CH0); /* Enable offset correction */ adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf); adcch_enable_correction(&adcch_conf, offset_correction, 1, 1); adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf); printf("*Measure Gain correction\n\r"); printf("Set PA0 pin to 1.9 Volt"); printf(" and press a key to trigge measurement.\r\n"); printf("Reminder on STK600: Set AREF0 jumper to do it.\n\r"); getchar(); /* Capture value for 1.9 Volts */ adc_start_conversion(&ADCA, ADC_CH0); adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); captured_value = adc_get_unsigned_result(&ADCA, ADC_CH0); correction_measures_done = true; main_adc_correction_enable: /* Enable offset & gain correction */ adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf); adcch_enable_correction(&adcch_conf, offset_correction, expected_value, captured_value); adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf); printf("\n\r* ADC correction enabled: "); printf("Offset correction %d, ", offset_correction); if (expected_value > captured_value) { printf("Gain correction 1.%03u\n\r", (uint16_t) ((((uint32_t)expected_value - captured_value) * 1000) / captured_value)); } else { printf("Gain correction 0.%03u\n\r", (uint16_t) (((uint32_t)expected_value * 1000) / captured_value)); } }
/** * \internal * \brief Test correction conversion in 12-bit mode using the DAC * * These values are then measured using the ADC on the pins that are connected * to the DAC channel, and the results are compared and checked to see if they * are within the acceptable range of values that passes the test. * * \param test Current test case. */ static void run_correction_conversion_test( const struct test_case *test) { int16_t volt_output; int16_t volt_input; uint16_t error; /** Measures and enables offset and gain corrections */ /* ADC channel configuration structure */ struct adc_channel_config adcch_conf; uint16_t offset_correction; /* Expected value for gain correction at 1.9V * expected_value = Max. range * 1.9V / (VCC / 1.6) * Max. range = 12 bits signed = 11 bits unsigned */ const uint16_t expected_value = ((1 << 11) * 1900UL) / (3300L * 1000L / 1600L); /* Captured value for gain correction */ uint16_t captured_value; /* DAC Output 0 Volt */ main_dac_output(0); /* Capture value for 0 Volt */ adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); offset_correction = adc_get_unsigned_result(&ADCA, ADC_CH0); /* Enable offset correction */ adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf); adcch_enable_correction(&adcch_conf, offset_correction, 1, 1); adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf); /* DAC Output 1.9 Volts */ main_dac_output(1900); /* Capture value for 1.9 Volts */ adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); adc_wait_for_interrupt_flag(&ADCA, ADC_CH0); captured_value = adc_get_unsigned_result(&ADCA, ADC_CH0); /* Enable offset & gain correction */ adcch_enable_correction(&adcch_conf, offset_correction, expected_value, captured_value); adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf); /* Check values */ for (volt_output = -CONV_MAX_VOLTAGE; volt_output <= CONV_MAX_VOLTAGE; volt_output += CONV_VOLTAGE_STEP) { main_dac_output(volt_output); volt_input = main_adc_input(); if (volt_output > volt_input) { error = volt_output - volt_input; } else { error = volt_input - volt_output; } test_assert_true(test, error < CONF_TEST_ACCEPT_DELTA_CORRECTION, "ADC result is outside acceptable range (expected %d, captured %d)", volt_output, volt_input); } }
int main(void) { board_init(); sysclk_init(); adc_init(); //Offset is stored in EEPROM int adc_offset = 0; if (nvm_eeprom_read_byte(EEPROM_ADDR_ID) == EEPROM_ID){ adc_offset = nvm_eeprom_read_byte(EEPROM_ADDR_OFFSET_POS) - nvm_eeprom_read_byte(EEPROM_ADDR_OFFSET_NEG); } PORTE_DIRSET = MASK_DIGIT012; PORTD_DIRSET = 0xFF; int32_t i = 0; uint8_t disp = 0; uint8_t adccnt = 0; unsigned int adc_readings[AVG_READINGS]; //Setup ADC hardware adc_init(); //Main loop...measure VCC-INT while(1){ // Vref = 2.048V // 4096 count ADC (12-bit) // 2.048V / 4096count = 0.0005 V/Count // We want i to be result in mV // = 0.5 mV/count //So divide count by 2 to get mV reading if (adccnt < AVG_READINGS){ adc_readings[adccnt] = adc_get_unsigned_result(&MY_ADC, MY_ADC_CH); if (adc_readings[adccnt] < 0){ adc_readings[adccnt] = 0; } adccnt++; } else { int32_t adctemp = 0; for (adccnt = 0; adccnt < AVG_READINGS; adccnt++){ adctemp += (int32_t)(adc_readings[adccnt] + adc_offset); } i = adctemp / AVG_READINGS; //Limit negative values to 0 //i = i - 2048; if (i < 0) i = 0; i = i / 2; adccnt = 0; } //Switch between mV and V ranges if (i > 999){ if (disp == 0){ display((i / 10)%10, 0, 2); _delay_ms(2); } else if (disp == 1){ display((i / 100) % 10, 0, 1); _delay_ms(2); } else { display(i / 1000, 1, 0); _delay_ms(2); } } else { if (disp == 0){ display(i % 10, 0, 2); _delay_ms(2); } else if (disp == 1){ display((i / 10) % 10, 0, 1); _delay_ms(2); } else { display(i / 100, 0, 0); _delay_ms(2); } } disp++; if (disp > 2){ disp = 0; } adc_start_conversion(&MY_ADC, MY_ADC_CH); } }
int main (void) { En_RC32M(); //Enable LowLevel & HighLevel Interrupts PMIC_CTRL |= PMIC_HILVLEN_bm | PMIC_LOLVLEN_bm |PMIC_MEDLVLEN_bm; PORT_init(); TimerD0_init(); TimerC0_init(); TimerE1_init(); USARTE0_init(); ADCA_init(); //wdt_enable(); // Globally enable interrupts sei(); //Address[0]=Address[0] + RobotID ; //// NRF Initialize NRF_init () ; while(1) { asm("wdr"); // BUZZER //adc = adc +(adc_get_unsigned_result(&ADCA,ADC_CH0)-adc)*0.01; adc = adc_get_unsigned_result(&ADCA,ADC_CH0); if (adc<=2250)//10 volt Buzzer_PORT.OUTSET = Buzzer_PIN_bm; else if(shoot_alarm_flg && (charge_count>=30000)) Buzzer_PORT.OUTSET = Buzzer_PIN_bm; else Buzzer_PORT.OUTCLR = Buzzer_PIN_bm; //SHOOT PORTC_OUTCLR=KCK_SH_PIN_bm; if((KCK_Ch_Limit_PORT.IN & KCK_Ch_Limit_PIN_bm)>>KCK_Ch_Limit_PIN_bp) { full_charge=1; tc_disable_cc_channels(&TCC0,TC_CCDEN); charge_count=0; } else { if((flg || flg_sw)==0) { tc_enable_cc_channels(&TCC0,TC_CCDEN); } } if (charge_flg)//full_charge { if (Robot_D[RobotID].KCK ) { if( KCK_Sens || (Robot_D[RobotID].KCK%2)) { flg = 1; } } } if (KCK_DSH_SW)//bazi vaghta begir nagir dare { flg_sw = 1; } if (free_wheel >= 500 ) { NRF_init(); } if(KCK_Sens) LED_Green_PORT.OUTSET = LED_Green_PIN_bm; else LED_Green_PORT.OUTCLR = LED_Green_PIN_bm; // //motor test //switch(flag2sec) //{ case 200: //// M.Setpoint=1000; //Robot_D[RobotID].M0b = 0xE8;//low //Robot_D[RobotID].M0a = 0X03;//high //break; // //case 400: ////M.Setpoint=2000; //Robot_D[RobotID].M0b = 0xD0;//low //Robot_D[RobotID].M0a = 0X07;//high //break; // //case 600: ////M.Setpoint=500; //Robot_D[RobotID].M0b = 0xF4;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 800: ////M.Setpoint=4000; //Robot_D[RobotID].M0b = 0xA0;//low //Robot_D[RobotID].M0a = 0X0F;//high //break; // //case 1000: ////M.Setpoint=1000; //Robot_D[RobotID].M0b = 0xE8;//low //Robot_D[RobotID].M0a = 0X03;//high //break; // //case 1200: ////M.Setpoint=500; //Robot_D[RobotID].M0b = 0xF4;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 1400: ////M.Setpoint=-500; //Robot_D[RobotID].M0b = 0x0C;//low //Robot_D[RobotID].M0a = 0XFE;//high //break; // //case 1600: ////M.Setpoint=400; //Robot_D[RobotID].M0b = 0x90;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 1800: ////M.Setpoint=350; //Robot_D[RobotID].M0b = 0x5E;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 2000: ////M.Setpoint=340; //Robot_D[RobotID].M0b = 0x54;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 2200: ////M.Setpoint=330; //Robot_D[RobotID].M0b = 0x4A;//low //Robot_D[RobotID].M0a = 0X01;//high //break; // //case 2600: ////M.Setpoint=100; //Robot_D[RobotID].M0b = 0x64;//low //Robot_D[RobotID].M0a = 0X00;//high //break; // //case 2800: ////M.Setpoint=50; //Robot_D[RobotID].M0b = 0x32;//low //Robot_D[RobotID].M0a = 0X00;//high //break; // //case 3000: ////M.Setpoint=1000; //Robot_D[RobotID].M0b = 0xE8;//low //Robot_D[RobotID].M0a = 0X03;//high //break; // //case 3200: ////M.Setpoint=-50; //Robot_D[RobotID].M0b = 0xCE;//low //Robot_D[RobotID].M0a = 0XFF;//high //flag2sec=0; //break; // //} //Robot_D[RobotID].M0b = 0xD0;//0X18;//-1000//01;//low37121 //Robot_D[RobotID].M0a = 0x07;//0XFC;//high //Robot_D[RobotID].M1b = 0XE8;//2000//ghalat17325 //Robot_D[RobotID].M1a = 0X03; //Robot_D[RobotID].M2b = 0XDC;//1000//low13703 //Robot_D[RobotID].M2a = 0X05;//high //Robot_D[RobotID].M3b = 0xF4;//3000//32;//ghalat30258 //Robot_D[RobotID].M3a = 0X01;//76; ////SEND TEST DATA TO FT232 //char str1[20]; //uint8_t count1 = sprintf(str1,"%d\r",adc); // //for (uint8_t i=0;i<count1;i++) //{ //usart_putchar(&USARTE0,str1[i]); // //} //usart_putchar(&USARTE0,'a'); }