Forums

Analyzer of Quality Electrical Energy, with RTOS ( ARM , Driver ADC, lwip , GPRS , Processing data , FFT , Handle of Thread , Real-Time, Socket )

Started by Jhoberg April 26, 2007
Analyzer of Quality Electrical Energy

This desing and develop is constructed  whit RTOS (eCos), consists of
a  ADC of 16NBits with two channels one to capture Current and another
one of Voltege, captures the data with a sampling frequency  of 7680Hz
to acquire 128 samples of the two you wave, are captured with thread,
another thdreas for communication with GPRS, other to process the data
by means of the FFT, disturbing the waves in its harmonic components
to determine as they are his compoenetes and thus to analyze its
harmonic Distortion, sends by means of Sockets the data of the waves
with interface of network PPP to be stored remotely in real-time.

//********** Analizer Quality Electrical Energy *********
//********** Desing and Develop By Jhoberg Quevedo Ruiz
//**********email:jrquevedor
//**********email:jrquevedor@gmail.com*******************

//********** FSAMPLE: 7680Hz
//********** NSAMPLE: 128
//********** CH0    : Current
//********** CH1    : Voltege
//********** NetWork Interface PPP
//Capute Voltege and Current, FFT, Socket to Transmit Wave in REAL-
TIME
//Handle Thread cuminucations, Capture, Sample and Processing in REAL-
TIME
//RTOS for ARM

#include <pkgconf/io_serial.h>
#include <pkgconf/libc.h>
#include <cyg/io/io.h>
#include <cyg/hal/hal_io.h>
#include <cyg/hal/hal_intr.h>
#include <cyg/io/serial.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <stdlib.h>
#include "lwip/sys.h"
#define LWIP_COMPAT_SOCKETS 2
#include "lwip/sockets.h"

//***********Definicion variables eCos

#define STACK_SIZE 0x750
cyg_alarm_t test_timer1;
//variables ecos hilos
static cyg_thread thread_s[1];
static char stack[1][STACK_SIZE];
cyg_thread_entry_t AD7654_program,samples_program;
cyg_handle_t AD7654_threadA,samples_thread;
cyg_mutex_t cliblock,adcblock;

//Interrupcion
cyg_handle_t handle_ADC_ISR0;
cyg_interrupt intrADC;
//semaforo
cyg_sem_t semaforo_ADC,semaforo_nsamples;

//handle_I
cyg_io_handle_t handle_IO;

int aux=0;

#define MAX_BUFFRX 512
#define TOPTCP 2048
#define MAX_BUFFTX 2048
char buffrx[MAX_BUFFRX];
char bufftx[MAX_BUFFTX];


//************fin Definicion variables eCos

#define LED_1	(1<<16)
#define LED_2	(1<<17)
#define LED_3	(1<<18)

  int axgnd=0;
  int axmin=0;
  int axmax=0;
  float *iaux;
  float *toppw;
  volatile float posicion=0;

  float jh=0;
  float jh2=0;
  float jh3=0;
  float axjh=0;
  float TOPDESPLAZAMIENTO=0.1;
  float POSMAX=0;
  float POSMIN=0;


//**********definicion de la clase ADC ANALOG DEVICE en
#define NMUESTRAS 128
#define FSIGNAL 16
#define FSAMPLE 24
#define FNOISE  60
#define NSAMPLES 128

///////////
float  dataImgI[NMUESTRAS];
float  dataImgV[NMUESTRAS];
float  vsignal[NMUESTRAS];
float  vsignalax[NMUESTRAS];
float w[NMUESTRAS/2];
float f[NMUESTRAS/2];
float potencia[NMUESTRAS];
float img1[NMUESTRAS];
float real1[NMUESTRAS];
float dataRealI[NMUESTRAS];
float dataRealV[NMUESTRAS];
///////////

cyg_uint32 ADRMEMORYK=0x297;

int   Mapear(void);
bool  ConvertionStart(void);
bool  ReadAdc(unsigned short *,cyg_uint32 *);
bool  setChanel(int);

int Mapear(void)
{
  //Current
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_PDR ,AT91_PIO_PSR_NCS3);
  HAL_WRITE_UINT32(AT91_EBI + AT91_EBI_MCR,AT91_EBI_MCR_ALE_8M|
(0<<4) );
  HAL_WRITE_UINT32(AT91_EBI + AT91_EBI_CSR1, AT91_EBI_CSR_DBW_16 |
AT91_EBI_CSR_NWS_8 | AT91_EBI_CSR_WSE
		   | AT91_EBI_CSR_PAGES_1M | AT91_EBI_CSR_TDF_1 | AT91_EBI_CSR_CSEN
| (ADRMEMORYK<<20)  );
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_PER ,  AT91_PIO_PSR_TIOA1|
AT91_PIO_PSR_P23|AT91_PIO_PSR_P24 );
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_OER, AT91_PIO_PSR_TIOA1|
AT91_PIO_PSR_P23|AT91_PIO_PSR_P24 );
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_SODR, AT91_PIO_PSR_TIOA1);

  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_SODR, AT91_PIO_PSR_P23);
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_CODR, AT91_PIO_PSR_P24);
  return 1;
}

int Mapeara2(void)
{
  //Voltage
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_PDR ,AT91_EBI_CSR1);
  HAL_WRITE_UINT32(AT91_EBI + AT91_EBI_MCR,AT91_EBI_MCR_ALE_8M|
(0<<4) );
  HAL_WRITE_UINT32(AT91_EBI + AT91_EBI_CSR1, AT91_EBI_CSR_DBW_16 |
AT91_EBI_CSR_NWS_8 | AT91_EBI_CSR_WSE
		   | AT91_EBI_CSR_PAGES_1M | AT91_EBI_CSR_TDF_1 | AT91_EBI_CSR_CSEN
| (ADRMEMORYK<<20)  );
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_PER ,  AT91_PIO_PSR_TIOA1|
AT91_PIO_PSR_P23|AT91_PIO_PSR_P24 );
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_OER, AT91_PIO_PSR_TIOA1|
AT91_PIO_PSR_P23|AT91_PIO_PSR_P24 );
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_SODR, AT91_PIO_PSR_TIOA1);

  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_SODR, AT91_PIO_PSR_P23);
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_CODR, AT91_PIO_PSR_P24);
  return 1;
}


bool ConvertionStart(void)
{
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_CODR, AT91_PIO_PSR_TIOA1);
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_SODR, AT91_PIO_PSR_TIOA1);
  return 1;
}
bool ReadAdc(cyg_uint16 *Datoax, cyg_uint32 *address)
{
  HAL_READ_UINT16((*address<<20) ,*Datoax );
  return 1;
}
bool setChanel(int chop)
{
  switch(chop)
    {
    case 0:{  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_CODR,
AT91_PIO_PSR_P23);
              HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_CODR,
AT91_PIO_PSR_P24);
	      //Voltaje
	      break;}
    case 1:{  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_CODR,
AT91_PIO_PSR_P23);
              HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_SODR,
AT91_PIO_PSR_P24);
	      break;}
    case 2:{  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_SODR,
AT91_PIO_PSR_P23);
    HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_CODR, AT91_PIO_PSR_P24);
	      //Current
	      break;}
    case 3:{  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_SODR,
AT91_PIO_PSR_P23);
              HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_SODR,
AT91_PIO_PSR_P24);
	      break;}
    }
  return 1;
}



typedef struct adc_7654
{
  const cyg_uint32 ADRMEMORY;
  volatile cyg_uint16 Dato;
  int SamplesI[NMUESTRAS];
  int SamplesV[NMUESTRAS];
  char *cDato;
  int topsamples;
  int numsample;

  unsigned long powersample;
  float powertop;
  float *PowerDato;
  int ratepower;
  int MaxV;
  int MinV;
  volatile unsigned int *MaxI;
  volatile unsigned int *MinI;

  volatile float instantVol;
  volatile float instantCur;
  int Comdato;
  int Comando;

  int ntfpv;
  int ntfpi;
  float fp;

  int (* apMapear)(void);
  bool (* apConvertionStart)(void);
  bool (* apReadAdc)(cyg_uint16 *,cyg_uint32 *);
  bool (* apsetChanel)(int);


};
struct adc_7654 adc,axadc;

//** function fft
// i  : 1 transformada inversa, :-1 trasnformada
// j  : NMUESTRAS
// real : argumento real
// img: argumento imaginario
bool fft(int i, int j, float real[], float img[])
{

  float f = sqrt(1.0F / (float)j);
  int i1;
  int k;
   for( k = i1 = 0; k < j; k++)

        {

	  if(i1 >= k)
            {
	      float f1 = (real[i1]) * f;
                float f2 = (img[i1]) * f;
                real[i1] = (real[k]) * f;
                img[i1] = (img[k]) * f;
                real[k] = f1;
                img[k] = f2;
           }

	  int k1;
            for(k1 = j / 2; k1 >= 1 && i1 >= k1; k1 /= 2)
	      i1 -= k1;


            i1 += k1;
        }


  int l1 = 1;
  int i2;

  for( i2 = 2 * l1; l1 < j; i2 = 2 * l1)
    {

      float f3 = ((float)i * 3.141593F) / (float)l1;
      int j2;
      for( j2 = 0; j2 < l1; j2++)
	{
	  float f4 = (float)j2 * f3;
	  float f5 = (float)cos(f4);
	  float f6 = (float)sin(f4);
	  int l;
	  for( l = j2; l < j; l += i2)
	    {
	      int j1 = l + l1;
	      float f7 = f5 * (real[j1]) - f6 * (img[j1]);
	      float f8 = f5 * (img[j1]) + f6 * (real[j1]);
                    real[j1] = real[l] - f7;
                    img[j1] = img[l] - f8;
                    real[l] += f7;
                    img[l] += f8;
	    }

	}

      l1 = i2;
      l1 = i2;


    }

  return 1;
}
//**********end FFT **************

void levelCurrent(float al,float am,float aM)
{
  al=(2*(al-am)/(aM-am))-1;
  al=al*9770;
}

void levelVoltage(float al,float am,float aM)
{
  al=(2*(al-am)/(aM-am))-1;
  al=al*9870;
}


cyg_uint32 fun_isr0 (cyg_vector_t vector, cyg_addrword_t data )
{
  cyg_uint32 resultPIO;
  HAL_READ_UINT32(AT91_PIO + AT91_PIO_ISR, resultPIO);
  cyg_interrupt_mask(vector);
  cyg_interrupt_acknowledge(vector);
  return (CYG_ISR_HANDLED | CYG_ISR_CALL_DSR);

}

void fun_dsr0(cyg_vector_t vector, cyg_ucount32 count, cyg_addrword_t
data){
  cyg_interrupt_unmask(vector);
}


static void socket_thread(void *arg)
{

	int idsock,idsockecho;
	int lentx,i,lenrx,len;
        int cliente_len;
	int vartop,varmem,var;
	int transmisiones=0;
	int sizebuf=0;
	unsigned long ciclosamples=0;
       	char comando [40]="Desconectado";
	float axsock=0;
	struct sockaddr_in cliente_addr,rem;
	strcpy(bufftx,"info");
	volatile char  *cadenainfo;
	volatile char *apcomando;
	cadenainfo=malloc(sizeof(char  [400]));
	apcomando=malloc(sizeof(char  [20]));
	do {
	    cliente_len=sizeof(cliente_addr);
	    cliente_addr.sin_family = AF_INET;
	    cliente_addr.sin_port = htons(777);
	    inet_aton("192.168.2.2",&cliente_addr.sin_addr);
	    if( (idsock = socket(AF_INET,SOCK_STREAM,0)) < 0)
	      {
	      cyg_thread_delay(100);

	      }
   	    else
	      {

		lentx=strlen(bufftx);
		lenrx=20;
		connect(idsock,(struct sockaddr
*)&cliente_addr,sizeof(cliente_addr));

		for(i=0; ( ( ((lenrx=recv(idsock,buffrx,20,MSG_DONTWAIT) ) > 0) &&
buffrx[i]!=NULL)  ||
		      (send(idsock,bufftx,lentx,MSG_DONTWAIT) )>0) ;i++ )
		  {

		    strcpy(bufftx,"info");
		    len=strlen(bufftx);
		    sizebuf+=len;
		    if(sizebuf>TOPTCP){
		      sizebuf=0;
		      close(idsock);
		      pbuf_free(idsock);

		    }
		    else send(idsock,bufftx,len,MSG_DONTWAIT);

		    cyg_scheduler_lock();
		    cyg_mutex_lock(&adcblock);
		    axadc.Comando=1;
		    cyg_mutex_unlock(&adcblock);
		    cyg_scheduler_unlock();

		    if(lenrx>0){

			if(strncmp(buffrx,"apagar",6)==0){

			HAL_WRITE_UINT32( AT91_PIO + AT91_PIO_CODR ,LED_1 );

			cyg_scheduler_lock();
			cyg_mutex_unlock(&adcblock);
			sprintf(comando,"Carga Apagada [Ok], Energia [%f]KW*h \n",*toppw);
			cyg_mutex_unlock(&adcblock);
			cyg_scheduler_unlock();
			len=strlen(comando);
			send(idsock,comando,len,MSG_DONTWAIT);

			}

		    }

		    sprintf(comando," %f %f %f %f %lu %d %d
\n",*axadc.PowerDato,axadc.instantCur,axadc.instantVol,posicion,axadc.powersample,axadc.ntfpv,axadc.ntfpi);
		    len=strlen(comando);
		    sizebuf+=len;
		    if(sizebuf>TOPTCP){
		      sizebuf=0;
		      close(idsock);
		      pbuf_free(idsock);
		    }
		    else send(idsock,comando,len,MSG_DONTWAIT);
		    varmem=0;
		    vartop=0;
		    strcpy(apcomando,"@");
		    len=strlen(apcomando);
		    sizebuf+=len;
		    if(sizebuf>TOPTCP){
		      sizebuf=0;
		      close(idsock);
		      pbuf_free(idsock);

		    }
		    else send(idsock,apcomando,len,MSG_DONTWAIT);

		    if(transmisiones==0) {
			strcpy(apcomando,"V");
			len=strlen(apcomando);
		    } else{
		      strcpy(apcomando,"I");
		      len=strlen(apcomando);
		      }
		    sizebuf+=len;
		    if(sizebuf>TOPTCP){
		      sizebuf=0;
		      close(idsock);
		       pbuf_free(idsock);

		    }
		    else  send(idsock,apcomando,len,MSG_DONTWAIT);
			for(var=0;var<NSAMPLES;var++)
			  {
			    if(transmisiones==0)
			      sprintf(apcomando,"%d, \n",axadc.SamplesI[var]);

			    else
			      sprintf(apcomando,"%d, \n",axadc.SamplesV[var]);

			    varmem=0;
			    while((varmem<6)&&((char *)apcomando)[varmem]!=' ')
			      {
				((char *)cadenainfo+vartop)[varmem]=(char *)apcomando[varmem];
				varmem++;
			      }
			    vartop=vartop+varmem;

			  }

		len=strlen(cadenainfo);
		sizebuf+=len;
		if(sizebuf>TOPTCP){
		  sizebuf=0;
		  close(idsock);
		  pbuf_free(idsock);

		}else 	send(idsock,cadenainfo,len,MSG_DONTWAIT);

		    if(*axadc.PowerDato>216E6)
		      {
			cyg_mutex_lock(&adcblock);

			HAL_WRITE_UINT32( AT91_PIO + AT91_PIO_CODR ,LED_1 );
			*adc.PowerDato=0;
			*toppw=0;
			cyg_mutex_unlock(&adcblock);
			sprintf(comando,"allenergy \n");
			len=strlen(comando);
			send(idsock,comando,len,MSG_DONTWAIT);


		      }

		    cyg_scheduler_lock();
		    cyg_mutex_lock(&adcblock);
		    ciclosamples=axadc.powersample;
		    axadc.Comando=0;
		    cyg_mutex_unlock(&adcblock);
		    cyg_scheduler_unlock();

		    if(sizebuf>TOPTCP){
		      sizebuf=0;
		      close(idsock);
		       pbuf_free(idsock);

		    }
		     transmisiones++;
		     if(transmisiones>2){
		     transmisiones=0;
		    }
		  }
	       close(idsock);
	       cyg_thread_delay(7000);
	      }

	}while(1);

}

void tmain()
{
    cyg_io_handle_t handle_ser1;
    Cyg_ErrNo err;
    const char comando_modem [] = "ATE0\r\n";
    const char comando0_modem[] = "ATD*99#\r\n";
    const char comando1_modem[] = "ATZ    \r";
    char respuesta_modem [20];
    int len = strlen(comando_modem);
    int cadenatx_len = strlen(comando1_modem);
    int cadenarx_len,j;
    char buferr[20];

    err = cyg_io_lookup( "/dev/ser1", &handle_ser1 );
    cyg_io_set_config(handle_ser1,1,&buferr,&len);
    err = cyg_io_write( handle_ser1, comando_modem , &len );
    cyg_thread_delay(100);
    len= strlen(comando0_modem);
    err = cyg_io_write( handle_ser1, comando0_modem , &len );
    cyg_io_set_config(handle_ser1,0,&buferr,&len);
    cyg_thread_delay(100);
    lwip_init();
    sys_thread_new(socket_thread, (void*)"socket",5);

}

void  cyg_user_start ( void )
{
  HAL_WRITE_UINT32( AT91_PIO + AT91_PIO_PDR , AT91_PIO_PSR_TXD0 |
  AT91_PIO_PSR_RXD0);
  HAL_WRITE_UINT32( AT91_PIO + AT91_PIO_PDR , AT91_PIO_PSR_TXD1 |
  AT91_PIO_PSR_RXD1 );

  adc.topsamples=NMUESTRAS;
  adc.numsample=0;
  adc.powersample=0;
  adc.powertop=216E6;
  adc.cDato=(char *)malloc(sizeof(char [4]));
  adc.apMapear=Mapear;
  adc.apConvertionStart=ConvertionStart;
  adc.apReadAdc=ReadAdc;
  adc.apsetChanel=setChanel;
  adc.ADRMEMORY=&ADRMEMORYK;
  adc.apMapear();
  adc.Comdato=0;
  adc.PowerDato=malloc(sizeof(float ));;
  adc.ratepower=10;
  adc.MaxI=malloc(sizeof(int ));
  adc.MinI=malloc(sizeof(int ));
  *adc.MaxI=0;
  *adc.MinI=0xFFFF;
  adc.instantVol=0;
  adc.instantCur=0;
  adc.ntfpi=0;
  adc.ntfpv=0;
  adc.fp=1;
  adc.Comando=0;
  iaux=malloc(sizeof(float ));
  *iaux=0;
  toppw=malloc(sizeof(float ));
  *toppw=0;

  HAL_WRITE_UINT32( AT91_PIO + AT91_PIO_PDR ,AT91_PIO_PSR_IRQ2);
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_IDR, 0xFFFFFFFF);
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_PER, AT91_PIO_PSR_IRQ2);
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_ODR, AT91_PIO_PSR_IRQ2);
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_IER, AT91_PIO_PSR_IRQ2);
  HAL_WRITE_UINT32(AT91_PIO + AT91_PIO_SODR, AT91_PIO_PSR_IRQ2);
  cyg_interrupt_create(CYGNUM_HAL_INTERRUPT_EXT2,0 ,
1,&fun_isr0,&fun_dsr0,&handle_ADC_ISR0,&intrADC);
 
hal_interrupt_set_level(CYGNUM_HAL_INTERRUPT_EXT2,AT91_AIC_SMR_EDGE_NEG);
  cyg_interrupt_attach(handle_ADC_ISR0);
  cyg_interrupt_unmask(CYGNUM_HAL_INTERRUPT_EXT2);

  cyg_mutex_init(&cliblock);
  cyg_mutex_init(&adcblock);
  cyg_semaphore_init(&semaforo_nsamples,0);
  cyg_semaphore_init(&semaforo_ADC,0);

  cyg_thread_create(10, samples_program, (cyg_addrword_t) 0,
		    "Hilo para AD7654 de ANALOG", (void *) stack[0],STACK_SIZE,
		    &samples_thread,&thread_s[0]);

  cyg_thread_create(7,AD7654_program, (cyg_addrword_t) 0,
		    "Hilo  que contien el LWIP", (void *) stack[1],STACK_SIZE,
		    &AD7654_threadA, &thread_s[1]);

  cyg_thread_resume(samples_thread);
  cyg_thread_resume(AD7654_threadA);

  HAL_WRITE_UINT32( AT91_PIO + AT91_PIO_CODR ,LED_2  );
  HAL_WRITE_UINT32( AT91_PIO + AT91_PIO_CODR ,LED_3  );

}


void AD7654_program(cyg_addrword_t data)
{
  int var,len,k;
  char  outstring;
  char buferr[10];
  float promedioI,promedioV,M,prompos;

   for(k = 0; k < NMUESTRAS/2; k++)
    f[k] = k*FSAMPLE/(NMUESTRAS/2);
   for(k = 0 ;k< NMUESTRAS/2; k++)
     w[k] = 2*3.1416*f[k];

   do{
     cyg_semaphore_wait(&semaforo_nsamples);
     axadc.Comando=1;

     k=0;
     promedioI=0;
     promedioV=0;
     while( k<NMUESTRAS)
       {
	 levelCurrent( axadc.SamplesI[k],19616,44491);
	 datosRealI[k] =axadc.SamplesI[k], promedioI += axadc.SamplesI[k] ;

	 levelVoltage( axadc.SamplesV[k],19616,44491);
	 dataRealV[k] =axadc.SamplesV[k], promedioV += axadc.SamplesV[k] ;

	 k++;
       }
     M=k;

    promedioI /= M;
    for(k = 0; k<M; k++){
      datosRealI[k] -= promedioI;
      dataRealV[k] -= promedioV;
    }
    //FFT Current
    fft(-1,NMUESTRAS/2,dataRealI,dataImgI);

    //FFT Voltage
    fft(-1,NMUESTRAS/2,dataRealV,dataImgV);

    axadc.Comando=0;

   }while(1);
}

// funcion de muestreo, con frecuencia variable

void bitAdc_alarm_func(cyg_handle_t alarmH, cyg_addrword_t data)
{

  cyg_scheduler_lock();
  cyg_mutex_lock(&adcblock);
  *iaux=0;
  axmax=0;
  axmin=0;
  axgnd=0;
  jh=0;
  jh2=0;
  jh3=0;

 if(adc.numsample>adc.topsamples)
    {
      if(axadc.Comando==0)
      axadc=(struct adc_7654)adc;

     cyg_semaphore_post(&semaforo_nsamples);

    	if((*adc.PowerDato)<adc.powertop)
	  {
	    axmax=adc.MaxV;
	    axmin=adc.MinV;
	    axgnd=axmin+(((axmax)-(axmin))/2 );
	    jh2=(axmax-axgnd);
	    if(jh2>0)
	      adc.instantVol=jh2*1E3/65535E0;
	    else
	      adc.instantVol=120E0;


	    axmax=(*adc.MaxI);
	    axmin=(*adc.MinI);
	    axgnd=axmin+(((axmax)-(axmin))/2 );
	    //para calcuar con dcgnd variable
	     jh2=( axmax - axgnd);

	    jh=4565E-6;
	    if( jh2 > 0)
	      jh3=jh2*jh;
	    else
	      jh3=0;

	    if(jh3>0)
	      {
		adc.instantCur=jh3;
		jh=(adc.instantVol*(jh3));
		*toppw+=jh;
		*adc.PowerDato=*toppw;

	      }
	    else{
	      adc.instantCur=0E0;
	      adc.instantVol=120E0;

	    }
	    adc.MaxV=0;
	    *adc.MinI=0xFFFF;
	    *adc.MaxI=0;
	    adc.MinV=0xFFFF;
	    adc.numsample=0;
	    adc.powersample++;
	  }else{
	    adc.Comdato=1;
	  }

    }else
      {

	//Current
	adc.apsetChanel(1);
	adc.apConvertionStart();
	adc.apReadAdc(&adc.Dato,adc.ADRMEMORY);
	adc.SamplesI[adc.numsample]=adc.Dato;
	if(*adc.MaxI<adc.SamplesI[adc.numsample])
	  {
	    *adc.MaxI=adc.SamplesI[adc.numsample];
	    adc.ntfpi=adc.numsample;

	  }
	if(*adc.MinI>adc.SamplesI[adc.numsample])
	  *adc.MinI=(unsigned int)adc.SamplesI[adc.numsample];



        //Voltege
	adc.apsetChanel(0);
	adc.apConvertionStart();
	adc.apReadAdc(&adc.Dato,adc.ADRMEMORY);
	adc.SamplesV[adc.numsample]=adc.Dato;
	if(adc.MaxV<adc.SamplesV[adc.numsample])
	  {
	  adc.MaxV=adc.SamplesV[adc.numsample];
	   adc.ntfpi=adc.numsample;
	  }
	if(adc.MinV>adc.SamplesV[adc.numsample])
	  {
	  adc.MinV=adc.SamplesV[adc.numsample];
	  adc.ntfpv=adc.numsample;
	  }
	adc.numsample++;
      }
 cyg_mutex_unlock(&adcblock);
 cyg_scheduler_unlock();

}


void samples_program(cyg_addrword_t data)
{
  int message = (int) data;
  int delay;
  int n,x;
  char output;
  cyg_handle_t test_counterH, system_clockH, test_alarmH;
  cyg_tick_count_t ticks;
  cyg_alarm test_alarm;
  cyg_handle_t tiempo_real;
  unsigned how_many_alarms = 0, prev_alarms = 0, tmp_how_many;
  system_clockH = cyg_real_time_clock();
  cyg_clock_to_counter(system_clockH, &test_counterH);
  cyg_alarm_create(test_counterH, bitAdc_alarm_func,
		   (cyg_addrword_t) &how_many_alarms ,
		   &test_alarmH, &test_alarm);

  cyg_alarm_initialize(test_alarmH, cyg_current_time()+1, 160);
  tmain();
  for (;;)
    {

    }
}

// fin frecuencia de muestreo

//******* Analizer Quality Electrical Energy ***********

Op Thu, 26 Apr 2007 22:05:07 +0200 schreef Jhoberg <jrquevedor@gmail.com>:
> Analyzer of Quality Electrical Energy > > This desing and develop is constructed whit RTOS (eCos), consists of > a ADC of 16NBits with two channels one to capture Current and another > one of Voltege, captures the data with a sampling frequency of 7680Hz > to acquire 128 samples of the two you wave, are captured with thread, > another thdreas for communication with GPRS, other to process the data > by means of the FFT, disturbing the waves in its harmonic components > to determine as they are his compoenetes and thus to analyze its > harmonic Distortion, sends by means of Sockets the data of the waves > with interface of network PPP to be stored remotely in real-time.
On what hardware? What is the precision of the measurement?
> //********** Analizer Quality Electrical Energy ********* > [snip]
-- Gemaakt met Opera's revolutionaire e-mailprogramma: http://www.opera.com/mail/