📄 cs5532.c
字号:
/****************************************Copyright (c)**************************************************
**
**
**
** 文 件 名: ca5532.c
** 最后修改日期: 2007-10-8 9:40
** 描 述: cs5532驱动程序
** 版 本: V1.0
** 主 控 芯 片:M16 晶振频率:7.37MHZ,
** IDE:ICCAVR 6.31
**********************************************************************************************************/
#include <STC89C51RC_RD_PLUS.H>
#include "DEFINE.H"
#include "CS5532.H"
extern void us_delay(uint08 ud);
extern void ms_delay(uint08 md);
uint08 cs5532_buf[4];
#define DL_US 2
/**********************************************************************
functionName:void cs5532_wr_byte(uint08 dat)
description:写字节
**********************************************************************/
void cs5532_wr_byte(uint08 dat)
{
uint08 i;
for(i=0;i<8;i++)
{
if(dat&0x80)
SET_SDI();
else
CLR_SDI();
us_delay(DL_US);
SET_SCK();
us_delay(DL_US);
CLR_SCK();
dat<<=1;
us_delay(DL_US);
}
SET_SDI();
}
/**********************************************************************
functionName:uint08 cs5532_rd_byte(void)
description:读字节
**********************************************************************/
uint08 cs5532_rd_byte(void)
{
uint08 i;
uint08 temp=0;
//CLR_SDI();
for(i=0;i<8;i++)
{
temp<<=1;
SET_SCK();
us_delay(DL_US);
if(STU_SDO)
temp|=1;
CLR_SCK();
us_delay(DL_US);
}
return temp;
}
/**********************************************************************
functionName:void cs5532_soft_rst(void)
description:软件复位
**********************************************************************/
void cs5532_soft_rst(void)
{
uint08 i;
CLR_SCK();
EA=0;
for(i=0;i<15;i++)
{
cs5532_wr_byte(0xFF);
}
cs5532_wr_byte(0xFE);
EA=1;
}
/**********************************************************************
functionName:void cs5532_wr_reg(uint08 cmd,uint32 dat)
description:写cs5532寄存器
**********************************************************************/
void cs5532_wr_reg(uint08 cmd,uint32 dat)
{
uint08 temp[4];
uint32 temp32;
EA=0;
temp32=dat;
temp32>>=24;
temp[0]=(uint08)temp32;
temp32=dat;
temp32>>=16;
temp[1]=(uint08)temp32;
temp32=dat;
temp32>>=8;
temp[2]=(uint08)temp32;;
temp[3]=(uint08)dat;
cs5532_wr_byte(cmd);
cs5532_wr_byte(temp[0]);
cs5532_wr_byte(temp[1]);
cs5532_wr_byte(temp[2]);
cs5532_wr_byte(temp[3]);
EA=1;
}
/**********************************************************************
functionName:void cs5532_rd_reg(uint08 cmd)
description:读cs5532寄存器
**********************************************************************/
uint32 cs5532_rd_reg(uint08 cmd)
{
uint32 rv=0;
cs5532_wr_byte(cmd);
cs5532_buf[3]=cs5532_rd_byte(); //高八位
cs5532_buf[2]=cs5532_rd_byte();
cs5532_buf[1]=cs5532_rd_byte();
cs5532_buf[0]=cs5532_rd_byte();
rv=cs5532_buf[3];
rv<<=8;
rv|=cs5532_buf[2];
rv<<=8;
rv|=cs5532_buf[1];
rv<<=8;
rv|=cs5532_buf[0];
return(rv);
}
/**********************************************************************
functionName:void cs5532_init(void)
description:初始化CS5532
**********************************************************************/
void cs5532_init(void)
{
uint08 i;
ms_delay(100); //等待时钟稳定
cs5532_soft_rst(); //软件复位
ms_delay(100);
cs5532_wr_reg(CMD_WR_CFG,0x20000000); //系统复位,RS=1
ms_delay(100);
cs5532_rd_reg(CMD_RD_CFG); //读取配置寄存器
while(cs5532_buf[3]&0x10) //等待RV返回0
{
cs5532_rd_reg(CMD_RD_CFG);
}
//设置配置寄存器,这里不用设置,全部用默认值,0x00000000
//设置通道设置寄存器
//校准
cs5532_wr_byte(0x45);
for(i=0;i<4;i++)
{
cs5532_wr_byte(0x30); //
// cs5532_wr_byte(0x31); //G=32
//cs5532_wr_byte(0x20); //G=16
//cs5532_wr_byte(0x18); //G=8
//cs5532_wr_byte(0x30); //G=64
//cs5532_wr_byte(0x00); //G=1
cs5532_wr_byte(0x80);
cs5532_wr_byte(0x00);
cs5532_wr_byte(0xC0);
}
}
/**********************************************************************
functionName:void ca5532_cali(void)
description:CS5532校准
**********************************************************************/
/*void cs5532_cali(void)
{
;
}*/
/**********************************************************************
functionName:sint32 cs5532_sample(void)
description:CS5532采样,输出通道1的数据
**********************************************************************/
/*sint32 cs5532_sample(void)
{
sint32 temp;
uint32 tmp[3];
//cs5532_rd_reg(CMD_RD_ADC);
cs5532_rd_reg(0x0C);
cs5532_buf[0]=cs5532_rd_byte(); //高八位
cs5532_buf[1]=cs5532_rd_byte();
cs5532_buf[2]=cs5532_rd_byte();
cs5532_buf[3]=cs5532_rd_byte();
tmp[0]=cs5532_buf[0];
tmp[1]=cs5532_buf[1];
tmp[2]=cs5532_buf[2];
tmp[0]<<=16;
tmp[1]<<=8;
tmp[2]+=tmp[0];
tmp[2]+=tmp[1];
temp=tmp[2];
temp<<=8;
temp>>=8;
return(temp);
} */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -