📄 lm75_test.c
字号:
/********************************************************************
* 文 件 名:LM75_test.c
* 功 能:学习I2C内核的操作方法及LM75的访问方法。
* 说 明:借助LA1024逻辑分析仪(I2C总线分析插件)调试程序。
********************************************************************/
#include "system.h"
#include "alt_types.h"
#include "LM75.h"
#include "sys/alt_irq.h"
#include <stdio.h>
#include <string.h>
/******************************************************************
* 与硬件相关的宏定义,用户可根据实际情况修改
******************************************************************/
// 用户添加并命名的外设基地址,在SYSTEM中定义,用户需要根据不同的命名来修改此处
#ifndef I2C_MASTER_BASE //这是I2C核的基地址
#define I2C_MASTER_BASE 0xffffffff //user's definition here
#endif
#if I2C_MASTER_BASE == 0xffffffff
#error "No definition of I2C_MASTER core.\n"
#endif
#ifndef LM75_OS_BASE //这是LM75_OS PIO核的基地址
#define LM75_OS_BASE 0xfffffffe //user's definition here
#endif
#if LM75_OS_BASE == 0xfffffffe
#warning "No definition of LM75_OS PIO core.\n"
#endif
#ifndef LM75_OS_IRQ //这是LM75_OS PIO核的中断号
#define LM75_OS_IRQ 0xffff //user's definition here
#endif
#if LM75_OS_IRQ == 0xffff
#warning "No definition of LM75_OS_IRQ.\n"
#endif
#define I2C_SPEED (10000u) //LM75工作速度:10kbps
//定义初始化数据
#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
//定义LM75_dev类型的数据并初始化为零
LM75_dev dev = {0};
//LM75设置
void LM75_SET(void)
{
alt_u8 content[3]; //用以储存寄存器的内容
alt_u16 tmp;
*content = CONFIG;
LM75_WriteReg (&dev, LM75_CONFIG, content, 1);//预设CONFIG寄存器
tmp = (THYST*2) << 7;
*content = (alt_u8) tmp; // low significant data byte of THYST
*(content+1) = (alt_u8) (tmp >> 8);// high significant data byte of THYST
LM75_WriteReg (&dev, LM75_THYST, content, 2);//预设THYST寄存器
tmp = (TOS*2) << 7;
*content = (alt_u8) tmp; // low significant data byte of TOS
*(content+1) = (alt_u8) (tmp >> 8);// high significant data byte of TOS
LM75_WriteReg (&dev, LM75_TOS, content, 2);//预设TOS寄存器
}
int main(void)
{
alt_u8 content[3]; //用以储存寄存器的内容
alt_u8 temperature; //用以存储温度
alt_u8 sign; //用以存储温度正负号
alt_u8 int_flag; //用以告知外部中断事件发生
alt_irq_context context;
//在使用LM75访问函数前,一定要初始化LM75_dev类型的全局变量
dev.base = I2C_MASTER_BASE;
dev.dev_addr = LM75_ADDRESS;
dev.irq = LM75_OS_IRQ;
dev.pio_base = LM75_OS_BASE;
dev.int_flag = NO_INT;
LM75_Init(&dev, I2C_SPEED); //初始化LM75设备以及I2C频率
LM75_SET(); //预设
context = alt_irq_disable_all();
alt_irq_enable_all(context);
while(1)
{
context = alt_irq_disable_all();
int_flag = dev.int_flag; //读取并暂存中断事件标志
dev.int_flag = NO_INT; //清中断事件标志
alt_irq_enable_all(context);
LM75_ReadReg (&dev, LM75_TEMP, content, 2);//读取温度
if(((*content) & 0x80) != 0) //如果高字节最高位为1温度则为负
{
sign = 1;
}
else
{
sign =0;
}
// 组合高字节的0-7bit和低字节的第8bit
temperature = ((*content) << 1) | ((*content+1) >> 7);
temperature = temperature >> 1; //1bit为0.5度,除以2 不计小数
if(sign != 0)
{
printf("Current temperature is: -");//输出负号
}
else
{
printf("Current temperature is: +");//输出正号
}
printf("%d degree\n", temperature); //输出温度
if(int_flag != NO_INT) //若有报警中断事件发生
{
//;增加代码
}
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -