⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ads1240.c

📁 24位AD压力实验板 protell99se打开
💻 C
字号:
#include "Ads1240.h"
#include <msp430x14x.h>
#include "init.h"
extern void Delay_1ms( long int value);
int tosc = 40;

unsigned char AD1240_Reg_Val[15];
unsigned char PGA_Plus;
void writetoreg(uchar ad_reg)
{  uchar i;
   for(i=8;i>=1;i--)
   {  P1OUT&=~BIT2;//SCLK=0
      if(ad_reg&0x80) P6OUT|=BIT4;//DIN=1
      else P6OUT&=~BIT4;//DIN=0
      _NOP();_NOP();_NOP();
      ad_reg=ad_reg<<1;
      P1OUT|=BIT2;//SCLK=1
      _NOP();_NOP();_NOP();
   }
   for(i=15;i>=1;i--) {;}
}
//****************************************************************************************
//函数:void ADS1240AssertCS( int fAssert)
//功能:设置AD1240片选
//变量:int fAssert
//返回:无
//****************************************************************************************
void Pause(unsigned int num)
{
   while(num--);
}
//****************************************************************************************
//函数:void En_AD_DRDY_INT(void)
//功能:设置AD1240片选
//变量:int fAssert
//返回:无
//****************************************************************************************
void En_AD_DRDY_INT(void)
{
P1DIR = 0xfe;   // All P1.x inputs
P1IES|=BIT0;
P1SEL&=~BIT0;
P1IE|=BIT0;
}
//****************************************************************************************
//函数:void Dis_AD_DRDY_INT(void)
//功能:设置AD1240片选
//变量:int fAssert
//返回:无
//****************************************************************************************
void Dis_AD_DRDY_INT(void)
{
//P1IES|=BIT0;
P1SEL|=BIT0;
//P1IE|=BIT0;
}
//****************************************************************************************
//函数:unsigned char Open_AD_Power(void)
//功能:设置AD1240片选
//变量:int fAssert
//返回:无
//****************************************************************************************
unsigned char Open_AD_Power(void)
{
_NOP();
P6OUT&=~BIT5;//Open_AD_Power
_NOP();
return 0;
}
//****************************************************************************************
//函数:void Close_AD_Power(void)
//功能:设置AD1240片选
//变量:int fAssert
//返回:无
//****************************************************************************************
void Close_AD_Power(void)
{
   _NOP();
   P6OUT|=BIT5;//Close_AD_Power
   P6OUT &=~ADS1240_RESET;
   P5DIR |=ADS1240_DOUT;
   P5OUT &=~(ADS1240_DIN+ADS1240_SCLK+ADS1240_DOUT);
   P1DIR |= ADS1240_DRDY;
   P1OUT &=~ADS1240_DRDY;
   _NOP();

}
//****************************************************************************************
//函数:int ADS1240Init(void)
//功能:设置AD1240片选
//变量:int fAssert
//返回:无
//****************************************************************************************
int ADS1240Init(void)
{
 // unsigned char i;

  _NOP();
   P6OUT|=ADS1240_RESET;
   P5OUT|=(ADS1240_DIN+ADS1240_SCLK);
  _NOP();
  //P5DIR |=(ADS1240_DIN+ADS1240_SCLK);
  P5DIR &=~ADS1240_DOUT;

  P1DIR &=~ADS1240_DRDY;
  Delay_1ms(1);//52 cycle

   return ADS1240_NO_ERROR;
}
//========================================================================================
//函数:void adc_init(void)
//功能:读取AD1240积存器
//变量:Byte
//返回:无
//========================================================================================
void adc_init(void)
{
   unsigned char i,j;
   P6OUT&=~ADS1240_RESET;//RSET LO
   Delay_1ms(1);//52 cycle
   P6OUT|=ADS1240_RESET;//RSET HI, LO-->HI > 16 clock
   Delay_1ms(1);//52 cycle

AD1240_Reg_Val[0]=PGA_Plus;//ADS1240_GAIN_32;//PGA_Plus;//SETUP
AD1240_Reg_Val[1]=ADS1240_MUXP_AIN0+ADS1240_MUXN_AIN1;//MUX
AD1240_Reg_Val[2]=0x00;//BUFEN_BIT+RANGE_BIT;//

while(P1IN&BIT0) {;}
ADS1240WriteRegister(ADS1240_SETUP_REGISTER, 3, AD1240_Reg_Val); 
ADS1240SendByte(ADS1240_CMD_SELFCAL);


//ADS1240SendByte(ADS1240_CMD_SYSOCAL);

//ADS1240SendByte(ADS1240_CMD_SYSGCAL);
//while(P1IN&BIT0) {;}
//Delay_1ms(100);
    //for (i=0; i<1; i++)
      //ADS1240WaitForDataReady(0); 
/*while(P1IN&BIT0) {;}
   writetoreg(0x50);//WREG CMD 1
   writetoreg(0x02);//WREG CMD 2
   writetoreg(0x00);//Set PGA 1
   writetoreg(0x01);//MUX AIN0+,AIN1-
   writetoreg(0x01);//ACR 7.5Hz  0 U/B 0 0 0 0 0 0  U/B=0,Bipolar 1,Unipolar
   ADS1240SendByte(ADS1240_CMD_SELFCAL);*/
        for(i=1;i>=1;i--)
   {  for(j=191;j>=1;j--) {;}
  }
   while(P1IN&BIT0) {;}

}

//****************************************************************************************
//函数:void ADS1240AssertCS( int fAssert)
//功能:设置AD1240片选
//变量:int fAssert
//返回:无
//****************************************************************************************
int ADS1240WaitForDataReady(int Timeout)
{
   if (Timeout > 0)
   {
      // wait for /DRDY = 1
     while (!(P1IN & ADS1240_DRDY) && (Timeout-- >= 0))
         ;
      // wait for /DRDY = 0
    while ( (P1IN & ADS1240_DRDY) && (Timeout-- >= 0))
       ;
     // if (Timeout < 0)
         return ADS1240_TIMEOUT_WARNING;
   }
   else
   {
      // wait for /DRDY = 1
    while (!(P1IN & ADS1240_DRDY))
         ;
      // wait for /DRDY = 0
      while ( (P1IN & ADS1240_DRDY))
        ;
   }
   return ADS1240_NO_ERROR;
}

//****************************************************************************************
//函数:void ADS1240AssertCS( int fAssert)
//功能:设置AD1240片选
//变量:int fAssert
//返回:无
//****************************************************************************************
void ADS1240AssertCS( int fAssert)
{
  /* if (fAssert)
      P5OUT &= ~ADS1240_CS;
   else
      P5OUT |=  ADS1240_CS;*/
}
//****************************************************************************************
//函数:void ADS1240SendByte(int Byte)
//功能:发送AD1240单字数据或命令
//变量:Byte
//返回:无
//****************************************************************************************
void ADS1240SendByte(unsigned char Byte)
{
   unsigned char i;
   for(i=8;i>=1;i--)
   {  P5OUT&=~ADS1240_SCLK;//SCLK=0

   	if(Byte&0x80)
          P5OUT|=ADS1240_DIN;//DIN=1
      else
          P5OUT&=~ADS1240_DIN;//DIN=0

   	Byte <<= 1;

   P5OUT|=ADS1240_SCLK;//SCLK=1
   }
for(i=14;i>=1;i--) {;}
}
//****************************************************************************************
//函数:unsigned char ADS1240ReceiveByte(void)
//功能:接收AD1240单字数据或命令
//变量:Byte
//返回:无
//****************************************************************************************
unsigned char ADS1240ReceiveByte(void)
{
    unsigned char Result = 0;
    int i;
    for (i=0; i<8; i++)
      {
        P5OUT&=~ADS1240_SCLK;//SCLK=0
      	_NOP();
      	P5OUT|=ADS1240_SCLK;//SCLK=1

      	Result <<= 1;
        if(P5IN&ADS1240_DOUT)
          Result |=  1;//DOUT
       }
   for(i=14;i>=1;i--) {;}
   return Result;
}
//****************************************************************************************
//函数:long ADS1240ReadData(int fWaitForDataReady)
//功能:发送AD1240数据或命令
//变量:Byte
//返回:无
//****************************************************************************************
long ADS1240ReadData(int fWaitForDataReady)
{
   long Data;
   // if requested, synchronize to /DRDY
   if(fWaitForDataReady)
      ADS1240WaitForDataReady(0);

   // assert CS to start transfer
   //ADS1240AssertCS(1);

   // send the command byte
   ADS1240SendByte(ADS1240_CMD_RDATA);

   // delay according to the data sheet
   Pause(50 * tosc);

   // get the conversion result
   Data = ADS1240ReceiveByte();
   Data = (Data << 8) | ADS1240ReceiveByte();
   Data = (Data << 8) | ADS1240ReceiveByte();

   // sign extend data
   if (Data & 0x800000)
      Data |= 0xff000000;

   // de-assert CS
  // ADS1240AssertCS(0);
   return Data;
}
//****************************************************************************************
//函数:long ADS1240ReadData(int fWaitForDataReady)
//功能:读取AD1240积存器
//变量:Byte
//返回:无
//****************************************************************************************
int ADS1240ReadRegister(int StartAddress, int NumRegs, unsigned char * pData)
{
   int i;

   // assert CS to start transfer
   //ADS1240AssertCS(1);
   *pData=0;
   // send the command byte
   ADS1240SendByte(ADS1240_CMD_RREG | (StartAddress & 0x0f));

   // send the command argument
   ADS1240SendByte(NumRegs-1);

   // pause according to the data sheet
   Pause(10 * tosc);

   // get the register content
   for (i=0; i< NumRegs; i++)
   {
      *pData++ = ADS1240ReceiveByte();
   }

   // de-assert CS
   //ADS1240AssertCS(0);
  i=0;
   return ADS1240_NO_ERROR;
}
//****************************************************************************************
//函数:long ADS1240ReadData(int fWaitForDataReady)
//功能:发送AD1240单字数据或命令函数
//变量:Byte
//返回:无
//****************************************************************************************
int ADS1240WriteRegister(int StartAddress, int NumRegs, unsigned char * pData)
{
   int i;

   // assert CS to start transfer
  // ADS1240AssertCS(1);

   // send the command byte
   ADS1240SendByte(ADS1240_CMD_WREG | (StartAddress & 0x0f));

   // send the command argument
   ADS1240SendByte(NumRegs-1);

   // pause according to the data sheet
 Pause(50 * tosc);

   // send the data bytes
   for (i=0; i< NumRegs; i++)
   {
      ADS1240SendByte(*pData++);
   }
   i=0;
  // Pause(500 * tosc);
   //Pause(500 * tosc);
   // Pause(500 * tosc);
   // de-assert CS
  // ADS1240AssertCS(0);
   return ADS1240_NO_ERROR;

}
//****************************************************************************************
//函数:int ADS1240SendResetCommand(void)
//功能:复位ADS1240芯片
//变量:int
//返回:无
//****************************************************************************************
int ADS1240SendResetCommand(void)
{
   // assert CS to start transfer
  // ADS1240AssertCS(1);

   // send the command byte
   ADS1240SendByte(ADS1240_CMD_RESET);

   // de-assert CS
  // ADS1240AssertCS(0);

   // wait for t11 (16 tosc)
   Pause(16 * tosc);

   return ADS1240_NO_ERROR;
}
//****************************************************************************************
//函数:int ADS1240SetChannel(unsigned char MuxCode)
//功能:选择AD的采样通道
//变量:int
//返回:无
//****************************************************************************************
int ADS1240SetChannel(unsigned char MuxCode)
{
   ADS1240WriteRegister(ADS1240_MUX_REGISTER, 0x01, &MuxCode);
   return ADS1240_NO_ERROR;
}
//****************************************************************************************
//函数:int ADS1240SetGain(int GainCode)
//功能:设置ADS1240芯片的增益
//变量:int
//返回:无
//****************************************************************************************
int ADS1240SetGain(int GainCode)
{
   unsigned char Temp;

   // the gain code is only part of the register, so we have to read it back
   // and massage the new gain code into it
   ADS1240ReadRegister(ADS1240_SETUP_REGISTER, 0x01, &Temp);

   // clear prev gain code;
   Temp &= ~0x07;
   Temp |= GainCode & 0x07;
   Pause(50 * tosc);
   // write the register value containing the new gain code back to the ADS
  ADS1240WriteRegister(ADS1240_SETUP_REGISTER, 0x01, &Temp);
   //return ADS1240_NO_ERR/OR;
  Pause(50 * tosc);
  Temp =0;
  ADS1240ReadRegister(ADS1240_SETUP_REGISTER, 0x01, &Temp);
  return ADS1240_NO_ERROR;
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -