📄 main.c
字号:
#include "systemInit.h"
#include "uartGetPut.h"
#include "LM3S_I2CM.h"
#include <stdio.h>
// 定义LM75A(NXP半导体数字温度传感器,I2C接口)
#define LM75A_SLA (0x90 >> 1) // 定义LM75A的器件地址
#define LM75A_REG_TMP 0x00 // 定义LM75A的温度寄存器地址
// 读取LM75A的温度值,并通过UART发送显示
void LM75A_TmpDisp(char *pcTmp)
{
char Tab[8][4] = {"000", "125", "250", "375", "500", "625", "750", "875"};
char s[20];
short t;
t = pcTmp[0];
t <<= 8;
t |= pcTmp[1] & 0xE0;
if (t < 0)
{
t = -t;
uartPutc('-');
}
sprintf(s, "%d", (int)(t) / 256);
uartPuts(s);
uartPutc('.');
uartPuts(Tab[(t >> 5) & 0x07]);
uartPuts("\r\n");
}
// 主函数(程序入口)
int main(void)
{
char cBuf[2];
unsigned long ulStatus;
tI2CM_DEVICE LM75A = {LM75A_SLA, LM75A_REG_TMP, 1, cBuf, 2};
jtagWait(); // 防止JTAG失效,重要!
clockInit(); // 时钟初始化:晶振,6MHz
uartInit(); // UART初始化
I2CM_Init(); // I2C主机初始化
for (;;)
{
ulStatus = I2CM_DataRecv(&LM75A);
if (ulStatus == I2C_MASTER_ERR_NONE) // 没有错误
{
LM75A_TmpDisp(cBuf); // 显示LM75A温度
}
else
{
uartPuts("Error\r\n");
if (ulStatus & I2C_MASTER_ERR_ADDR_ACK) // 地址应答错误
{
uartPuts("cannot find LM75A.\r\n");
}
if (ulStatus & I2C_MASTER_ERR_DATA_ACK) // 数据应答错误
{
uartPuts("cannot get data from LM75A.\r\n");
}
if (ulStatus & I2C_MASTER_ERR_ARB_LOST) // 多主机通信仲裁失败
{
uartPuts("arbitration lost.\r\n");
}
break;
}
SysCtlDelay(1500 * (TheSysClock / 3000));
}
for (;;)
{
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -