Beispiel #1
0
u16 getAverage(u8 channel)
{
	u8 i;
	u16 temp,max,min,ret;


	temp = getAdcValue(channel<<4);
	ret= temp;
	max =temp;
	min = temp;
	for(i=0;i<9;i++)
	{
		delay_us(200);
		 temp = getAdcValue(channel<<4);
	 	if(temp > max)
	 	{
			max = temp;
	 	}

		 if(temp < min)
		{
			min = temp;
	 	}
	 	ret += temp;
	}
	

	return (ret - max - min)>>3;
Beispiel #2
0
/**
 * @return kg/hour value
 */
float getRealMaf(DECLARE_ENGINE_PARAMETER_F) {
	int mafAdc = getAdcValue("maf", engineConfiguration->mafAdcChannel);
	/**
	 * here we drop from 12 bit ADC to 8 bit index
	 */
	return engine->mafDecodingLookup[mafAdc >> 4];
}
 /** Read white line sensors and encode the values in lower three LSBs */
 STATUS readWhiteLineSensors(UINT *pValue) {
	UINT wl1, wl2, wl3;
	STATUS ret;

	ASSERT(pValue != NULL);

	ret = getAdcValue(ADC_WHITE_LINE1, &wl1);   /* Left */
	ASSERT(ret == STATUS_OK); 
	ret = getAdcValue(ADC_WHITE_LINE2, &wl2);	/* Middle */
	ASSERT(ret == STATUS_OK);
	ret = getAdcValue(ADC_WHITE_LINE3, &wl3);	/* Right */
	ASSERT(ret == STATUS_OK); 

	*pValue = COLOR(wl1) << 2 | COLOR(wl2) << 1 | COLOR(wl3);

	return STATUS_OK;
 }
Beispiel #4
0
/**
 * get battery voltage
 * Measure AVCC again the the internal band gap reference
 *
 *	REFS1 REFS0          --> internal bandgap reference
 *	MUX3 MUX2 MUX1 MUX0  --> 1110 1.1V (VBG) (for instance Atmega 328p)
 */
uint16_t Battery::getBatteryVoltageInternal() {
    uint16_t adcValue = getAdcValue(
                            (0<<REFS1) | (1<<REFS0),												// Voltage Reference = AVCC with external capacitor at AREF pin
                            (1<<MUX3) | (1<<MUX2) | (1<<MUX1) | (0<<MUX0)							// Input Channel = 1.1V (V BG)
                        );
    adcValue = AVR_BANDGAP_VOLTAGE * 1023 / adcValue;							// calculate battery voltage in mV

    return adcValue;
}
Beispiel #5
0
/* usbFunctionRead() is called when the host requests a chunk of data from
 * the device. For more information see the documentation in usbdrv/usbdrv.h.
 */
uchar   usbFunctionRead(uchar *data, uchar len)
{
    if(len > bytesRemaining)
        len = bytesRemaining;
    //eeprom_read_block(data, (uchar *)0 + currentAddress, len);

    *(ushort*)data = (ushort)getAdcValue();
    currentAddress += len;
    bytesRemaining -= len;
    return len;
}
Beispiel #6
0
uint16_t Battery::getBatteryVoltageExternal() {
    pinMode(tEnablePin, OUTPUT);
    digitalWrite(tEnablePin, LOW);

    uint16_t adcValue = getAdcValue(
                            (1<<REFS1) | (1<<REFS0),												// Voltage Reference = Internal 1.1V Voltage Reference
                            tAdcPin
                        );

    float tmpFloat = ((adcValue * AVR_BANDGAP_VOLTAGE) / 1023) / tFact;			// calculate battery voltage in mV
    adcValue = (uint16_t)tmpFloat;

    pinMode(tEnablePin, INPUT);

    return adcValue;
}
Beispiel #7
0
/*
 * Return TPS ADC readings.
 * We need ADC value because TunerStudio has a nice TPS configuration wizard, and this wizard
 * wants a TPS value.
 */
int getTPS12bitAdc(DECLARE_ENGINE_PARAMETER_SIGNATURE) {
#if !EFI_PROD_CODE
	if (engine->mockTpsAdcValue != MOCK_UNDEFINED) {
		return engine->mockTpsAdcValue;
	}
#endif
	if (engineConfiguration->tps1_1AdcChannel == EFI_ADC_NONE)
		return -1;
#if EFI_PROD_CODE

	return getAdcValue("tps10", engineConfiguration->tps1_1AdcChannel);
	//	return tpsFastAdc / 4;
#else
	return 0;
#endif /* EFI_PROD_CODE */
}
Beispiel #8
0
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if (htim->Instance == htim1.Instance)
	{
		HAL_GPIO_TogglePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin);
	}
	else if (htim->Instance == htim2.Instance) {
		char *buffer = malloc(sizeof(char) * 16);

		uint32_t value = getAdcValue(nextChannel);

		int l = sprintf(buffer, "%d: %08ld\n", nextChannel, value);

		UART_Send((uint8_t *)buffer, l);

		nextChannel++;

		if (nextChannel == 3) {
			nextChannel = 0;
		}
	}
}
Beispiel #9
0
static void printAdcValue(adc_channel_e channel) {
	int value = getAdcValue("print", channel);
	float volts = adcToVoltsDivided(value);
	scheduleMsg(&logger, "adc voltage : %.2f", volts);
}
 int main() {
	AdcChannel channel[] = {
		ADC_BATTERY_VOLTAGE,
		ADC_WHITE_LINE1,
		ADC_WHITE_LINE2,
		ADC_WHITE_LINE3,
		ADC_IR_PROXIMITY1,
		ADC_IR_PROXIMITY2,
		ADC_IR_PROXIMITY3,
		ADC_IR_PROXIMITY4,
		ADC_IR_PROXIMITY5,
		ADC_IR_RANGE1,
		ADC_IR_RANGE2,
		ADC_IR_RANGE3,
		ADC_IR_RANGE4,
		ADC_IR_RANGE5,
		ADC_SERVO_POD1,
		ADC_SERVO_POD1,
		ADC_LAST
	};

	char strChannel[17][20] = {
		"ADC_BATTERY_VOLTAGE",
		"ADC_WHITE_LINE1",
		"ADC_WHITE_LINE2",
		"ADC_WHITE_LINE3",
		"ADC_IR_PROXIMITY1",
		"ADC_IR_PROXIMITY2",
		"ADC_IR_PROXIMITY3",
		"ADC_IR_PROXIMITY4",
		"ADC_IR_PROXIMITY5",
		"ADC_IR_RANGE1",
		"ADC_IR_RANGE2",
		"ADC_IR_RANGE3",
		"ADC_IR_RANGE4",
		"ADC_IR_RANGE5",
		"ADC_SERVO_POD1",
		"ADC_SERVO_POD2",
		"ADC_LAST"
	};

	char buf[17];
	UINT value, idx;

	initAdc();
	initLcd();
	
	for(idx = 0; idx < (sizeof(channel)/sizeof(channel[0])); idx ++) {
		lcdClear();
		lcdCursor(1,1);
		lcdString(strChannel[idx]);
		lcdCursor(2,1);

		if(getAdcValue(channel[idx], &value) == STATUS_OK) {
			snprintf(buf, sizeof(buf), "%d", value);
			lcdString(buf);
		}
		else {
			lcdString("Error");
		}
		_delay_ms(DELAY_COUNT);
	}
	

	return 0;
 }
Beispiel #11
0
/*
 * Return TPS ADC readings.
 * We need ADC value because TunerStudio has a nice TPS configuration wizard, and this wizard
 * wants a TPS value.
 */
int getTPS10bitAdc(void) {
    int adc = getAdcValue(engineConfiguration->tpsAdcChannel);
    return (int) adc / 4; // Only for TunerStudio compatibility. Max TS adc value in 1023
}
Beispiel #12
0
static void printAdcValue(int channel) {
	int value = getAdcValue(channel);
	float volts = adcToVoltsDivided(value);
	scheduleMsg(&logger, "adc voltage : %f", volts);
}
Beispiel #13
0
int main(void)
{
    uchar   i;

    /* calibration value from last time */
    uchar calibrationValue = eeprom_read_byte(EEPROM_OSCCAL);
    if(calibrationValue != 0xff) {
        OSCCAL = calibrationValue;
    }

    wdt_enable(WDTO_1S);
    /* Even if you don't use the watchdog, turn it off here. On newer devices,
     * the status of the watchdog (on/off, period) is PRESERVED OVER RESET!
     */
    /* RESET status: all port bits are inputs without pull-up.
     * That's the way we need D+ and D-. Therefore we don't need any
     * additional hardware initialization.
     */
    odDebugInit();
    DBG1(0x00, 0, 0);       /* debug output: main starts */

    sbi(DDRB, WHITE_LED);
    sbi(DDRB, YELLOW_LED);
    cbi(DDRB, 4);
    cbi(PORTB, 4);
    sbi(MCUCR, PUD);

    timerInit();	//Create a timer that will trigger a flag at a ~60hz rate
    adcInit();		//Setup the ADC conversions
    usbInit();
    usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
    i = 0;
    while(--i) {            /* fake USB disconnect for > 250 ms */
        wdt_reset();
        _delay_ms(1);
    }
    usbDeviceConnect();
    //set_sleep_mode(SLEEP_MODE_PWR_SAVE);
    //sleep_mode();
    //sleep_enable();
    sei();
    DBG1(0x01, 0, 0);       /* debug output: main loop starts */
    for(;;) {               /* main event loop */
        DBG1(0x02, 0, 0);   /* debug output: main loop iterates */
        //sleep_cpu();
        sbi(PORTB, YELLOW_LED);
        wdt_reset();
        usbPoll();
        cbi(PORTB, YELLOW_LED);

        if(usbInterruptIsReady()) {
            /* called after every poll of the interrupt endpoint */
            reportBuffer.adcvalue = getAdcValue();
            DBG1(0x03, 0, 0);   /* debug output: interrupt report prepared */
            usbSetInterrupt((void *)&reportBuffer, sizeof(reportBuffer));
        }

        timerPoll();
        adcPoll();
        _delay_ms(5);
    }
    return 0;
}