Código DMA KL25Z

CÓDIGO


ProcessorExpert.c

/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "DMAT1.h"
#include "DMA1.h"
#include "AD1.h"
#include "DA1.h"
#include "TU1.h"
#include "TU2.h"
#include "DMAT2.h"
#include "FMSTR1.h"
#include "UART0.h"
/* Including shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"


uint32 mide_rms(uint8 lim_inf,uint8 lim_sup,uint16 *adc_ch0);

void gen_sen(uint16);

#define OFFSET 32767

LDD_TDeviceData *ptr_dac,
                           *ptr_adc,
                           *ptr_timerdac,
                           *ptr_timeradc,
                           *ptr_dma,
                           *ptr_dma2;
uint16 *ap_seno;
uint16 seno[33]={2047,2446,2830,3184,3494,3749,3938,4054,4094,4054,
             3938,3749,3494,3184,2830,2446,2047,1647,1263,909,599,344,
              155,39,0,39,155,344,599,909,1263,1647,0xFFFF};//Datos que generan la señal

int16 gen_seno[32]={0,399,783,1137,1447,1702,1891,2007,2047,2007,1891,1702,1447,1137,783,399,
-1,-400,-784,-1138,-1448,-1703,-1892,-2008,-2047,-2008,-1892,-1703,-1448,-1138,-784,-400};

uint16 adc_ch0[64];//Guarda dos ciclos de 32 muestras del ADC

bool flag_measure=FALSE;
uint32 cont_ciclos;
uint32 rms;
uint16 ad_result;
uint8 lim_inf;
uint8 lim_sup;
uint16 amp;
uint16 amp_ant;
uint16 frec=600;
uint16 frec_ant=600;
TU1_TValueType ticks_frec;
TU1_TValueType new_ticks_frec;
LDD_TError error;

/* User includes (#include below this line is not maintained by Processor Expert) */

/*lint -save  -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/
  
  amp=100;
 
  if(amp!=amp_ant)
  gen_sen(amp);
  amp_ant=amp;
 
  ptr_dma=DMAT1_Init(NULL);
  DMAT1_AllocateChannel(ptr_dma);
  DMAT1_EnableChannel(ptr_dma);
 
 
  ptr_dma2=DMAT2_Init(NULL);
  DMAT2_AllocateChannel(ptr_dma2);
  DMAT2_EnableChannel(ptr_dma2);
 
 
  ptr_dac=DA1_Init(NULL); //Inicializar el DAC
  ptr_timerdac=TU1_Init(NULL);
  ptr_adc=AD1_Init(NULL);
  AD1_SelectSampleGroup(ptr_adc,0);
  AD1_StartLoopTriggeredMeasurement(ptr_adc);
  ADC_PDD_EnableDmaRequest(ADC0_BASE_PTR,PDD_ENABLE);
  ptr_timeradc=TU2_Init(NULL);
  ap_seno=seno;
 
  TU1_GetPeriodTicks(ptr_timerdac,&ticks_frec);
 
 
   for(;;)
   {
          if(amp!=amp_ant)
          {
           gen_sen(amp);
           amp_ant=amp;
          }
          if(frec!=frec_ant)
                 {
                  new_ticks_frec=ticks_frec*600/frec;
                  
                  error=TU1_SetPeriodTicks(ptr_timerdac,new_ticks_frec);
                  
                  frec_ant=frec;
                 }
                 
          if(flag_measure) //ciclo listo?
          {
                flag_measure=FALSE;
                cont_ciclos++;
               rms=mide_rms(lim_inf,lim_sup,adc_ch0);
          } 
          FMSTR1_Poll();
         
   }
  
  
  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END ProcessorExpert */
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.3 [05.08]
**     for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/


uint32 mide_rms(uint8 lim_inf,uint8 lim_sup,uint16 *adc_ch0)
{
       uint8 i;
       uint32 rms;
       int32 var;
       dlong suma=0;
       for (i=lim_inf;i<=lim_sup;i++)
       {
       var=adc_ch0[i]-OFFSET;    
       suma+=var*var;                   
       }
       suma = suma >> 5;
       rms = (int32) sqrt(suma);
return rms;
}

void gen_sen(uint16 amp)
{
int i;

for(i=0;i<32;i++)
       seno[i]=gen_seno[i]*amp/100+2047;
      
seno[32]=0xffff;
}


Events.c

/* MODULE Events */

#include "Cpu.h"
#include "Events.h"

#ifdef __cplusplus
extern "C" {
#endif

extern uint16 *ap_seno;
extern LDD_TDeviceData* ptr_dac;
extern uint16 seno[];
extern LDD_TDeviceData *ptr_dma;
extern LDD_TDeviceData *ptr_dma2;
extern uint16 adc_ch0[];
extern bool flag_measure;
extern uint8 lim_inf;
extern uint8 lim_sup;


/* User includes (#include below this line is not maintained by Processor Expert) */

/*
** ===================================================================
**     Event       :  Cpu_OnNMIINT (module Events)
**
**     Component   :  Cpu [MKL25Z128LK4]
*/
/*!
**     @brief
**         This event is called when the Non maskable interrupt had
**         occurred. This event is automatically enabled when the [NMI
**         interrupt] property is set to 'Enabled'.
*/
/* ===================================================================*/
void Cpu_OnNMIINT(void)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  DMAT1_OnComplete (module Events)
**
**     Component   :  DMAT1 [DMATransfer_LDD]
*/
/*!
**     @brief
**         Called at the end of a DMA transfer. If the Half complete
**         property in initialization section is anabled, this event is
**         also called when current major iteration count reaches the
**         halfway point. See SetEventMask() and GetEventMask() methods.
**         This event is enabled only if Interrupts property in Channel
**         select section is enabled.
**     @param
**         UserDataPtr     - Pointer to the user or
**                           RTOS specific data. This pointer is passed
**                           as the parameter of Init method.
*/
/* ===================================================================*/
void DMAT1_OnComplete(LDD_TUserData *UserDataPtr)
{
  /* Write your code here ... */
       static bool flag = 0;
           uint8 inc_add;
                    if(!flag)
                           {
                          
                                  inc_add = 64;
                                  lim_inf=0;
                                  lim_sup=31;
                          
                           }
                    else
                    {
                           inc_add = 0;
                           lim_inf=32;
                           lim_sup=63;
                    }
                    flag = !flag;
                    DMAT1_SetDestinationAddress(ptr_dma,(LDD_DMA_TAddress)adc_ch0+ inc_add);//modifica direccion destino
                    DMAT1_SetByteCount(ptr_dma,64 );//Recarga contador
                    flag_measure=TRUE;   //ciclo listo
}

/*
** ===================================================================
**     Event       :  DMAT1_OnError (module Events)
**
**     Component   :  DMAT1 [DMATransfer_LDD]
*/
/*!
**     @brief
**         Called when error in channel settings is detected. See
**         SetEventMask() and GetEventMask() methods. This event is
**         enabled only if Interrupts property in Channel select
**         section is enabled.
**     @param
**         UserDataPtr     - Pointer to the user or
**                           RTOS specific data. This pointer is passed
**                           as the parameter of Init method.
*/
/* ===================================================================*/
void DMAT1_OnError(LDD_TUserData *UserDataPtr)
{
  /* Write your code here ... */
}

/*
** ===================================================================
**     Event       :  TU1_OnCounterRestart (module Events)
**
**     Component   :  TU1 [TimerUnit_LDD]
*/
/*!
**     @brief
**         Called if counter overflow/underflow or counter is
**         reinitialized by modulo or compare register matching.
**         OnCounterRestart event and Timer unit must be enabled. See
**         [SetEventMask] and [GetEventMask] methods. This event is
**         available only if a [Interrupt] is enabled.
**     @param
**         UserDataPtr     - Pointer to the user or
**                           RTOS specific data. The pointer passed as
**                           the parameter of Init method.
*/
/* ===================================================================*/
void TU1_OnCounterRestart(LDD_TUserData *UserDataPtr)
{
       if(*ap_seno == 0xFFFF)
             ap_seno = seno;
             DA1_SetValue(ptr_dac,(LDD_DAC_TData)*ap_seno);
             ap_seno++;
      
      
}

/*
** ===================================================================
**     Event       :  DMAT2_OnComplete (module Events)
**
**     Component   :  DMAT2 [DMATransfer_LDD]
*/
/*!
**     @brief
**         Called at the end of a DMA transfer. If the Half complete
**         property in initialization section is anabled, this event is
**         also called when current major iteration count reaches the
**         halfway point. See SetEventMask() and GetEventMask() methods.
**         This event is enabled only if Interrupts property in Channel
**         select section is enabled.
**     @param
**         UserDataPtr     - Pointer to the user or
**                           RTOS specific data. This pointer is passed
**                           as the parameter of Init method.
*/
/* ===================================================================*/
void DMAT2_OnComplete(LDD_TUserData *UserDataPtr)
{
  /* Write your code here ... */
       DMAT2_SetByteCount(ptr_dma2,2);
       FMSTR1_Recorder();
      
}

/*
** ===================================================================
**     Event       :  DMAT2_OnError (module Events)
**
**     Component   :  DMAT2 [DMATransfer_LDD]
*/
/*!
**     @brief
**         Called when error in channel settings is detected. See
**         SetEventMask() and GetEventMask() methods. This event is
**         enabled only if Interrupts property in Channel select
**         section is enabled.
**     @param
**         UserDataPtr     - Pointer to the user or
**                           RTOS specific data. This pointer is passed
**                           as the parameter of Init method.
*/
/* ===================================================================*/
void DMAT2_OnError(LDD_TUserData *UserDataPtr)
{
  /* Write your code here ... */
}

/* END Events */

#ifdef __cplusplus
}  /* extern "C" */

#endif

No hay comentarios:

Publicar un comentario