Glory分享的个人空间 https://bbs.21ic.com/?1856018 [收藏] [复制] [RSS]

日志

有关AD7799的配置

已有 3320 次阅读2017-1-5 18:05 |个人分类:ICCAVR|系统分类:单片机

#include <iom64v.h>
#include <macros.h>
#include "delay.h"
//#include <stdint.h>
//#include <stdio.h>
//#include <string.h>
//#include "timeout.h"
//#include "avr_compat.h"
#include "eeprom.h"
#define uchar unsigned char
#define uint  unsigned int

 //unsigned char SPDAT ;

//#define ADC_VREF_TYPE 0xC0

#define ADC_CSACTIVE  PORTB &= ~(1<<PORTB0); /* CS=0 */
#define ADC_CSPASSIVE PORTB |= (1<<PORTB0);  /* CS=1 */

#define VER_H    1
#define VER_L    1

unsigned char CONH=0x10; /*CONFIGURATION REGISTER[00,BO(1),U/B(1),0(0),G2(0),
G1(0),G0(0),0,0,REF_DET(0),BUF(1),0(0),CH2(0),CH1(0),CH0(0)]*/
unsigned char CONL=0x00; /* GAIN: 1x, AIN1 */

//SPCR SPI控制寄存器

//SPSR SPI状态寄存器

//SPDAT SPI数据寄存器
void SPImasterInit(void)
{
  SPCR=0x5C;//SPI控制寄存器,中断禁止,SPI使能,高位在前,主机模式,时钟空闲时为高,后沿触发移位,时钟分频64
  SPSR=0x00;//清中断标志位
}
/* Write byte 2 AD7799 */
void WriteByteToAd7799(unsigned char data)
{
  ADC_CSACTIVE;   
  //DDRB|=(1<<PB1)|(1<<PB2);       //设置SCK,MOSI为输出,其他为输入
  //DDRB&=~(1<<PB3);               //设置MISO为输入
  //PORTB|=(1<<PB3);               //设置输入上拉
  //SPCR|=(1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);//使能SPI,主模式,Fck/128
  //SPDAT= data;
  SPDR = data;
  //while(~SPSR&0x80); //等待数据发送完
  //SPSR=0x00; //清中断标志位
  while(!(SPSR & (1<<SPIF))); //等待数据发送完
  ADC_CSPASSIVE;
}

/* Read byte from AD7799 */
unsigned char ReadByteFromAd7799(void)   
{   
    unsigned char returnData;
    ADC_CSACTIVE;   
    SPDR = 0x00;
    while((SPSR & (1<<SPIF))==0);
    returnData = SPDR;
    ADC_CSPASSIVE;
    return (returnData);
}
/* Reset AD7799 */
void AD7799_RESET(void){
    
    WriteByteToAd7799(0xff);
    WriteByteToAd7799(0xff);
    WriteByteToAd7799(0xff);
    WriteByteToAd7799(0xff);
        
}         
/* Init AD7799 */
void AD7799_INIT(void)   
{   
    WriteByteToAd7799(0x10);   //b0001 0000   
    /* Writes to Communications Register Setting Next Operation

as Write to CONFIGURATION Register*/   
    WriteByteToAd7799(37);   //B0011 0111
    WriteByteToAd7799(30);   //B0011 0000
    /*CONFIGURATION REGISTER[00,BO(0),U/B(0),0(0),G2(1),G1

(1),G0(1),0,0,REF_DET(0),BUF(1),0(0),CH2(0),CH1(0),CH0(0)]*/   
    
    WriteByteToAd7799(0x08);   
    //b0000 1000   
    /* Writes to Communications Register Setting Next Operation

as Write to Mode Register*/   
    WriteByteToAd7799(0x90);   
    WriteByteToAd7799(0x0F);   
    /* Writes to Mode Register Initiating Internal Zero-Scale

Calibration*/   
    //WaiteRDY();
    DelayMs(5);   
    /* Wait for RDY pin to go low to indicate end of

calibration cycle*/   
    WriteByteToAd7799(0x08);   
    /* Writes to Communications Register Setting Next Operation

as Write to  Mode Register*/   
    WriteByteToAd7799(0xb0);   
    WriteByteToAd7799(0x0F);   
    /* Writes to Mode Register Initiating Internal Full-Scale

Calibration*/   
    //WaiteRDY();   
    DelayMs(5);
    /* Wait for RDY pin to go low to indicate end of

calibration cycle*/   
    WriteByteToAd7799(0x08);//b0000 1000   
    /* Writes to Communications Register Setting Next Operation

as Write to Mode Register*/   
    WriteByteToAd7799(0x00);   //00010000   
    WriteByteToAd7799(0x0F);   //00001111 (16.7HZ65dB)

    /* Mode Register[MD2(0),MD1(0),MD0(0),PSW(0),0(0),0(0),0

(0),0(0),(0),(0),0(0),0(0),FS3(1),FS2(0),FS1(1),FS0(0)]*/   
    /* Continuous-Conversion Mode. Fadc=16.7HZ;*/
}
/* Wait for READY from AD7799 */
void WaiteRDY(void)   
{   
    unsigned int iint ;   
    iint=0 ;   
    //while(~AD7799_RDY)
    //while(bit_is_set(PINB,PB3))    
    while((PINA&0x08)==0)
    {   
        iint++;   
        if(iint>20000)   
        {   
            //reset ad7799   
            AD7799_RESET();
            AD7799_INIT();   
            break;
        }   
    }   
}
/*------------------------------------------------*/
/* Read 24-bit of data from AD7799 */
unsigned long ReadAd7799ConversionData(void)
{
  unsigned long ConverData;
  unsigned char ADSAT ;
  unsigned char ErrNUM=0;
  WaiteRDY();

  WriteByteToAd7799(0x40); //写通信寄存器设置下一个操作为读状态STATUS寄存器
  WriteByteToAd7799(0xff); //伪写通信寄存器,为读状态寄存器提供时钟

  //ADSAT=SPDAT; //读取接收到的数据
  ADSAT=SPDR;
  while((ADSAT&0x40)||(!(ADSAT&0x08))) //出错或者读写异常
    {
//reset ad7799
  WriteByteToAd7799(0xff);
 
  WriteByteToAd7799(0xff);
/*----------防止时序混乱,重新同步----------*/
  WriteByteToAd7799(0xff);
 
  WriteByteToAd7799(0xff);

//-------------------------------------------------------------------------------------
  AD7799_INIT();

  WaiteRDY();
  WriteByteToAd7799(0x40); //写通信寄存器设置下一个操作为读状态STATUS寄存器
  WriteByteToAd7799(0xff); //伪写通信寄存器,为读状态寄存器提供时钟
  //ADSAT=SPDAT; //读取接收到的数据
  ADSAT=SPDR;
  ErrNUM++;
  if(ErrNUM>5)break;
    }

   WriteByteToAd7799(0x58); //写通信寄存器设置下一个操作为连续读数据寄存器
   WaiteRDY();
/* Wait for RDY pin to go low to indicate end of calibration cycle*/
  //if(!AD7799_RDY)
  //if(bit_is_clear(PINB,PB3))  
  if((PINA&0x08)==0x80)
  {
   ConverData=0 ;
/*-----------------Read Conversion Result from AD7799's Data Register----------------*/
   WriteByteToAd7799(0xff); //伪写通信寄存器,为读数据寄存器寄存器提供时钟
   //ConverData=SPDAT;
   ConverData=SPDR;
   ConverData=ConverData<<8 ;

   WriteByteToAd7799(0xff); //伪写通信寄存器,为读数据寄存器寄存器提供时钟
   //ConverData=ConverData+SPDAT;
   ConverData=ConverData+SPDR;
   ConverData=ConverData<<8 ;

   WriteByteToAd7799(0xff); //伪写通信寄存器,为读数据寄存器寄存器提供时钟
   //ConverData=ConverData+SPDAT;
   ConverData=ConverData+SPDR;
   }
   if(ErrNUM>5)return(0);
   else return(ConverData);
}


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)