#include "ADS8328.h"
void ADS8328_SPI_GPIO_Config(void)
{
/* 定义GPIO结构体 */
GPIO_InitTypeDef GPIO_InitStructure;
/* 开启GPIO时钟 */
ADS8328_SPI_GPIO_APBxClock_FUN ( ADS8328_SPI_CS_CLK, ENABLE );
ADS8328_SPI_GPIO_APBxClock_FUN ( ADS8328_SPI_SCK_CLK, ENABLE );
ADS8328_SPI_GPIO_APBxClock_FUN ( ADS8328_SPI_MISO_CLK, ENABLE );
ADS8328_SPI_GPIO_APBxClock_FUN ( ADS8328_SPI_MOSI_CLK, ENABLE );
ADS8328_SPI_GPIO_APBxClock_FUN ( ADS8328_SPI_EOC_CLK, ENABLE );
ADS8328_SPI_GPIO_APBxClock_FUN ( ADS8328_SPI_CONVST_CLK, ENABLE );
/* ADS8328_SPI_CS_PIN引脚初始化,并设置为推挽输出 */
GPIO_InitStructure.GPIO_Pin = ADS8328_SPI_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(ADS8328_SPI_CS_PORT, &GPIO_InitStructure);
/* ADS8328_SPI_SCK_PIN引脚初始化,并设置为推挽输出 */
GPIO_InitStructure.GPIO_Pin = ADS8328_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(ADS8328_SPI_SCK_PORT, &GPIO_InitStructure);
/* ADS8328_SPI_MOSI_PIN引脚初始化,并设置为推挽输出 */
GPIO_InitStructure.GPIO_Pin = ADS8328_SPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(ADS8328_SPI_MOSI_PORT, &GPIO_InitStructure);
/* ADS8328_SPI_MISO_PIN引脚初始化,并设置为浮空输入 */
GPIO_InitStructure.GPIO_Pin = ADS8328_SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(ADS8328_SPI_MISO_PORT, &GPIO_InitStructure);
/* ADS8328_SPI_CONVST_PIN引脚初始化,并设置为推挽输出 */
GPIO_InitStructure.GPIO_Pin = ADS8328_SPI_CONVST_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(ADS8328_SPI_CONVST_PORT, &GPIO_InitStructure);
/* ADS8328_SPI_EOC_PIN引脚初始化,并设置为浮空输入 */
GPIO_InitStructure.GPIO_Pin = ADS8328_SPI_EOC_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(ADS8328_SPI_EOC_PORT, &GPIO_InitStructure);
/* ADS8328_SPI_CS_HIGH为高电平 */
ADS8328_SPI_CS_HIGH;
/* ADS8328_SPI_CONVST_HIGH为高电平 */
ADS8328_SPI_CONVST_HIGH;
/* 延迟500ms */
SysTick_Delay_ms(500);
}
uint8_t ADS8328_SPI_Send4Bits(uint8_t data)
{
uint8_t num = 0;
int i = 0;
data &= 0x0F;
for(i=0;i<4;i++)
{
num <<= 1;
ADS8328_SPI_SCK_HIGH;
if((data & 0x08) >> 3)
{
ADS8328_SPI_MOSI_HIGH;
}
else
{
ADS8328_SPI_MOSI_LOW;
}
if(ADS8328_SPI_MISO_READ)
{
num++;
}
SysTick_Delay_us(5);
ADS8328_SPI_SCK_LOW;
SysTick_Delay_us(5);
data <<= 1;
}
SysTick_Delay_us(5);
return(num);
}
unsigned int ADS8328_SPI_Send16Bits(unsigned int data)
{
unsigned int num = 0;
int i = 16;
while(i--)
{
num <<= 1;
ADS8328_SPI_SCK_HIGH;
if((data & 0x8000) >> 15)
{
ADS8328_SPI_MOSI_HIGH;
}
else
{
ADS8328_SPI_MOSI_LOW;
}
if(ADS8328_SPI_MISO_READ)
{
num++;
}
SysTick_Delay_us(5);
ADS8328_SPI_SCK_LOW;
SysTick_Delay_us(5);
data <<= 1;
}
SysTick_Delay_us(5);
return(num);
}
unsigned int ADS8328_SPI_ReadHalfWord(void)
{
unsigned int num = 0;
int i = 16;
while(i--)
{
num <<= 1;
ADS8328_SPI_SCK_HIGH;
if(ADS8328_SPI_MISO_READ)
{
num++;
}
ADS8328_SPI_SCK_LOW;
}
SysTick_Delay_us(5);
return(num);
}
void ADS8328_Write_CFR(unsigned int data)
{
ADS8328_SPI_CS_LOW;
ADS8328_SPI_Send16Bits(data);
ADS8328_SPI_CS_HIGH;
}
void ADS8328_Read_CFR(unsigned int *data1)
{
ADS8328_SPI_CS_LOW;
*data1 = ADS8328_SPI_Send16Bits(0xC000)&0x0FFF;
ADS8328_SPI_CS_HIGH;
}
unsigned int ADS8328_Read_Data(void)
{
unsigned int ret = 0;
ADS8328_SPI_CS_LOW;
SysTick_Delay_us(3);
ADS8328_SPI_CS_HIGH;
while(!ADS8328_SPI_EOC);
ADS8328_SPI_CONVST_LOW;
SysTick_Delay_us(1);
ADS8328_SPI_CONVST_HIGH;
ADS8328_SPI_CS_LOW;
ret = ADS8328_SPI_ReadHalfWord();
ADS8328_SPI_CS_HIGH;
return ret;
}
void ADS8328_Init_Config(void)
{
unsigned int AD_Reg_Rd = 0;
ADS8328_SPI_CONVST_LOW;
SysTick_Delay_ms(100);
ADS8328_SPI_CONVST_HIGH;
ADS8328_SPI_CS_LOW;
ADS8328_SPI_Send4Bits(0x0F);
ADS8328_SPI_CS_HIGH;
SysTick_Delay_us(100);
ADS8328_Read_CFR(&AD_Reg_Rd);
ADS8328_Write_CFR(0x0EFF);
ADS8328_Read_CFR(&AD_Reg_Rd);
ADS8328_Write_CFR(0x02FD);
ADS8328_Read_CFR(&AD_Reg_Rd);
ADS8328_Write_CFR(0x06FF);
ADS8328_Read_CFR(&AD_Reg_Rd);
ADS8328_Write_CFR(0x02FD);
ADS8328_Read_CFR(&AD_Reg_Rd);
}
void ADS8328_Init(void)
{
ADS8328_SPI_GPIO_Config();
ADS8328_Init_Config();
}
求各位大神帮忙看看,搞了快一个礼拜了,有点烦啊 |