📄 main.c
字号:
#include "config.h"
#define LM75 0x90
/****************************************************************************
* 名 称:UART0_Init()
* 功 能:初始化串口0。设置为8位数据位,1位停止位,无奇偶校验
* 入口参数:无
* 出口参数:无
****************************************************************************/
void UART0_Init(uint32 bps)
{
uint16 Fdiv;
PINSEL0 = (PINSEL0 & (~0x0F)) | 0x05; // 不影响其它管脚连接,设置I/O连接到UART0
U0LCR = 0x83; // DLAB = 1,可设置波特率
Fdiv = (Fpclk / 16) / bps; // 设置波特率
U0DLM = Fdiv / 256;
U0DLL = Fdiv % 256;
U0LCR = 0x03;
}
/****************************************************************************
* 名 称:UART0_SendByte()
* 功 能:向串口发送字节数据,并等待发送完毕。
* 入口参数:data 要发送的数据
* 出口参数:无
****************************************************************************/
void UART0_SendByte(uint8 data)
{
U0THR = data; // 发送数据
while( (U0LSR&0x40)==0 ); // 等待数据发送完毕
}
/****************************************************************************
* 名 称:UART0_SendStr()
* 功 能:向串口发送一字符串
* 入口参数:srt 要发送的字符串的指针
* 出口参数:无
****************************************************************************/
void UART0_SendStr(char *str)
{
while(1)
{
if( *str == '\0' ) break;
UART0_SendByte(*str++); // 发送数据
}
}
void I2C_Init(uint32 fi2c)
{
if(fi2c>400000) fi2c = 400000;
PINSEL0 = (PINSEL0 & (~0xF0)) | 0x50; // 不影响其它管脚连接,设置I/O连接到I2C
I2SCLH = (Fpclk/fi2c + 1) / 2; // 设置I2C时钟为fi2c
I2SCLL = (Fpclk/fi2c) / 2;
I2CONCLR = 0x2C;
I2CONSET = 0x40; // 使能主I2C
/* 设置I2C中断允许 */
VICIntSelect = 0x00000000; // 设置所有通道为IRQ中断
VICVectCntl0 = 0x29; // I2C通道分配到IRQ slot 0,即优先级最高
VICVectAddr0 = (int)IRQ_I2C; // 设置I2C中断向量地址
VICIntEnable = 0x0200; // 使能I2C中断
}
int main (void)
{
uint8 tem[2];
char disp_buf[32];
uint16 value;
float val;
I2C_Init(100000);
UART0_Init(115200);
while(1)
{
ISendByte(LM75,0x00);
IRcvStr(LM75,0x00,tem,2);
value=tem[0]*256+tem[1];
value=value>>7;
sprintf(disp_buf,"Digital Output is(Hex):0x%x\r\n",value);
UART0_SendStr(disp_buf);
if(value>0x100) //判断正负
{
value=~value;
val=((value&0xff)+1)*0.5;
sprintf(disp_buf,"Current temperature is:-%5.1fC.\r\n",val);
UART0_SendStr(disp_buf);
}
else
{
val=value*0.5;
sprintf(disp_buf,"Current temperature is:%5.1fC.\r\n",val);
UART0_SendStr(disp_buf);
}
}
return 0;
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -