Example #1
0
u16 adc_measure(void){
	u8 channel_array[16];
	u16 val,valLoaded;

	/* Select the channel we want to convert. 16=temperature_sensor. */
	channel_array[0] = 6;
	adc_set_regular_sequence(ADC1, 1, channel_array);

	/*
	  * Start the conversion directly (not trigger mode).
	  */
	adc_start_conversion_direct(ADC1);

	/* Wait for end of conversion. */
	while (!(ADC_SR(ADC1) & ADC_SR_EOC));

	val = ADC_DR(ADC1);
	
	return val;
	
	gpio_set(GPIOA, GPIO1);
	adc_start_conversion_direct(ADC1);

	/* Wait for end of conversion. */
	while (!(ADC_SR(ADC1) & ADC_SR_EOC));

	valLoaded = ADC_DR(ADC1);
	
	gpio_clear(GPIOA, GPIO5 | GPIO1);
	return val - valLoaded;
}
Example #2
0
/**
 * ADC1+2 interrupt hander
 */
void adc1_2_isr(void)
{
  uint8_t channel = 0;
  uint16_t value  = 0;
  struct adc_buf * buf;

#ifdef USE_AD1
  // Clear Injected End Of Conversion
  ADC_SR(ADC1) &= ~ADC_SR_JEOC;
  for(channel = 0; channel < NB_ADC1_CHANNELS; channel++) {
    buf = adc1_buffers[channel];
    if(buf) {
      value = *adc_injected_channels[channel];
      adc_push_sample(buf, value);
    }
  }
  adc_new_data_trigger = 1;
#endif
#ifdef USE_AD2
  // Clear Injected End Of Conversion
  ADC_SR(ADC2) &= ~ADC_SR_JEOC;
  for(channel = 0; channel < NB_ADC2_CHANNELS; channel++) {
    buf = adc2_buffers[channel];
    if(buf) {
      value = *adc_injected_channels[channel];
      adc_push_sample(buf, value);
    }
  }
  adc_new_data_trigger = 1;
#endif
}
Example #3
0
void adc_start_conversion_regular(uint32_t adc)
{
	/* Wait until the regular channel ready and ADC ON */
    while (ADC_SR(adc) & ADC_SR_RCNR);
    while (!(ADC_SR(adc) & ADC_SR_ADONS));

	/* Start conversion on regular channels. */
	ADC_CR2(adc) |= ADC_CR2_SWSTART;

	/* Wait until the ADC starts the conversion. */
	while (ADC_CR2(adc) & ADC_CR2_SWSTART);
}
Example #4
0
void adc_start_conversion_injected(uint32_t adc)
{
	/* Wait until the injected channel ready and ADC ON */
	while (ADC_SR(adc) & ADC_SR_JCNR);
    while (!(ADC_SR(adc) & ADC_SR_ADONS));

	/* Start conversion on injected channels. */
	ADC_CR2(adc) |= ADC_CR2_JSWSTART;

	/* Wait until the ADC starts the conversion. */
	while (ADC_CR2(adc) & ADC_CR2_JSWSTART);
}
void adc1_2_isr(void)
{
    /* Clear Injected End Of Conversion (JEOC) */
    ADC_SR(ADC1) &= ~ADC_SR_JEOC;
    temperature = adc_read_injected(ADC1,1);
    v_refint = adc_read_injected(ADC1,2);
    lisam_adc1 = adc_read_injected(ADC1,3);
    lisam_adc2 = adc_read_injected(ADC1,4);
}
Example #6
0
batt_state adc_testBatt(void){
	u8 channel_array[16];
	u16 val,valLoaded;
	gpio_set(GPIOA, GPIO5);
	delay_ms(25);
	/* Select the channel we want to convert. 16=temperature_sensor. */
	channel_array[0] = 6;
	adc_set_regular_sequence(ADC1, 1, channel_array);

	/*
	  * Start the conversion directly (not trigger mode).
	  */
	adc_start_conversion_direct(ADC1);

	/* Wait for end of conversion. */
	while (!(ADC_SR(ADC1) & ADC_SR_EOC));

	val = ADC_DR(ADC1);
	
	if(val < 0x0A3D){
	  return BATTCRIT;
	}
	val = val & 0xFFFC;
	gpio_set(GPIOA, GPIO1);
	delay_ms(25);
	adc_start_conversion_direct(ADC1);

	/* Wait for end of conversion. */
	while (!(ADC_SR(ADC1) & ADC_SR_EOC));

	valLoaded = ADC_DR(ADC1);
	
	gpio_clear(GPIOA, GPIO5 | GPIO1);
	
	valLoaded = valLoaded & 0xFFFC;
	// kontroluje pokles vetsi nez 0,75 V po zatizeni 15 Ohm
	if(val - valLoaded > 0x1E0){
	    return BATTLOW;
	}
	
	return BATTOK;
}
Example #7
0
uint16_t adc_read(uint8_t ch)
{
    while (!(ADC_SR(ADC1) & ADC_SR_EOC));
    uint16_t last_res = ADC_DR(ADC1);

    /* Select the channel we want to convert. 16=temperature_sensor. */
    uint8_t channel_array[16];
    channel_array[0] = ch;
    adc_set_regular_sequence(ADC1, 1, channel_array);

    /* Start the conversion directly (not trigger mode). */
    adc_start_conversion_direct(ADC1);
    
    return last_res;
}
Example #8
0
File: adc.c Project: 3yc/libopencm3
int main(void)
{
	u8 channel_array[16];
	u16 temperature;

	rcc_clock_setup_in_hse_16mhz_out_72mhz();
	gpio_setup();
	usart_setup();
	adc_setup();

	gpio_clear(GPIOB, GPIO7);	/* LED1 on */
	gpio_set(GPIOB, GPIO6);		/* LED2 off */

	/* Send a message on USART1. */
	usart_send(USART1, 's');
	usart_send(USART1, 't');
	usart_send(USART1, 'm');
	usart_send(USART1, '\r');
	usart_send(USART1, '\n');

	/* Select the channel we want to convert. 16=temperature_sensor. */
	channel_array[0] = 16;
	adc_set_regular_sequence(ADC1, 1, channel_array);

	/*
	 * If the ADC_CR2_ON bit is already set -> setting it another time
	 * starts the conversion.
	 */
	adc_on(ADC1);

	/* Wait for end of conversion. */
	while (!(ADC_SR(ADC1) & ADC_SR_EOC));

	temperature = ADC_DR(ADC1);

	/*
	 * That's actually not the real temperature - you have to compute it
	 * as described in the datasheet.
	 */
	my_usart_print_int(USART1, temperature);

	gpio_clear(GPIOB, GPIO6); /* LED2 on */

	while(1); /* Halt. */

	return 0;
}
Example #9
0
int main(void)
{
	uint8_t channel_array[16];
	uint16_t temperature;

	rcc_clock_setup_in_hse_16mhz_out_72mhz();
	gpio_setup();
	usart_setup();
	adc_setup();

	gpio_clear(GPIOB, GPIO7);	/* LED1 on */
	gpio_set(GPIOB, GPIO6);		/* LED2 off */

	/* Send a message on USART1. */
	usart_send(USART1, 's');
	usart_send(USART1, 't');
	usart_send(USART1, 'm');
	usart_send(USART1, '\r');
	usart_send(USART1, '\n');

	/* Select the channel we want to convert. 16=temperature_sensor. */
	channel_array[0] = 16;
	adc_set_regular_sequence(ADC1, 1, channel_array);

	/*
	 * Start the conversion directly (not trigger mode).
	 */
	adc_start_conversion_direct(ADC1);

	/* Wait for end of conversion. */
	while (!(ADC_SR(ADC1) & ADC_SR_EOC));

	temperature = ADC_DR(ADC1);

	/*
	 * That's actually not the real temperature - you have to compute it
	 * as described in the datasheet.
	 */
	my_usart_print_int(USART1, temperature);

	gpio_clear(GPIOB, GPIO6); /* LED2 on */

	while(1); /* Halt. */

	return 0;
}
Example #10
0
void adc_clear_eoc_injected(uint32_t adc)
{
    ADC_SR(adc) &= ~ADC_SR_JEOC;
}
Example #11
0
void adc_clear_eoc(uint32_t adc)
{
    ADC_SR(adc) &= ~ADC_SR_EOC;
}
void adc1_2_isr(void)
{
    /* Clear Injected End Of Conversion (JEOC) */
    ADC_SR(ADC1) &= ~ADC_SR_JEOC;
    temperature = adc_read_injected(ADC1,1);
}
Example #13
0
bool adc_get_overrun_flag(uint32_t adc)
{
    return ADC_SR(adc) & ADC_SR_OVR;
}
Example #14
0
bool adc_eoc(uint32_t adc)
{
    return (ADC_SR(adc) & ADC_SR_EOC) != 0;
}
Example #15
0
  void adc_isr(void)
#endif
{
  uint8_t channel = 0;
  uint16_t value  = 0;
  struct adc_buf * buf;

#if USE_ADC_WATCHDOG
  /*
    We need adc sampling fast enough to detect battery plug out, but we did not
    need to get actual actual value so fast. So timer fire adc conversion fast,
    at least 500 hz, but we inject adc value in sampling buffer only at 50hz
   */
  const uint32_t timeStampDiff = get_sys_time_msec() - adc_watchdog.timeStamp;
  const bool_t shouldAccumulateValue = timeStampDiff > 20;
  if (shouldAccumulateValue)
    adc_watchdog.timeStamp = get_sys_time_msec();

  if (adc_watchdog.cb != NULL) {
    if (adc_awd(adc_watchdog.adc)) {
      ADC_SR(adc_watchdog.adc) &= ~ADC_SR_AWD; // clear int flag
      adc_watchdog.cb();
    }
  }
#endif

#if USE_AD1
  // Clear Injected End Of Conversion
  if (adc_eoc_injected(ADC1)){
    ADC_SR(ADC1) &= ~ADC_SR_JEOC;
#if USE_ADC_WATCHDOG
    if (shouldAccumulateValue) {
#endif
    for (channel = 0; channel < nb_adc1_channels; channel++) {
      buf = adc1_buffers[channel];
      if (buf) {
        value = adc_read_injected(ADC1, channel+1);
        adc_push_sample(buf, value);
      }
    }
#if USE_ADC_WATCHDOG
  }
#endif

#if !USE_AD2 && !USE_AD3
    adc_new_data_trigger = TRUE;
#endif
  }
#endif
#if USE_AD2
  if (adc_eoc_injected(ADC2)){
    ADC_SR(ADC2) &= ~ADC_SR_JEOC;
#if USE_ADC_WATCHDOG
    if (shouldAccumulateValue) {
#endif
    for (channel = 0; channel < nb_adc2_channels; channel++) {
      buf = adc2_buffers[channel];
      if (buf) {
        value = adc_read_injected(ADC2, channel+1);
        adc_push_sample(buf, value);
      }
    }
#if USE_ADC_WATCHDOG
  }
#endif
#if !USE_AD3
    adc_new_data_trigger = TRUE;
#endif
  }
#endif
#if USE_AD3
  if (adc_eoc_injected(ADC3)){
    ADC_SR(ADC3) &= ~ADC_SR_JEOC;
#if USE_ADC_WATCHDOG
    if (shouldAccumulateValue) {
#endif
    for (channel = 0; channel < nb_adc3_channels; channel++) {
      buf = adc3_buffers[channel];
      if (buf) {
        value = adc_read_injected(ADC3, channel+1);
        adc_push_sample(buf, value);
      }
    }
#if USE_ADC_WATCHDOG
  }
#endif
    adc_new_data_trigger = TRUE;
  }
#endif


  return;
}
Example #16
0
bool adc_awd(uint32_t adc)
{
    return ADC_SR(adc) & ADC_SR_AWD;
}
Example #17
0
bool adc_get_overrun_flag(uint32_t adc)
{
    return ((ADC_SR(adc) & ADC_SR_OVR) != 0);
}
Example #18
0
bool adc_eoc_injected(uint32_t adc)
{
    return (ADC_SR(adc) & ADC_SR_JEOC) != 0;
}
Example #19
0
void adc_clear_overrun_flag(uint32_t adc)
{
    ADC_SR(adc) &= ~ADC_SR_OVR;
}
Example #20
0
void adc_clear_overrun_flag(uint32_t adc)
{
    /* need to write zero to clear this */
    ADC_SR(adc) &= ~ADC_SR_OVR;
}
Example #21
0
bool adc_get_watchdog_flag(uint32_t adc)
{
    return ((ADC_SR(adc) & ADC_SR_AWD) != 0);
}
Example #22
0
void adc_clear_watchdog_flag(uint32_t adc)
{
    ADC_SR(adc) &= ~ADC_SR_AWD;
}
Example #23
0
  void adc_isr(void)
