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

📄 nbt_spi.c

📁 tiMSP430F147体能监测设备源码
💻 C
📖 第 1 页 / 共 2 页
字号:
//  VDW    : VCO Divider Word  (9 significant bits)
//  RDW    : Reference Divider Word  (7 significant bits)
//------------------------------------------------------------------------------
void ICS307_Program(unsigned char Config, unsigned int VDW, unsigned char RDW)
{
  register unsigned char temp;
  VDW &= 0x1FF;            // only 9 bits count
  RDW &= 0x7F;             // only 7 bits count
  temp = ((unsigned char) VDW) << 7;   // bit-mangle 3rd byte
  temp |= RDW;
  SPI_open(DEVICE_CLOCK);
  SPI_SelPin(0);
  SPI_sendReceiveByte(Config);
  SPI_sendReceiveByte(VDW >> 1);
  SPI_sendReceiveByte(temp);
  SPI_SelPin(1);
//  SPI_SelPin(0);
//  SPI_SelPin(1);
  SPI_close();
}

//-------------------------------------------------------------
// Programs command word 'data' into the clock generator chip
//-------------------------------------------------------------
void ICS307_ProgramW(unsigned long data)
{
  SPI_open(DEVICE_CLOCK);   // select spi channel
  SPI_SelPin(0);            // select device
  SPI_sendReceiveByte((data >>16)&0xFF);
  SPI_sendReceiveByte((data>>8)&0xFF);
  SPI_sendReceiveByte(data & 0xFF);
  SPI_SelPin(1);
  SPI_close();
}

//---------------------------------------------------------------------------------------------

//------------------------------------------------------------------
// Configures MAX1104
// Leaves ADC and DAC on and enabled, both in continuous conversion mode
// leaves SPI channel set to MAX1104 and the device selected
//------------------------------------------------------------------
void MAX1104_Open()
{
  SPI_open(DEVICE_AUDIO_CODEC);
  SPI_SelPin(0);
//  SPI_sendReceiveByte(MAX1104_START | MAX1104_A1 | MAX1104_A0 | MAX1104_C0 | MAX1104_E1 | MAX1104_E2);
  SPI_sendReceiveByte(MAX1104_START | MAX1104_A1 | MAX1104_A0 | MAX1104_C0 | MAX1104_E1| MAX1104_E0);
  DelayMs(1);
}


//------------------------------------------------------------------
// Configures MAX1104
// Powers down ADC and DAC
// deselects the device closes SPI channel
//------------------------------------------------------------------
void MAX1104_Close()
{
  SPI_SelPin(1); // terminate contiuous conversion
  DelayMs(1);
  SPI_SelPin(0);
  SPI_sendReceiveByte(MAX1104_START | MAX1104_A1 | MAX1104_A0);  // power down
  SPI_SelPin(1); // deselect
  SPI_close();
}



#define MAX1104_TEST_AMPLITUDE 50
//-----------------------------------------------------------------------------
//  outputs a squarewave and digitises it again to compare the amplitudes
//  ' cycles' is the number of cycles over which the measurement is averaged
//-----------------------------------------------------------------------------
static unsigned char Max1104ReadWrite(unsigned char cycles)
{
  volatile unsigned char InByte;
  register unsigned char temp;
  register unsigned char min=0xFF;
  register unsigned char max=0;
  signed int difference;
  MAX1104_Open();
  EA = 0;
  difference = 0;
  for(cycles=0; cycles<20; cycles++)
  {
    temp = 127 + MAX1104_TEST_AMPLITUDE;
    InByte = SPI_sendReceiveByte(temp);     // send byte out to D/A
    InByte = SPI_sendReceiveByte(temp);     // send byte out to D/A
    Delay10Us(10);
    InByte = SPI_sendReceiveByte(temp);     // read byte back after A/D conversion
    if(InByte > max) max = InByte;
    temp = 127 - MAX1104_TEST_AMPLITUDE;
    InByte = SPI_sendReceiveByte(temp);     // send byte out to D/A
    InByte = SPI_sendReceiveByte(temp);     // send byte out to D/A
    Delay10Us(10);
    InByte = SPI_sendReceiveByte(temp);     // read byte back after A/D conversion
    if(InByte < min) min = InByte;
    difference += (max-min);
  }
  EA = 1;
  MAX1104_Close();
  difference /= cycles;
  return difference;
}

#define AVERAGING 8
#define SCALING 2
//-----------------------------------------------------------------------------
//  Allows adjustment of the analog gain trimmer
// displays the adjustment direction graphically
//-----------------------------------------------------------------------------
void MAX1104_Adjust(void)
{
  do
  {
    signed int result;
    register unsigned char i;
    signed int position;
    result = 0;
    for (i=0; i<AVERAGING; i++) // calculate average over a few readings to reduce jitter
      result += Max1104ReadWrite(5);
    result /= AVERAGING;
    position = (result - MAX1104_TEST_AMPLITUDE);
    position /= SCALING;
    LCD_GotoXY(0,0);
    if(0 == position)
    {
      print("OK Press Any Key");
    }
    else
    {
      print("Adjust VR2 ",result, position);
      {
        if(position > 0)
          print("left ");
        else
          print("right");
      }
    }
    LCD_BarGraph_Bi(position,BG_RES_PERDIGIT*8,8,1,BG_RES_PERDIGIT*8);
 /*                             // this is the old text-based bar-graph routine
    LCD_GotoXY(0,1);            // draw bargraph
    if(position < 0)
      position = 0;
    for(i=0;i<16;i++)
    {
      if(position < MIDSCALE)
      {
        if(i<position)
        {
          LCD_WriteChar('.');
        }
        else
        {
          if(i<=MIDSCALE)
            LCD_WriteChar('>');
          else
            LCD_WriteChar('.');
        }
      }
      else
      {
        if((i<position) && (i>MIDSCALE))
          LCD_WriteChar('<');
        else
          LCD_WriteChar('.');
      }
    }
*/
  }
  while (!(KbHit));   // wait for any key
  while(KbHit)
    GetKey(0);
  LCD_ClrScr();
}


//----------------------------------------------------------------------
// Reads the frequency Counter
// Returns: Frequency in kHz
//----------------------------------------------------------------------
unsigned int FrequencyCount(void)
{
   unsigned int retval;
   DelayMs(5);  // wait for update
   retval   = FREQ1_PORT;
   retval <<= 8;
   retval  |= FREQ0_PORT;
   return retval;
}

#define MAX_FREQ_DEVIATION 50   // maximum allowable tolerance value for frequency measurement [kHz]
//---------------------------------------------------------------------------
// Measures the frequency of the Nanoboard on-board Variable Clock Generator
// Then reprograms the Clock frequency and compares the two frequencies
// We do this because there is no way to obtain a status from the ICS307
// Returns : 0 = Success
//           1 = No frequency change detected
//---------------------------------------------------------------------------
unsigned char TestICS307(void)
{  register unsigned int Freq1, Freq2;
   register unsigned char retval = 0;
   ICS307_ProgramW(ICS307_30MHZ);
//   ICS307_Program(ICS307_Default_Config, ICS307_Default_VCO_DIV, ICS307_Default_REF_DIV);  // set to 20MHZ
   Freq1 = FrequencyCount();
   if(((30000 + MAX_FREQ_DEVIATION) - Freq1) > ( 2*MAX_FREQ_DEVIATION)) retval = 1;  // make sure frequency counter reads close to 20.000Hz
//   ICS307_Program(ICS307_Default_Config,ICS307_Default_VCO_DIV +16 ,ICS307_Default_REF_DIV);  // set to 40MHZ
   ICS307_ProgramW(ICS307_25MHZ);
   Freq2 = FrequencyCount();
   if(((25000 + MAX_FREQ_DEVIATION) - Freq2) > ( 2*MAX_FREQ_DEVIATION)) retval = 1;  // make sure frequency counter reads close to 40.000Hz
//   ICS307_Program(ICS307_Default_Config, ICS307_Default_VCO_DIV, ICS307_Default_REF_DIV);  // set back to 20MHZ
   ICS307_ProgramW(ICS307_30MHZ);
   return retval;
}

//------------------------------------------------------------------
// Reads Electronic Signature of both SPI Flash Memories
// Returns :  0 both signatures received
//            1 Missing Configuration Flash Signature
//            2 Missing Embedded Flash Signature
//            3 Both Signatures Missing
//------------------------------------------------------------------
unsigned char TestM25P40Signature(void)
{
  register unsigned char retval = 0;   // return value
  SPI_open(DEVICE_FLASH_CONFIGURATION);  // check for response from configuration flash first
  if(
//       (M25P40_RES_RESPONSE != Flash_readElectronicSignature())    // for 4MBit device
//     &&
       (M25P80_RES_RESPONSE != Flash_readElectronicSignature())    // for 8MBit device
    )
  {
    retval |= DEVICE_FLASH_CONFIGURATION;
  }
  SPI_close();
  SPI_open(DEVICE_FLASH_EMBEDDED);   // check for response from embedded flash next
  if(
//       (M25P40_RES_RESPONSE != Flash_readElectronicSignature())    // for 4MBit device
//     &&
       (M25P80_RES_RESPONSE != Flash_readElectronicSignature())    // for 8MBit device
    )
  {
    retval |= DEVICE_FLASH_EMBEDDED;
  }
  SPI_close();
  return(retval);
}

//-------------------------------------------------------------------------------
// Test data is exchanged with the Nanoboard controller by sending and receiving
// a 32 bit (4 byte) command to SPI address 3.
// Currently only the first and last bytes are used to exchange data.
// The last byte sent contains the bits to set the LEDs and the last byte received
// contains the state of the jumpers. ('1' = open '0' = inserted)
// The first byte sent (0xc0) places the Nanoboard controller into test mode.
// If the first byte is not 0xC0 (eg. 0x00) the Nanoboard controller reverts back
// to non-test mode
//-------------------------------------------------------------------------------
unsigned char Nanoboard_TestModeOn(unsigned char leds)
{
  unsigned char byte1,byte2,byte3,byte4;
  SPI_open(DEVICE_TESTER);
  SPI_startCommand();
  byte1 = SPI_sendReceiveByte(0xC0);  // Bit 31 = LED BANK enable,  BIT30 = disable normal jumper function
  byte2 = SPI_sendReceiveByte(0x00);  // reserved for future expansion
  byte3 = SPI_sendReceiveByte(0x00);  // reserved for future expansion
  byte4 = SPI_sendReceiveByte(leds);   //Set LEDSs and return status of jumpers.
  SPI_endCommand();
  SPI_close();
  return byte4;
}

//-----------------------------------------------------------------------------------
// turns NanoBoard Test Mode off, LEDs and Jumpers revert to their normal functions
//-----------------------------------------------------------------------------------
void Nanoboard_TestModeOff(void)
{
  SPI_open(DEVICE_TESTER);
  SPI_startCommand();
  SPI_sendReceiveByte(0x00);  // Bit 31 = LED BANK enable,  BIT30 = disable normal jumper function
  SPI_sendReceiveByte(0x00);  // reserved for future expansion
  SPI_sendReceiveByte(0x00);  // reserved for future expansion
  SPI_sendReceiveByte(0x00);  // don't care
  SPI_endCommand();
  SPI_close();
}









⌨️ 快捷键说明

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