How to show the ADC value in the USART?

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi! i´m trying to show the adc value in the usart but i can´t because i need to convert an uint16t[] with the accumulate samples of the ADC of the sensor 

to uint8 value that´s  required for  "usart_write_buffer_wait(&usart_instance,ADC_value,sizeof(value));" what can i do?

 

this is my main without the configurations but with debugging i got the adc values in an array of 100 elements 

 

#include <asf.h> 
#include <string.h>

#include <stdio.h>
#include <stdlib.h>

struct usart_module usart_instance;
struct tcc_module tcc_instance;
struct adc_module adc_instance;

 

//DEFINICIONES
//#define PWM_TOP_VALUE 1023
#define FILTER_SIZE 100

volatile uint8_t tx_character;
volatile uint8_t rx_character;
volatile bool command_flag;
uint16_t ADC_value[FILTER_SIZE];
uint8_t Text1[] = "Alarm on, ADC_value is:";
uint8_t Text2[] = "Alarm off, ADC_value is: ";

 

int main (void)
{
    
    system_init();
    adc_setup();
    usart_setup();
    tcc_setup();
    system_interrupt_enable_global();
    port_setup();
    
   
    
    
    adc_read_buffer_job(&adc_instance, ValorADC, FILTER_SIZE);

 

while (1) 
    {
        
        if ( ADC_value < 500){
           
            
            
            port_pin_set_output_level(PIN_PB08,1); 
            usart_write_buffer_wait(&usart_instance, Text1,sizeof(Text1));
            
         
            usart_write_buffer_wait(&usart_instance,ADC_value,sizeof(value));
        
           
            
        }
        else
        {
                        
             port_pin_set_output_level(PIN_PB08,0); 
            usart_write_buffer_wait(&usart_instance, Text2,sizeof(Text2));
            
         
            usart_write_buffer_wait(&usart_instance,ADC_value,sizeof(value));
         
        }

This topic has a solution.
Last Edited: Tue. Jun 18, 2019 - 07:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can't output all values with one call but to convert a single uint16_t to ASCII you can, e.g.,

char buf[10]
sprintf(buf, "%d", value);

which you can output

usart_write_buffer_wait(&usart_instance, (uint8_t*)buf, strlen(buf));

BTW, try to show the actual code you are using, there is no way the code above compiles (ValorADC ?).

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok, here is my code for this fire alarm 

 

#include <asf.h>
#include <string.h>

#include <stdio.h>
#include <stdlib.h>

//CONSTRUCT
struct usart_module usart_instance;
struct tcc_module tcc_instance;
struct adc_module adc_instance; //////////////

//DEFINItions

#define FILTER_SIZE 100

//INSTANCes

void usart_setup(void);
void usart_callback_read(struct usart_module *const usart_module);//(struct usart_module *const usart_module);
void adc_setup(void);
void tcc_setup(void);
void adc_callback_data(struct adc_module *const module);

volatile uint8_t tx_character;
volatile uint8_t rx_character;
volatile bool command_flag;
uint8_t Text1[] = "Alarma on, ADC value is: ";
uint8_t Text2[] = "Alarma off, ADC value is: ";

volatile uint16_t adc_value[FILTER_SIZE];

void adc_setup(void)
{
    {
        struct adc_config adc_configuration;

        adc_get_config_defaults(&adc_configuration);
        
        adc_configuration.clock_source = GCLK_GENERATOR_0; // 8 MHz
        adc_configuration.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16; // 500 kHz (max. ~2 MHz) 1250
        adc_configuration.reference = ADC_REFERENCE_INT1V;
        adc_configuration.gain_factor = ADC_GAIN_FACTOR_1X;
        adc_configuration.positive_input = ADC_POSITIVE_INPUT_PIN0; // PA02
        adc_configuration.negative_input = ADC_NEGATIVE_INPUT_GND;
        adc_configuration.resolution = ADC_RESOLUTION_10BIT;

        
        adc_init(&adc_instance, ADC, &adc_configuration);
        adc_enable(&adc_instance);
        //Configure READ_BUFFER callback
        adc_register_callback(&adc_instance, adc_callback_data, ADC_CALLBACK_READ_BUFFER);
        adc_enable_callback(&adc_instance, ADC_CALLBACK_READ_BUFFER);
    }
    
    
}
void adc_callback_data(struct adc_module *const module)
{
    uint8_t index;
    uint32_t adc_data = 0;
    
    ///Accumulate samples
    for(index = 0; index < FILTER_SIZE; index++) {
        adc_data += adc_value

;
    }
    
    /* Launch a new asynchronous ADC read */
    adc_read_buffer_job(&adc_instance, adc_value, FILTER_SIZE);
}

void port_setup(void){
    struct port_config pin_configuration;
    port_get_config_defaults(&pin_configuration);

    //pin del buzzer
    pin_configuration.direction=PORT_PIN_DIR_OUTPUT;
    port_pin_set_config(PIN_PB08,&pin_configuration);

}

void usart_setup(void) {
    
    struct usart_config usart_conf;
    usart_get_config_defaults(&usart_conf);
    
    usart_conf.generator_source = GCLK_GENERATOR_0;
    usart_conf.transfer_mode = USART_TRANSFER_ASYNCHRONOUSLY;
    usart_conf.parity = USART_PARITY_NONE;
    usart_conf.stopbits = USART_STOPBITS_1;
    usart_conf.character_size = USART_CHARACTER_SIZE_8BIT;
    usart_conf.baudrate = 9600;
    usart_conf.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
    usart_conf.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
    usart_conf.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
    usart_conf.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
    usart_conf.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
    usart_init(&usart_instance,EDBG_CDC_MODULE,&usart_conf);
    
    while(usart_init(&usart_instance,EDBG_CDC_MODULE,&usart_conf)!= STATUS_OK){}
    
    usart_enable(&usart_instance);
    usart_register_callback(&usart_instance, usart_callback_read, USART_CALLBACK_BUFFER_TRANSMITTED);
    usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_TRANSMITTED);
    
}

void usart_callback_read(struct usart_module *const usart_module)
{
    command_flag = true;
    usart_read_buffer_job(&usart_instance, (uint8_t *)&tx_character, 200);
}

void tcc_setup(void){
    
    struct tcc_config tcc_configuration;
    
    tcc_get_config_defaults(&tcc_configuration, TCC0);
    
    tcc_configuration.counter.clock_source = GCLK_GENERATOR_0;
    tcc_configuration.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1;
    tcc_configuration.compare.wave_generation =TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
    tcc_configuration.compare.match[0] = 0;
    tcc_configuration.pins.enable_wave_out_pin[0] = true;
    tcc_configuration.pins.wave_out_pin[0] = PIN_PB30E_TCC0_WO0;
    tcc_configuration.pins.wave_out_pin_mux[0] = MUX_PB30E_TCC0_WO0;
    tcc_configuration.wave_ext.invert[0] = true;
    tcc_init(&tcc_instance, TCC0, &tcc_configuration);
    tcc_enable(&tcc_instance);
}

int main (void)
{
    
    system_init();
    adc_setup();
    usart_setup();
    tcc_setup();
    
    system_interrupt_enable_global();
    port_setup();

    
    adc_read_buffer_job(&adc_instance, adc_value, FILTER_SIZE);//////////////

    
    while (1) /////////////////////////////
    {
        
        if ( adc_value < 500){
            
            
            
            port_pin_set_output_level(PIN_PB08,1);
            usart_write_buffer_wait(&usart_instance, Text1,sizeof(Text1));
            
            
            usart_write_buffer_wait(&usart_instance,adc_value,sizeof(adc_value));
            
            
            
        }
        else
        {
            
            port_pin_set_output_level(PIN_PB08,0);
            usart_write_buffer_wait(&usart_instance, Text2,sizeof(Text2));
            
            usart_write_buffer_wait(&usart_instance, adc_value,sizeof(adc_value));
            
        }
        
        
        
    }
    
    
    
    
}

Last Edited: Mon. Jun 17, 2019 - 10:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Still not ok code

volatile uint16_t adc_value[]FILTER_SIZE;

BTW are you sure about this (given that adc_value is going to be an array)?
 

        if ( adc_value < 500){

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sorry i just edited that to

volatile uint16_t adc_value[FILTER_SIZE];

 

sorry I have not expressed my doubt well

 

my question is how to go from uint16 [] adc_value to uint8 for this 

 

if ( adc_value < 500){
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's still not clear, what do you need to do with every adc value?

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks /Lars,

i´m trying to show the adc_value in the USART, my adc_value is an array of 100 elements and i want to make an uint8 with the average of this array

 

i tried with itoa but nothing, also tryed to calculate the average of the array with this 

 

int i;

int sum=0

uint8_t average;

for(i=0; i<100; i++)
{
    sum = sum + adc_value[100];
}
    average=sum/100;

 

but it didn´t work

 

what can i do?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lajon wrote:

It's still not clear, what do you need to do with every adc value?

/Lars

 

thanks /Lars,

i´m trying to show the adc_value in the USART, my adc_value is an array of 100 elements and i want to make an uint8 with the average of this array

 

i tried with itoa but nothing, also tryed to calculate the average of the array with this 

 

int i;

int sum=0

uint8_t average;

for(i=0; i<100; i++)
{
    sum = sum + adc_value[100];
}
    average=sum/100;

 

but it didn´t work

 

what can i do?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
int i;
int sum = 0
int average;
for(i = 0; i < 100; i++) {
    sum += adc_value[i];
}
average = sum / 100;

char buf[10]
sprintf(buf, "%d", average);
usart_write_buffer_wait(&usart_instance, (uint8_t*)buf, strlen(buf));

maybe

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it compiles but didn´t work 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

adc_read_buffer_job is not done there, it's done when the callback happens.

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

then were i should put it?

this is my code now:

#include <asf.h>
#include <string.h>

#include <stdio.h>
#include <stdlib.h>

//CONSTRUCT
struct usart_module usart_instance;
struct tcc_module tcc_instance;
struct adc_module adc_instance; //////////////

//DEFINItions

#define FILTER_SIZE 100





//INSTANCes



void usart_setup(void);
void usart_callback_read(struct usart_module *const usart_module);//(struct usart_module *const usart_module);
void adc_setup(void);
void tcc_setup(void);
void adc_callback_data(struct adc_module *const module);

volatile uint8_t tx_character;
volatile uint8_t rx_character;
volatile bool command_flag;
uint8_t Text1[] = "Alarma on, ADC value is: ";
uint8_t Text2[] = "Alarma off, ADC value is: ";


//declaciones media




//volatile uint16_t adc_value[FILTER_SIZE];
uint16_t adc_value[FILTER_SIZE];




void adc_setup(void)
{
	{
		struct adc_config adc_configuration;

		adc_get_config_defaults(&adc_configuration);
		
		adc_configuration.clock_source = GCLK_GENERATOR_0; // 8 MHz
		adc_configuration.clock_prescaler = ADC_CLOCK_PRESCALER_DIV16; // 500 kHz (max. ~2 MHz) 1250
		adc_configuration.reference = ADC_REFERENCE_INT1V;
		adc_configuration.gain_factor = ADC_GAIN_FACTOR_1X;
		adc_configuration.positive_input = ADC_POSITIVE_INPUT_PIN0; // PA02
		adc_configuration.negative_input = ADC_NEGATIVE_INPUT_GND;
		adc_configuration.resolution = ADC_RESOLUTION_10BIT;

		
		adc_init(&adc_instance, ADC, &adc_configuration);
		adc_enable(&adc_instance);
		//Configure READ_BUFFER callback
		adc_register_callback(&adc_instance, adc_callback_data, ADC_CALLBACK_READ_BUFFER);
		adc_enable_callback(&adc_instance, ADC_CALLBACK_READ_BUFFER);
	}
	
	
}
void adc_callback_data(struct adc_module *const module)
{
	uint8_t index;
	uint32_t adc_data = 0;
	
	///Accumulate samples
	for(index = 0; index < FILTER_SIZE; index++) {
		adc_data += adc_value
; } /* Launch a new asynchronous ADC read */ adc_read_buffer_job(&adc_instance, adc_value, FILTER_SIZE); } void port_setup(void){ struct port_config pin_configuration; port_get_config_defaults(&pin_configuration); //pin del buzzer pin_configuration.direction=PORT_PIN_DIR_OUTPUT; port_pin_set_config(PIN_PB08,&pin_configuration); } void usart_setup(void) { struct usart_config usart_conf; usart_get_config_defaults(&usart_conf); usart_conf.generator_source = GCLK_GENERATOR_0; usart_conf.transfer_mode = USART_TRANSFER_ASYNCHRONOUSLY; usart_conf.parity = USART_PARITY_NONE; usart_conf.stopbits = USART_STOPBITS_1; usart_conf.character_size = USART_CHARACTER_SIZE_8BIT; usart_conf.baudrate = 9600; usart_conf.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING; usart_conf.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0; usart_conf.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1; usart_conf.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2; usart_conf.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3; usart_init(&usart_instance,EDBG_CDC_MODULE,&usart_conf); while(usart_init(&usart_instance,EDBG_CDC_MODULE,&usart_conf)!= STATUS_OK){} usart_enable(&usart_instance); usart_register_callback(&usart_instance, usart_callback_read, USART_CALLBACK_BUFFER_TRANSMITTED); usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_TRANSMITTED); } void usart_callback_read(struct usart_module *const usart_module) { command_flag = true; usart_read_buffer_job(&usart_instance, (uint8_t *)&tx_character, 200); } void tcc_setup(void){ struct tcc_config tcc_configuration; tcc_get_config_defaults(&tcc_configuration, TCC0); tcc_configuration.counter.clock_source = GCLK_GENERATOR_0; tcc_configuration.counter.clock_prescaler = TCC_CLOCK_PRESCALER_DIV1; tcc_configuration.compare.wave_generation =TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM; tcc_configuration.compare.match[0] = 0; tcc_configuration.pins.enable_wave_out_pin[0] = true; tcc_configuration.pins.wave_out_pin[0] = PIN_PB30E_TCC0_WO0; tcc_configuration.pins.wave_out_pin_mux[0] = MUX_PB30E_TCC0_WO0; tcc_configuration.wave_ext.invert[0] = true; tcc_init(&tcc_instance, TCC0, &tcc_configuration); tcc_enable(&tcc_instance); } int main (void) { system_init(); adc_setup(); usart_setup(); tcc_setup(); system_interrupt_enable_global(); port_setup(); int i; int sum = 0; int average; for(i = 0; i < 100; i++) { sum += adc_value[i]; } average = sum / 100; adc_read_buffer_job(&adc_instance, adc_value, FILTER_SIZE);////////////// while (1) ///////////////////////////// { if ( average < 500){ port_pin_set_output_level(PIN_PB08,1); usart_write_buffer_wait(&usart_instance, Text1,sizeof(Text1)); char buf[10]; sprintf(buf, "%d", average); usart_write_buffer_wait(&usart_instance, (uint8_t*)buf, strlen(buf)); } else { port_pin_set_output_level(PIN_PB08,0); usart_write_buffer_wait(&usart_instance, Text2,sizeof(Text2)); char buf[10]; sprintf(buf, "%d", average); usart_write_buffer_wait(&usart_instance, (uint8_t*)buf, strlen(buf)); } } }

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is one way to do it

volatile bool adc_done = false;
void adc_callback_data(struct adc_module *const module)
{
    adc_done = true;
}

// In main
    .
    .
    .
    while (1) {
        if (adc_done) {
            adc_done = false;
            
            int i;
            int sum = 0;
            for(i = 0; i < 100; i++) {
                sum += adc_value[i];
            }
            
            adc_read_buffer_job(&adc_instance, adc_value, FILTER_SIZE);
            
            int average = sum / 100;
            if (average < 500) {
                .
                .
                .
            }
            else {
                .
                .
                .
            }
        }
    }

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks /Lars!! but it doesn´t work it stops at this loop

 

while (1) {
        if (adc_done) {
            adc_done = true; //with debugging it crash here!!
            
            uint8_t i;
            uint16_t sum = 0;

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And without debugging? Do you get any output? adc_done = true there is not what I wrote BTW.

/Lars

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i tryed  adc_done = true and  adc_done = false      but it doesn´t work, it not get any output with or without debugging

 

i´m working on the sam d21 xplained pro board BTW

Last Edited: Tue. Jun 18, 2019 - 06:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ITS WORKING!!!! enlightened laugh  JUST QUIT THE 

if (adc_done) 

AND NOW IT PRINTS THE VALUE

 

THANKS LARS!!!!!