原创 ADS7870的LPC213X之SSP接口配置及应用

2007-12-14 06:41 6652 6 6 分类: MCU/ 嵌入式
ADS7870的LPC213X之SSP接口配置及应用
雁塔菜农 发表于 2006-6-27 0:06:00


/*----------------------------------------------------------------
ARM水鸟  HotPower@126.com
2006.06.27 00:12  于西安大雁塔村队部
-----------------------------------------------------------------*/


void AdsObj::AdsInit(void)
{
  PINSEL->PIN_SEL1 |= (P0_16_EINT0 << P0_16_PINSEL);// P0.16连接到EINT0


  AdsSSPInit();//SSP初始化


  ADSPORT->IODIR |= (__1 << ADSTART) | (__1 << ADRST);
  ADSPORTCS->IODIR |= (1 << ADCS);
  ADSPORTCS->IOSET  = (1 << ADCS);
  ADSPORT->IOCLR = (1 << ADSTART);//自动转换


  ADSPORT->IOCLR = (__1 << ADRST);//
  _delay_loop_(1);
  ADSPORT->IOSET = (__1 << ADRST);//
  _delay_loop_(1);


  AdsWriteReg(ADS7870_SERIFCTRL, 0x00);
  AdsIdTest();
  if (AdsTestStatus) {
    AdsWriteReg(ADS7870_REFOSC, ADS7870_REFOSC_OSCE | ADS7870_REFOSC_REFE | ADS7870_REFOSC_BUFE);
  }
  AdsIdValue = 0;
  AdsCount = 0;
  for (int i = 0; i < 8; i ++) {
    AdsTestRes = 0;
 AdsTestVal = 0;
    AdsTestCount = 0;
    AdsTestSum = 0;
    AdsTestMax = 0;
    AdsTestMin = 0xffff;
  }
  for (int i = 0; i < 4; i ++) {
    AdsCountA = 0;
 AdsTestValV = 0;
 AdsTestValA = 0xffff;
 AdsTestSumA = 0;
 AdsTestMaxA = 0;
 AdsTestMinA = 0xffff;
  }
}


void AdsObj::AdsSSPInit(void)
{
  POWER->P_CONP |= (1 << PCSPI1);
  PINSEL->PIN_SEL1 |= (P0_17_SCK1 << P0_17_PINSEL) | (P0_19_MOSI1 << P0_19_PINSEL) | (P0_18_MISO1 << P0_18_PINSEL);
  SSP->SSP_CR0 = (0x01 << 8) |              // SCR  设置SPI时钟分频0x01
                 (0x00 << 7) |              // CPHA 时钟输出相位,仅SPI模式有效
                 (0x00 << 6) |              // CPOL 时钟输出极性,仅SPI模式有效
                 (0x00 << 4) |              // FRF  帧格式 00=SPI,01=SSI,10=Microwire,11=保留
                 (0x07 << 0);               // DSS  数据长度,0000-0010=保留,0011=4位,0111=8位,1111=16位


  SSP->SSP_CR1 = (0x00 << 3) |              // SOD  从机输出禁能,1=禁止,0=允许
                 (0x00 << 2) |              // MS   主从选择,0=主机,1=从机
                 (0x01 << 1) |              // SSE  SSP使能,1=允许SSP与其它设备通信
                 (0x00 << 0);               // LBM  回写模式
            
//  SSP->SSP_CPSR = 8;                     // PCLK分频值
//  SSP->SSP_CPSR = 18;                     // PCLK分频值
  SSP->SSP_CPSR = 0x52;                     // PCLK分频值
   // SSP->SSP_IMSC = 0x07;                     // 中断屏蔽寄存器
  SSP->SSP_ICR  = 0x03;                     // 中断清除寄存器
}


void AdsObj::AdsConvertStart(void)
{
//unsigned short result;
unsigned int cNum;
  AdsCount &= 7;//只取8个通道(0~3测试电流,4~7测试电压)
  cNum = AdsCount;
/*-----------------------------------------------------------------
  D7 D6 D5 D4 D3 D2 D1 D0  cNumber
   X  X  X  X  X  0  X  X  电流测试
   X  X  X  X  X  1  X  X  电压测试


   X  X  X  X  0  X  X  0  电流测试
   X  X  X  X  1  X  X  1  电压测试
------------------------------------------------------------------*/
  if (cNum >= 4) {//电压测试用单端(4~7)
    cNum <<= 1;//电压测试用单端(8~f)
    cNum ++;//电压测试只测试端电压
  }
  else {//电流测试用差分(0~3)
    cNum += 4;
    cNum |= ADS7870_GAIN_20X;//电流增益20倍
  }
  AdsWriteReg(ADS7870_REFOSC, ADS7870_REFOSC_OSCE | ADS7870_REFOSC_REFE | ADS7870_REFOSC_BUFE);
  ADSPORTCS->IOCLR = (1 << ADCS);//打开片选信号
  AdsReadWriteByte(ADS7870_CONVERT | cNum);
//  sprintf(str, "ADC%1d.%05d", cNumber, result);
//  Uart.puts(str);
}


void AdsObj::AdsConvertStop(void)
{
unsigned short result;
//char str[16];
//  while(AdsReadReg(ADS7870_GAINMUX) & ADS7870_GAINMUX_CNVBSY);
  ADSPORTCS->IOCLR = (1 << ADCS);//打开片选信号
  AdsReadWriteByte(ADS7870_REG_READ | ADS7870_REG_16BIT | ADS7870_RESULTHI);
  result = AdsReadWriteByte(0xff) << 0x08;//读转换电压高4/3位
  result |= AdsReadWriteByte(0xff);//读转换电压低8位
  ADSPORTCS->IOSET = (1 << ADCS);//关闭片选信号
  if (result >= 0x8000) {
    result ^= 0xfff0;
 result += 0x0010;
  }
  AdsTestRes[AdsCount] = result;
//  sprintf(str, "AdsTestRes[%1d]=%04X", AdsCount, result);
//  Uart.puts(str);
}


unsigned char AdsObj::AdsReadWriteByte(unsigned char cData)
{
  SSP->SSP_DR = cData;
  while( (SSP->SSP_SR & (1 << BSY)) );     // 等待TFE置位,即发送FIFO空  
  return SSP->SSP_DR;
}


unsigned char AdsObj::AdsReadReg(unsigned char cReg)
{
unsigned char cData;
  ADSPORTCS->IOCLR = (1 << ADCS);//打开片选信号
  AdsReadWriteByte(ADS7870_REG_READ | cReg);
  cData = AdsReadWriteByte(0xff);
  ADSPORTCS->IOSET = (1 << ADCS);//关闭片选信号
  return cData;
}


void AdsObj::AdsWriteReg(unsigned char cReg, unsigned char cData)
{
  ADSPORTCS->IOCLR = (1 << ADCS);//打开片选信号
  AdsReadWriteByte(ADS7870_REG_WRITE | cReg);//写寄存器地址
  AdsReadWriteByte(cData);//写入控制命令
  ADSPORTCS->IOSET = (1 << ADCS);//关闭片选信号
}



void AdsObj::AdsIdTest(void)
{
  AdsWriteReg(ADS7870_SERIFCTRL, 0x00);//设置左移3线非8051模式
  AdsIdValue = AdsReadReg(ADS7870_ID);//读芯片ID
  if (AdsIdValue == ADS7870_ID_VALUE) {
    AdsTestStatus = true;
  }
  else {
    AdsTestStatus = false;
  }
}



阅读全文(912) | 回复(0) | 引用通告(4) | 编辑
 




PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
6
关闭 站长推荐上一条 /3 下一条