⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5463_init.c

📁 电能计量芯片CS5463初始化
💻 C
字号:

void start_5460(uchar which);
extern void start_5460_cmd(void);
extern code uchar bit_table[8];
uchar send_data,receive_data;
extern uchar pbuf[PBUF_LEN],chip_select; 
extern uchar tinyp spi_bcd_buf[SPI_BUF_LEN]; 
 
extern code uchar calib_addr_table[3][6];


code uchar calibration_table[6][4]={
    {0x46,0x00,0x00,0x00},    //dc voltage offset default value
    {0x48,0x40,0x00,0x00},    //ac voltage gain default value
    {0x62,0x00,0x00,0x00},    //ac voltage offset default value
    {0x42,0x00,0x00,0x00},    //dc current offset default value
    {0x44,0x40,0x00,0x00},    //ac current gain default value
    {0x60,0x00,0x00,0x00}     //ac current offset default value
};
/*
code uchar sync_cmd_table[4]=        //3bytes sync0, 1byte sync1
{
    0xff,0xff,0xff,0xfe
};
*/

code uchar ctrl_reg_default[4]=	{0x78,0x00,0x00,0x08};  //old is 0x0e(有源晶体)   
code uchar clr_drdy_table[4]=	{0x5e,0xff,0xff,0xff};
code uchar mask_reg_default[4]=	{0x74,0x80,0x00,0x00};	//default mask register setted value 
code uchar pulse_reg_default[4]={0x4a,0x00,0x0f,0xa0};	//default pulse register setted value
code uchar tbase_reg_default[4]={0x5a,0x80,0x00,0x00};	//default timer base register setted value


//2005-05-26
/*Operational Mode Register
  23      22      21      20      19      18      17      16  
  15      14      13      12      11      10      9       8
                          TCG     E2MODE  XVDEL 
  7       6       5       4       3       2       1       0
  XDEL    VHPF    IHPF    IIR     E3MODE1 E3MODE0 POS     AFC
*/

code uchar operational_reg_default[4]={   //default operational mode register setted value
    0x64,0x00,0x0,0x09
};

code uchar calibration_cmd[6]={
    0xd1,            //dc voltage offset
    0xd6,            //ac voltage gain
    0xd5,            //ac voltage offset
    0xc9,            //dc current offset
    0xce,            //ac current gain
    0xcd             //ac current offset
};

code uchar calibration_rd_table[6]={
    0x06,            //dc voltage offset register
    0x08,            //ac voltage gain   register
    0x22,            //ac voltage offset register
    0x02,            //dc current offset register
    0x04,            //ac current gain   register
    0x20             //ac current offset register
};


void Calibration_5460(uchar which,uchar chip)
{
    //uchar i;
    
    CPL_SPI_DRDY;
    SELECT_CS5460_CHIP(chip);
    _NOP();
    _NOP();
    _NOP();
    _NOP();
    _NOP();
    _NOP();

 //   for(i=which;i<6; i++)
  //  {
  //         Write_To_5460_Code(calibration_table[i]);                   //Write default to calibration register
 //   }                    
    if((which==0)||(which==3))           //DC OFFSET
    { 
        Write_To_5460_Code(calibration_table[which]);                   //Write default to calibration register      
        Write_To_5460_Code(calibration_table[which+1]);                 //Write default to calibration register 
        Write_To_5460_Code(calibration_table[which+2]);                  //Write default to calibration register  
    }                                   
    else if((which==1)||(which==4))      //AC GAIN
    {
        Write_To_5460_Code(calibration_table[which]);                   //Write default to calibration register      
        Write_To_5460_Code(calibration_table[which+1]);                 //Write default to calibration register     
    }
   else                                 //AC OFFSET
        Write_To_5460_Code(calibration_table[which]);                   //Write default to calibration register    
    
  //  Write_To_5460_Code(calibration_table[which]);                   //Write default to calibration register
    Write_To_5460_Code(clr_drdy_table);                             //clear DRDY
    send_data=0xa0;
    Send_5460();
                                                                    //power halt
    send_data=calibration_cmd[which];
    Send_5460();
                                                                    //send coordination calibration command
    Delay_10MS(200);                                                //Wait 8s
    Delay_10MS(200);
    Delay_10MS(200);
    Read_From_5460(pbuf,calibration_rd_table[which]);           //read calibration register value
    //pbuf[0]=calibration_table[which][0];
    if(Set_Data_To_Memory(pbuf,calib_addr_table[chip][which],3))
    {                                                          
        Set_Data_To_Memory(pbuf,calib_addr_table[chip][which],3); 
    }
                                                                    //startup cs5460
    send_data=0xe8;
    Send_5460();
    UNSELECT_CS5460_CHIP(chip);
}


void Init_5460(uchar which)
{
    uchar i;
    RESET_CS5460_LOW(which);
    Delay_10MS(1);
    RESET_CS5460_HIGH(which);
    Delay_10MS(1);
    SELECT_CS5460_CHIP(which);
    _NOP();
    _NOP();
    _NOP();
    _NOP();
    _NOP();
    _NOP();
    CLR_CLK;
    //SET_SDA_IN;

    SET_SDA_IN;
    for(i=0;i<31;i++)
    {
        SET_CLK;
        _NOP();
        _NOP();
        CLR_CLK;
    }

    CLR_SDA_IN;
    SET_CLK;
    _NOP();
    _NOP();
    CLR_CLK;

                        //Write_To_5460_Code(sync_cmd_table);                          //Write SPI SYNC CMD
                                                                   //power halt
//    send_data=0xa0;
//    Send_5460();
 
    Write_To_5460_Code(ctrl_reg_default);
    Write_To_5460_Code(clr_drdy_table);                                //clear STATUS REGISTER DRDY
    Write_To_5460_Code(mask_reg_default);                              //set every regiter default value
    Write_To_5460_Code(pulse_reg_default);

 //   Write_To_5460_Code(tbase_reg_default);   

            Write_To_5460_Code(operational_reg_default);


    Load_Phase_Adjust(which,0);
   
    if(!Get_Data_From_Memory(&pbuf[1],calib_addr_table[which][0],3))        //dc offset vol
    {
        pbuf[0]=calibration_table[0][0];
        Write_To_5460(pbuf);
    }
    else     
     
        Write_To_5460_Code(calibration_table[0]);
    
    if(!Get_Data_From_Memory(&pbuf[1],calib_addr_table[which][1],3))        //dc gain vol
    {
        pbuf[0]=calibration_table[1][0];
        Write_To_5460(pbuf);
    }
    else   
         
        Write_To_5460_Code(calibration_table[1]);
/*
    if(!Get_Data_From_Memory(&pbuf[1],calib_addr_table[which][2],3))        //ac offset vol
    {
        pbuf[0]=calibration_table[2][0];
        Write_To_5460(pbuf);
    }
    else
        Write_To_5460_Code(calibration_table[2]);
*/
    if(!Get_Data_From_Memory(&pbuf[1],calib_addr_table[which][3],3))        //dc offset current
    {
        pbuf[0]=calibration_table[3][0];
        Write_To_5460(pbuf);
    }
    else 
    
        Write_To_5460_Code(calibration_table[3]);

    if(!Get_Data_From_Memory(&pbuf[1],calib_addr_table[which][4],3))        //dc gain current
    {
        pbuf[0]=calibration_table[4][0];
        Write_To_5460(pbuf);
    }
    else  
        Write_To_5460_Code(calibration_table[4]);
/*
    if(!Get_Data_From_Memory(&pbuf[1],calib_addr_table[which][5],3))        //ac offset current
    {
        pbuf[0]=calibration_table[5][0];
        Write_To_5460(pbuf);
    }
    else
        Write_To_5460_Code(calibration_table[5]);
 */
    send_data=0xe8;                                                         //START 5460 CONVET
    Send_5460();  
    UNSELECT_CS5460_CHIP(which);
}

 
void Read_From_5460(uchar *ps,uchar which)
{
    send_data=which;
    Send_5460();
    Receive_5460();
    ps[0]=receive_data;
    Receive_5460();
    ps[1]=receive_data;
    Receive_5460();
    ps[2]=receive_data;
}

void Write_To_5460(uchar *ps)
{
    send_data=ps[0];
    Send_5460();
    send_data=ps[1];
    Send_5460();
    send_data=ps[2];
    Send_5460();
    send_data=ps[3];
    Send_5460();
}

void Write_To_5460_Code(uchar code *ps)
{
    send_data=ps[0];
    Send_5460();
    send_data=ps[1];
    Send_5460();
    send_data=ps[2];
    Send_5460();
    send_data=ps[3];
    Send_5460();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -