📄 5532.c
字号:
[原]CS5532 C51驱动程序2007-09-03 09:10//The program for CS5532-ASZ
//This is a 24bit ADC and PGIA
//Made by OurWay and 2006/03/21
//#include <reg51.h>
//#include <intrins.h>
//根据实际情况定义
//sbit SDI5532 = P2^1;
//sbit SDO5532 = P2^2;
//sbit CLK5532 = P2^3;
//sbit CS5532 = P2^0;
//sbit ACC7 = ACC^7;
//sbit ACC0 = ACC^0;
//#define BYTE unsigned char
//#define WORD unsigned int
#define Adjust5532Run 0
#define ReadSADC5532Run 1
#define ReadMADC5532Run 1
//The ADC results varibles define
struct{
unsigned char top;
unsigned char high;
unsigned char mid;
unsigned char low;
}
RegDat;
//The CS5532-ASZ comm define
#define RegRead 0x08
#define RegWrite 0x00
//=== Offset Register ===
#define OffsetRS 0x09
//=== Gain Register ===
#define GainRS 0x0a
//=== Configuration Register ===
#define ConfigWrite 0x03 //write config
#define ConfigRead 0x0b //read config
#define PSS 0x80 //Power Save Select
#define PDW 0x40 //Power Down Mode
#define RS 0x20 //Reset System
#define RV 0x10 //Reset Valid
#define IS 0x08 //Input Short
#define GB 0x04 //Guard Signal Bit
#define VRS 0x02 //Voltage Reference Select(Ref>2.5V,VRS=0)
#define A1 0x01
#define A0 0x80
#define OLS 0x40
#define OGS 0x10
#define FRS 0x08
//=== Channel Setup Register ===
#define SetupCH1 0x05
#define SetupCH2 0x15
//Channel Select Bits
#define CH1 0x00 //CS1=0,CS0=0
#define CH2 0x40 //CS1=0,CS0=1
//Gain Bits
#define Gain1 0x00
#define Gain2 0x08
#define Gain4 0x10
#define Gain8 0x18
#define Gain16 0x20
#define Gain32 0x28
#define Gain64 0x30
//=== Converter mode ===
#define SingleC 0x80
#define ContinC 0xC0
#define Setup1 0x00
#define Setup2 0x08
#define Setup3 0x10
#define Setup4 0x18
#define Setup5 0x20
#define Setup6 0x28
#define Setup7 0x30
#define Setup8 0x38
//The data(8bit) form MCU to CS5532
void SendByte5532(unsigned char Dat)
{
unsigned char i;
CLK5532 = 0;
for(i=8;i>0;i--)
{
SDI5532=(bit)(Dat & 0x80);
CLK5532=1;
_nop_();_nop_();
_nop_();_nop_();
CLK5532=0;
_nop_();_nop_();
_nop_();_nop_();
Dat = Dat<<1;
}
SDI5532 = 1;
}
//The Setup CS5532's register
void WriteReg5532(BYTE command,BYTE low,BYTE mid,BYTE high,BYTE top)
{
CS5532 = 0;
SendByte5532(command);
SendByte5532(low);
SendByte5532(mid);
SendByte5532(high);
SendByte5532(top);
CS5532 = 1;
}
//The data(8bit) form CS5532 to MCU
unsigned char ReceiveByte5532(void)
{
unsigned char i;
ACC=0;
for(i=8;i>0;i--)
{
ACC=ACC<<1;
ACC0=SDO5532;
CLK5532=1;
_nop_();_nop_();
_nop_();_nop_();
CLK5532=0;
_nop_();_nop_();
_nop_();_nop_();
}
return(ACC);
}
//Receive ADC signal data form CS5532 to MCU
#if ReadSADC5532Run
void ReadSADC5532(unsigned char command)
{
CS5532 = 0;
SendByte5532(command);
do{_nop_();CLK5532=0;SDI5532=0;}while(SDO5532!=0);
SendByte5532(0x00); //8bit SCLK and SDI=0;
RegDat.top = ReceiveByte5532();
RegDat.high = ReceiveByte5532();
RegDat.mid = ReceiveByte5532();
RegDat.low = ReceiveByte5532();
CS5532 = 1;
}
#endif
#if ReadMADC5532Run
void ReadMADC5532(unsigned char command)
{
CS5532 = 0;
do{_nop_();}while(SDO5532!=0);
//SDO5532 = 1;
SendByte5532(command); //8bit SCLK and SDI=command;
RegDat.top = ReceiveByte5532();
RegDat.high = ReceiveByte5532();
RegDat.mid = ReceiveByte5532();
RegDat.low = ReceiveByte5532();
CS5532 = 1;
}
#endif
//Receive CS5532's Register from CS5532 to MCU
void ReadReg5532(unsigned char command)
{
CS5532 = 0;
SendByte5532(command);
RegDat.top = ReceiveByte5532();
RegDat.high = ReceiveByte5532();
RegDat.mid = ReceiveByte5532();
RegDat.low = ReceiveByte5532();
CS5532 = 1;
}
#if Adjust5532Run
void Adjust5532(unsigned char command)
{
CS5532 = 0;
SendByte5532(command);
do{_nop_();}while(SDO5532!=0);
SendByte5532(0x0a);
RegDat.top = ReceiveByte5532();
RegDat.high = ReceiveByte5532();
RegDat.mid = ReceiveByte5532();
RegDat.low = ReceiveByte5532();
CS5532 = 1;
}
#endif
//The initialization CS5532
void Init5532(void)
{
WriteReg5532(0xff,0xff,0xff,0xff,0xff);
WriteReg5532(0xff,0xff,0xff,0xff,0xff);
WriteReg5532(0xff,0xff,0xff,0xff,0xff);
WriteReg5532(0xff,0xff,0xff,0xff,0xfe);
}
//The CS5532-ASZ subpram end
//用的时间注意我定义的宏,这是个查询方式采集AD值的程序。
/****************************************Copyright (c)**************************************************
**
**
**
** 文 件 名: ca5532.c
** 最后修改日期: 2007-10-8 9:40
** 描 述: cs5532驱动程序
** 版 本: V1.0
** 主 控 芯 片:M16 晶振频率:7.37MHZ,
** IDE:ICCAVR 6.31
**********************************************************************************************************/
#include
#include
#include "DEFINE.H"
#include "CS5532.H"
#include "CS5532.H"
uint08 cs5532_buf[4];
/**********************************************************************
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();
NOP();NOP();
SET_SCK();
NOP();NOP();
CLR_SCK();
dat<<=1;
NOP();
}
SET_SDI();
}
/**********************************************************************
functionName:uint08 cs5532_rd_byte(void)
description:
**********************************************************************/
uint08 cs5532_rd_byte(void)
{
uint08 i;
uint08 temp=0;
for(i=0;i<8;i++)
{
temp<<=1;
SET_SCK();
NOP();NOP();
if(STU_SDO)
temp|=1;
CLR_SCK();
NOP();NOP();NOP();
}
return temp;
}
/**********************************************************************
functionName:void cs5532_soft_rst(void)
description:
**********************************************************************/
void cs5532_soft_rst(void)
{
uint08 i;
for(i=0;i<15;i++)
{
cs5532_wr_byte(0xFF);
}
cs5532_wr_byte(0xFE);
}
/**********************************************************************
functionName:
description:
**********************************************************************/
void cs5532_wr_reg(uint08 cmd,uint32 dat)
{
uint08 temp[4];
temp[0]=dat>>24;
temp[1]=dat>>16;
temp[2]=dat>>8;
temp[3]=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]);
}
/**********************************************************************
functionName:void cs5532_rd_reg(uint08 cmd)
description:
**********************************************************************/
void cs5532_rd_reg(uint08 cmd)
{
cs5532_wr_byte(cmd);
cs5532_buf[0]=cs5532_rd_byte();
cs5532_buf[1]=cs5532_rd_byte();
cs5532_buf[2]=cs5532_rd_byte();
cs5532_buf[3]=cs5532_rd_byte();
}
/**********************************************************************
functionName:
description:
**********************************************************************/
void cs5532_init(void)
{
ms_delay(50); //等待电源稳定
cs5532_soft_rst(); //软件复位
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -