uint16_t adc_get_value_mv(uint8_t u8_adc_channel) { uint32_t rawvalue = 0; // First try smaller range (more precise) rawvalue = adc_get_value(u8_adc_channel, adcRef1V25); // Check for 1V25 overflow if(rawvalue <= 0xFF0) { // 4096 -> 1250mv return (uint16_t)(rawvalue * 1250 / 4096); } else { // Convert again with higher range rawvalue = adc_get_value(u8_adc_channel, adcRef2V5); if(rawvalue <= 0xFF0) { // 4096 -> 2500mV return (uint16_t)(rawvalue * 2500 / 4096); } else { // Final conversion with maximum range rawvalue = adc_get_value(u8_adc_channel, adcRef5VDIFF); // 4096 -> 5000mV return (uint16_t)(rawvalue * 5000 / 4096); } } }
int main(void) { init_PM(); init_LCD(); init_Potentiometer(); init_CurrentSensor(); init_INTC(); fill_Display(); set_Velocidad(velocidad); set_Direccion(direccion); init_PWM(velocidad,direccion); while (1) { adc_start(&AVR32_ADC); adc_value_pot = adc_get_value(&AVR32_ADC,EXAMPLE_ADC_POTENTIOMETER_CHANNEL); adc_start(&AVR32_ADC); adc_value_current = adc_get_value(&AVR32_ADC,3); set_Duty_Cycle(adc_value_pot); set_Current(adc_value_current); if(bandera ==1){ set_Velocidad(velocidad); set_Direccion(direccion); update_PWM(velocidad, direccion); } delay_ms(400); }//WHILE }//MAIN
static int8_t get_analog_pin(sensor_result_t *res) { /* * measure voltage at PA07; * the measured voltage needs to be between 0 and Vddana/2 * */ /* Or use a the potentiometer: VCC | + | | R1 = 100k | | + | + | | R2 = 100k | o---> PA07 | | + | GND Reference is VDDANA/2 */ samr21_adc_init(ANALOG_PIN); res->adc_value = adc_get_value(); res->error = ADC->INTFLAG.reg & ADC_INTFLAG_OVERRUN ? ADC_OVERRUN : SUCCESS; res->sensor_value = 100 * (float) res->adc_value / 4095.0; res->unit = '%'; return res->error; }
inline uint16_t ctrl_get_input(ctrl_channel ch) { if (ch >= CTRL_NB_CHANNELS) { return 0; } return adc_get_value(&adcs[ch]); }
static int8_t get_temperature(sensor_result_t *res) { samr21_adc_init(TEMPERATURE); res->adc_value = adc_get_value(); res->error = ADC->INTFLAG.reg & ADC_INTFLAG_OVERRUN ? ADC_OVERRUN : SUCCESS; res->sensor_value = ((float) res->adc_value / (float) 4095 - (float) 0.667 ) / (float)0.0024 + 25; res->unit = 'C'; return res->error; }
static void echo_standby_timer_func(void) { int val0, val1; val0 = adc_get_value(0); //printk(KERN_NOTICE "echstdby: echo standby key val0 : %d\n", val0); val1 = adc_get_value(1); //printk(KERN_NOTICE "echstdby: echo standby key val1 : %d\n", val1); if(( val0 < 32) || (val1 < 32 )) { clear_echo_standby_mode(); printk(KERN_NOTICE "echstdby: rebooting now...."); machine_restart(); } else { /* start scanning again*/ mod_timer(&echo_standby_timer, jiffies + HZ); } }
int main() { while(true){ int adcpin = 0; unsigned int adcvalue = 0; adc_get_value(adcpin, &adcvalue); std::cout << adcvalue << std::endl; } }
static int8_t get_io_supply(sensor_result_t *res) { samr21_adc_init(IO_SUPPLY); res->adc_value = adc_get_value(); res->error = ADC->INTFLAG.reg & ADC_INTFLAG_OVERRUN ? ADC_OVERRUN : SUCCESS; // SCALED_IO_VCC = 1/4 VCC, 1V Reference means 4095 = 1V res->sensor_value = 4 * ((float) res->adc_value / 4095.0); res->unit = 'V'; return res->error; }
unsigned int CGpioControl::ReadAdc(int nPort) { #ifdef __TI_AM335X__ unsigned int nValue=0; m_gpioLocker.Lock(); adc_get_value(nPort , &nValue ) ; m_gpioLocker.Unlock(); return nValue; #else return 0; #endif }
void ui_adc_read(void) { #if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL) static signed short adc_value_temp = -1; #endif #if defined(EXAMPLE_ADC_LIGHT_CHANNEL) static uint16_t adc_value_light = -1; #endif #if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL) static signed short adc_value_pot = -1; #endif /* launch conversion on all enabled channels */ adc_start(&AVR32_ADC); #if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL) /* get value for the temperature adc channel */ adc_value_temp = adc_get_value(&AVR32_ADC, EXAMPLE_ADC_TEMPERATURE_CHANNEL); #endif #if defined(EXAMPLE_ADC_LIGHT_CHANNEL) /*get value for the light adc channel */ adc_value_light = adc_get_value(&AVR32_ADC, EXAMPLE_ADC_LIGHT_CHANNEL); ui_msg[0] = MESSAGE_ATD_SENSOR_LIGHT; ui_msg[1] = adc_value_light >> 8; ui_msg[2] = adc_value_light & 0xFF; uhi_aoa_write(ui_msg, 3, NULL); #endif #if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL) /* get value for the potentiometer adc channel */ adc_value_pot = adc_get_value(&AVR32_ADC, EXAMPLE_ADC_POTENTIOMETER_CHANNEL); #endif }
/* This will display the integer value read on the ADC, between 0 and 1024. * ADC must be initialised prior to calls to adc_display() (it means that adc_on() * must be called before using this function. * adc_num is an ADC channel number (integer between 0 and 7) * use LPC_ADC_NUM(x) for channel selection. * returns ADC convertion value or negative value on error. */ int adc_display(int adc_num, int uart_num) { uint16_t val = 0; int ret = 0; adc_start_convertion_once(adc_num, 0); msleep(10); ret = adc_get_value(&val, adc_num); if (ret < 0) { return ret; } else { uprintf(uart_num, "ADC(%d): %d (raw: 0x%04x)\r\n", adc_num, val, val); } return val; }
int co_click_get_measure(uint8_t mikrobus_index, uint16_t *measure) { float tmp = 0.f; if (measure == NULL) { fprintf(stderr, "co: Cannot store measure using null pointer.\n"); return -1; } if (adc_get_value(mikrobus_index, &tmp) < 0) return -1; *measure = (tmp / 5.f) * 65535; return 0; }
/* Display the temperature computed from adc convertion of the voltage output of * a TMP36 analog temperature sensor * ADC must be initialised prior to calls to TMP36_display() (it means that adc_on() * must be called before using this function. * adc_num is an ADC channel number (integer between 0 and 7) * use LPC_ADC_NUM(x) for channel selection. */ void TMP36_display(int adc_num, int uart_num) { uint16_t val = 0; int ret = 0; adc_start_convertion_once(adc_num, 0); msleep(8); ret = adc_get_value(&val, adc_num); if (ret == 0) { int micro_volts = 0; /* depends on vref, should use a precise 3.0V Vref and multiply by 3000 */ micro_volts = (val * 3200); int converted = ((micro_volts / 100) - 5000); uprintf(uart_num, "TMP36: %d,%d (orig: %d, raw: %04x)\r\n", (converted / 100), (converted % 100), val, val); } }
uint8_t calibrateGP2(void) { //////////////////////////////////////// //Code de Recuperation des valeurs des gp2 avant //////////////////////////////////////// //////////////////////////////////////// //Placer le robot face a un mur //////////////////////////////////////// //trajectory_goto_d(&traj, END, -10); //while(!trajectory_is_ended(&traj)); for (int i = 0; i < 25; ++i) { wait_ms(500); printf("dist = %d cm",i*5); printf("gp2 right %d gp2 left %d gp2 middle %d \n",adc_get_value(ADC_REF_AVCC | MUX_ADC0),adc_get_value(ADC_REF_AVCC | MUX_ADC1),adc_get_value(ADC_REF_AVCC | MUX_ADC2)); trajectory_goto_d(&traj, END, -5); while(!trajectory_is_ended(&traj)); } return DONE; }
/*! * \brief Get the current light sensor value. * * \param buf char buffer in which the light sensor value is stored. * \param result returns the light sensor value. * * \return true upon success, false if error. */ bool b_light_get_value( char* buf, U32* result ) { int i_current_val; /* enable channel for sensor */ adc_enable( adc, ADC_LIGHT_CHANNEL ); /* start conversion */ adc_start( adc ); /* get value for sensor */ i_current_val = ( #ifdef EVK1100_REVA ADC_MAX_VALUE - #endif adc_get_value( adc, ADC_LIGHT_CHANNEL )) * 100 / ADC_MAX_VALUE; /* Disable channel for sensor */ adc_disable( adc, ADC_LIGHT_CHANNEL ); sprintf( buf, "%d%%\r\n", i_current_val); *result= i_current_val; return true; }
/** * Get internal temperature * @return Celsius temperature (in tenth degrees) */ int16_t adc_get_inttemp(void) { uint32_t rawvalue = 0; float temp; /* Factory calibration temperature from device information page. */ float cal_temp_0 = (float)((DEVINFO->CAL & _DEVINFO_CAL_TEMP_MASK) >> _DEVINFO_CAL_TEMP_SHIFT); float cal_value_0 = (float)((DEVINFO->ADC0CAL2 & _DEVINFO_ADC0CAL2_TEMP1V25_MASK) >> _DEVINFO_ADC0CAL2_TEMP1V25_SHIFT); /* Temperature gradient (from datasheet) */ float t_grad = -6.27; rawvalue = adc_get_value(adcSingleInpTemp, adcRef1V25); temp = (cal_temp_0 - ((cal_value_0 - rawvalue) / t_grad)); //temp = ((cal_value_0 - rawvalue) / t_grad); return (int16_t)(temp*10); }
int main(void) { int16_t a; int32_t b; uart_init(); fdevopen(uart0_dev_send,NULL); sei(); adc_init(); while(1) { printf_P(PSTR("\n\nHello everybody\n This is the ADC test\n")); wait_ms(20); /* simple polling */ a = adc_get_value( ADC_REF_AVCC | MUX_ADC0 ); printf_P(PSTR("polling : ADC0 = %i\n"),a); wait_ms(20); /* pre-launch */ adc_launch( ADC_REF_AVCC | MUX_ADC1 ); wait_ms(1); /* this function should take less time */ a = adc_get_value( ADC_NO_CONFIG ); printf_P(PSTR("pre-launch : ADC1 = %i\n"),a); wait_ms(20); /* test of free running mode */ a = adc_get_value( ADC_REF_AVCC | MUX_ADC2 | ADC_MODE_TRIGGERED ); printf_P(PSTR("free run mode : ADC2 = %i\n"),a); wait_ms(1); /* this function should take less time */ a = adc_get_value( ADC_REF_AVCC | MUX_ADC3 | ADC_MODE_TRIGGERED ); printf_P(PSTR("free run mode : ADC3 = %i\n\n"),a); wait_ms(1); /* test of different outputs formats */ a = adc_get_value( ADC_REF_AVCC | MUX_ADC0 | ADC_MODE_16_BITS ); printf_P(PSTR("normal output 16: ADC0 = %u ( div = %u)\n"), a, ((uint16_t)a)/(1<<6)); b = adc_get_value32( (void*)(ADC_REF_AVCC | MUX_ADC0 | ADC_MODE_16_BITS) ); printf_P(PSTR("normal output 16(32): ADC0 = %ld ( div = %lu)\n"), b, b/(1l<<6)); /* ADC_MODE_10_BITS default */ a = adc_get_value( ADC_REF_AVCC | MUX_ADC0 ); printf_P(PSTR("normal output 10: ADC0 = %u\n"),a); /* ADC_MODE_10_BITS default */ b = adc_get_value32( (void*)(ADC_REF_AVCC | MUX_ADC0 ) ); printf_P(PSTR("normal output 10(32): ADC0 = %lu\n"),b); printf_P(PSTR("now try a signed differential conversion\n")); a = adc_get_value( ADC_REF_AVCC | MUX_ADC0 | ADC_MODE_10_BITS ); b = adc_get_value( ADC_REF_AVCC | MUX_ADC1 | ADC_MODE_10_BITS ); printf_P(PSTR("computed : ADC0-ADC1 = %i\n"), ((int16_t)a - (int16_t)b) /2); a = adc_get_value( ADC_REF_AVCC | MUX_ADC0_ADC1); printf_P(PSTR("signed output 10: ADC0-ADC1 = %i\n"),a); b = adc_get_value32( (void*)(ADC_REF_AVCC | MUX_ADC0_ADC1 ) ); printf_P(PSTR("signed output 10(32): ADC0-ADC1 = %li\n"),b); a = adc_get_value( ADC_REF_AVCC | MUX_ADC0_ADC1 | ADC_MODE_16_BITS ); printf_P(PSTR("signed output 16: ADC0-ADC1 = %i ( div = %i)\n"), a, a/(1<<6)); b = adc_get_value32( (void*)(ADC_REF_AVCC | MUX_ADC0_ADC1 | ADC_MODE_16_BITS ) ); printf_P(PSTR("signed output 16(32): ADC0-ADC1 = %li ( div = %li)\n\n"), b, b/(1<<6)); /* test of interrupt mode : we scan once the 8 inputs */ adc_register_event(event); adc_launch( ADC_REF_AVCC | MUX_ADC0 | ADC_MODE_INT ); wait_ms(20); wait_ms(2000); } return 0; }
static int leddev_ioctl (struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { unsigned int cmd_buf[10]; switch (cmd) { case GPIO_GET_VALUE: if (copy_from_user (cmd_buf, (void *) arg, 4)) return -EFAULT; cmd_buf[1] = gpio_get_value (cmd_buf[0]); DBG_GPIO_FUNCTION (cmd_buf[0], cmd_buf[1], "gpio_get_value"); if (copy_to_user ((unsigned *) arg, &cmd_buf[1], 4)) return -EFAULT; break; case GPIO_SET_HIGH: if (copy_from_user (cmd_buf, (void *) arg, 4)) return -EFAULT; DBG_GPIO_FUNCTION (cmd_buf[0], -1, "gpio_set_high"); gpio_set_high (cmd_buf[0]); break; case GPIO_SET_LOW: if (copy_from_user (cmd_buf, (void *) arg, 4)) return -EFAULT; DBG_GPIO_FUNCTION (cmd_buf[0], -1, "gpio_set_low"); gpio_set_low (cmd_buf[0]); break; case GPIO_SET_AS_INPUT: if (copy_from_user (cmd_buf, (void *) arg, 4)) return -EFAULT; DBG_GPIO_FUNCTION (cmd_buf[0], -1, "gpio_set_as_input"); gpio_set_as_input (cmd_buf[0]); break; case GPIO_SET_AS_OUTPUT: if (copy_from_user (cmd_buf, (void *) arg, 4*2)) return -EFAULT; DBG_GPIO_FUNCTION (cmd_buf[0], cmd_buf[1], "gpio_set_as_output"); gpio_set_as_output (cmd_buf[0], cmd_buf[1]); break; case GPIO_SET_AS_FUNCTION: if (copy_from_user (cmd_buf, (void *) arg, 4)) return -EFAULT; DBG_GPIO_FUNCTION (cmd_buf[0], -1, "gpio_set_as_function"); gpio_set_as_function (cmd_buf[0]); break; case ECOLED_SET_VALUE: if (copy_from_user (&ecoStandbyValue, (void *) arg, sizeof(ecoStandbyValue))) return -EFAULT; gpio_set_as_output (GPIO_SPI1_SCK, ecoStandbyValue); break; case ECOLED_GET_VALUE: if (copy_to_user ((unsigned *) arg, &ecoStandbyValue, sizeof(ecoStandbyValue))) return -EFAULT; break; case IOCTL_FAN_CTL: #if defined (CONFIG_PNX0106_W6) if (copy_from_user (&fan_ctl_mode, (void *) arg, sizeof(fan_ctl_mode))) return -EFAULT; gpio_set_as_output (GPIO_IEEE1394_D4, (fan_ctl_mode == 0) ? 0 : 1); // 0 == fan off #else /* GPIO_IEEE1394_D4 is not the correct GPIO on non-w6 platforms... */ printk ("IOCTL_FAN_CTL: hardcoded gpio pin definition supports W6 platforms only...\n"); return -ENODEV; #endif break; case IOCTL_MISC_COUNTRY_DETECT: { unsigned int w_country_code; unsigned int w_ad_value; adcdrv_set_mode (1); // may be initialized twice, anyway should be ok gpio_set_as_input (GPIO_V2); // B8--Version_2 gpio_set_as_input (GPIO_V3); // L16--Version_3 w_country_code = (gpio_get_value (GPIO_V2)) << 1; w_country_code |= (gpio_get_value (GPIO_V3)) << 0; w_ad_value = adc_read (ADC_CHANNEL_3); w_country_code |= (w_ad_value << 16); if (copy_to_user ((unsigned *) arg, &w_country_code, sizeof(w_country_code))) return -EFAULT; printk ("IOCTL_MISC_COUNTRY_DETECT called\n"); break; } case IOCTRL_INIT_RDS_TIMER: init_rds_check_timer(); break; case IOCTRL_READ_RDS_MSG_BLOCK: //printk("\n rds msg read blocked "); interruptible_sleep_on (&read_rds_msg_wq); break; case IOCTL_MISC_GET_NTC: { int ntc_val; ntc_val = adc_get_value (ADC_CHANNEL_2); // ADC10B_GPA2, pin U4 if (copy_to_user ((int *) arg, &ntc_val, 4)) return -EFAULT; break; } case IOCTL_MISC_WAKEUP_THREAD: { int pid; struct task_struct *p; if (copy_from_user (&pid, (void *) arg, 4)) return -EFAULT; printk ("Try to wake up pid:%d\n", pid); if ((pid > 0) && (p = find_task_by_pid (pid))) wake_up_process (p); break; } default: return -EFAULT; } return 0; }
/** \brief Main application entry point - init and loop to display ADC values */ int main(void) { #if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL) signed short adc_value_temp = -1; #endif #if defined(EXAMPLE_ADC_LIGHT_CHANNEL) signed short adc_value_light = -1; #endif #if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL) signed short adc_value_pot = -1; #endif /* Init system clocks */ sysclk_init(); /* init debug serial line */ init_dbg_rs232(sysclk_get_cpu_hz()); /* Assign and enable GPIO pins to the ADC function. */ gpio_enable_module(ADC_GPIO_MAP, sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0])); /* Configure the ADC peripheral module. * Lower the ADC clock to match the ADC characteristics (because we * configured the CPU clock to 12MHz, and the ADC clock characteristics are * usually lower; cf. the ADC Characteristic section in the datasheet). */ AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET; adc_configure(&AVR32_ADC); /* Enable the ADC channels. */ #if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL) adc_enable(&AVR32_ADC, EXAMPLE_ADC_TEMPERATURE_CHANNEL); #endif #if defined(EXAMPLE_ADC_LIGHT_CHANNEL) adc_enable(&AVR32_ADC, EXAMPLE_ADC_LIGHT_CHANNEL); #endif #if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL) adc_enable(&AVR32_ADC, EXAMPLE_ADC_POTENTIOMETER_CHANNEL); #endif /* Display a header to user */ print_dbg("\x1B[2J\x1B[H\r\nADC Example\r\n"); while (true) { /* Start conversions on all enabled channels */ adc_start(&AVR32_ADC); #if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL) /* Get value for the temperature adc channel */ adc_value_temp = adc_get_value(&AVR32_ADC, EXAMPLE_ADC_TEMPERATURE_CHANNEL); /* Display value to user */ print_dbg("HEX Value for Channel temperature : 0x"); print_dbg_hex(adc_value_temp); print_dbg("\r\n"); #endif #if defined(EXAMPLE_ADC_LIGHT_CHANNEL) /* Get value for the light adc channel */ adc_value_light = adc_get_value(&AVR32_ADC, EXAMPLE_ADC_LIGHT_CHANNEL); /* Display value to user */ print_dbg("HEX Value for Channel light : 0x"); print_dbg_hex(adc_value_light); print_dbg("\r\n"); #endif #if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL) /* Get value for the potentiometer adc channel */ adc_value_pot = adc_get_value(&AVR32_ADC, EXAMPLE_ADC_POTENTIOMETER_CHANNEL); /* Display value to user */ print_dbg("HEX Value for Channel pot : 0x"); print_dbg_hex(adc_value_pot); print_dbg("\r\n"); #endif /* Slow down the display of converted values */ delay_ms(500); } return 0; }
/*! * \brief Get the current temperature value. * * \param pxLog a Log structure. * * \return true upon success, false if error. */ bool b_temperature_get_value( xLogDef *pxLog ) { int i_current_val, value, index = 0; /* enable channel for sensor */ adc_enable( adc, ADC_TEMPERATURE_CHANNEL ); // start conversion adc_start( adc ); // get value for sensor value = adc_get_value( adc, ADC_TEMPERATURE_CHANNEL ); /* Disable channel for sensor */ adc_disable( adc, ADC_TEMPERATURE_CHANNEL ); if(value > temperature_code[0]) { i_current_val = -20; } else { while(temperature_code[index++] > value); i_current_val = (index - 1 - 20); } // Alloc memory for the log string. pxLog->pcStringLog = pvPortMalloc( 12*sizeof( char ) ); if( NULL == pxLog->pcStringLog ) { return( false ); } pxLog->pfFreeStringLog = vPortFree; // Because pvPortMalloc() was used to // alloc the log string. // Build the log string. if( i_current_val <= l_temp_min ) { sprintf( pxLog->pcStringLog, "%3dC | min", i_current_val ); // if alarms have to be checked and no alarm for min was pending if (( b_temp_alarm == pdTRUE ) && ( b_temp_alarm_min == pdFALSE )) { // alarm has been taken into account, // don't reenter this test before leaving min area b_temp_alarm_min = pdTRUE; // allow alarm if max is reached b_temp_alarm_max = pdFALSE; // post alarm to SMTP task v_SMTP_Post("Min Temp Alarm", NULL); } } else if( i_current_val >= l_temp_max ) { sprintf( pxLog->pcStringLog, "%3dC | max", i_current_val ); // if alarms have to be checked and no alarm for max was pending if (( b_temp_alarm == pdTRUE ) && ( b_temp_alarm_max == pdFALSE )) { // alarm has been taken into account, // don't reenter this test before leaving max area b_temp_alarm_max = pdTRUE; // allow alarm if min is reached b_temp_alarm_min = pdFALSE; // post alarm to SMTP task v_SMTP_Post("Max Temp Alarm", NULL); } } else { sprintf( pxLog->pcStringLog, "%3dC", i_current_val ); // if alarms have to be checked if ( b_temp_alarm == pdTRUE ) { // no alarm is pending b_temp_alarm_max = pdFALSE; b_temp_alarm_min = pdFALSE; } } return( true ); }
void vControl ( void *pvParameters ) { portTickType xLastWakeTime; signed char valx, valy; unsigned char ls, rs, lb, rb; can_frame_t out_frame; can_frame_t in_frame; out_frame.id = 1; out_frame.dlc = 6; xLastWakeTime = xTaskGetTickCount (); /* Button init */ buttons_init (); /* FSM init */ fsm_init (); /* CAN init */ can_init (); /* ADC init */ adc_init ( ctrlNUM_ADC_VALUES ); /* Touch init */ touch_init ( 30, 30, 30, 30 ); while (1) { vTaskDelayUntil ( &xLastWakeTime, ctrlTASK_FREQUENCY ); if ( adc_conversion_complete () == pdTRUE ) { adc_disable (); adc_get_value ( &valx, 0 ); adc_get_value ( &valy, 0 ); touch_measure ( &ls, &rs, &lb, &rb ); out_frame.data[0] = valx; out_frame.data[1] = valy; out_frame.data[2] = ls; out_frame.data[3] = rs; out_frame.data[4] = lb; out_frame.data[5] = rb; if (fsm_get_state() == ST_PLAY) { can_transmit (&out_frame); } can_receive (&in_frame, 0); if (in_frame.data[0] == GAME_SENSOR_TRIGGERED) { // TODO: set triggered state fsm_event_t *event = pvPortMalloc (sizeof (fsm_event_t)); event->type = EV_STOP; event->ptr = NULL; fsm_event_put (event, portMAX_DELAY); in_frame.data[0] = 0; } else if (in_frame.data[0] == GAME_SENSOR_CLEARED) { // TODO: set cleared state in_frame.data[0] = 0; } adc_enable (); adc_conversion_start (); } fsm_update (); } }
/*! * \brief Get the current potentiometer value. * * \param pxLog a Log structure. * * \return true upon success, false if error. */ bool b_potentiometer_get_value( xLogDef *pxLog ) { int i_current_val; /* enable channel for sensor */ adc_enable( adc, ADC_POTENTIOMETER_CHANNEL ); /* start conversion */ adc_start( adc ); /* get value for sensor */ i_current_val = adc_get_value( adc, ADC_POTENTIOMETER_CHANNEL ) * 100 / ADC_MAX_VALUE; /* Disable channel for sensor */ adc_disable( adc, ADC_POTENTIOMETER_CHANNEL ); // Alloc memory for the log string. pxLog->pcStringLog = pvPortMalloc( 16*sizeof( char ) ); if( NULL == pxLog->pcStringLog ) { return( false ); } pxLog->pfFreeStringLog = vPortFree; // Because pvPortMalloc() was used to // alloc the log string. // Build the log string. if( i_current_val <= ul_pot_min ) { sprintf( pxLog->pcStringLog, "%3d%% | min", i_current_val ); // if alarms have to be checked and no alarm for min was pending if (( b_pot_alarm == pdTRUE ) && ( b_pot_alarm_min == pdFALSE )) { // alarm has been taken into account, // don't reenter this test before leaving min area b_pot_alarm_min = pdTRUE; // allow alarm if max is reached b_pot_alarm_max = pdFALSE; // post alarm to SMTP task v_SMTP_Post("Min Potentiometer Alarm", NULL); } } else if( i_current_val >= ul_pot_max ) { sprintf( pxLog->pcStringLog, "%3d%% | max", i_current_val ); // if alarms have to be checked and no alarm for max was pending if (( b_pot_alarm == pdTRUE ) && ( b_pot_alarm_max == pdFALSE )) { // alarm has been taken into account, // don't reenter this test before leaving max area b_pot_alarm_max = pdTRUE; // allow alarm if min is reached b_pot_alarm_min = pdFALSE; // post alarm to SMTP task v_SMTP_Post("Max Potentiometer Alarm", NULL); } } else { sprintf( pxLog->pcStringLog, "%3d%%", i_current_val ); // if alarms have to be checked if ( b_pot_alarm == pdTRUE ) { // no alarm is pending b_pot_alarm_max = pdFALSE; b_pot_alarm_min = pdFALSE; } } return( true ); }
/*=== internal functions =========================================================================*/ static void samr21_adc_init(sensor_type_t sensor) { uint8_t refsel1, refsel2; refsel1 = ADC->REFCTRL.bit.REFSEL; // generic Clock selection ID.. 0x1E = GCLK_ADC (page103 - Datasheet) GCLK->CLKCTRL.reg = GCLK_CLKCTRL_ID_ADC | GCLK_CLKCTRL_GEN_GCLK3 | GCLK_CLKCTRL_CLKEN; while (GCLK->STATUS.bit.SYNCBUSY); //Power Manager - enable ADC (Page 122 - Datasheet) PM->APBCMASK.reg |= PM_APBCMASK_ADC; ADC->CTRLA.reg = ADC_CTRLA_ENABLE; WAIT_ADC_SYNC(); switch (sensor) { case TEMPERATURE: // 1 Volt Reference for Temperature Measurement ADC->REFCTRL.reg = ADC_REFCTRL_REFSEL_INT1V; // sync-asynchronicity of CLK_ADC_APB and GLK_ADC=>some registers need sync WAIT_ADC_SYNC(); // case Temperature-measurement - select Temp-sensor-Inputs ADC->INPUTCTRL.reg = ADC_INPUTCTRL_MUXNEG_GND | ADC_INPUTCTRL_MUXPOS_TEMP; WAIT_ADC_SYNC(); // Temperature Sensor Output in VREF-Register enable SYSCTRL->VREF.reg = (SYSCTRL->VREF.reg & ~0x03) | SYSCTRL_VREF_TSEN; WAIT_ADC_SYNC(); break; #if BOARD==SAMR21_XPLAINED_PRO case IO_SUPPLY: ADC->REFCTRL.reg = ADC_REFCTRL_REFSEL_INT1V; WAIT_ADC_SYNC(); ADC->INPUTCTRL.reg = ADC_INPUTCTRL_MUXNEG_GND | ADC_INPUTCTRL_MUXPOS_SCALEDIOVCC; WAIT_ADC_SYNC(); SYSCTRL->VREF.bit.BGOUTEN = 0; SYSCTRL->VREF.bit.TSEN = 0; WAIT_ADC_SYNC(); break; case ANALOG_PIN: PORT->Group[0].DIRCLR.reg = PORT_PA07; PORT->Group[0].PINCFG[7].reg |= PORT_PINCFG_PMUXEN; PORT->Group[0].PMUX[7 / 2].bit.PMUXO |= PORT_PMUX_PMUXO_B_Val; ADC->REFCTRL.reg = ADC_REFCTRL_REFSEL_INTVCC1; WAIT_ADC_SYNC(); ADC->INPUTCTRL.reg = ADC_INPUTCTRL_MUXNEG_IOGND | ADC_INPUTCTRL_MUXPOS_PIN7; WAIT_ADC_SYNC(); SYSCTRL->VREF.bit.BGOUTEN = 1; SYSCTRL->VREF.bit.TSEN = 0; break; #endif default: /* Invalid sensor value */ return; } // Pre scaler & Resolution Selection (8MHz / 8 = 1Mhz used at page 1071 for temp. meas.) ADC->CTRLB.reg = ADC_CTRLB_PRESCALER_DIV8 | ADC_CTRLB_RESSEL_12BIT; // adjusting Result + Average Control 1...1024 samples possible ADC->AVGCTRL.reg = ADC_AVGCTRL_ADJRES(4) | ADC_AVGCTRL_SAMPLENUM_32; WAIT_ADC_SYNC(); // sampling Time length Control ADC->SAMPCTRL.reg = ADC_SAMPCTRL_SAMPLEN(4); WAIT_ADC_SYNC(); // calibration values uint32_t lin0 = (*((uint32_t *)ADC_FUSES_LINEARITY_0_ADDR) & ADC_FUSES_LINEARITY_0_Msk) >> ADC_FUSES_LINEARITY_0_Pos; uint32_t lin1 = (*((uint32_t *)ADC_FUSES_LINEARITY_1_ADDR) & ADC_FUSES_LINEARITY_1_Msk) >> ADC_FUSES_LINEARITY_1_Pos; uint32_t bias = (*((uint32_t *)ADC_FUSES_BIASCAL_ADDR) & ADC_FUSES_BIASCAL_Msk) >> ADC_FUSES_BIASCAL_Pos; ADC->CALIB.bit.LINEARITY_CAL = (lin1 << 5) | lin0; ADC->CALIB.bit.BIAS_CAL = bias; refsel2 = ADC->REFCTRL.bit.REFSEL; if (refsel1 != refsel2) { /* * refer to datasheet: * 30.6.2 Basic Operation * 30.6.2.1 Initialization * Before enabling the ADC, the asynchronous clock source must be selected and enabled, and * the ADC reference must be configured. The first conversion after the reference is changed * must not be used. */ adc_get_value(); } }