?? main.c
字號:
/*******************************************************************************
程序名稱:LM75溫度傳感器
硬件接法:SCL接P1.6,SDA接P1.7
運行效果:數碼管顯示當前溫度
*******************************************************************************/
#include "Disp.h"
#include "I2C.h"
#include <reg51.h>
#include <string.h>
#include <stdio.h>
/*
函數:Delay()
功能:延時1ms~65.536s
參數:
t>0時,延時(t*0.001)s
t=0時,延時65.536s
*/
void Delay(unsigned int t)
{
do
{
TH0 = 0xFC;
TL0 = 0x66;
TR0 = 1;
while ( !TF0 );
TR0 = 0;
TF0 = 0;
} while ( --t != 0 );
}
/*
函數:SysInit()
功能:系統初始化
*/
void SysInit()
{
TMOD &= 0xF0;
TMOD |= 0x01; //設置T0為16位定時器
DispInit(); //數碼管掃描顯示初始化
I2C_Init(); //初始化I2C總線
}
/*
函數:LM75A_GetTemp
功能:讀出LM75A的溫度值
返回:LM75A溫度寄存器的數值(乘以0.125可得到攝氏度值)
*/
int LM75A_GetTemp()
{
unsigned char buf[2];
int t;
I2C_Gets(0x90,0x00,2,buf);
t = buf[0];
t <<= 8;
t += buf[1];
t >>= 5; //去掉無關位
return t;
}
/*
函數:DispTemp()
功能:在數碼管上顯示出溫度值
參數:
t:補碼,除以8以后才是真正溫度值
*/
void DispTemp(int t)
{
bit f = 1;
unsigned char i;
unsigned char c;
float x;
unsigned char s[10];
x = (float)t / 8.0;
sprintf(s,"%g",x);
for ( i=0; i<4; i++ )
{
c = s[i];
if ( c == '\0' ) break;
if ( c == '.' )
{
DispDotOn(i-1);
f = 0;
}
else
{
if ( !f )
{
switch(c)
{
case '3':
c = '0';
break;
case '8':
c = '5';
break;
default:
break;
}
}
DispChar(c);
}
}
if ( f )
{
DispDotOn(i-1);
DispChar('0');
}
}
void main()
{
int t;
SysInit();
for (;;)
{
t = LM75A_GetTemp();
DispClear();
DispTemp(t);
Delay(1000);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -