Example #1
0
uint16_t adc_read_channel16(uint8_t ch)
{
   uint16_t retval;
   
   //turn on if necessary
   if(mode == DEV_MODE_OFF)
      adc_on();
   
   //set to appropriate channel
   if(ch != adc_channel)
      adc_set_channel(ch);
   
   //start the conversion, enable interrupt 
   ADCSRA |= (1 << ADIF);
   ADCSRA |= (1 << ADSC) | (1 << ADIE) | (1 << ADEN);
   
   // Wait for the conversion interrupt handler to post the semaphore.
   mos_sem_wait(&adc_sem);
   retval = adc_val;
   if(ch != adc_channel)
      adc_set_channel(adc_channel);
   
   if(mode == DEV_MODE_OFF)
      adc_off();
   
   return retval;
}
Example #2
0
uint16_t adc_poll(uint8_t ch)
{
   uint16_t ret_val;

   if(mode == DEV_MODE_OFF)
      adc_on();

   if(ch != adc_channel)
      adc_set_channel(ch);
   
   ADCSRA &= ~(1 << ADIE); //disable the AD Interrupt
   ADCSRA |= (1 << ADIF); //clear any old conversions...
   ADCSRA |= (1 << ADEN) | (1 << ADSC); //turn on on and start conversion

   //poll ADSC (clears once conversion is complete)
   while (ADCSRA & (1 << ADSC))
      ;

   ret_val = ADCL;  
   ret_val |= (ADCH << 8);

   if(ch != adc_channel)
      adc_set_channel(adc_channel);
   
   if(mode == DEV_MODE_OFF)
      adc_off();
   
   return ret_val;
}
Example #3
0
uint16_t adc_get_conversion16(uint8_t ch)
{

   ADC12CTL0 &= ~ENC;	// Clear the ENC bit to allow setting register bits

//   adc_on();      // do this elsewhere!
                    // in your code, to facilitate multiple sampling
                    // noticing that the stabilization period of the Vref is quoted 17mS
                    // for each time you do this!

   adc_set_channel(ch);

   //set source of sampling signal (SAMPCON) to be the output of
   //the sampling timer
//   ADC12CTL1 |= SHP;      // This sets Pulse Sampling Mode....do that in adc_init not here

   //start the conversion
   ADC12CTL0 |= ENC | ADC12SC;

   //polling wait for conversion to complete
   while(ADC12CTL1 & ADC12BUSY);

   uint16_t sample = ADC12MEM0;
   return sample;
}
Example #4
0
//**************************************************************************************
//
//!  adc_init
//!
//!  @param    ADCx: ADCx.ADC_CHANNEL, ADCx.ADC_PRESCALAR, ADCx.ADC_REF,
//!                           ADCx.ADC_MODE
//!
//!  @return   none
//!
//!  @brief    This function is used to initialize the ADC peripherial.
//
//**************************************************************************************
void adc_init(ADC_Init ADCx)
{
    adc_set_channel(ADCx.ADC_CHANNEL);
    adc_pin_init(ADCx.ADC_CHANNEL);
    adc_set_prescalar(ADCx.ADC_PRESCALAR);
    adc_set_ref(ADCx.ADC_REF);
    adc_set_mode(ADCx.ADC_MODE);
}
Example #5
0
static void adc_configure(int ain_id)
{
	/* Set ADC channel */
	adc_set_channel(0, ain_id);

	/* Disable DMA */
	STM32_ADC_CR2 &= ~(1 << 8);

	/* Disable scan mode */
	STM32_ADC_CR1 &= ~(1 << 8);
}
Example #6
0
void adc_init() {
	#if DEBUG_L(1)
	fprintf_P(stderr,PSTR("\nadc: init"));
	#endif
	power_adc_enable();

	//Set Voltage to AVCC with external capacitor at AREF pin
	ADMUX|= (uint8_t)(1<<REFS0);
	ADMUX&=(uint8_t)~(1<<REFS1);
	//ADMUX&=~(1<<ADLAR); // Default disabled
	
	// Enable ADC, Inturupt, Trigger mode and set prescaler
	//ADCSRA=(((1<<ADEN)|(1<<ADIE)|(1<<ADATE))&0b11111000)|(ADC_PRESCALE);
	ADCSRA|= (uint8_t)(1<<ADEN)|(1<<ADIE)|(1<<ADATE);
	ADCSRA = (uint8_t)(ADCSRA & 0b11111000)|((uint8_t)ADC_PRESCALE);
	
	// Enable Free Running Mode 
	ADCSRB|= (1<<7); //reserved bit.
	ADCSRB&= (uint8_t)~(0b111); //(ADTS2:0)=0
	
	// Disable Digital reads from analog pins
	DIDR0 |= (uint8_t)((1<<ADC4D)|(1<<ADC5D)|(1<<ADC6D)|(1<<ADC7D));
	
	set_sleep_mode(SLEEP_MODE_ADC);
	#if DEBUG_L(3)
	fprintf_P(stderr,PSTR("\nadc: init: setup convertions"));
	#endif
	adc_set_channel(curr_ch);
	//Start the convertions
	ADCSRA|= (1<<ADSC);

	// Wait one adc clock cycle and change the channel, done by interupt later.
	_delay_loop_2(ADC_CYCLE_DELAY);
	adc_set_channel(++curr_ch);
	
	// Wait for one set of convertions to complete.
	//_delay_loop_2(ADC_CYCLE_DELAY*26);
	#if DEBUG_L(1)
	fprintf_P(stderr,PSTR("\t[done]"));
	#endif
}
Example #7
0
uint16_t adc_get_conversion16(uint8_t ch)
{
   ADC12CTL0 &= ~ENC;	// clear the ENC bit to allow setting control registers

   adc_set_channel(ch);
   
   //start the conversion
   ADC12CTL0 |= ENC | ADC12SC;

   //polling wait for conversion to complete
   while(ADC12CTL1 & ADC12BUSY);

   uint16_t sample = ADC12MEM0;
   return sample;
}
Example #8
0
static void __attribute__((unused)) adc_configure_all(void)
{
	int i;

	/* Set ADC channels */
	STM32_ADC_SQR1 = (ADC_CH_COUNT - 1) << 20;
	for (i = 0; i < ADC_CH_COUNT; ++i)
		adc_set_channel(i, adc_channels[i].channel);

	/* Enable DMA */
	STM32_ADC_CR2 |= (1 << 8);

	/* Enable scan mode */
	STM32_ADC_CR1 |= (1 << 8);
}
Example #9
0
void adc_read(double *data, uint8_t mode)
{
	int16_t raw;
	uint8_t msb, lsb;

	adc_set_channel(mode);

	ADCSRA |= (1 << ADSC); //Starter konvertering.

	while (ADCSRA & (1 << ADSC)); //Venter på at konverteringen er ferdig.

	lsb = ADCL; //Leser verdien på ADC-en.
	msb = ADCH;

	raw = (msb << 8) | (lsb);
	
	*data = ((double)raw * 5) /  1023; //Gjør om til spenning.
}
Example #10
0
void battery_callback()
{
	if (!charging_allowed)
		return;

	adc_configure(&batt_adc_config);
    BATTERY_VOLTAGE_TRIS = 1;
	BATTERY_VOLTAGE_DIGITAL = 0;

    adc_set_channel(1);
    last_battery_voltage = adc_convert_one();

    //Disable charging if battery voltage is greater than max charge level
    if (last_battery_voltage >= kBatteryChargedLevel)
    	disable_charging();
    else if (last_battery_voltage < kBatteryHysteresisLevel) //Reenable charging once battery level falls below hysteresis
    	enable_charging();
}
void main(void)
{

  lcd_init();
  draw_adc_display(); 

  while(1)
      { 
         
/* Another method for ADC_INT_DIS and ADC_INT_EN_SLEEP mode is shown below */
/* 
         unsigned int adc_result=0;
         
         adc_init(ADC_INT_DIS, 200, 0); 
         adc_result=adc_convert(0);
*/

/* After a adc_init() command using interrupt, at least one conversion will take place. */
/* If you use ADC_INT_DIS mode no conversion will be initiated */
#if  ADC_NO_INT_MODE_NEEDED == 1 
         /* Dont use ADC interrupt */
         adc_init(ADC_INT_DIS, 200, 0); 
         adc_convert(0);
#endif
#if  ADC_SLEEP_MODE_NEEDED == 1 
         /* Start conversion with sleep. for subsequent conversions use the adc_convert(x) function */         
         adc_init(ADC_INT_EN_SLEEP, 200, 1); 
         adc_convert(2);
#endif
#if  ADC_INT_MODE_NEEDED == 1 
         /* Start auto conversions in channel 3 using interrupt */
         adc_init(ADC_INT_EN, 200, 3); 
         /* switch to channel 4. The switch will be done after one complete conversion at channel 3 */
         /* the function will return after at least one conversion is perfomed at channel 4 */
         adc_set_channel(4);           
#endif
#if  ADC_SCAN_MODE_NEEDED == 1      
         /* Scan channels 5,6,7 */
         adc_init( ADC_INT_EN_SCAN, 200, (1<<ADC_CH5)|(1<<ADC_CH6)|(1<<ADC_CH7) ); 
#endif         
         display_adc_readings();               
      }
 
}
Example #12
0
/*this implements specific commands to the adc device*/
uint8_t dev_ioctl_DEV_ADC(int8_t request, ...)
{
   int arg;
   va_list ap;
   
   va_start(ap, request);
   
   switch(request) {
   case ADC_SET_CHANNEL:
      // extract from varargs (must be at least 16 bits)
      arg = va_arg(ap, int);
      adc_channel = arg; //record channel setting
      adc_set_channel(adc_channel);
      break;

   default:
      return DEV_BAD_IOCTL;
   }

   va_end(ap);
   
   return DEV_OK;
}