Exemple #1
0
void InitJoystick(void) {

  // Init ADC interface
  InitADC12();

  // A7 as analog input
  P6SEL   |= BIT7;   // P6.7 ADC10 function
  P6DIR   = 0x0;     // P6.7 input direction

  // P2.6 as input
  P2SEL   &= ~BIT6;   // P2.6 as GPIO
  P2DIR   &= ~BIT6;   // P2.6 as input

}
/*--------------------------------------------------------------------------------------------------*/
int CheckVoltage(void){
	uint8_t low, high;
	InitADC12();		// make ADC12 input, disable  pull up
	ADCSRB = 0x08;		// make ADC on PK4, ADC12
	ADMUX = 0x44;
	// start the conversion
	sbi(ADCSRA, ADSC);
	// ADSC is cleared when the conversion finishes
	while (bit_is_set(ADCSRA, ADSC));
	// we have to read ADCL first; doing so locks both ADCL
	// and ADCH until ADCH is read.  reading ADCL second would
	// cause the results of each conversion to be discarded,
	// as ADCL and ADCH would be locked when it completed.
	low  = ADCL;
	high = ADCH;

	//combine the two bytes
	return (high << 8) | low;
}