Files
CHJ/user/Device/MCP9808.c

80 lines
3.4 KiB
C
Raw Normal View History

2026-03-20 21:16:58 +08:00
#include "../main/SystemInclude.h"
2026-03-20 21:19:04 +08:00
//##############################################################################
#ifndef ENABLE_USE_MCP9808
#pragma message("[undefined] ENABLE_USE_MCP9808")
#elif(ENABLE_USE_MCP9808)
//##############################################################################
2026-03-20 21:16:58 +08:00
/******************************************************************************/
void WriteWordRegister(u8 regBuf, u16 regData)
{
2026-03-20 21:19:04 +08:00
//I2C_Init();
2026-03-20 21:16:58 +08:00
SI2C_Start(); // send START command
2026-03-20 21:19:04 +08:00
SI2C_Write(MCP_ADR); //WRITE Command (see Section 4.1.4 <20><>Address Byte<74><65>)
//also, make sure bit 0 is cleared <20><>0<EFBFBD><30>
2026-03-20 21:16:58 +08:00
SI2C_Write(regBuf); // Write CONFIG Register
SI2C_Write(regData>>8); // Write data
SI2C_Write(regData); // Write data
SI2C_Stop(); // send STOP command
}
/******************************************************************************/
u16 ReadWordRegister(u8 regBuf)
{
TypeWord tempInt;
2026-03-20 21:19:04 +08:00
//I2C_Init();
2026-03-20 21:16:58 +08:00
SI2C_Start(); // send START command
2026-03-20 21:19:04 +08:00
SI2C_Write(MCP_ADR); //WRITE Command (see Section 4.1.4 <20><>Address Byte<74><65>)
//also, make sure bit 0 is cleared <20><>0<EFBFBD><30>
2026-03-20 21:16:58 +08:00
SI2C_Write(regBuf); // Write CONFIG Register
SI2C_Start(); // send Repeat START command
2026-03-20 21:19:04 +08:00
SI2C_Write(MCP_ADR | 0x01); //READ Command
//also, make sure bit 0 is set <20><>1<EFBFBD><31>
2026-03-20 21:16:58 +08:00
tempInt.Byte[1] = SI2C_Read(); // READ 8 bits
SI2C_Ack();
tempInt.Byte[0] = SI2C_Read(); // READ 8 bits
SI2C_NoAck();
SI2C_Stop(); // send STOP command
return tempInt.Word;
}
/******************************************************************************/
void WriteByteRegister(u8 regBuf, u8 regData)
{
2026-03-20 21:19:04 +08:00
//I2C_Init();
2026-03-20 21:16:58 +08:00
SI2C_Start(); // send START command
2026-03-20 21:19:04 +08:00
SI2C_Write(MCP_ADR); //WRITE Command (see Section 4.1.4 <20><>Address Byte<74><65>)
//also, make sure bit 0 is cleared <20><>0<EFBFBD><30>
2026-03-20 21:16:58 +08:00
SI2C_Write(regBuf); // Write CONFIG Register
SI2C_Write(regData); // Write data
SI2C_Stop(); // send STOP command
}
/******************************************************************************/
u16 ReadByteRegister(u8 regBuf)
{
u8 temp;
2026-03-20 21:19:04 +08:00
//I2C_Init();
2026-03-20 21:16:58 +08:00
SI2C_Start(); // send START command
2026-03-20 21:19:04 +08:00
SI2C_Write(MCP_ADR); //WRITE Command (see Section 4.1.4 <20><>Address Byte<74><65>)
//also, make sure bit 0 is cleared <20><>0<EFBFBD><30>
2026-03-20 21:16:58 +08:00
SI2C_Write(regBuf); // Write CONFIG Register
SI2C_Start(); // send Repeat START command
2026-03-20 21:19:04 +08:00
SI2C_Write(MCP_ADR | 0x01); //READ Command
//also, make sure bit 0 is set <20><>1<EFBFBD><31>
2026-03-20 21:16:58 +08:00
temp = SI2C_Read(); // READ 8 bits
SI2C_NoAck();
SI2C_Stop(); // send STOP command
return temp;
}
2026-03-20 21:19:04 +08:00
//##############################################################################
#endif
//##############################################################################