void main() {
	int signal2;
	int signal4;

	void initMSP430();
	IFG1=0; 													// clear interrupt flag1
	WDTCTL = WDTPW + WDTHOLD;									// disable WDT
	P1DIR |= BIT0 | BIT6;										// set leds to output

	while(1) {

		signal2 = readADC(2);									// read analog pin 2
		signal4 = readADC(4);									// read analog pin 4

		if (signal2 > 0x200) {
			P1OUT |= BIT0;										// turn on red led
			P1OUT &= ~BIT6;
		} else if (signal4 > 0x200) {
			P1OUT &= ~BIT0;
			P1OUT |= BIT6;										// turn on green led
		} else {
			P1OUT &= ~BIT0;										// turn off both leds
			P1OUT &= ~BIT6;
		}
	}
}
示例#2
0
int main(void) {
	clock_prescale_set(clock_div_16);

	uint16_t lightThreshold;
	uint16_t sensorValue;

	// Set up ADC
	ADMUX |= (1 << REFS0);			/* reference voltage on AVCC */
	ADCSRA |= (1 << ADPS1) | (1 << ADPS0);	/* ADC clock prescaler /8 */
	ADCSRA |= (1 << ADEN);			/* enable ADC */

	LED_DDR = 0xff;

	while (1) {

		lightThreshold = readADC(POT);
		sensorValue = readADC(LIGHT_SENSOR);

		if (sensorValue < lightThreshold) {
			LED_PORT = 0xff;
		}
		else {
			LED_PORT = 0x00;
		}
	}

	return (0);
}
示例#3
0
//Clutch actuation method
void actuate_clutch(void)
{
	clutch_posn = readADC(15);

	if (clutch_state == 1) {
		while (clutch_posn < clutch_half) {
			P3OUT &= ~PIN2; //DIR
			P3OUT |= PIN1; //PWMH

			clutch_posn = readADC(15);
		}

		P3OUT &= ~PIN1;
	}
	else if (clutch_state == 0) {
		while (clutch_posn > clutch_extend) {
			P3OUT |= PIN2; //DIR
			P3OUT |= PIN1; //PWMH

			clutch_posn = readADC(15);
		}

		P3OUT |= PIN2; //DIR
		P3OUT |= PIN1; //PWMH
	}
}
int main(void) {
  // -------- Inits --------- //
  uint16_t lightThreshold;
  uint16_t sensorValue;
  // Set up ADC
  ADMUX |= (1 << REFS0);                  /* reference voltage on AVCC */
  ADCSRA |= (1 << ADPS1) | (1 << ADPS0);     /* ADC clock prescaler /8 */
  ADCSRA |= (1 << ADEN);                                 /* enable ADC */

  LED_DDR = 0xff;
  // ------ Event loop ------ //
  while (1) {

    lightThreshold = readADC(POT);
    sensorValue = readADC(LIGHT_SENSOR);

    if (sensorValue < lightThreshold) {
      LED_PORT = 0xff;
    }
    else {
      LED_PORT = 0x00;
    }
  }                                                  /* End event loop */
  return 0;                            /* This line is never reached */
}
示例#5
0
int main()
{
	uint16_t t1RawADC = 0;
	uint16_t t2RawADC = 0;
	float temp1 = 0;
	float temp2 = 0;

	initADC();
	lcd_init();
	while(1)
	{
		t1RawADC = readADC(TEMP1_ADC_CHANNEL);
		t2RawADC = readADC(TEMP2_ADC_CHANNEL);

		temp1 = convertRawADCToTemp(t1RawADC);
		temp2 = convertRawADCToTemp(t2RawADC);

		printTemperatures(temp1, temp2);
//		LCD_clear();
//		LCD_int(temp1);

		_delay_ms(100);
	}

}
示例#6
0
文件: main.c 项目: mlautman/ROBOCKEY
	void ADC_go_to_puck(){
		float 	kp_puck_offset		=  	1;
		float kd_puck_offset = 0;//100;
		float kp_puck_slow = 0;
		float kd_puck_slow = 0;
		if (abs(theta_angleOG)<120){
			kp_puck_slow = 0;//.2;
			kd_puck_slow = 25;
		}

		//perform a J-Stroke if facing own goal, go straight at puck if facing other goal
		photo_ar = readADC(0);
		photo_al = readADC(1);
		int diff = (photo_al-photo_ar);
		//int d_diff = diff - lastPhotoDiff;

		//slow down as you approach the puck
		int d_photo = abs(photo_ar -last_photo_ar)+abs(photo_al-last_photo_al); //difference in photo val
		int d_offset_photo = abs(photo_al - last_photo_al) + abs(last_photo_ar - photo_ar);
		int kd_photo = d_photo*kd_puck_slow;
		int kp_photo = kp_puck_slow*(photo_ar+photo_al);
		int slow = kd_photo + kp_photo;

		//differential turn to puck
		int kp_diff = diff*kp_puck_offset;
		int kd_diff = d_offset_photo*kd_puck_offset;
		int offset = kp_diff;


		last_photo_ar = photo_ar;
		last_photo_al = photo_al;

		lastPhotoDiff =  diff;

		int adcPWM = 700; 
//		adcPWM -= (int)(float)(photo_ar + photo_al) /* (velocity)*/    ; 
		//int offset = (int)((float) diff * kp_puck);

		int photo_left; int photo_right;
		if (offset< 0 ) {
			photo_left = adcPWM - slow;
			photo_right = adcPWM -  abs(kp_diff) + kd_diff - slow;
		}else	{ photo_left = adcPWM - abs(kp_diff ) + kd_diff - slow;
			photo_right = adcPWM - slow ;
		}if (photo_left < pwmMin) photo_left = pwmMin;
		if (photo_right < pwmMin) photo_right = pwmMin;
		set_motors(photo_left,photo_right);
		if (debug_goto) {
			m_usb_tx_string("\n\tphoto_ar: ");
			m_usb_tx_int(photo_ar);
			m_usb_tx_string("\tphoto_al: ");
			m_usb_tx_int(photo_al);
			m_usb_tx_string("\tkd_photo: ");
			m_usb_tx_int(kd_photo);
			m_usb_tx_string("\tkp_photo: ");
			m_usb_tx_int(kp_photo);
			m_usb_tx_string("\n\r"); 
		}
	}
