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

📄 lm75.c

📁 this document is source code for arm9 of atmel
💻 C
字号:
/*----------------------------------------------------------------------------
*         ATMEL Microcontroller Software Support  -  ROUSSET  -
*----------------------------------------------------------------------------
* The software is delivered "AS IS" without warranty or condition of any
* kind, either express, implied or statutory. This includes without
* limitation any warranty or condition with respect to merchantability or
* fitness for any particular purpose, or against the infringements of
* intellectual property rights of others.
*----------------------------------------------------------------------------
* File Name           : lm75.c
* Object              : lm75 Temp Sensor routines.
*                     :
* Version | mm | dd | yy | author :
*  1.0      05   16   06    PFi   : Creation
*
*--------------------------------------------------------------------------------------
* 1)Description:
*
* The functions below shows how to communicate with the LM75 Digital Temperature Sensor from National Semiconductor
* connnected on the TWI port.
*
* The registers of the LM75 are configured by default to access the temperature register.
*
* 2)Configuration and pins used in this example:
*
* ------| AT91 / IO  | LM75 / IO
* CLOCK | TWCK / PA3 | SCL / 1
* DATA  | TWD / PA4  | SDA / 2
*
* LM75 I2C slave address:
* Therefore, the complet slave address is:
* | 1 | 0 | 0 | 1 |A2 |A1 |A0 |
* |MSB| - | - | - | - | - |LSB|
* |    Internal   | External  |
* - External address pins (A0,A1,A2 = 1)
*
* ---------------
* * WARNINGS ** :
* ---------------
* The functions below does includes timeout loop in case of a bad read or write access.
* It does not check if write access have been correctly written into the registers
*
* 3) Remarks:
*
* This example runs in polling mode.and makes no use of IRQs. To have further details
* and implementation example about using IRQs, refer to any Interrupt project examples,
* like AT91SAM7S64-Interrupt-IAR4_XX-X_X
* available on www.at91.com --> KIT --> AT91SAM7S-EK --> Software
*
***************************************************************************************/

/* Include Standard LIB  files */
#include "Board.h"
#include "..\common\init.h"
#include "..\common\lib_twi.h"
#include "lm75.h"
#include "math.h"

char PointerReg;      /* LM75 Pointer Register */

char WordRead[2];       /* Store the Word(16) received from the LM75 */


/*-----------------------------------------------------------------------------
Function:   short AT91F_LM75_ReadConfigurationRegister(void);
Arguments: None

Return Value: <read>: The value of the 8 bit configuration register.
-----------------------------------------------------------------------------*/
char AT91F_LM75_ReadConfigurationRegister(void)
{
  char  ConfigReg=0;

  /* Send the pointer register Config. REg. and read it */
  AT91F_TWI_ReadSingleIadr(AT91C_BASE_TWI,
                           AT91C_LM75_I2C_ADDRESS,
                           (char)CONFIG_REG,
                           AT91C_TWI_IADRSZ_1_BYTE,
                           &ConfigReg);

  return (ConfigReg);
}

/*-----------------------------------------------------------------------------
Function:  int AT91F_LM75_SetConfigurationRegister(void)
Arguments: <ConfigRegValue>: Value to write into the configuration register

Return Value: none
-----------------------------------------------------------------------------*/
int AT91F_LM75_SetConfigurationRegister(char ConfigRegValue)
{
  /* Send the Config. reg. */
  AT91F_TWI_WriteSingleIadr(AT91C_BASE_TWI,
                            AT91C_LM75_I2C_ADDRESS,
                            (char)CONFIG_REG,
                            AT91C_TWI_IADRSZ_1_BYTE,
                            &ConfigRegValue);

  return (1);
}

/*-----------------------------------------------------------------------------
Function:   short AT91F_LM75_ReadThystRegister(void)
Arguments: None

Return Value: <ThysReg>: The value of the Thys register (16-bit reg.).
-----------------------------------------------------------------------------*/
float AT91F_LM75_ReadThystRegister(void)
{
  float ThystReg;
    
  /* Send the pointer register to Thyst. reg. and read it */
  AT91F_TWI_ReadMultipleIadr(AT91C_BASE_TWI,
                             AT91C_LM75_I2C_ADDRESS,
                             2,
                             (char)THYST_REG,
                             AT91C_TWI_IADRSZ_1_BYTE,
                             WordRead);
  
  /* Shift the value and format it */
  ThystReg = WordRead[0]<<1 ;
  ThystReg  += WordRead[1]>>7;

  return (ThystReg*0.5);    /* each LSB = 0.5癈 */
}

/*-----------------------------------------------------------------------------
Function:  int AT91F_LM75_SetThysRegister(short ThystRegValue)
Arguments: <ThystRegValue>: Value to write into the configuration register

Return Value: 1
-----------------------------------------------------------------------------*/
int AT91F_LM75_SetThysRegister(short ThystRegValue)
{
  char data2write[2];
  data2write[0] = (char)ThystRegValue;
  data2write[1] = (char)(ThystRegValue>>4);
  
  /* Send the pointer register to Thyst reg */ 
  AT91F_TWI_WriteMultipleIadr(AT91C_BASE_TWI,
                              AT91C_LM75_I2C_ADDRESS,
                              (char)THYST_REG,
                              AT91C_TWI_IADRSZ_1_BYTE,
                              data2write,
                              2);

  return (1);
}

/*-----------------------------------------------------------------------------
Function:   short AT91F_LM75_ReadTosRegister(void)
Arguments: None

Return Value: <TosReg>: The value of the Tos register (16-bit reg.)
-----------------------------------------------------------------------------*/
float AT91F_LM75_ReadTosRegister(void)
{
  float TosReg;

 /* Send the pointer register Tos Reg. and Read it  */
  AT91F_TWI_ReadMultipleIadr(AT91C_BASE_TWI,
                             AT91C_LM75_I2C_ADDRESS,
                             2,
                             (char)TOS_REG,
                             AT91C_TWI_IADRSZ_1_BYTE,
                             WordRead);

  // Shift the value and format it
  TosReg = WordRead[0]<<1 ;
  TosReg  += WordRead[1]>>7;
  
  return (TosReg*0.5);  //* each LSB = 0.5癈
}

/*-----------------------------------------------------------------------------
Function:  int AT91F_LM75_SetTosRegister(short TosRegValue)
Arguments: <TosRegValue>: Value to write into the configuration register

Return Value: 1
-----------------------------------------------------------------------------*/
int AT91F_LM75_SetTosRegister(short TosRegValue)
{
  char data2write[2];
  data2write[0] = (char)TosRegValue;
  data2write[1] = (char)(TosRegValue>>4);

  /* Set the pointer register to  Tos register and write it */
  AT91F_TWI_WriteMultipleIadr(AT91C_BASE_TWI,
                              AT91C_LM75_I2C_ADDRESS,
                              (char)TOS_REG,
                              AT91C_TWI_IADRSZ_1_BYTE,
                              data2write,
                              2);
 
  return (1);
}


/*-----------------------------------------------------------------------------
Function:   short AT91F_LM75_ReadTempRegister(void)
Arguments: None

Return Value: <TosReg>: The value of the Temp register in 癈
-----------------------------------------------------------------------------*/
float AT91F_LM75_ReadTempRegister(void)
{
  float TempReg;        // LM75 Temperature Register
  
  /* Set the pointer register to Temp. Reg. and Read it */
  AT91F_TWI_ReadMultipleIadr(AT91C_BASE_TWI,
                             AT91C_LM75_I2C_ADDRESS,
                             2,
                             (char)TEMP_REG,
                             AT91C_TWI_IADRSZ_1_BYTE,
                             WordRead);

  /* Shift the value and format it */
  TempReg = WordRead[0]<<1 ;
  TempReg  += WordRead[1]>>7;
  
  return (TempReg*0.5);        //* each LSB = 0.5癈
}

⌨️ 快捷键说明

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