#ifndef ADE7753_CS
#define ADE7753_CS PIN_B2
#endif

         //Name      Address No. Bits
#define   _WAVEFORM     0x01,3   //24
#define   _AENERGY      0x02,3   //24
#define   _RAENERGY     0x03,3   //24
#define   _LAENERGY     0x04,3   //24
#define   _VAENERGY     0x05,3   //24
#define   _RVAENERGY    0x06,3   //24
#define   _LVAENRGY     0x07,3   //24
#define   _LVARENERGY   0x08,2   //16
#define   _MODE         0x09,2   //16
#define   _IRQEN        0x0A,2   //16
#define   _STATUS       0x0B,2   //16
#define   _RSTSTATUS    0x0C,2   //16
#define   _CH1OS        0x0D,1   //8
#define   _CH2OS        0x0E,1   //8
#define   _GAIN         0x0F,1   //8
#define   _PHCAL        0x10,1   //6
#define   _APOS         0x11,2   //16
#define   _WGAIN        0x12,2   //12
#define   _WDIV         0x13,1   //8
#define   _CFNUM        0x14,2   //12
#define   _CFDEN        0x15,2   //12
#define   _IRMS         0x16,3   //24
#define   _VRMS         0x17,3   //24
#define   _IRMSOS       0x18,2   //12
#define   _VRMSOS       0x19,2   //12
#define   _VAGAIN       0x1A,2   //12
#define   _VADIV        0x1B,1   //8
#define   _LINECYC      0x1C,2   //16
#define   _ZXTOUT       0x1D,2   //12
#define   _SAGCYC       0x1E,1   //8
#define   _SAGLVL       0x1F,1   //8
#define   _IPKLVL       0x20,1   //8
#define   _VPKLVL       0x21,1   //8
#define   _IPEAK        0x22,3   //24
#define   _RSTIPEAK     0x23,3   //24
#define   _VPEAK        0x24,3   //24
#define   _RSTVPEAK     0x25,3   //24
#define   _TEMP         0x26,1   //8
#define   _PERIOD       0x27,2   //16
#define   _TMODE        0x3D,2   //8
#define   _CHKSUM       0x3E,1   //6
#define   _DIEREV       0x3F,1   //

#byte   POSTDEC0=   getenv("SFR:POSTDEC0")
#byte   POSTINC0=   getenv("SFR:POSTINC0")
#word   FSR0=      getenv("SFR:FSR0L")
 

 
void r_ADE7753(int8 addres,int8 len){
 
output_low(ADE7753_CS);           //Desactiva CS para iniciar transmision
   spi_write(addres);      //Escribe la direccion que sera leida
   Delay_us(4);            //Retardo de 4us para iniciar lectura
   do{
      spi_write(0);        //Se envian los pulsos de lectura
      POSTDEC0=spi_read(); //Se lee el dato del bufer y se almacena en la direccion del puntero
   }while(--len);          //predecrementa la variable len, que almacena la longitud, hasta que sea 0
   
output_high(ADE7753_CS);           //Activa Cs para finalizar la transmicion
}
 
void read_ADE7753(int8 &_val, _addres, _len){
   FSR0=_val;            //Cargamos el valor de la direccion de _val en el registro FSR0
   r_ADE7753(_addres,_len);//Enviamos la direccion y la longitud a leer
}
 
void write_ADE7753(int32 *val,int8 addres,int8 len){
   
output_low(ADE7753_CS);
   spi_write(0x80&addres);
   do{
      spi_write(*val++);
   }while(--len);
   
output_high(ADE7753_CS);
}
 