int main( void )
{
	uint16_t leftLightSensor = 0;
	uint16_t rightLightSensor = 0;
	char buffer0[5];
	char buffer1[5];

	initUSART();

	initADC();

	initPWM();

	while ( 1 )
	{
		/* Start ADC7 conversion */
		leftLightSensor = readADC( 7 );

		/* Convert 10-bit uint16_t adcValue to ASCII and store in buffer */
		itoa( leftLightSensor, buffer0, 10 );

		/* Print out leftLightSensor ADC Value */
		printString( "ADC Channel 6 (Left CD Sensor): " );
		printString( buffer0 );
		printString( "\n\n" );

		/* Start ADC6 conversion */
		rightLightSensor = readADC( 6 );

		/* Convert 10-bit uint16_t adcValue to ASCII and store in buffer */
		itoa( rightLightSensor, buffer1, 10 );

		/* Print out rightLightSensor ADC Value */
		printString( "ADC Channel 7 (Right CD Sensor): " );
		printString( buffer1 );
		printString( "\n\n" );

		/* Arch right if rightLightSensor reading is greater than leftLightSensor */
		if ( ( rightLightSensor - 100 ) > leftLightSensor )
		{
			leftwheel( 55, 1 );
			rightwheel( 35 , 1 );
		}
		/* Arch left if leftLightSensor reading is greater than rightLightSensor */
		else if ( ( leftLightSensor - 100 ) > rightLightSensor )
		{
			leftwheel( 35 , 1 );
			rightwheel( 45, 1 );
		}
		/* Go forward if both light sensors have the same reading */
		else
		{
			leftwheel( 35 , 1 );
			rightwheel( 35 , 1 );
		}
		_delay_ms( 500 );
	}
}
/*le o AD de um dos eixos analogicos do joystick*/
uint16_t readJoystick(char eixo){
	if(eixo == 'x'){
		return readADC(ADC_Channel_14);	//joystick X esta ligado no canal 14 do ADC1
	}else if(eixo == 'y'){
		return readADC(ADC_Channel_15); //joystick Y esta ligado no canal 15 do ADC1
	}else{
		return 9999;					//eixo invalido
	}
}
示例#9
0
// timer interrupt handler, read the ADC values and set the parameters
// for the sensor values, and prints them to console.
void hw_timer_handler(void)
{
    front_sensor_val = readADC(0);
    left_sensor_val = readADC(1);
    right_sensor_val = readADC(2);
    back_sensor_val = readADC(3);
    printf ("Front value: %d \nBack value: %d \nRight value: %d \nLeft value: %d \n",
    front_sensor_val, back_sensor_val, right_sensor_val, left_sensor_val);
}
示例#10
0
文件: main.cpp 项目: lmurray/softfm
int main()
    {
    //------------------------------------------------------------------------
    // Initialization

    sei();

    // The Arduino bootloader connects pins 0 and 1 to the USART, disconnect
    // them here so they can be used as normal digital I/O.
#if defined(__AVR_ATmega168__)
    UCSR0B = 0;
#else
    UCSRB = 0;
#endif

    //------------------------------------------------------------------------
    // Setup

    // Set up directions
    DDRB |=  ( 1 << LED_PIN );
    DDRB |=  ( 1 << LO_OUT );
    DDRD |=  ( 1 << ADC_CS2 );
    DDRD |=  ( 1 << ADC_CS1 );
    DDRD |=  ( 1 << ADC_CLK );
    DDRD |=  ( 1 << SFT_CLK );
    DDRD &= ~( 1 << SFT_D3 );
    DDRD &= ~( 1 << SFT_D2 );
    DDRD &= ~( 1 << SFT_D1 );
    DDRD &= ~( 1 << SFT_D0 );

    // Set initial output signals
    PORTB |= ( 1 << LED_PIN );
    PORTD |= ( 1 << ADC_CS2 );
    PORTD |= ( 1 << ADC_CS1 );

    // TODO: Period needs to take into account multiplication by 1024 * 1024
    FrequencyTimer2::setPeriod( 1000000 / 982 + 2 ); // P = 1 / f + adjust
    FrequencyTimer2::enable();

    //------------------------------------------------------------------------
    // Main loop

    while( true )
        {
        PORTD &= ~( 1 << ADC_CS1 ); // Enable/start ADC1 conversion (Lower)
        readADC();
        PORTD |= ( 1 << ADC_CS1 ); // Disable/end ADC1 conversion (Raise)

        PORTD &= ~( 1 << ADC_CS2 ); // Enable/start ADC2 conversion (Lower)
        readADC();
        PORTD |= ( 1 << ADC_CS2 ); // Disable/end ADC2 conversion (Raise)
        }

    return 0;
    }
