Ingo Electronics


  AD7280A library functions 


#include  "AD7280A_lib.h"


#define  CS                  RC6_bit
#define  PD_ad7280     RC1_bit
#define  led                 RA0_bit
#define  cnv_start        RA4_bit

unsigned char crc8_table[256]  ;
unsigned int channel_value[6];

//Baitai kurie naudojami komandoms i AD7280A siusti
unsigned char byte_1;
unsigned char byte_2;
unsigned char byte_3;
unsigned char byte_4;

unsigned long reg;
unsigned char reg_data;
unsigned char cell_balance_status;

unsigned long byte1_rd ;
unsigned long byte2_rd ;
unsigned long byte3_rd ;
unsigned long byte4_rd ;

unsigned char ch1_value ;
unsigned char ch2_value ;
unsigned char ch3_value ;
unsigned char ch4_value ;
unsigned char ch5_value ;
unsigned char ch6_value ;
/*
AD7280 CRC polynomial
P(x) = x^8 + x^5 + x^3 + x^2 + x^1 + x^0 = 0b100101111 = 0x12F
*/
#define GP  0x12F
#define DI  0x2F

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
void spi_write_32bit(unsigned long value){

byte_1 =  value>>24;
byte_2 =  value>>16;
byte_3 =  value>>8;
byte_4 =  value;

CS=0;
delay_us(30);
byte1_rd = SPI1_Read(byte_1);
byte2_rd = SPI1_Read(byte_2);
byte3_rd = SPI1_Read(byte_3);
byte4_rd = SPI1_Read(byte_4);
delay_us(110);
CS=1;
delay_ms(3);

}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
void ad7280_crc8_build_table(){
 
int i,j;
unsigned char crc;

for (i=0; i<256; i++) {
crc = i;
for (j=0; j<8; j++)
crc = (crc << 1) ^ ((crc & 0x80) ? DI : 0);
crc8_table[i] = crc & 0xFF;

}

}
//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
static unsigned char ad7280_calc_crc8(unsigned char *crc_tab, unsigned long val)
{
         unsigned char crc;

         crc = crc_tab[(val >> 16) & 0xFF];
         crc = crc_tab[crc ^ ((val >> 8) & 0xFF)];

         return  crc ^ (val & 0xFF);
}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
static char ad7280_check_crc(unsigned char *crc_tab, unsigned long val)
{
        unsigned char crc ;
        crc = ad7280_calc_crc8(crc_tab, val >> 10);

         if (crc != ((val >> 2) & 0xFF))  {return 0;}


         else {return 1;};
}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
void ad7280_write_long(unsigned long addr,unsigned long val){

reg = addr << 21 | val << 13;

reg |= ad7280_calc_crc8(crc8_table, reg >> 11) << 3 | 0x2;

byte_1 =  reg>>24;
byte_2 =  reg>>16;
byte_3 =  reg>>8;
byte_4 =  reg;

CS=0;
delay_us(30);
SPI1_Write(byte_1);
SPI1_Write(byte_2);
SPI1_Write(byte_3);
SPI1_Write(byte_4);
delay_us(110);
CS=1;
delay_ms(3);

}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
unsigned char  ad7280_check_register(unsigned char reg_addr, unsigned char reg_data){

ad7280_write_long(reg_read, reg_addr << 2);

spi_write_32bit(0xF800030A);

reg = byte1_rd<<24 | byte2_rd<<16|byte3_rd<<8|byte4_rd ;

if(ad7280_check_crc(crc8_table, reg) !=1 || reg_data   !=  ((char)(reg >> 13))  )   return 0; //error

return 1;

}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
unsigned char  ad7280_read_register(unsigned char reg_addr){

ad7280_write_long(reg_read, reg_addr << 2);

spi_write_32bit(0xF800030A);

reg = byte1_rd<<24 | byte2_rd<<16|byte3_rd<<8|byte4_rd ;

if(ad7280_check_crc(crc8_table, reg) ==1 ) {reg_data = (char)(reg>>13) ;}

return reg_data;

}

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
unsigned int  ad7280_read_singl_channel(unsigned char ch_addr){

ad7280_write_long(reg_read, ch_addr << 2);

ad7280_write_long (contr_HB, 0b10100000);  //conversion six cell voltages only

ad7280_write_long(cnvst_contr , 0b00000010);

// Initiate conversions through the falling edge of CNVST
cnv_start = 0;delay_ms(1); cnv_start = 1;
delay_ms(10);

ad7280_write_long(cnvst_contr,0x01);

spi_write_32bit(0xF800030A);

reg = byte1_rd<<24 | byte2_rd<<16|byte3_rd<<8|byte4_rd ;

if(ad7280_check_crc(crc8_table, reg) !=1 )   return 0xFFFF; //error

reg = (reg >> 11) & 0x0FFF;
reg = ((reg*967)/1000) +1000 ;
return (int)reg;
}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
unsigned int  ad7280_read_all_channels(unsigned char cnt,unsigned int *array){

unsigned long sum;
unsigned char i;
sum=0;
reg=0;

ad7280_write_long(reg_read, 0x00);

ad7280_write_long( contr_HB,  0b10100000);  //conversion six cell voltages only

ad7280_write_long(cnvst_contr,0b00000010);

// Initiate conversions through the falling edge of CNVST
cnv_start = 0;delay_ms(1); cnv_start = 1;
delay_ms(10);

for (i=0 ; i < cnt; i++) {
spi_write_32bit(0xF800030A);
reg = byte1_rd<<24 | byte2_rd<<16|byte3_rd<<8|byte4_rd ;
reg = (reg >> 11) & 0x0FFF;
reg = ((reg*967)/1000) +1000 ;
array[i] = (int)reg;
sum +=  (int)reg;
}

ad7280_write_long(cnvst_contr,0x01);

sum =   ((sum*967)/1000) +1000 ;

return sum;

}

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
void channel_balance(){

if ( channel_value[0] > 3600) { ad7280_write_long(cell_balance,cell_balance|CB1_ON);}
else { ad7280_write_long(cell_balance,cell_balance|CB1_OFF);}
if ( channel_value[1] > 3600) { ad7280_write_long(cell_balance,cell_balance|CB2_ON);}
else { ad7280_write_long(cell_balance,cell_balance|CB2_OFF);}
if ( channel_value[2] > 3600) { ad7280_write_long(cell_balance,cell_balance|CB3_ON);}
else { ad7280_write_long(cell_balance,cell_balance|CB3_OFF);}
if ( channel_value[3] > 3600) { ad7280_write_long(cell_balance,cell_balance|CB4_ON);}
else { ad7280_write_long(cell_balance,cell_balance|CB4_OFF);}
if ( channel_value[4] > 3600) { ad7280_write_long(cell_balance,cell_balance|CB5_ON);}
else { ad7280_write_long(cell_balance,cell_balance|CB5_OFF);}
if ( channel_value[5] > 3600) { ad7280_write_long(cell_balance,cell_balance|CB6_ON);}
else { ad7280_write_long(cell_balance,cell_balance|CB6_OFF);}

}
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''