void calibrate(uint32_t val){ uint32_t nrOfCalibrations = 16; float percent = whitePercent; whitePercent = 0.3; if(allWhite( )){ for(uint16_t i=0;i<nrOfSensors;i++){ ADC_MIN[i]=0; } for(uint32_t i = 0; i < nrOfCalibrations; i++){ readAdc( ); for(uint32_t j = 0; j < nrOfSensors; j++){ ADC_MIN[j] += ADC_avgBuff[j]; } } for(uint16_t i=0;i<nrOfSensors;i++){ ADC_MIN[i]=ADC_MIN[i]/nrOfCalibrations; ADC_DIF[i] = ADC_MAX[i] - ADC_MIN[i]; } }else{ GPIO_ToggleBits(GPIOD, GPIO_Pin_14); for(uint16_t i=0;i<nrOfSensors;i++){ ADC_MAX[i]=0; } for(uint32_t i = 0; i < nrOfCalibrations; i++){ readAdc( ); for(uint32_t j = 0; j < nrOfSensors; j++){ ADC_MAX[j] += ADC_avgBuff[j]; } } for(uint16_t i=0;i<nrOfSensors;i++){ ADC_MAX[i]=ADC_MAX[i]/nrOfCalibrations; ADC_DIF[i] = ADC_MAX[i] - ADC_MIN[i]; } } whitePercent = percent; }
uint16_t readOversampledAdc(unsigned char adc_input) { uint16_t value = 0; for (uint8_t i=0; i<64; i++) value += readAdc(adc_input); return value / 64; }
void regulator( ){ // Read ADC. readAdc( ); AdcToPercetage( ); setLED( ); // Was the line found? if(theLineIsLost( )){ *isFindingLineValue = 1.0; isFindingLine = true; //GPIO_SetBits(GPIOD, GPIO_Pin_14); }else{ *isFindingLineValue = -1.0; isFindingLine = false; //GPIO_ResetBits(GPIOD, GPIO_Pin_14); } // Decide upon former. if(isFindingLine){ stopTimer++; if(*virtualSensorValue < 0.0){ drive_turn_right(80); } else{ drive_turn_left(90); } } else{ pid( ); } }
u16 readFilterAdc(u8 cmd) { u16 i, j; u16 buf[READ_TIMES]; u16 sum; u16 temp; for(i=0; i<READ_TIMES; i++){ buf[i]=readAdc(cmd); } for(i=0; i<READ_TIMES-1; i++){ //升序排列 for(j=i+1; j<READ_TIMES; j++){ if(buf[i]>buf[j]){ temp=buf[i]; buf[i]=buf[j]; buf[j]=temp; } } } sum=0; for(i=LOST_VAL; i<READ_TIMES-LOST_VAL; i++){ sum+=buf[i]; } temp=sum/(READ_TIMES-2*LOST_VAL); return temp; }
/**************************************************************************//** * @brief Read touchscreen X or Y position (12bit resolution) * @param[in] ch X (ADC_X) or Y (ADC_Y) touchscreen readout * @return Touchscreen X or Y position *****************************************************************************/ static uint32_t getTouchChSample12bit( ADC_SingleInput_TypeDef ch ) { int i; uint32_t value, min, max, acc; if ( ch == ADC_X ) { GPIO_PinModeSet(LCD_TOUCH_Y1, gpioModePushPull, 0); GPIO_PinModeSet(LCD_TOUCH_Y2, gpioModePushPull, 1); GPIO_PinModeSet(LCD_TOUCH_X1, gpioModeInput, 0); GPIO_PinModeSet(LCD_TOUCH_X2, gpioModeInput, 0); } else { GPIO_PinModeSet(LCD_TOUCH_X1, gpioModePushPull, 1); GPIO_PinModeSet(LCD_TOUCH_X2, gpioModePushPull, 0); GPIO_PinModeSet(LCD_TOUCH_Y1, gpioModeInput, 0); GPIO_PinModeSet(LCD_TOUCH_Y2, gpioModeInput, 0); } sInit.input = ch; ADC_InitSingle(ADC0, &sInit); delayUs( 10 ); acc = 0; max = 0; min = 4096; for ( i=0; i<5; i++ ) { value = readAdc(); acc += value; min = EFM32_MIN( min, value ); max = EFM32_MAX( max, value ); } /* Throw away largest and smallest sample */ acc = acc - min - max; /* Average */ acc = acc / 3; if ( ch == ADC_X ) { GPIO_PinModeSet(LCD_TOUCH_Y1, gpioModeInput, 0); GPIO_PinModeSet(LCD_TOUCH_Y2, gpioModeInput, 0); } else { GPIO_PinModeSet(LCD_TOUCH_X1, gpioModeInput, 0); GPIO_PinModeSet(LCD_TOUCH_X2, gpioModeInput, 0); } return acc; }
void updateSimpleSensors(void) { ADC_Init(); //Send ADC+digital pins //ADC pins for (uint8_t i=0; i<=7; i++) { uint8_t eepos = 100 + i; simpleSensorTypes[i] = eepromReadByte(eepos); if (simpleSensorTypes[i] == TYPE_ADC) //ADC { simpleSensorValues[i] = readAdc(i); } if (simpleSensorTypes[i] == TYPE_DIGIN) //DIGITAL { if (GETBIT(PINA, i) > 0) simpleSensorValues[i] = 1; else simpleSensorValues[i] = 0; } } ADCSRA &= ~(1 << ADEN); //disable ADC to save power //Digital pins for (uint8_t i=0; i<=3; i++) { uint8_t eepos = 110 + i; simpleSensorTypes[i+8] = eepromReadByte(eepos); if (simpleSensorTypes[i+8] == TYPE_DIGIN || simpleSensorTypes[i+8] == TYPE_DIGOUT) //DIGITAL { //Digital pins is C2-5 if (GETBIT(PINC, (i+2)) > 0) simpleSensorValues[i+8] = 1; else simpleSensorValues[i+8] = 0; } } //3=COUNTER, not handled here, 0=digital out }
/**************************************************************************//** * @brief Check if touch screen is "touched" * @return true if screen touched *****************************************************************************/ static bool touched( void ) { uint32_t adcValue; GPIO_PinModeSet(LCD_TOUCH_X1, gpioModePushPull, 0); GPIO_PinModeSet(LCD_TOUCH_X2, gpioModePushPull, 1); GPIO_PinModeSet(LCD_TOUCH_Y1, gpioModeInput, 0); GPIO_PinModeSet(LCD_TOUCH_Y2, gpioModePushPull, 1); delayUs( 10 ); GPIO_PinModeSet(LCD_TOUCH_Y2, gpioModeInputPull, 1); delayUs( 10 ); sInit.input = ADC_Y; ADC_InitSingle(ADC0, &sInit); adcValue = readAdc(); GPIO_PinModeSet(LCD_TOUCH_X1, gpioModeInput, 0); GPIO_PinModeSet(LCD_TOUCH_X2, gpioModeInput, 0); GPIO_PinModeSet(LCD_TOUCH_Y2, gpioModeInput, 0); return ( adcValue < 3800 ) ? true : false; }
/* The Sensor reading task */ void vTaskSensor (void *pvParameters) { bool success; unsigned char x; char sendString [ADC_READING_STRING_LEN * MAX_NUM_ADCS + 1]; /* +1 for terminator */ CodedCommand codedSensorCommand; portBASE_TYPE xStatus; while (1) { xStatus = xQueueReceive (xSensorCommandQueue, &codedSensorCommand, portMAX_DELAY); ASSERT_STRING (xStatus == pdPASS, "Failed to receive from sensor command queue."); success = false; /* Assume failure */ /* Print out what command we're going to execute */ rob_lcd_goto_xy (0, 1); rob_print_from_program_space (PSTR ("CMD: ")); if (codedSensorCommand.buffer[CODED_COMMAND_INDEX_POS] != CODED_COMMAND_INDEX_UNUSED) { rob_print_character ('#'); rob_print_unsigned_long (codedSensorCommand.buffer[CODED_COMMAND_INDEX_POS]); rob_print_character (' '); } rob_print_character (codedSensorCommand.buffer[CODED_COMMAND_ID_POS]); rob_lcd_goto_xy (0, 2); /* Now do it */ switch (codedSensorCommand.buffer[CODED_COMMAND_ID_POS]) { case '*': /* The only sensor command supported at the moment */ { RobMemset (sendString, ' ', sizeof (sendString)); calibrateAdcs(); for (x = 0; x < MAX_NUM_ADCS; x++) { unsigned int milliVolts; /* Write something like "FL:40 " (for ADC 2 of 6 (which is Front Left), object detected at 40 cm), or "FL: " if nothing is there. */ memcpy (&sendString[ADC_READING_STRING_LEN * x], channelToString[x], SENSOR_STRING_LEN); memcpy (&sendString[(ADC_READING_STRING_LEN * x) + SENSOR_STRING_LEN], NOTHING_THERE_STRING, NOTHING_THERE_STRING_LEN); milliVolts = readAdc (x); if (objectDetected (milliVolts)) { itoa (voltageToDistance (milliVolts), &(sendString[(ADC_READING_STRING_LEN * x) + SENSOR_STRING_LEN + 1]), 10); /* +1 to leave the ':' there */ } /* Overwrite the null terminator that itoa() puts in with a space */ sendString[RobStrlen (sendString)] = ' '; } sendString[sizeof (sendString) - 1] = 0; /* Add terminator */ sendSerialString (sendString, sizeof (sendString)); rob_print (sendString); success = true; } break; default: { ASSERT_ALWAYS_PARAM (codedSensorCommand.buffer[CODED_COMMAND_ID_POS]); } break; } if (!success) { sendSerialString (ERROR_STRING, sizeof (ERROR_STRING)); } } }
char isLatchClosed() { bit_set(PORTB, 2); // powering hall sensor up int value = abs(readAdc() - 128); bit_clear(PORTB, 2); // power down hall sensor return value <= 5; }