LPC1788-IO模拟SPI--驱动AD7708--寄存器操作
扫描二维码
随时随地手机看文章
最近在使用AD7708采集数据-本来是想使用LPC1788自带的SSP进行控制,但是在多次实验过程中发现,单个读取寄存器是没有问题的,但是在连续读取时出现问题,一直没有解决,项目着急使用就直接用IO口模拟SPI,比较简单就准确读出了数据,也没有出现上述问题,记之!
个人还是觉得是自己配置的原因,后续会继续研究SSP的控制AD7708,希望能够搞清楚!
主函数只需要调用两个函数---AD7708_Reg_Init();----AD7708_ReadAD_Data();
/*-------ad7708_spi_io.h-------------*/
#ifndef __AD7708_SPI_IO_H
#define __AD7708_SPI_IO_H
#include"lpc177x_8x.h"
#define AD7708_CS_LOW() (LPC_GPIO0->CLR |=(1<<6))
#define AD7708_CS_HIGH() (LPC_GPIO0->SET |=(1<<6))
#define AD7708_SCLK_LOW() (LPC_GPIO0->CLR |=(1<<7))
#define AD7708_SCLK_HIGH() (LPC_GPIO0->SET |=(1<<7))
#define AD7708_DIN_LOW() (LPC_GPIO0->CLR |=(1<<9))
#define AD7708_DIN_HIGH() (LPC_GPIO0->SET |=(1<<9))
#define AD7708_RESET_LOW() (LPC_GPIO0->CLR |=(1<<18))
#define AD7708_RESET_HIGH() (LPC_GPIO0->SET |=(1<<18))
#define AD7708_RDY_LOW() (((LPC_GPIO0->PIN) & (1<<17)) == 0x00)
#define AD7708_DOUT_HIGH() (((LPC_GPIO0->PIN) & (1<<8)) != 0x00)
void AD7708_Reg_Init(void);
void AD7708_Reset(void);
void AD7708_Write8Bit(uint8_t data);
uint8_t AD7708_Read8Bit(void);
uint16_t AD7708_Read16Bit(void);
uint16_t AD7708_ReadAD_Data(uint8_t Channel);
#endif
/*--------ad7708_spi_io.c------------------------*/
#include "ad7708_spi_io.h"
/*----------短延时---------*/
void AD7708_Delay(uint16_t time)
{
uint16_t i;
for(i=0;i}
/*-----------AD7708-IO初始化-------*/
uint8_t AD7708_IO_Init(void)
{
LPC_IOCON->P0_6=0x30; //CS
LPC_GPIO0->DIR |=(1<<6);
LPC_GPIO0->SET |=(1<<6);
LPC_IOCON->P0_7=0xA0; //SCLK
LPC_GPIO0->DIR |=(1<<7);
LPC_GPIO0->SET |=(1<<7);
LPC_IOCON->P0_8=0xA0; //DOUT-MISO
LPC_GPIO0->DIR &=(~(1<<8));//输入
LPC_IOCON->P0_9=0xA0; //DIN-MOSI
LPC_GPIO0->DIR |=(1<<9);
LPC_GPIO0->SET |=(1<<9);
LPC_IOCON->P0_17=0x20; //RDY
LPC_GPIO0->DIR &=(~(1<<17));//输入
LPC_IOCON->P0_18=0x30; //RESET
LPC_GPIO0->DIR |=(1<<18);
LPC_GPIO0->SET |=(1<<18);
}
/*------------------复位AD7708-------------------*/
void AD7708_Reset(void)
{
AD7708_IO_Init();
AD7708_RESET_HIGH(); //RESET=1
AD7708_Delay(10);
AD7708_RESET_LOW(); //RESET=0
AD7708_Delay(100);
AD7708_RESET_HIGH();//RESET=1
AD7708_Delay(10);
AD7708_CS_HIGH(); //CS=1
AD7708_DIN_HIGH(); //DIN=1
AD7708_SCLK_HIGH(); //SCK=1
}
/*-----------判断AD7708是否忙状态----监测RDY引脚------------*/
uint8_t AD7708_Busy()
{
uint16_t i;
uint8_t temp;
temp = 0;
for(i=0;i<1000;i++)
{
if(AD7708_RDY_LOW())
{
temp = 1;
break;
}
AD7708_Delay(10);
}
return (temp);
}
/*-----------写入8位数据-------------*/
void AD7708_Write8Bit(uint8_t data)
{
uint8_t i,temp;
AD7708_CS_LOW();
AD7708_Delay(5);
for(i=0;i<8;i++)
{
AD7708_Delay(5);
AD7708_SCLK_LOW();
AD7708_Delay(10);
temp = data & 0x80;
if(temp)
{
AD7708_DIN_HIGH();
}
else
{
AD7708_DIN_LOW();
}
AD7708_Delay(20);
AD7708_SCLK_HIGH();
data <<= 1;
AD7708_Delay(5);
}
AD7708_SCLK_HIGH();
AD7708_DIN_HIGH();
AD7708_Delay(10);
AD7708_CS_HIGH();
}
/*---------------读取8位数据--------------------*/
uint8_t AD7708_Read8Bit(void)
{
uint8_t i,temp=0x00;
AD7708_CS_LOW();
AD7708_Delay(5);
for(i=0;i<8;i++)
{
AD7708_Delay(5);
AD7708_SCLK_LOW();
AD7708_Delay(30);
if(AD7708_DOUT_HIGH())
{
temp += 0x01;
}
AD7708_Delay(5);
AD7708_SCLK_HIGH();
if(i != 7)
{
temp <<= 1;
}
AD7708_Delay(5);
}
AD7708_SCLK_HIGH();
AD7708_Delay(2);
AD7708_CS_HIGH();
return (temp);
}
/*-------------读取16位数据-----------------*/
uint16_t AD7708_Read16Bit(void)
{
uint8_t i;
uint16_t temp= 0x0000;
AD7708_CS_LOW();
AD7708_Delay(5);
for(i=0;i<16;i++)
{
AD7708_Delay(5);
AD7708_SCLK_LOW();
temp = temp << 1;
AD7708_Delay(30);
if(AD7708_DOUT_HIGH())
{
temp |= 0x01;
}
AD7708_Delay(10);
AD7708_SCLK_HIGH();
AD7708_Delay(5);
}
AD7708_SCLK_HIGH();
AD7708_Delay(2);
AD7708_CS_HIGH();
return (temp);
}
/*----------------内部校准------------------*/
void AD7708_Cal(uint8_t channel)
{
AD7708_Write8Bit(0x02);
AD7708_Write8Bit((channel<<4)|0x0f);//ADC控制寄存器
AD7708_Delay(2);
AD7708_Write8Bit(0x01); //模式寄存器
AD7708_Write8Bit(0x04);//内部零标量校准
AD7708_Busy();
AD7708_Delay(2);
AD7708_Write8Bit(0x01);
AD7708_Write8Bit(0x05);//内部满量程校准
AD7708_Busy(); //等待校准完成
AD7708_Delay(2);
}
/*----------------初始化AD7708寄存器---------------*/
void AD7708_Reg_Init(void)
{
uint8_t i;
AD7708_Reset();
AD7708_Delay(10);
for(i=0;i<8;i++)
{
AD7708_Cal(i);//校准
}
AD7708_Write8Bit(0x07);//IO寄存器
AD7708_Write8Bit(0x00);
AD7708_Delay(2);
AD7708_Write8Bit(0x03);//滤波寄存器
AD7708_Write8Bit(0x12);
AD7708_Delay(2);
AD7708_Write8Bit(0x02);//ADC控制寄存器-0通道-单极性
AD7708_Write8Bit(0x0f);
AD7708_Delay(2);
AD7708_Write8Bit(0x01);//模式寄存器
AD7708_Write8Bit(0x02);
AD7708_Delay(2);
}
/*******************************************************************************************************/
/*-------------读取AD转换数据------------------*/
uint16_t AD7708_ReadAD_Data(uint8_t Channel)
{
uint16_t temp;
AD7708_Write8Bit(0x02);//ADC控制寄存器--通道号--单极性
AD7708_Write8Bit(0x0f|(Channel<<4));
AD7708_Delay(2);
AD7708_Write8Bit(0x01);
AD7708_Write8Bit(0x02); //启动单次转换
AD7708_Delay(2);
AD7708_Busy(); //转换完成
AD7708_Write8Bit(0x44); //读
temp = AD7708_Read16Bit();//读取转换数据
return (temp);
}