LPC1788-IO模擬SPI--驅(qū)動AD7708--寄存器操作
最近在使用AD7708采集數(shù)據(jù)-本來是想使用LPC1788自帶的SSP進(jìn)行控制,但是在多次實驗過程中發(fā)現(xiàn),單個讀取寄存器是沒有問題的,但是在連續(xù)讀取時出現(xiàn)問題,一直沒有解決,項目著急使用就直接用IO口模擬SPI,比較簡單就準(zhǔn)確讀出了數(shù)據(jù),也沒有出現(xiàn)上述問題,記之!
個人還是覺得是自己配置的原因,后續(xù)會繼續(xù)研究SSP的控制AD7708,希望能夠搞清楚!
主函數(shù)只需要調(diào)用兩個函數(shù)---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);
}
/*------------------復(fù)位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是否忙狀態(tài)----監(jiān)測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位數(shù)據(jù)-------------*/
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位數(shù)據(jù)--------------------*/
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位數(shù)據(jù)-----------------*/
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);
}
/*----------------內(nèi)部校準(zhǔn)------------------*/
void AD7708_Cal(uint8_t channel)
{
AD7708_Write8Bit(0x02);
AD7708_Write8Bit((channel<<4)|0x0f);//ADC控制寄存器
AD7708_Delay(2);
AD7708_Write8Bit(0x01); //模式寄存器
AD7708_Write8Bit(0x04);//內(nèi)部零標(biāo)量校準(zhǔn)
AD7708_Busy();
AD7708_Delay(2);
AD7708_Write8Bit(0x01);
AD7708_Write8Bit(0x05);//內(nèi)部滿量程校準(zhǔn)
AD7708_Busy(); //等待校準(zhǔn)完成
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);//校準(zhǔn)
}
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轉(zhuǎn)換數(shù)據(jù)------------------*/
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); //啟動單次轉(zhuǎn)換
AD7708_Delay(2);
AD7708_Busy(); //轉(zhuǎn)換完成
AD7708_Write8Bit(0x44); //讀
temp = AD7708_Read16Bit();//讀取轉(zhuǎn)換數(shù)據(jù)
return (temp);
}