|
//-----------------------------------------------------------------
// 名稱: SPI接口溫度傳感器TC72應(yīng)用測(cè)試
//-----------------------------------------------------------------
// 說明: 本例運(yùn)行時(shí),單片機(jī)將持續(xù)從TC72傳感器讀取溫度數(shù)據(jù)并轉(zhuǎn)換為
// 浮點(diǎn)型字符串送LCD顯示(內(nèi)置SPI接口需要在實(shí)物電路測(cè)試)
//
//-----------------------------------------------------------------
#include "REG51RX2.H"
#include <intrins.h>
#include <stdio.h>
#include <math.h>
#define INT8U unsigned char
#define INT16U unsigned int
//SPI使能與禁用(注意TC72是高電平使能,低電平禁用)
#define SPI_EN() P1 |= (1<<4)
#define SPI_DI() P1 &= ~(1<<4)
//TC72寄存器地址定義
#define TC72_CTRL 0x80 //控制寄存器
#define TC72_TEMP_LSB 0x01 //溫度低字節(jié)
#define TC72_TEMP_MSB 0x02 //溫度高字節(jié)
#define TC72_MANU_ID 0x03 //制造商ID
//SPI接口引腳定義(模擬方式)
sbit SCK = P3^4; //串行時(shí)鐘
sbit CE = P3^5; //片選
sbit SDI = P3^6; //串行數(shù)據(jù)輸入
sbit SDO = P3^7; //串行數(shù)據(jù)輸出
//內(nèi)置SPI寄存器及寄存器位定義
//SPCON C3h SPI Control SPR2 SPEN SSDIS MSTR CPOL CPHA SPR1 SPR0
//SPSTA C4h SPI Status SPIF WCOL SSERR MODF - - - -
//SPDAT C5h SPI Data SPD7 SPD6 SPD5 SPD4 SPD3 SPD2 SPD1 SPD0
//sfr SPCON = 0xC3;
//sfr SPSTA = 0xC4;
//sfr SPDAT = 0xC5;
//extern void Initialize_LCD();
extern void LCD_ShowString(INT8U r, INT8U c,INT8U *str);
extern void delay_ms(INT16U x);
INT8U T[2]; //兩字節(jié)原始溫度數(shù)據(jù)
float TempX = 0.0; //浮點(diǎn)溫度值
//-----------------------------------------------------------------
// SPI主機(jī)初始化
//-----------------------------------------------------------------
//void SPI_MasterInit()
//{
// SPCON=0xD0;
// SPSTA=0xC0;
//}
//-----------------------------------------------------------------
// SPI數(shù)據(jù)傳輸
//-----------------------------------------------------------------
//INT8U SPI_SendData(INT8U dat)
//{
// SPDAT=dat;
// while((SPSTA&0x80)==0x00);
// SPSTA=0xC0;
// return SPDAT;
//}
//-----------------------------------------------------------------
// 從當(dāng)前地址讀取一字節(jié)數(shù)據(jù)(模擬方式)
//-----------------------------------------------------------------
INT8U ReadByte()
{
INT8U i,d=0x00;
for(i=0;i<8;i++)
{
SCK=1;
SCK=0;
d=(d<<1)|SDO;
}
return d;
}
//-----------------------------------------------------------------
// 向當(dāng)前地址寫入一字節(jié)數(shù)據(jù)(模擬方式)
//-----------------------------------------------------------------
void WriteByte(INT8U dat)
{
INT8U i;
for(i=0;i<8;i++)
{
SCK=0;
dat<<=1;
SDI=CY;
SCK=1;
}
}
//-----------------------------------------------------------------
// 向TC72寫入兩字節(jié)(地址,數(shù)據(jù))(模擬方式)
//-----------------------------------------------------------------
void Write_TC72(INT8U addr, INT8U dat)
{
CE=1;
WriteByte(addr);
WriteByte(dat);
CE=0;
}
//-----------------------------------------------------------------
// 寫TC72配置數(shù)據(jù)
//-----------------------------------------------------------------
void Config_TC72()
{
Write_TC72(TC72_CTRL,0x15);
}
//-----------------------------------------------------------------
// 從TC72讀取兩字節(jié)溫度數(shù)據(jù)并轉(zhuǎn)換為浮點(diǎn)溫度值
//-----------------------------------------------------------------
void Read_TC72_Temperature()
{
Config_TC72();
delay_ms(20);
CE=1;
WriteByte(TC72_TEMP_MSB);
T[1]=ReadByte();
T[0]=ReadByte();
CE=0;
TempX=(((int)((T[1]<<8)|T[0]))>>6)*0.25;
}
//-----------------------------------------------------------------
// 主程序
//-----------------------------------------------------------------
void main()
{
char DisplayBuffer[17];
Initialize_LCD();
LCD_ShowString(0,0,"TC72 Sensor Test");
while(1)
{ Config_TC72();
Read_TC72_Temperature();
sprintf(DisplayBuffer,"%TEMP:%6.1f\xDF\x43",TempX);
LCD_ShowString(1,0,DisplayBuffer);
}
}
|
|