/* -----------------------------------------------------------------------
* @ brief ADC Detect configuration
* @ author
* @ date
* @ statement
* -----------------------------------------------------------------------*/
#include "adcs5532.h"
#include "io.h"
#include "stm32f4xx.h"
#include "bsp.h"
#include "ucos_ii.h"
uint32_t addata1 = 0,addata2 = 0;
void cs5532_Test(void)
{
uint32_t q;
uint32_t *ADC_BUF;
ADC_BUF=&q;
if(ReadCs5532AD(AD_CH1,ADC_BUF)==1)
{
//printf("AD1值为: %d \n" , *ADC_BUF);
addata1=*ADC_BUF;
addata1=addata1;
}
else
{
//printf("AD1值读取错误 %d \n" , 0 );
addata1=0;
}
//LED_ON;
OSTimeDlyHMSM(0,0,0,10);
if(ReadCs5532AD(AD_CH2,ADC_BUF)==1)
{
//printf("AD2值为: %d \n" , *ADC_BUF);
addata2= *ADC_BUF;
addata2=addata2;
}
else
{
// printf("AD2值读取错误 %d \n" , 0 );
addata2=0;
}
//LED_OFF;
OSTimeDlyHMSM(0,0,0,10);
}
/* functions ------------------------------------------------------------*/
void Adcs5532_IO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = CS5532_SCK_Pin;
GPIO_InitStructure.GPIO_Mode = CS5532_SCK_MODE;
GPIO_InitStructure.GPIO_Speed = CS5532_SCK_SPEED;
GPIO_InitStructure.GPIO_OType = CS5532_SCK_OTYPE;
GPIO_InitStructure.GPIO_PuPd = CS5532_SCK_PUPD;
GPIO_Init(CS5532_SCK_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CS5532_SDI_Pin;
GPIO_InitStructure.GPIO_Mode = CS5532_SDI_MODE;
GPIO_InitStructure.GPIO_Speed = CS5532_SDI_SPEED;
GPIO_InitStructure.GPIO_OType = CS5532_SDI_OTYPE;
GPIO_InitStructure.GPIO_PuPd = CS5532_SDI_PUPD;
GPIO_Init(CS5532_SDI_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CS5532_SDO_Pin;
GPIO_InitStructure.GPIO_Mode = CS5532_SDO_MODE;
GPIO_InitStructure.GPIO_Speed = CS5532_SDO_SPEED;
GPIO_InitStructure.GPIO_OType = CS5532_SDO_OTYPE;
GPIO_InitStructure.GPIO_PuPd = CS5532_SDO_PUPD;
GPIO_Init(CS5532_SDO_GPIO_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = LIGHT_PIN;
GPIO_InitStructure.GPIO_Mode = LIGHT_IO_MODE;
GPIO_InitStructure.GPIO_Speed = LIGHT_IO_SPEED;
GPIO_InitStructure.GPIO_OType = LIGHT_IO_OTYPE;
GPIO_InitStructure.GPIO_PuPd = LIGHT_IO_PUPD;
GPIO_Init(LIGHT_PORT, &GPIO_InitStructure);
}
void Delay_100ns(uint32_t ns)
{
int i, j;
for (i = 0; i < ns; i++)
{
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
}
}
void delay_us(INT16U n)
{
while(n--)
{
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();//72个__NOP();延时1us
}
}
void delay_nms(INT16U n)//循环1000次就是1ms
{
while(n--)
{
delay_us(1);//调用之前的1us延时
}
}
/**********************************************************************************************
// 函数: void WriteCs5532Byte(uint8_t da)
// 描述: 给CS5532写1个字节的数据
// 参数: 给CS5532写入的数据,1个字节
// 返回: non
// 说明: non
// 版本: V1.0, 2018-09-07
************************************************************************************************/
void WriteCs5532Byte(uint8_t da)
{
uint8_t i;
Set_CS5532_SCLK_Low();
//Delay330ns(1);
delay_us(1);
for(i=0;i<8;i++)
{
if((da&0x80)==0x80)
{
Set_CS5532_SDI_High();
}
else
{
Set_CS5532_SDI_Low();
}
delay_us(2);
Set_CS5532_SCLK_High();
delay_us(3);
Set_CS5532_SCLK_Low();
delay_us(3);
//Delay330ns(1);
// __NOP();
// __NOP();
da = da<<1;
}
}
/**********************************************************************************************
// 函数: uint8_t ReadCs5532Byte(void)
// 描述: CS5532读出一个字节的数据
// 参数: non
// 返回: CS5532读出的一个字节数据
// 说明: non
// 版本: V1.0, 2018-09-07
************************************************************************************************/
uint8_t ReadCs5532Byte(void)
{
uint8_t i,dat;
Set_CS5532_SCLK_Low();
// Delay330ns(20);
// __NOP();
// __NOP();
delay_us(1);
for(i=0;i<8;i++)
{
dat = dat<<1;
if(Read_CS5532_SDO_Data() == 0x00) //0x01
{
dat = dat|0x01;
}
delay_us(2);
Set_CS5532_SCLK_High();
// __NOP();
// __NOP();
delay_us(3);
// Delay330ns(1);
Set_CS5532_SCLK_Low();
delay_us(3);
// __NOP();
// __NOP();
//delay_us();
// Delay330ns(1);
}
return dat;
}
/**********************************************************************************************
// 函数: void WriteCs5532Dword(uint32_t da)
// 描述: 给CS5532写4个字节的数据
// 参数: 给CS5532写入的数据,long类型,32位数据
// 返回: non
// 说明: non
// 版本: V1.0, 2018-09-07
************************************************************************************************/
void WriteCs5532Dword(uint32_t da)
{
union type1
{
uint8_t ch[4];
uint32_t lon;
};
union type1 temp;
temp.lon= da;
WriteCs5532Byte(temp.ch[3]);
WriteCs5532Byte(temp.ch[2]);
WriteCs5532Byte(temp.ch[1]);
WriteCs5532Byte(temp.ch[0]);
}
/**********************************************************************************************
// 函数: static uint32_t ReadCs5530(void)
// 描述: 读取CS5532的数据
// 参数: 1.选择AD的通道 2.读出的AD数据的地址指针
// 返回: 1:读取AD OK 0:读取AD溢出
// 说明: 参数1:AD_CH1和AD_CH2
// 版本: V1.0, 2018-09-14
************************************************************************************************/
uint8_t ReadCs5532AD(uint8_t ADCHANNEL,uint32_t *out)
{
union type1
{
uint8_t ch[4];
uint32_t lon;
};
static union type1 temp;
WriteCs5532Byte(ADCHANNEL);
while(Read_CS5532_SDO_Data() != 0x01); //0x01
Set_CS5532_SDI_Low();
WriteCs5532Byte(CS5532_NULL);
temp.ch[3] = ReadCs5532Byte();
temp.ch[2] = ReadCs5532Byte();
temp.ch[1] = ReadCs5532Byte();
temp.ch[0] = ReadCs5532Byte();
// UART1_Waite_SendByte(temp.ch[0]);
if((temp.ch[0]&0x04)==0x04)
{
return 0;//
}
temp.lon = temp.lon>>8;
*out = temp.lon;
return 1;
}
/**********************************************************************************************
// 函数: static void cs5532_init(void)
// 描述: CS5530的初始化,固定速度为15sps
// 参数: non
// 返回: non
// 说明: non
// 版本: V1.0, 2018-09-07
************************************************************************************************/
void cs5532_init(void)
{
uint8_t loop;
uint32_t da;
//
// //延时20ms,等待晶振启动
DelayMs(20);
// //发送ADC串口复位指令,初始化ADC到可访问状态
// //发送SYNC1命令字,至少15个,这里发送30个
for (loop=0;loop<30;loop++)
{
WriteCs5532Byte(SYNC1);
}
// //发送一个SYNC0命令字
WriteCs5532Byte(SYNC0);
// //启动系统复位
WriteCs5532Byte(W_CONFIG_REG);//写配置寄存器
WriteCs5532Dword(SYSTEM_RESET);
DelayMs(50); //直接延时,不进行复位成功的循环检测
//// 复位后读取配置寄存器的时候需要连续读两次,第一次RV为1,第二次RV为0
WriteCs5532Byte(R_CONFIG_REG);//读配置寄存器
ReadCs5532Byte();
ReadCs5532Byte();
ReadCs5532Byte();
ReadCs5532Byte();
WriteCs5532Byte(R_C
- 1
- 2
前往页