示例#11
0
int main(void) {
	char lcdString[80];

	// Initialize serial port for output
    uart_init();
    stdout = &uart_output;
    stdin  = &uart_input;

	setupADC(0x02);

	// Setup the LCD
	DDRB = 0xFF; // Set port B as output!
	PORTB = 0x00; // And zero it
	hd44780_connection *conn_struct = hd44780_createConnection(&PORTB, 0, &PORTB, 5, &PORTB, 4, &PORTB, 6);
	hd44780_driver *connDriver = hd44780_hl_createDriver(TMBC20464BSP_20x4, conn_struct, (uint8_t (*)(void*))hd44780_initLCD4Bit, (void (*)(void*, uint16_t))hd44780_sendCommand);
	hd44780_hl_init(connDriver, 0, 0);

	I2C_Config *masterConfig = I2C_buildDefaultConfig();
	I2C_masterBegin(masterConfig);

	DS1307_ToD time;	
	DS1307_setSQW(1, 0, DS1307_SQW_1Hz);
	DS1307_readToD(&time);

	DDRC &= 0xF7;
	PORTC |= 0x8;
	// See here for interrupts http://www.protostack.com/blog/2010/09/external-interrupts-on-an-atmega168/
	PCICR |= 1 << PCIE1;
	PCMSK1 |= 1 << PCINT11;
	sei();


	_delay_ms(500);
	fprintf(stdout, "Start loop...\n");	

	uint16_t adc_val = readADC();
	while (1) {
		if (interruptReceived) {
			interruptReceived = 0;
			DS1307_readToD(&time);

			sprintf(lcdString, "%.2u-%.2u-%.4u\n%.2u:%.2u:%.2u\n\n%.1fC", time.dayOfMonth, time.month, time.year, time.hours, time.minutes, time.seconds, adcReadToTemp(adc_val, REF_VOLTAGE, SERIES_RESISTOR, THERMISTOR_NOMINAL, TEMPERATURE_NOMINAL, B_COEFFICIENT));
			hd44780_hl_printText(connDriver, 0, 0, lcdString);
		}

		adc_val = (adc_val +  readADC()) / 2;
	}

    return 0;
}
int main()
{
	int value1,value2;
	initADC(MGRNUM);
	while(1)
	{
		value1 = readADC(MGRNUM, AIN0);
		pauseNanoSec(100000);
		value2 = readADC(MGRNUM, AIN1);
		printf("value1: %d  value2: %d\n",value1,value2);
		pauseNanoSec(100000);
	}

	return 0;
}
示例#13
0
文件: widget.cpp 项目: Qmax/PT6
void Widget::initialiseHWLibraries()
{
    QPluginLoader loader4("libQmaxPTInterface.so",this);
    ILineEdit = qobject_cast<IQmaxLineEdit*>(loader4.instance());
    INumberPanel=qobject_cast<IQmaxNumberPanel*>(loader4.instance());
	QPluginLoader loaderApp("libPTApplicationcardInterface.so", this);
	IAppCard = qobject_cast<IApplicationCardInterface*> (loaderApp.instance());
	qDebug() << "Appcard" << IAppCard;
	IAppCard->setDeviceName(SLOT0);
	IAppCard->enumerateAPPCard();
	IAppCard->setSPIAppendBit(0xC000);
	QPluginLoader loader2("libSPIMemoryInterface.so", this);
	ISPIMemory = qobject_cast<ISPIMemoryInterface*> (loader2.instance());
	ISPIMemory->setHardwarwObject(IAppCard);
	m_objAD7190Component = new AD7190Component(IAppCard);
	m_objAD5318Component = new AD5318Components(IAppCard);
	resetDAC();
	m_objAD7190Component->resetADC(1);
	m_objAD7190Component->resetADC(2);
	m_nFrequency=1000.0;
	m_nAVGSamples=1.0;
	ADCtimer = new QTimer(this);
	connect(ADCtimer, SIGNAL(timeout()), this, SLOT(readADC()));


}
示例#14
0
void main()
{
   auto int rc;

	brdInit();

	// initially start up A/D oscillator and charge up cap
	anaIn(0, SINGLE, GAINSET);

	printf("\t\t<<< Analog input channels 0 - 6: >>>\n");
	printf("\t LN0IN\t LN1IN\t LN2IN\t LN3IN\t LN4IN\t LN5IN\t LN6IN\n");
	printf("\t------\t------\t------\t------\t------\t------\t------\n");

  logAddr = MINFLASHADDR;

   while(1)
   {
 		costate{   // Task 1
			waitfor(ADSPIBUSY != (rc=readADC()));
         if(rc<0){
         	 exit(rc);             // 0 = successful finish
         }
     		waitfor(DelaySec(60));     // Delay, yield to other costate 1 minute
      }

 		costate{  // Task 2
	      waitfor(sampleComplete);   // wait for flag to log & display data
			logADC();
      }
   }
}
示例#15
0
// returns the peak frequency
uint16_t fftSingleCycle() {
    // 10 bits to 6 bits unsigned, range [0, 63]
    // then shift so have range [-32, 31]
    uint16_t start_t = TMR0;
    for (int i = 0; i < FFT_LEN; i++)
        fftReal[i] = readADC();
    
    uint16_t stop_t = TMR0;
    
    // pre-process for the fft
    for (int i = 0; i < FFT_LEN; i++)
        fftReal[i] = (int16_t)((uint16_t)fftReal[i] >> 4) - 31;
    
    // Timer clock is Fosc/4, so do >>2
    sample_freq = (_XTAL_FREQ << (FFT_LEN_BITS))/((stop_t > start_t) 
                            ? (stop_t - start_t) 
                            : (65536 - start_t + stop_t));
    
    // Reset the imaginary array
    for (int i = 0; i < FFT_LEN; i++)
        fftImag[i] = 0;
    
    max_index = optfft(fftReal, fftImag);
    peak_freq = (max_index * sample_freq) >> (FFT_LEN_BITS+2);
    return (uint16_t)peak_freq;
}
示例#16
0
int analogRead(adcPin pin)
{
  uint32_t value = readADC(pin);

  if(readResolution <= defaultResolution)
  value = value >> (defaultResolution - readResolution);
  else
示例#17
0
 void action()
 {
    //cmd byte will be in 1st and 2nd postions
   //check for analog read
   if((buffer[2]&(0xE0))==0x60)
   {
     //analog read
	 analogResponse(readADC(buffer[1]&0x0F));
	
   } 
   else if( (buffer[2]&(0xFC))==0x04 )//check for digital action
   {
     //check for read or write
	 if((buffer[2]&(0x03))==0x00)
	 {
	    //digital read
		digitalRead(buffer[1]&(0x1F));
	 }
	 else if((buffer[2]&(0x03))==0x02)
	 {
	   //digital read output register
	   digitalReadOut(buffer[1]&(0x1F));
	 }
	 else
	 {
	   //digital write
	   digitalWrite(buffer[1]&(0x1F),buffer[2]&(0x02));
	 }
   }
   else
   { 
     sendResponse("OK");
   }
 }
 // Return the voltage at capacitor in Volt
 float readCapVoltage(volatile uint8_t number){
	 uint16_t volt = readADC(number);
	 float zdiode_corr = 0;
	 if (volt > 430){
		 zdiode_corr = pow((volt-430),2.2)/200000000;
	 }
	 return ((float) volt) * (ADC_FACTOR + zdiode_corr);
 }
示例#19
0
int main(void){
// ----- Initialize ----- //
 uint16_t xaxis; 	//PC0 ADC VALUE
 uint16_t yaxis; 	//PC1 ADC VALUE 
 DDRB |= (1 <<PB0)|(1<<PB5)|(1<<PB6)|(1<<PB7);
 PORTC |= (1<<PC2); 	//THUMBSTICK PIN PULLUP RESISTOR SET

 initUSART();
 initADC();
 initPWM();
 OCR1B = VERT_MAX_POS;

 while(1){
  xaxis = readADC(PC0);
  yaxis = readADC(PC1);

  //X-AXIS 360 DEGREE ROTATION 
  if(xaxis == 514 || xaxis == 515)
	OCR1A = 1023;
  else if(xaxis < 514)
	OCR1A = 180;
  else
	OCR1A = rotateRight(xaxis);
  
  if(yaxis < 500 && OCR1B > VERT_MIN_POS){
	if(yaxis >=300)
		OCR1B -=LOWSPEED;
	else if(yaxis >=100)
		OCR1B -=MEDIUMSPEED;
	else
		OCR1B -=HIGHSPEED;

  }else if(yaxis > 530 && OCR1B < VERT_MAX_POS){
	if(yaxis <=700)
		OCR1B +=LOWSPEED;
	else if(yaxis <=900)
		OCR1B +=MEDIUMSPEED;
	else
		OCR1B +=HIGHSPEED;
   }
 _delay_ms(20); 
checkJButtonState();
}
 return(0);
 }
示例#20
0
// Timer1 ISR function Frequency: 10kHz   Priority: Level 7
void __ISR(_TIMER_1_VECTOR, IPL7SOFT) LEDBrightness()
{
//  This logic could be run in the ewhile(1) loop, but will not guarantee
//  that OC1RS is reset every time, resulting in a less smooth dimmer i.e.
    unsigned int ADCval;
    ADCval = readADC();            // read ADC (0-1024)
    OC1RS =  (40000*ADCval)/1024;   //convert ADV calue to duty cycle
    IFS0bits.T1IF = 0;             // clear interrupt flag
}
示例#21
0
// Reading PIR status using ADC on channel AN1
void readPIR(void)
{
    initADC(1);
    readADC();
    resPIR = resADC; 
    if(resPIR > 500)
        resPIR = 1;
    else
        resPIR = 0;
}
示例#22
0
int mcp3208_IR_cm(int channel) {
const int MCP3208_dinoutPIN = 27;
const int MCP3208_clkPIN = 25;
const int MCP3208_csPIN = 26;
const float referenceVoltage = 5.0; // MCP3208 reference voltage setting. I use 5.0v for the 5.0v IR sensors from Parallax
  int mcp3208reading  = readADC(channel, MCP3208_dinoutPIN, MCP3208_clkPIN, MCP3208_csPIN);
  float mcp3208volts = (float)mcp3208reading * referenceVoltage / 4096.0;
  int mcp3208cm = 27.86 * pow(mcp3208volts, -1.15); // https://www.tindie.com/products/upgradeindustries/sharp-10-80cm-infrared-distance-sensor-gp2y0a21yk0f/
  return(mcp3208cm);
}
示例#23
0
文件: main.c 项目: mlautman/ROBOCKEY
	void find_puck(void){
		//TODO make this usable

		/* IR pt  L C P R	 */
		photo_d1 = ~(PINB & 0x0F) &0x0F ;


		if ((photo_d1 & 0b00000100) != 0b00000000){	
			//		if (Super_state == PT_super);//  if (direction == 1){rotate(-1);} else rotate(1);
			Super_state = ADC_super; 
		}
		if ((photo_d1 & 0b00000010) != 0b00000000){ 	Super_state = To_Goal_super;  }
		if ( (photo_d1 & 0b00000110) == 0 ) {		Super_state = PT_super; }

		switch (photo_d1){
			case 0b00000000:	rotate(0);  break; 
			case 0b00000001: 	rotate(1); break; 
			case 0b00001000: 	rotate(-1); break; 
			case 0b00001001: 	if (position[1]>0.0 ) // robot is to the top of the board
							if( (int)(10*position[2]) > -15  && (int)(10*position[2]) <  15 ) { rotate(1);}
							else { rotate(-1);}
						else {
							if ((int)(10*position[2]) > -15  && (int)(10*position[2]) < 15 ){ rotate(-1);}
							else { rotate(1); }
						}break;
		}	

		if (debug_find_puck){
			photo_ar = readADC(0);
			photo_al = readADC(1);
			m_usb_tx_string("\t digital left: \t "); 	m_usb_tx_int(((photo_d1 >> 3) & 0x01));
			m_usb_tx_string("\t digital cent: \t "); 	m_usb_tx_int(((photo_d1 >> 2) & 0x01));
			m_usb_tx_string("\t digital right: \t "); 	m_usb_tx_int(((photo_d1 >> 0) & 0x01));
			m_usb_tx_string("\t digital near: \t "); 	m_usb_tx_int(((photo_d1 >> 1) & 0x01));
			m_usb_tx_string("\t analog L: \t "); 		m_usb_tx_int( photo_al );
			m_usb_tx_string("\t analog R: \t "); 		m_usb_tx_int( photo_ar);
			m_usb_tx_string("\t");
			m_usb_tx_int(Super_state);
			m_usb_tx_string("\n\r");
		}
	}	 
示例#24
0
文件: uv3r.c 项目: haxwithaxe/UV3RMod
void getSelfBias(void)
{

  i	= readADC(ADC_BIAS);		// ADC_15 

  asm("	ldx	_i				;
      lda	#0CAh				; 3280 
      ldy	#0Ch				;
      div					;
      sta	_selfBias			;
      ");						//

}
示例#25
0
void getPulsedLightSensors()
{
	switch(getPulsedLight_check)
	{
		case 0:
			setBottomLEDleft(1);
			setBottomLEDright(1);

			timer_puls_bottomled = TIMER_PULS_BOTTOMLED;
			getPulsedLight_check = 1;
		break;
		case 1:
			if(timer_puls_bottomled == 0)
			{
				lichtsensor_up_pulsed_1 = readADC(7);
				lichtsensor_down_pulsed_1 = readADC(6);
				
				setBottomLEDleft(0);
				setBottomLEDright(0);

				getPulsedLight_check = 2;
				timer_puls_bottomled = TIMER_PULS_BOTTOMLED;
			}
		break;
		case 2:
			if(timer_puls_bottomled == 0)
			{
				lichtsensor_up_pulsed_2 = readADC(7);
				lichtsensor_pulsed_up = (lichtsensor_up_pulsed_2 - lichtsensor_up_pulsed_1);
				
				lichtsensor_down_pulsed_2 = readADC(6);
				lichtsensor_pulsed_down = (lichtsensor_down_pulsed_2 - lichtsensor_down_pulsed_1);
				
				getPulsedLight_check = 0;
			}
		break;
	}
}
示例#26
0
void storeNewADC(uint16_t * arr, uint8_t size, uint8_t channel){
  
  uint8_t i;
  arr = arr + (size-1);
  
  //starting with last element of array, store value from the previous element
  for (i=0;i<(size-1);i++){
    *arr = *(arr-1);
    arr--;
  }
  
  //read ADC for newest value into array
  *arr = readADC(channel);
}
short averageADC(char times){
	char i;
	short acum = 0;
	for ( i = 0; i < times; i++) {
		acum+=readADC();
		_delay_ms(5);
	}
	acum = acum/times;
	if (!inScore && acum < GOAL_THRESH){
		++score;
		inScore = 1;
	} else if (inScore && acum >= GOAL_THRESH) {inScore = 0;}
	return acum;
}
示例#28
0
文件: uv3r.c 项目: madberry/UV3RMod
unsigned char getKeys()
{

  static unsigned char integrator = 0;
  static unsigned char prevKeys = 0;
#define KEYS_ADC_OFFSET 10
  unsigned char keys = 0;

  unsigned char keysADC = readADC(ADC_3);
  if (keysADC > 255 - KEYS_ADC_OFFSET)
    keys |= 0; //no key presses
  else if (keysADC > 207 - KEYS_ADC_OFFSET)
    keys |= LR_KEY;  
  else if (keysADC > 155 - KEYS_ADC_OFFSET)
    keys |= FA_KEY;
  else if (keysADC > 100 - KEYS_ADC_OFFSET)
    keys |= UV_KEY;
  else if (keysADC > 50 - KEYS_ADC_OFFSET)
    keys |= MENU_KEY;
  else 
    keys |= VOL_KEY;



  if (!R13) //PPT button NC
    keys |= PTT_KEY;

#define INTMAX 50

  //Debounce using integrator
  if (prevKeys == keys)
  {
    if (integrator > 0)
      integrator--;
  }
  else if (integrator < INTMAX)
    integrator++;

  if (integrator == 0)
    return 0;
  else if (integrator >= INTMAX)
  {
    prevKeys = keys;
    integrator = INTMAX;
    return prevKeys;
  }

  return 0;

}
示例#29
0
void printTempWithCh(uint8_t ch) {
	uint32_t adcVal = 0;

	enableADCWithCh(ch);

	for(uint16_t count = 0; count < ADC_COUNT; count++){
		adcVal += readADC();
	}

	adcVal /= ADC_COUNT;

	putStr("T = ");
	printTemp(adcVal);
	putStr(" C\n");
}
示例#30
0
void initActuators(void)
{
	shift_posn = readADC(14);
	in_neutral = 0;								// did this fix anything?

	if (shift_posn < rest_posn) {
		while (shift_posn < rest_posn) {
			P3OUT |= PIN3; //DIR
			P4OUT |= PIN2; //PWMH

			shift_posn = readADC(14);
		}
		P4OUT &= ~PIN2;
	}
	else if (shift_posn > rest_posn) {
		while (shift_posn > rest_posn) {
			P3OUT &= ~PIN3; //DIR
			P4OUT |= PIN2; //PWMH

			shift_posn = readADC(14);
		}
		P4OUT &= ~PIN2;
	}
}