#include "CS5532.h"
#include "CS5532_SPI.h"
int main(void)
{
static u32 temp=0,sum=0,sum1=0,sum2=0;
SPI_Initial();//主机模式,速度加倍 配置函数,省略没贴
CS5532_Initial();
while(1)
{
sum=read_adc();//连续转换模式
}
void CS5532_Reset(void)//串口复位
{
u8 i=0;
for(i=0;i<31;i++)
{
SPI_WriteByte(0xff);
}
SPI_WriteByte(0xfe);
}
void CS5532_Config()
{
CS5532_CS(0);
//写配置寄存器0x00080000, FRS=1//输出字速率及相应的滤波特性乘以系数5/6
SPI_WriteByte(0x03);
SPI_WriteByte(0x00);
SPI_WriteByte(0x08);
SPI_WriteByte(0x00);
SPI_WriteByte(0x00);
//写通道设置寄存器
SPI_WriteByte(0x05);
/*ADC64X*/
SPI_WriteByte(0X31);
SPI_WriteByte(0X80);
/*ADC15hz*/
SPI_WriteByte(0x31);
SPI_WriteByte(0x80);
SPI_WriteByte(0xc0);//执行转换命令 向CS5532中写入启动A/D连续转换 (8位串口命令)
}
/******************************
func: CS5532 初始化
input: none
output: none
*******************************/
void CS5532_Initial()
{
u8 i=0;
u32 tmp=0,reg1=0,reg2=0,reg3=0,reg0=0;
CS5532_CS(0);
while(tmp!=0x30000000)
{
CS5532_Reset();
/*//向命令寄存器中写入0x03.表明调用配置寄存器
// 0x22000000 写入CS5532复位命令 RS=1 激活一个复位周期,复位后自动归0
// VRS=1 电压参考选择 1 V<=Vref<=2.5V */
SPI_WriteByte(0x03);
SPI_WriteByte(0x22);
SPI_WriteByte(0x00);
SPI_WriteByte(0x00);
SPI_WriteByte(0x00);
/*/读取配置寄存器内容 1.先通过write_CS5532_byte(0x0b)这个命令,读配置寄存器
//2.再从配置寄存器读取内容传到MCU. */
SPI_WriteByte(0x0b);
reg0=SPI_ReadByte();
reg1=SPI_ReadByte();
reg2=SPI_ReadByte();
reg3=SPI_ReadByte();
tmp=reg0<<24|reg1<<16|reg2<<8|reg3;
}
CS5532_Config();
}
void CS5532_Delay(uint length)
{
while(length>0)
length--;
}
u32 read_adc()
{
u8 i;
u32 temp0=0,temp1=0,temp2=0,temp3=0;
u32 reg=0;
CS5532_CS(0);
for(i=0;i<8;i++)
{
SPI_WriteByte(0xff);
}
temp0=SPI_ReadByte();
temp1=SPI_ReadByte();
temp2=SPI_ReadByte();
temp3=SPI_ReadByte();
reg=temp0<<24|temp1<<16|temp2<<8|temp3;
// CS5532_CS(1);
return reg>>11;
}
u8 SPI_ReadByte(void)//
{
return (SPI_WriteByte(0xff));
}
u8 SPI_WriteByte(u8 TxData)
{
u8 RxData=0;
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET); //
SPI_I2S_SendData(SPI1,TxData);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
RxData=SPI_I2S_ReceiveData(SPI1);
return RxData;
}
|