|
/****************************lm92温度传感器*******************************/
#include "mega128.h"
#include "led and buz.h"
#include "ubyte.h"
#include <macros.h>
#include "delay.h"
#include "uv102_mega128.h"
#include "lm92.h"
#include "usart.h"
/******************************lm92 IIC void***************************************/
uchar tem_fg;
/****************************************************************************/
union lm_Byte2ToWord
{
uint WordS;
uchar ByteS[2];
}Byte2ToWord,TemperatuerVal;
//*******************************************************************
//*******************************************************************
struct Temperatuer
{
uint input;
uint hyst;
uint crit;
uint low;
uint high;
uint ID;
}ReadTemp , WriteTemp;
//bit lm92_FgTimeout ;
void lm92_I2cWait(void)
{
NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
}
void lm92_I2cStart(void)
{
lm92_scl_1;
lm92_sda_1;
lm92_I2cWait();
lm92_sda_0;
lm92_I2cWait();
lm92_scl_0;
lm92_I2cWait();
}
void lm92_I2cStop(void)
{
lm92_sda_0;
lm92_I2cWait();
lm92_scl_1;
lm92_I2cWait();
lm92_sda_1;
lm92_I2cWait();
}
void lm92_NOAck(void)
{
lm92_I2cWait();
lm92_sda_1;
lm92_I2cWait();
lm92_scl_1;
lm92_I2cWait();
lm92_scl_0;
lm92_I2cWait();
}
void lm92_TestAck(void)
{
lm92_I2cWait();
lm92_sda_0;
lm92_I2cWait();
lm92_scl_1;
lm92_I2cWait();
lm92_scl_0;
lm92_I2cWait();
}
uchar R_lm92_sda()
{
uchar ack;
ack=PINF.0;
lm92_I2cWait();
return ack;
}
uchar lm92_I2cReceiveByte(void)
{
uchar i,bytedata=0,j;
DDRF&=0xfE;
lm92_I2cWait();
lm92_sda_1;
for(i=0;i<8;i++)
{
lm92_scl_1;
lm92_I2cWait();
bytedata<<=1;
j=R_lm92_sda();
if(j) bytedata|=0x01;
lm92_scl_0;
lm92_I2cWait();
}
DDRF|=0x01;
lm92_I2cWait();
return bytedata;
}
void lm92_I2cSendByte(uchar bytedata)
{
uchar i;
// uchar ack;
for(i=0;i<8;i++)
{
if(bytedata&0x80)
lm92_sda_1;
else
lm92_sda_0;
bytedata<<=1;
lm92_I2cWait();
lm92_scl_1;
lm92_I2cWait();
lm92_scl_0;
lm92_I2cWait();
}
//lm92_sda_1;
//lm92_I2cWait();
// lm92_scl_1;
// lm92_I2cWait();
// lm92_I2cWait();
// ack=R_lm92_sda();
// lm92_scl_0;
// lm92_I2cWait();
// return ack;
}
/*void lm92_ByteWrite(uchar device,uchar address,uchar bytedata)
{
uchar i;
bit ack;
lm92_FgTimeout=1;
for(i=0;i<10;i++)
{
lm92_I2cStart();
ack=lm92_I2cSendByte(device);
if(ack==1)
{
lm92_I2cStop();
continue;
}
ack=lm92_I2cSendByte(address);
if(ack==1)
{
lm92_I2cStop();
continue;
}
ack=lm92_I2cSendByte(bytedata);
if(ack==1)
{
lm92_I2cStop();
continue;
}
lm92_I2cStop();
lm92_FgTimeout=0;
if(ack==0)break;
}
DelayMs(5);
}
uchar lm92_I2cByteRead(uchar device,uchar address)
{
uchar bytedata;
lm92_I2cStart();
lm92_I2cSendByte(device);
lm92_I2cSendByte(address);
lm92_I2cStart();
lm92_I2cSendByte(device|0x01);
bytedata=lm92_I2cReceiveByte();
lm92_NOAck();
lm92_I2cStop();
return bytedata;
} */
//温度转换
void Conversion(uint Temp)
{
TemperatuerVal.WordS = Temp>>1;
}
void lm92_WriteConfig(uchar hyst,uchar T_CRIT,uchar T_low,uchar T_high)
{
//写配置寄存器
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(ConfigurationRW);
lm92_TestAck();
lm92_I2cSendByte(TemperatureReadOnly);
lm92_TestAck();
lm92_I2cStop();
DelayMs(100);
//写HYST寄存器
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(ThystRW);
lm92_TestAck();
Conversion(hyst);
Txword1(TemperatuerVal.WordS) ;
lm92_I2cSendByte(TemperatuerVal.ByteS[0]);
lm92_TestAck();
lm92_I2cSendByte(TemperatuerVal.ByteS[1]);
lm92_TestAck();
lm92_I2cStop();
DelayMs(100);
//写临界温度寄存器
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(TcritRW);
lm92_TestAck();
Conversion(T_CRIT);
lm92_I2cSendByte(TemperatuerVal.ByteS[0]);
lm92_TestAck();
lm92_I2cSendByte(TemperatuerVal.ByteS[1]);
lm92_TestAck();
lm92_I2cStop();
DelayMs(100);
//写低限寄存器
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(TlowRW);
lm92_TestAck();
Conversion(T_low);
lm92_I2cSendByte(TemperatuerVal.ByteS[0]);
lm92_TestAck();
lm92_I2cSendByte(TemperatuerVal.ByteS[1]);
lm92_TestAck();
lm92_I2cStop();
DelayMs(100);
//写高限寄存器
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(ThighRW);
lm92_TestAck();
Conversion(T_high);
lm92_I2cSendByte(TemperatuerVal.ByteS[0]);
lm92_TestAck();
lm92_I2cSendByte(TemperatuerVal.ByteS[1]);
lm92_TestAck();
lm92_I2cStop();
DelayMs(100);
//读HYST寄存器
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(ThystRW);
lm92_TestAck();
lm92_I2cStart();
lm92_I2cSendByte(ReadCmd);
lm92_TestAck();
Byte2ToWord.ByteS[0]=lm92_I2cReceiveByte();
lm92_TestAck();
Byte2ToWord.ByteS[1]=lm92_I2cReceiveByte();
lm92_NOAck();
lm92_I2cStop();
Byte2ToWord.WordS = Byte2ToWord.WordS;
ReadTemp.hyst = Byte2ToWord.WordS;
/*****************
TI = 0;
SBUF = Byte2ToWord.ByteS[0];
DelayMs(100);
TI = 0;
SBUF = Byte2ToWord.ByteS[1];
**************串口发送测试程序 */
Txbyte1(0x70);
Txword1(ReadTemp.hyst) ;
Txbyte1(0x8d);
DelayMs(100);
//读临界温度寄存器
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(TcritRW);
lm92_TestAck();
lm92_I2cStart();
lm92_I2cSendByte(ReadCmd);
lm92_TestAck();
Byte2ToWord.ByteS[0]=lm92_I2cReceiveByte();
lm92_TestAck();
Byte2ToWord.ByteS[1]=lm92_I2cReceiveByte();
lm92_NOAck();
lm92_I2cStop();
Byte2ToWord.WordS = Byte2ToWord.WordS;
ReadTemp.crit = Byte2ToWord.WordS;
/***************
TI = 0;
SBUF = Byte2ToWord.ByteS[0];
DelayMs(100);
TI = 0;
SBUF = Byte2ToWord.ByteS[1];
***********/
Txbyte1(0x71);
Txword1(ReadTemp.crit);
Txbyte1(0x8d);
DelayMs(100);
//读低限寄存器
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(TlowRW);
lm92_TestAck();
lm92_I2cStart();
lm92_I2cSendByte(ReadCmd);
lm92_TestAck();
Byte2ToWord.ByteS[0]=lm92_I2cReceiveByte();
lm92_TestAck();
Byte2ToWord.ByteS[1]=lm92_I2cReceiveByte();
lm92_NOAck();
lm92_I2cStop();
Byte2ToWord.WordS = Byte2ToWord.WordS;
ReadTemp.low = Byte2ToWord.WordS;
/******************
TI = 0;
SBUF = Byte2ToWord.ByteS[0];
DelayMs(100);
TI = 0;
SBUF = Byte2ToWord.ByteS[1]; *******************/
Txbyte1(0x72);
Txword1(ReadTemp.low);
Txbyte1(0x8d);
DelayMs(100);
//读高限寄存器
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(ThighRW );
lm92_TestAck();
lm92_I2cStart();
lm92_I2cSendByte(ReadCmd);
lm92_TestAck();
Byte2ToWord.ByteS[0]=lm92_I2cReceiveByte();
lm92_TestAck();
Byte2ToWord.ByteS[1]=lm92_I2cReceiveByte();
lm92_NOAck();
lm92_I2cStop();
Byte2ToWord.WordS = Byte2ToWord.WordS;
ReadTemp.high = Byte2ToWord.WordS;
/************************************
TI = 0;
SBUF = Byte2ToWord.ByteS[0];
DelayMs(100);
TI = 0;
SBUF = Byte2ToWord.ByteS[1]; ************/
Txbyte1(0x73);
Txword1(ReadTemp.high);
Txbyte1(0x8d);
DelayMs(100);
//读芯片ID号
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(ManufacturerID);
lm92_TestAck();
lm92_I2cStart();
lm92_I2cSendByte(ReadCmd);
lm92_TestAck();
Byte2ToWord.ByteS[0]=lm92_I2cReceiveByte();
lm92_TestAck();
Byte2ToWord.ByteS[1]=lm92_I2cReceiveByte();
lm92_NOAck();
lm92_I2cStop();
ReadTemp.ID = Byte2ToWord.WordS;
/******************************
TI = 0;
SBUF = Byte2ToWord.ByteS[0];
DelayMs(100);
TI = 0;
SBUF = Byte2ToWord.ByteS[1];
****************/
Txbyte1(0x74);
Txword1(ReadTemp.ID);
Txbyte1(0x8d);
DelayMs(100);
}
//*******************************************************************
//*******************************************************************
//500Ms转换一次温度
uint lm92_Read(void)
{
uint Temp;
lm92_I2cStart();
lm92_I2cSendByte(WriteCmd);
lm92_TestAck();
lm92_I2cSendByte(TemperatureReadOnly);
lm92_TestAck();
lm92_I2cStart();
lm92_I2cSendByte(ReadCmd);
lm92_TestAck();
Byte2ToWord.ByteS[0]=lm92_I2cReceiveByte();
lm92_TestAck();
Byte2ToWord.ByteS[1]=lm92_I2cReceiveByte();
lm92_NOAck();
lm92_I2cStop();
Byte2ToWord.WordS = Byte2ToWord.WordS <<1 ;
Temp = Byte2ToWord.WordS;
return(Temp);
}
/******************************lm92 tempter test example**************************/
/****void main()
{
CLI();
IO_init();
Init_Usart1() ;
Init_Usart0() ;
lm92_WriteConfig(2,100,10,30);
ex_int_init();
off_buzzer;
buzzer_test_alam();
SEI();
while(1)
{
led_test();
if(tem_fg==1)
{
CLI();
buzzer_test_alam();
SEI();
tem_fg=0;
}
}
} ***********/
interrupt[USART0_RXC]
void Usart_Rxc0(void)
{
uchar temp ,a;
CLI();
temp=0;
a=UDR0;
if(a==0x4f)
{
buzzer_test();
temp=lm92_Read();
Txbyte0(temp);
Txbyte0(a);
}
SEI();
}
interrupt[USART1_RXC]
void Usart_Rxc1(void)
{
uint temp ,a;
CLI();
temp=0;
a=UDR1;
if(a==0x4f)
{
buzzer_test();
temp=lm92_Read();
Txbyte1(a);
Txbyte1(temp);
Txbyte1(a);
}
SEI();
}
interrupt[EXT_INT6]
void tem_alam(void)
{
CLI();
tem_fg=1;
EIFR|=0x40;
//SEI();
}
/**********************************LM92.H**************************************************/
#ifndef __lm92_h
#define __lm92_h
/*********************************hardware*******************************/
#define lm92_sda_1 PORTF|=0x01
#define lm92_sda_0 PORTF&=~0x01
#define lm92_scl_1 PORTF|= 0x02
#define lm92_scl_0 PORTF&=~0x02
#define lm92_int PINE.6
/********************************software**********************************/
//温度芯片特径字
#define TemperatureReadOnly 0x00 //ro, 16-bit
#define ConfigurationRW 0x01 //rw, 8-bit
#define ThystRW 0x02 //rw, 16-bit
#define TcritRW 0x03 //rw, 16-bit
#define TlowRW 0x04 //rw, 16-bit
#define ThighRW 0x05 //rw, 16-bit
#define ManufacturerID 0x07 //ro, 16-bit
#define Lm92ManufacturerID 0x8001 //ro, 16-bit
#define WriteCmd 0x90 //W0, 8/16-bit
#define ReadCmd 0x91 //r0, 8/16-bit
//*******************************************************************
//*******************************************************************
//温度寄存器定义
/*#define LM92_SYSCTL_ALARMS 2001
#define LM92_SYSCTL_TEMP 1200
#define LM92_ALARM_TEMP_HIGH 0x01
#define LM92_ALARM_TEMP_LOW 0x02
#define LM92_ALARM_TEMP_CRIT 0x04
#define LM92_TEMP_HIGH 0x08
#define LM92_TEMP_LOW 0x10
#define LM92_TEMP_CRIT 0x20
#define LM92_TEMP_HYST 0x40
#define LM92_TEMP_INPUT 0x80 */
//*******************************************************************
//*******************************************************************
//*******************************************************************
//*******************************************************************
//typedef struct Temperatuer ReadTemp WriteTemp;
//Temperatuer WriteTemp;
//*******************************************************************
//*******************************************************************
extern void lm92_WriteConfig(uchar hyst,uchar T_CRIT,uchar T_low,uchar T_high);
extern uint lm92_Read(void);
extern uchar tem_fg ;
//extern void
//extern void
#endif |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|