📄 lm75.c
字号:
/****************************************Copyright (c)**************************************************
** Guangzhou ZHIYUAN ELECTRONIC CO.,LTD.
** Research centre
** http://www.zyinside.com, http://www.zlgmcu.com
**
**---------------------------------------File Info-----------------------------------------------------
** File name: LM75.c
** Latest modified Date: 2006-2-20
** Latest Version: 1.0
** Descriptions: LM75 driver
**
**------------------------------------------------------------------------------------------------------
** Created by: ZhouShuwu
** Created date: 2006-2-20
** Version: 1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
//Includes
#include "system.h"
#include "LM75.h"
#include "oc_i2c.h"
#include "sys/alt_irq.h"
#include "altera_avalon_pio_regs.h"
#define CONFIG 0x00//Configuration Register is set to 0x00.
#define THYST 75//Hysteresis Register is set to 75.
#define TOS 80//Over-temp Shutdown threshold Register 80
#define I2C_BASE I2C_MASTER_BASE //I2C?úo?μ??ùμ??·
#define LM75_IRQ 0xfff//LM75_OS_IRQ //ó?LM75?àá??óμ??D??o?
#define LM75_INT_BASE 0xfff//LM75_OS_BASE //ó?LM75?àá??óμ?PIOμ??ùμ??·
#define I2C_SPEED (10000u) //LM751¤×÷?ù?è£o10kbps
static LM75_dev dev;
static alt_u8 content[3];
#define IRQCON 1 // External interrupt is connected to LM75_INT
// This is bit mask of LM75_INT
// user should modify it according to the real situation
static void LM75_ISR(void* context, alt_u32 id);
/*********************************************************************************************************
** Function name: LM75_init
**
** Descriptions: Initialize the I2C Open Core. The frequency of SCL is set as i2c_freq
** Interrupt will be enabled now.
** LM75_dev structure must be initialized before calling LM75_init()
**
** input parameters: dev: a pointer to LM75 device
** i2c_freq: work frequency of i2c
**
** Returned value: negative, fail
** zero or positive, successfully
**
** Used global variables: None
** Calling modules: None
**
** Created by: Zhou Shuwu
** Created Date: 2006/2/20
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int LM75_Init(LM75_dev* dev, alt_u32 i2c_freq)
{
alt_u32 prescale;
/* Check if LM75_dev structure is initialized ? */
if(dev->base == 0)
{
return -1; // No, return error information
}
// initialize the I2C freq
InitI2C(dev->base, i2c_freq, 0);
// initialize the PIO(INT pin of LM75)
prescale = IORD_ALTERA_AVALON_PIO_DIRECTION(dev->pio_base);
prescale &= ~IRQCON;
IOWR_ALTERA_AVALON_PIO_DIRECTION(dev->pio_base, prescale);
/* Enable the PIO interrupt, which is connected to the INT pin of LM75 */
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(dev->pio_base, IRQCON);
/* Clear edge capture register */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(dev->pio_base, 0);
/* register the LM75 interrupt service routine */
return alt_irq_register(dev->irq, dev, LM75_ISR);
}
/*********************************************************************************************************
** Function name: LM75_ReadReg
**
** Descriptions: read register of LM75
**
** input parameters: dev: a pointer to LM75 device
** reg: offset of the target register
** content: the content of register will be stored here
**
** Returned value: negative, fail
** zero or positive, successfully
**
** Used global variables: dev, must be initialized
** Calling modules: None
**
** Created by: Zhou Shuwu
** Created Date: 2006.2.20
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int LM75_ReadReg (LM75_dev* dev, alt_u8 reg, alt_u8* content, alt_u16 num)
{
if(dev->base == 0)
return -1; // invalid input parameters
I2CRead(dev->base, dev->dev_addr, reg, content, num);
return 0;
}
/*********************************************************************************************************
** Function name: LM75_WriteReg
**
** Descriptions: write register of LM75
**
** input parameters: dev: a pointer to LM75 device
** reg: offset of the target register
** content: the content of register will be stored here
**
** Returned value: negative, fail
** zero or positive, successfully
**
** Used global variables: dev, must be initialized
** Calling modules: None
**
** Created by: Zhou Shuwu
** Created Date: 2006.2.20
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int LM75_WriteReg(LM75_dev* dev, alt_u8 reg, alt_u8* content, alt_u16 num)
{
if(dev->base == 0)
return -1; // invalid input parameters
I2CWrite(dev->base, dev->dev_addr, reg, content, num);
return 0;
}
/*********************************************************************************************************
** Function name: LM75_ISR
**
** Descriptions: Interrupt service routine for LM75 keydown event
**
** input parameters: context: a pointer passed to ISR
** id: interrupt number
**
** Returned value: None
**
** Used global variables: None
** Calling modules: None
**
** Created by: Zhou Shuwu
** Created Date: 2006.2.20
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static void LM75_ISR(void* context, alt_u32 id)
{
LM75_dev* dev = (LM75_dev*) context;
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(dev->pio_base, 0);
/* Notify that a interrupt occur */
dev->int_flag = INT_EVENT;
}
void TemperatureInit(void)
{
dev.base = I2C_BASE;
dev.dev_addr = LM75_ADDRESS;
dev.irq = LM75_IRQ;
dev.pio_base = LM75_INT_BASE;
dev.int_flag = NO_INT;
content[0] = CONFIG;
LM75_WriteReg (&dev, LM75_CONFIG, content, 1);//CONFIG
//tmp = (THYST*2) << 7;
content[0] = 0x00; // low significant data byte of THYST
content[1] = THYST; // high significant data byte of THYST
LM75_WriteReg (&dev, LM75_THYST, content, 2);//THYST
content[0] = 0x00;
content[1] = TOS;
LM75_WriteReg (&dev, LM75_TOS, content, 2);
}
int TempertureGet(alt_u8* temperBuf)
{
return LM75_ReadReg (&dev, LM75_TEMP, temperBuf, 2);
}
/**********************end****************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -