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.

CCS/TM4C123GH6PM: need to find the minimum and maximum value of ramp

Part Number: TM4C123GH6PM


Tool/software: Code Composer Studio

hello all,

           i have to find the maximum and minimum value of a continuous ramp pulse with on time of 400 ms and i have to store this minimum and maximum value in a array of 500 index, can any one help me . i am getting 2 sample from adc and comp

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/flash.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/pin_map.h"
#include "driverlib/pwm.h"
#include "driverlib/sysctl.h"
#include "utils/ustdlib.h"
#include "utils/uartstdio.h"
//#include "drivers/pinout.h"
#include "inc/tm4c123gh6pm.h"
#include "driverlib/adc.h"
#include "driverlib/timer.h"

///////////// start of declaration/////////////////////////////////
const int sampleFreq = 150000,ref=2240;
uint32_t ADC0Value, present_ADC[5], past_ADC, max_ADC[500],min_ADC[500],System_Clock,present_ADCsample,past_ADCsample;
uint16_t samplePeriod,i;
uint32_t ui32Period,pcount=0,ncount=0,presentmaxvalue_ADC,prevmaxvalue_ADC,presentminvalue_ADC,prevminvalue_ADC,ADC_maxvalue,ADC_minvalue,min;
uint32_t l=0,k=0;
bool status,nextsample;
    bool max;
void systemSetup(void);
//void PWM_Enable(void);
void Timer(void);
void ADC(void);
void Interrupt_Enable(void);
////////////// end of declaration//////////////


void systemSetup(void)
{
    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
   MAP_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5);
    MAP_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0);
    System_Clock=SysCtlClockGet(); // 40MHz
    status=0;
    max=0;
    nextsample=0;

}

void ADC0SS3IntHandler(void)
{

    past_ADCsample=present_ADCsample;
    ROM_ADCSequenceDataGet(ADC0_BASE,3,&ADC0Value);
    ROM_ADCIntClear(ADC0_BASE,3);
       if(max==0)
                   {
                         present_ADCsample=ADC0Value;
                         if(nextsample)
                              {
                                      max=1;
                                      status=1;

                               }
                     }
    nextsample=1;
    if(status)
    {
        if(present_ADCsample>past_ADCsample)
        {
         //   prevmaxvalue_ADC= presentmaxvalue_ADC ;

            presentmaxvalue_ADC=present_ADCsample;

    //        if(presentmaxvalue_ADC>prevmaxvalue_ADC)
    //        {
                ADC_maxvalue=presentmaxvalue_ADC;
   //         }
            past_ADCsample=0;
            present_ADCsample=0;
            nextsample=0;
            max=0;

        }
        else
        {
         //   prevminvalue_ADC=presentminvalue_ADC ;
            presentminvalue_ADC=present_ADCsample;
       //     min=present_ADCsample;
      //      if(presentminvalue_ADC<prevminvalue_ADC)
                  //      {
                            ADC_minvalue=presentminvalue_ADC;
                 //       }
                        past_ADCsample=0;
                        present_ADCsample=0;
                        nextsample=0;
                        max=0;

        }

    }




}





void Timer0IntHandler(void)
{
 //   int i=0;
    ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);

   // l=l+1;

}






void Timer(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    ui32Period = SysCtlClockGet()/sampleFreq; // Interrupts evrey half second
    TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period-1);

}
void ADC(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
    GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);
    ADCSequenceDisable(ADC0_BASE, 3);
    ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_TIMER, 0);
    ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END);
    ADCSequenceEnable(ADC0_BASE, 3);


}
void Interrupt_Enable(void)
{
    IntEnable(INT_TIMER0A);
    TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    IntMasterEnable();
    ADCIntEnable(ADC0_BASE, 3);
    IntEnable(INT_ADC0SS3);
    TimerEnable(TIMER0_BASE, TIMER_A);
    TimerControlTrigger(TIMER0_BASE, TIMER_A, true);
}

int main(void)
{

systemSetup();
//     PWM_Enable();
    Timer();
    ADC();
    Interrupt_Enable();

while(1)
    {

   // PWMPulseWidthSet(PWM1_BASE, PWM_OUT_4,200);

    }




}



aring them . but i am not getting the result.

  • Hello Gurpreet,

    What are the highest and lowest voltages that the pulse can produce? If they are within range of 3.3V and 0V, then the ADC is likely your best option.

    We can help with general device questions but the bulk of the implementation will be up to you.

  • thnks for reply,

    can you suggest i am not getting output , can you help me to find the problem in code i have posted i

  • Hello Gurpreet,

    I can do a brief review of your code tomorrow for errors. In the meantime if you have TivaWare 2.2.0 you can also reference our example adc_udma_pingpong as well as the single_ended.c example in the peripherals/adc examples folder to compare to your code.

    Make sure you are entering both your interrupts - a quick glance at that interrupt enable code looked good but that is something you can verify with breakpoints.

    Also from a glance your ADC ISR looks very big. You may want to consider saving samples there into an array and scan the area for the max and min as part of your application when the pulse elapses.

    And you haven't answered the voltage range question, that is important to understand so I know what ADC values you'd expect when it works.

  • Hello Gurpreet,

    I ran your code and the TM4C ADC is working correctly. It looks like you have an application algorithm issue.

    I am always getting into this statement because past_ADCsample is always zero. You need to see what is causing that. But the ADC peripheral is working properly as far as I can see.

    		if(present_ADCsample>past_ADCsample)
    		{
    			//   prevmaxvalue_ADC= presentmaxvalue_ADC ;
    
    			presentmaxvalue_ADC=present_ADCsample;
    
    			//        if(presentmaxvalue_ADC>prevmaxvalue_ADC)
    				//        {
    				ADC_maxvalue=presentmaxvalue_ADC;
    				//         }
    			past_ADCsample=0;
    			present_ADCsample=0;
    			nextsample=0;
    			max=0;
    
    		}

  • thnaks dear 

    my volatge varies from 200 mV to 2.2V . i have written another code for same problem . still not working. can you see code for me and suggest modification

    #include <stdbool.h>
    #include <stdint.h>
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_sysctl.h"
    #include "inc/hw_types.h"
    #include "inc/hw_gpio.h"
    #include "driverlib/flash.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/gpio.h"
    #include "driverlib/rom.h"
    #include "driverlib/rom_map.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/pwm.h"
    #include "driverlib/sysctl.h"
    #include "utils/ustdlib.h"
    #include "utils/uartstdio.h"
    #include "drivers/pinout.h"
    #include "inc/tm4c123gh6pm.h"
    #include "driverlib/adc.h"
    #include "driverlib/timer.h"
    uint32_t ADC0Value, present_ADC[5], past_ADC, max_ADC[100],min_ADC[100],System_Clock,present_ADCsample=0,past_ADCsample=0,pcount,ncount;
    bool ramp;
    void systemSetup(void);
    void ADCConfigure(void);
    
    void ADCConfigure()
    {
    
        // PK1  is used with ADC0 //
        SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
        SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
        GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);
        ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
        ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE |
                                     ADC_CTL_END);
        ADCSequenceEnable(ADC0_BASE, 3);
        ADCIntClear(ADC0_BASE, 3);
        // PK2  is used with ADC1 //
    
    }
    void systemSetup(void)
    {
        SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
        MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
       MAP_GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5);
        MAP_GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5, 0);
        System_Clock=SysCtlClockGet(); // 40MHz
    
    
    }
    
    
    /**
     * main.c
     */
    int main(void)
    {
        systemSetup();
        ADCConfigure();
        ADCProcessorTrigger(ADC0_BASE, 3);
                        while(!ADCIntStatus(ADC0_BASE, 3, false))
                               {
                               }
    
                        ADCSequenceDataGet(ADC0_BASE,3,&ADC0Value);
                        past_ADCsample=ADC0Value;
                        ADCIntClear(ADC0_BASE, 3);
                        ADCProcessorTrigger(ADC0_BASE, 3);
                                            while(!ADCIntStatus(ADC0_BASE, 3, false))
                                                   {
                                                   }
    
                                            ADCSequenceDataGet(ADC0_BASE,3,&ADC0Value);
                                            present_ADCsample=ADC0Value;
                                            ADCIntClear(ADC0_BASE, 3);
                                            if( past_ADCsample<present_ADCsample)
                                                {
                                                    ramp=1;
                                                }
                                                else
                                                {
                                                    ramp=0;
                                                }
    
    
                                    //        past_ADCsample=present_ADCsample ;
    
        while(1)
            {
                    while(ramp==0)
                                   {
                                       past_ADCsample=present_ADCsample ;
                                       ADCProcessorTrigger(ADC0_BASE, 3);
                                       while(!ADCIntStatus(ADC0_BASE, 3, false))
                                       {
                                        }
    
                                       ADCSequenceDataGet(ADC0_BASE,3,&ADC0Value);
                                       ADCIntClear(ADC0_BASE, 3);
                                       present_ADCsample=ADC0Value;
                                       if(past_ADCsample<present_ADCsample)
                                          {
                                                  ramp=1;
                                          }
                                    else
                                         {
                                                  ramp=0;
                                                  max_ADC[ncount]=past_ADCsample;
                                                  ncount++;
                                                                                                 if(ncount>100)
                                                                                                 {
                                                                                                     ncount=0;
                                                                                                 }
                                                                                         //        past_ADCsample=present_ADCsample ;
                                         }
    
    
                                    }
    
    
                    while(!ramp)
                                     {
                                      past_ADCsample=present_ADCsample ;
                                       ADCProcessorTrigger(ADC0_BASE, 3);
                                       while(!ADCIntStatus(ADC0_BASE, 3, false))
                                        {
                                         }
                                       ADCIntClear(ADC0_BASE, 3);
                                       ADCSequenceDataGet(ADC0_BASE,3,&ADC0Value);
    
                                       present_ADCsample=ADC0Value;
                                       if(past_ADCsample>present_ADCsample)
                                         {
                                                   ramp=0;
    
    
                                         }
                                       else
                                         {
                                                   ramp=1;
                                                   min_ADC[pcount]=past_ADCsample;
                                                                                                  pcount++;
                                                                                                  if(pcount>100)
                                                                                                  {
                                                                                                      pcount=0;
                                                                                                  }
                                                                                                  //past_ADCsample=present_ADCsample ;
                                         }
    
    
                                       }
    
                       }
    }
    

  • Hello Gurpreet,

    As mentioned before, the TM4C operation with retrieving ADC results is working as expected but your application algorithm was incorrect. We do not provide application level help here, only device specific assistance. If you believe the issue is device related, then explain how I can re-create your issue because when I ran the code and debugged, I saw the correct ADC values but that your code did not properly store them and that is beyond our scope to provide detailed help with (though I did mention what I thought the root cause was).

  • thanks dear for replying

     can you just check the new code i have posted because i am stuck ,not getting anyclue.

  • Hello Gurpreet,

    I checked again and the ADC is giving expected results so it is configured fine. You will need to debug your own application implementation.