#endif
{
  uint8_t channel = 0;
  uint16_t value  = 0;
  struct adc_buf * buf;

#if USE_ADC_WATCHDOG
  if (adc_watchdog.cb != NULL) {
    if (adc_awd(adc_watchdog.adc)) {
      ADC_SR(adc_watchdog.adc) &= ~ADC_SR_AWD; // clear int flag
      adc_watchdog.cb();
    }
  }
#endif

#if USE_AD1
  // Clear Injected End Of Conversion
  if (ADC_SR(ADC1) & ADC_SR_JEOC){
    ADC_SR(ADC1) &= ~ADC_SR_JEOC;
    for (channel = 0; channel < nb_adc1_channels; channel++) {
      buf = adc1_buffers[channel];
      if (buf) {
        value = *(&ADC_JDR1(ADC1)+channel);
        adc_push_sample(buf, value);
      }
    }
#if !USE_AD2 && !USE_AD3
    adc_new_data_trigger = TRUE;
#endif
  }
#endif
#if USE_AD2
  if (ADC_SR(ADC2) & ADC_SR_JEOC){
    ADC_SR(ADC2) &= ~ADC_SR_JEOC;
    for (channel = 0; channel < nb_adc2_channels; channel++) {
      buf = adc2_buffers[channel];
      if (buf) {
        value = *(&ADC_JDR1(ADC2)+channel);
        adc_push_sample(buf, value);
      }
    }
#if !USE_AD3
    adc_new_data_trigger = TRUE;
#endif
  }
#endif
#if USE_AD3
  if (ADC_SR(ADC3) & ADC_SR_JEOC){
    ADC_SR(ADC3) &= ~ADC_SR_JEOC;
    for (channel = 0; channel < nb_adc3_channels; channel++) {
      buf = adc3_buffers[channel];
      if (buf) {
        value = *(&ADC_JDR1(ADC3)+channel);
        adc_push_sample(buf, value);
      }
    }
    adc_new_data_trigger = TRUE;
  }
#endif

  return;
}