This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

single end ADC process

Other Parts Discussed in Thread: LM3S3748

Hi I am using Stellaris LM3S3748 EVB I wrote a program to keep reading analog input from pin ADC0 and write it to GPIO pin PA0/U0RX

I want someone to verify that the code is correct and guide me if there is an issue.

This code is working without errors  

#include "hw_types.h"
#include "hw_memmap.h"
#include "driverlib/adc.h"
#include "inc/lm3s3748.h"
#include <stdio.h>
#include "driverlib/gpio.h"
#include "hw_types.h"
#include "hw_ints.h"
#include "driverlib/sysctl.h"
#include "utils/ustdlib.h"

void main()
{
	unsigned long ADC0Value[1];
// CLK for LM3S3748 16MHz for Phase Lock Loop
	SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
//ENABLE ADC0 PORT TO BE CONFIGURED
	SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
//ENABLE GPIO PORT-A0 (PA0/U0RX) TO BE CONFIGURED
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//SPECIFY  PORT-A0 (PA0/U0RX) TO WORK WITH THE ADC MODULE
	GPIOPinTypeADC(GPIO_PORTA_BASE, GPIO_PIN_0);
//CONFIGURE SAMPLE SEQUENCER 3 STEP 0 TO RUN WHEN THE PROCESSOR IS TRIGGERED
	ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
//CONFIGURE PIN ADC0, SET INTERRAUPT FLAG WHEN ADC IS DONE, TELL THE ADC IT'S THE LAST CONVERSION IN SEQUENCER 3
	ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END);
//AFTER CONFIGURATION IS DONE, ENABLE THE ADC MODULE
	ADCSequenceEnable(ADC0_BASE, 3);
//CLEAR INTERRUPT FLAG ON SEQUENCE 3
	ADCIntClear(ADC0_BASE, 3);


//FOREVER LOOP
	while(1)
	 {
//TRIGGER ADC
	ADCProcessorTrigger(ADC0_BASE, 3);
//WAIT UNTILL THE CONVERSION IS FINISHED
	while(!ADCIntStatus(ADC0_BASE, 3, false))
	{}
//CLEAR INTERRUPT FLAG
	ADCIntClear(ADC0_BASE, 3);
//RETRIVE ADC FIFO RESULT
	ADCSequenceDataGet(ADC0_BASE, 3, ADC0Value);
//SET GPIO PIN A0 AS AN OUTPUT
	GPIODirModeSet(GPIO_PORTA_BASE, 0x01, GPIO_DIR_MODE_OUT);
//WRITE ADC RESULT TO PIN PA0
	GPIOPinWrite(GPIO_PORTA_BASE, 0x01,ADC0Value[0]);

	SysCtlDelay(SysCtlClockGet() / 12);
	}
}

thanks in advance

  • Your request is a "bit" demanding - I'll try & assist - yet you should be thinking about how you might "self-check" your work. (forum & helpers may not always be available - and their "over-use" may lessen their motivation.)

    Much looks good - but for your code line:
    //WRITE ADC RESULT TO PIN PA0
    GPIOPinWrite(GPIO_PORTA_BASE, 0x01,ADC0Value[0]);

    Your mid parameter - a single bit - is unable to reveal a 12 bit, ADC Value - is it not?
    And - beyond that - the GPIO Ports from this vendor are restricted to 8 bits - which also prevents writing a 12 bit value in "one go."

    You may consider instead - copying the ADC result to a dedicated "short or long variable" and then reading that variable.

    Other methods of monitoring and/or displaying the ADC's captured value are outside of your post's, "Single-End ADC Process."

  • Hi cb1 thanks for your reply

    I got the idea I want to convert an ECG signal which has small amplitude (200mV) as maximum value so I guess 8 bits will cover the value, am I write? if not correct me please.

    Thanks in advance
  • Long past firm/I used your LM3S3748 MCU. I cannot (now) recall if that MCU has the ability to accept a reduced input voltage as its VDDA. That's important as best practice directs that your, "Signals to be measured" (reasonably) fill the capacity of your chosen ADC. Should your "Analog Voltage Reference" be confined at/near 3V3 - then your 200mV signal - without amplification - will (hardly) "budge its needle."   (i.e. you'll be using roughly (only) the bottom 1/15th of the ADCs signal capacity.)

    You may add a "gain stage" to better utilize your MCU's ADC - yet realize that a "x15 multiplier" impacts noise and all error/drift sources, as well.  (everything will be 15x stronger!)

    Beyond matching signal levels (ADC's input vs. Signal being measured) you must determine the measurement bandwidth required (how fast you will make such measures) noise & foreign signal rejection - and MOST OF ALL - patient SAFETY!

    And - while your school or firm has provided that (long retired) EVB - it is NOT available - thus any "accident" may render your hard work unusable. Migration to this vendor's newer devices (titled this forum) may make more sense and save you time & heart-break.

  • mohammad sadoun said:
    I got the idea I want to convert an ECG signal which has small amplitude (200mV) as maximum value so I guess 8 bits will cover the value, am I write?

    No, you are not right. At least in any meaningful sense.

    Will 200mV fit into 8 bits given the resolution of the ADC? Yes

    Will 200mV actually use close to 8 bits? No, not even 6 bits

    Will it be accurate to 8 bits? No

    Will it be overwhelmed by noise? Maybe, it depends on how well the input circuit was designed.

    I hope you have someone else designing the input circuit for you and taking care of the precautions needed for such connections1. They can probably give you a signal more closely matched to you input needs.

    Robert

    1 - There are a lot of safety requirements for this kind of work. Semiconductor companies often explicitly forbid the use of their devices in health related electronics.

  • so better if I use the UART communication right ?
  • Note that UART fails to answer the (great) mismatch between input signal & ADC's scale and "Patient Safety" (mentioned now by two here) remains unaddressed.

    You must process the binary signal from the ADC's FIFO prior to presenting it to the UART - and that is beyond the scope of your post...
  • mohammad sadoun said:
    so better if I use the UART communication right ?

    ????

    How would that make any difference?

    Robert

  • My point is that I want to write a code to Use ADC module and then send it via UART, the processing of the signal is gonna be implemented in between the ADC and UART, and for "Patient Safety" I have safety circuit as a front end circuit before the EVB.
  • I don't like your attitude when you reply show some respect or don't answer me any more
  • That wasn't disrespectful, that was a question.

    Robert
  • you asked it like you expect dummy answer, that is not acceptable so please step away from my questions
  • I asked it in abject confusion as to how your followup question had anything to do with the previous discussion. I don't at all follow your reasoning as to how a UART addresses either safety or the size of the signal.

    Thus the question as to how a UART would improve things.

    Robert
  • I don't like your attitude when you reply show some respect or don't answer me any more

    You're welcome.

  • mohammad sadoun said:
    you asked it like you expect dummy answer

    My friend - that's not been proven - and (may) reflect an "over-sensitivity."   I've studied both law & engineering - proving one's "intent" is (usually) most difficult!

    Note that both poster's Robert & f.m. are "top cabin" both here and at other venues.   They bring a wealth of tech capability - and give willingly of their time, effort & expertise.   And - as their assistance is fast, free & detailed - you may do well to, "Give them the benefit of the doubt."   (note too that I've been categorized as "unsympathetic" at times - as well)   Again - proving "intent" is never simple, never easy - and knowing Robert - I cannot agree w/your assessment.

    On the whole - we outsiders provide a reasonably significant (entirely unpaid/unrewarded) service here/elsewhere - and try to work this assistance in during "breaks" from our "bill-paying" ventures.   Not at "all times" are we, "At our best" - and (heaven forbid) even forum officials have been (admittedly rare) "short" w/posters. 

    You present good detail in your postings - I can assure you that none mentioned here expected anything (near) to a "dummy answer."

  • Robert I am sorry I overreacted, I hope there is no hard feelings.
    Thank you for your answers :)
  • You are a gentleman.

    No hard feelings. We all misinterpret text from time to time.

    Robert
  • Thank you for your morals

    Regards