单总线带1024位温度校准存储的可编程分辨率温度传感器
简介
单总线带1024位温度校准存储的可编程分辨率温度传感器。
提供9到12bit分辨率的温度测量,可以通过可编程非易失性存储单元实现温度的下限和上限报警。提供1024位非易失性存储用于二次校正温度曲线或存储用户信息。在工作于温度校正模式时,可在128℃的温度范围内每0.5℃一个温区调节-0.4375~+0.4375℃,修调
精度0.0625℃。
应用
工业控制器
系统监控
架构
精度的提升
指令
单总线时序
操作举例
封装
测试
结束语
致力于弥补空白,让您的设计更简单可靠
代码举例
//DQ 为开漏,外接1k上拉电阻
unsigned char UART_RX[20];
unsigned char DQNum = 0;
unsigned char dat[40];
unsigned char rom_temp[8];
unsigned char rom_s[100][8];
unsigned char LastRomNum = 0;
unsigned char LastBranch = 0;
unsigned char LastFamilyBranch = 0;
unsigned char NoMoreDevice = 0;
unsigned short CRC_Read;
unsigned short crc16 = 0;
unsigned char crc16h;
unsigned char crc16l;
unsigned char crc8 = 0;
void Init_OW() {
SetDQ(1); DelayUs(20);
SetDQ(0);
if(!speedup) DelayUs(500);
else DelayUs(60);
SetDQ(1); DelayUs(200);
}
unsigned char ReadOneBit_Normal(){
unsigned char dat = 0;
SetDQ(0); DelayUs(2);
SetDQ(1); DelayUs(3);
dat = DQ; DelayUs(55);
return(dat);
}
void WriteOneBit_Normal(unsigned char dat){
SetDQ(0); DelayUs(2);
SetDQ(dat); DelayUs(48);
SetDQ(1); DelayUs(10);
}
unsigned char ReadOneBit_Speed(){
unsigned char dat = 0;
SetDQ(0); DelayUs(2);
SetDQ(1); DelayUs(3);
dat = DQ; DelayUs(10);
return(dat);
}
void WriteOneBit_Speed(unsigned char dat){
SetDQ(0); DelayUs(2);
SetDQ(dat); DelayUs(8);
SetDQ(1); DelayUs(5);
}
unsigned char ReadOneBit(){
if(speedup) return(ReadOneBit_Speed());
else return(ReadOneBit_Normal());
}
void WriteOneBit(unsigned char dat){
if(speedup) WriteOneBit_Speed(dat);
else WriteOneBit_Normal(dat);
}
unsigned char ReadOneChar()
{
unsigned char i=0;
unsigned char j=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
dat>>=1;
if(ReadOneBit())
dat|=0x80;
}
return(dat);
}
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
WriteOneBit(dat&0x01);
dat>>=1;
}
}
void WriteOne_resum(unsigned char cmd) {
Init_OW();
WriteOneChar(0xA5);
WriteOneChar(cmd);
}
void WriteOne_skrom(unsigned char cmd) {
Init_OW();
WriteOneChar(0xCC);
WriteOneChar(cmd);
}
void WriteOne_mhrom(unsigned char cmd) {
unsigned char j;
Init_OW();
WriteOneChar(0x55);
for ( j = 0; j < 8; j++)
WriteOneChar(rom_s[DQNum][j]);
WriteOneChar(cmd);
}
void Rom_Cmd(unsigned char cmd)
{
if (DQNum == 0) WriteOne_skrom(cmd);
else if(DQNum == 0xff) WriteOne_resum(cmd);
else WriteOne_mhrom(cmd);
}
void Cal_Crc8(unsigned char num) {
unsigned char i = 0;
unsigned char j = 0;
unsigned char temp = 0;
unsigned char romb = 0;
crc8 = 0;
for ( i = 0; i < num; i++) {
for (j = 0; j < 8; j++) {
romb = (dat[i]>>j) & 1;
temp = (crc8 & 1) ^ romb;
crc8 = (crc8 & 0xf7) + ((crc8 & 0x08) ^ (temp << 3));
crc8 = (crc8 & 0xef) + ((crc8 & 0x10) ^ (temp << 4));
crc8 = crc8 >> 1;
crc8 = (crc8 & 0x7f) + (temp << 7);
}
}
}
void Cal_Crc16_bit(unsigned char dat) {
unsigned short temp = 0;
unsigned char romb = 0;
romb = dat & 1;
temp = (crc16 & 1) ^ romb;
crc16 = (crc16 & 0xfffd) + ((crc16 & 0x0002) ^ (temp << 1));
crc16 = (crc16 & 0xbfff) + ((crc16 & 0x4000) ^ (temp << 14));
crc16 = crc16 >> 1;
crc16 = (crc16 & 0x7fff) + (temp << 15);
}
void Cal_Crc16(unsigned char num) {
unsigned char i = 0;
unsigned char j = 0;
unsigned char temp = 0;
unsigned char romb = 0;
crc16 = 0;
for ( i = 0; i < num; i++)
for (j = 0; j < 8; j++)
Cal_Crc16_bit(dat[i]>>j);
crc16 = ~crc16;
}
void ReadRom(){
unsigned char i;
Init_OW();
WriteOneChar(0x33);
for (i = 0; i < 8; i++) rom_temp[i] = ReadOneChar();
for (i = 0; i < 8; i++) UartOutB(rom_temp[i]);
}
void SpeedUp(unsigned char mode){
Init_OW();
WriteOneChar(0x3C);
speedup = mode;
Init_OW();
DelayMs(10);
}
unsigned char ReadSP(){
unsigned char i;
dat[0]=0xAA;
Rom_Cmd(dat[0]);
//T1T2
UartOutB( TA1 = dat[1]=ReadOneChar());
UartOutB( dat[2]=ReadOneChar());
//ES
UartOutB( ES = dat[3]=ReadOneChar());
//DATA
UartOutB(SD[0] = dat[4]=ReadOneChar());
UartOutB(SD[1] = dat[5]=ReadOneChar());
UartOutB(SD[2] = dat[6]=ReadOneChar());
UartOutB(SD[3] = dat[7]=ReadOneChar());
UartOutB(SD[4] = dat[8]=ReadOneChar());
UartOutB(SD[5] = dat[9]=ReadOneChar());
UartOutB(SD[6] = dat[10]=ReadOneChar());
UartOutB(SD[7] = dat[11]=ReadOneChar());
//CRC
crc16l=ReadOneChar();
crc16h=ReadOneChar();
CRC_Read = (crc16h << 8) + crc16l;
Cal_Crc16(12);
UartOutW(crc16);
UartOutW(CRC_Read);
UartOutB(dat[14]=ReadOneChar());
LCD_clear();
Set_LCDYXPixel(0,0); ToLCDS("ReadSP CRC: ");
if (crc16 == CRC_Read)
ToLCDS("OK!");
else
ToLCDS("FAIL!");
Set_LCDYXPixel(1,0); ToLCDS("CALC_CRC: "); ToLCDW(crc16);
Set_LCDYXPixel(2,0); ToLCDS("READ_CRC: "); ToLCDW(CRC_Read);
Set_LCDYXPixel(3,0); ToLCDS("D:");
for (i = 4; i< 12; i++) { ToLCDB(dat[i]); if (i % 2 ) ToLCDS(" ");}
return (crc16 != CRC_Read);
}
void TranslateWriteData(){
unsigned char i;
TA1 = UART_RX[2]&0xf8;
TA2 = 0x00;
for (i = 0; i < 8; i++)
SD[i] = UART_RX[i+3];
}
void WriteSPTA(){
dat[0] = 0x0F;
Rom_Cmd(dat[0]);
WriteOneChar(dat[1] = TA1);
WriteOneChar(dat[2] = TA2);
}
unsigned char WriteSP(){
dat[0] = 0x0F;
Rom_Cmd(dat[0]);
WriteOneChar(dat[1] = TA1);
WriteOneChar(dat[2] = TA2);
WriteOneChar(dat[3] = SD[0]);
WriteOneChar(dat[4] = SD[1]);
WriteOneChar(dat[5] = SD[2]);
WriteOneChar(dat[6] = SD[3]);
WriteOneChar(dat[7] = SD[4]);
WriteOneChar(dat[8] = SD[5]);
WriteOneChar(dat[9] = SD[6]);
WriteOneChar(dat[10]= SD[7]);
//CRC
crc16l=ReadOneChar();
crc16h=ReadOneChar();
CRC_Read = (crc16h << 8) + crc16l;
Cal_Crc16(11);
UartOutW(crc16);
UartOutW(CRC_Read);
UartOutB(dat[13]=ReadOneChar());
// LCD_clear();
Set_LCDYXPixel(0,0); ToLCDS("WR_SP CRC: ");
Set_LCDYXPixel(1,0); ToLCDS("CALC_CRC: "); ToLCDW(crc16);
Set_LCDYXPixel(2,0); ToLCDS("READ_CRC: "); ToLCDW(CRC_Read);
if (crc16 == CRC_Read){
ToLCDS("OK!"); return 0;}
else{
ToLCDS("FAIL!");return 1; }
}
unsigned char WR_SP(){
unsigned char i;
WriteSP();
//Read Verify
Set_LCDYXPixel(4,0); ToLCDS("Read_Verify: ");
dat[0] = 0xaa;
Rom_Cmd(dat[0]);
//T1T2
dat[1] = ReadOneChar();
dat[2] = ReadOneChar();
//ES
dat[3] = ES = ReadOneChar();
//DATA
for ( i = 4; i < 12; i++) dat[i] = ReadOneChar();
if (TA1 == 0x88) SD[3] = dat[7];
else if (TA1 == 0x98){SD[3] = 0xAA; for (i = 4; i < 8; i++) SD[i] = 0;}
if ((dat[1] == TA1) &&
(dat[2] == TA2) &&
(~(dat[3]&0xa0)) &&
(dat[4] == SD[0]) &&
(dat[5] == SD[1]) &&
(dat[6] == SD[2]) &&
(dat[7] == SD[3]) &&
(dat[8] == SD[4]) &&
(dat[9] == SD[5]) &&
(dat[10]== SD[6]) &&
(dat[11]== SD[7]) ) {
ToLCDS("OK!");
//CRC
crc16l=ReadOneChar();
crc16h=ReadOneChar();
CRC_Read = (crc16h << 8) + crc16l;
Cal_Crc16(12);
Set_LCDYXPixel(5,0); ToLCDS("CRC: ");
for ( i = 0; i < 8; i++) UartOutB(SD[i] = dat[i+4]);
if (crc16 == CRC_Read) {
ToLCDS("OK!");
Set_LCDYXPixel(6,0); ToLCDS("AA: ");ToLCDB(dat[11]&0x80);
Set_LCDYXPixel(7,0); ToLCDS("PF: ");ToLCDB(dat[11]&0x20);
return 0;
}
else {
ToLCDS("FAIL!");
Set_LCDYXPixel(6,0); ToLCDS("CALC_CRC: "); ToLCDW(crc16);
Set_LCDYXPixel(7,0); ToLCDS("READ_CRC: "); ToLCDW(CRC_Read);
return 1;
}
}
else {
ToLCDS("FAIL!");
Set_LCDYXPixel(5,0); ToLCDS("ADDR:");
ToLCDB(TA1);ToLCDB(TA2);ToLCDS(" "); ToLCDB(dat[1]);ToLCDB(dat[2]); ToLCDS(" ");ToLCDB(ES);
Set_LCDYXPixel(6,0); ToLCDS("W:");
for ( i = 0; i < 8; i++) ToLCDB(SD[i]);
Set_LCDYXPixel(7,0); ToLCDS("R:");
for ( i = 4; i < 12; i++) ToLCDB(dat[i]);
return 2;
}
}
void ReadMemory(){
unsigned char i;
Rom_Cmd(0xF0);
WriteOneChar(TA1);
WriteOneChar(TA2);
for ( i = 0; i < Data_Num; i++) {
PP[i] = ReadOneChar();
}
}
unsigned char CopySP2431(){
unsigned char CP;
Rom_Cmd(0x55);
WriteOneChar(TA1);
WriteOneChar(TA2);
WriteOneChar(ES);
DelayMs(15);
CP = ReadOneChar();
UartOutB(CP);
return CP;
}
void InitFirstSearch() {
unsigned char i = 0;
TotalAlarmNum = 0;
LastAlarmNum = 0;
LastRomNum = 0;
LastBranch = 0;
LastFamilyBranch = 0;
NoMoreDevice = 0;
for ( i = 0; i < 8; i++)
rom_s[0][i] = 0;
}
unsigned char SearchRom(unsigned char cmd) {
unsigned char i = 0;
unsigned char bitNum = 1;
unsigned char thisBit = 1;
unsigned char bit_0 = 1;
unsigned char bit_1 = 1;
unsigned char LastZero = 0;
unsigned char LastNum = 0;
if (NoMoreDevice) return 0xf8;
if (Init_OW()) return 0xf4;
WriteOneChar(cmd);
for ( i = 0; i < 8; i++)
rom_temp[i] = 0;
if (cmd == 0xf0) LastNum = LastRomNum;
else LastNum = LastAlarmNum;
// 2
while (bitNum < 65) {
// 3
bit_0 = ReadOneBit();
bit_1 = ReadOneBit();
// 4
if ( (bit_0 == 1) && (bit_1 == 1) ) return 0xf2;
// 5
else if ( (bit_0 == 0) && (bit_1 == 0) ) {
if (bitNum < LastBranch) {
thisBit = (rom_s[LastNum][(bitNum-1)/8] >> ((bitNum-1)%8)) & 1;
if (!thisBit)
LastZero = bitNum;
}
else if (bitNum > LastBranch) {
thisBit = 0;
LastZero = bitNum;
}
else if (bitNum == LastBranch) {
thisBit = 1;
}
}
// 6
else if (bit_1 == 0) thisBit = 1;
else thisBit = 0;
// 7
rom_temp[(bitNum-1)/8] |= thisBit << ((bitNum-1)%8);
WriteOneBit(thisBit);
// 8
bitNum ++;
}
// 9
LastBranch = LastZero;
if (!LastZero) NoMoreDevice = 1;
// 10
if (cmd == 0xf0) {
LastRomNum++;
for ( i = 0; i < 8; i++)
rom_s[LastRomNum][i] = rom_temp[i];
}
else if (cmd == 0xec){
for ( i = 0; i < 8; ) {
if (rom_temp[i] != rom_s[LastNum][i]) LastNum++;
else i++;
if (LastNum > LastRomNum) return 0xff;
}
TotalAlarmNum++;
LastAlarmNum = LastNum;
}
return 0;
}
void SearchRom_All(unsigned char Output_rom) {
unsigned char i = 0;
unsigned char j = 0;
unsigned char error;
LCD_clear();
Set_LCDYXPixel(0, 0); ToLCDS("SerhA!");
InitFirstSearch();
DQ = 1;
DelayMs(3);
if (DQ == 0){
Set_LCDYXPixel(2, 0); ToLCDS("!DQ_Line Error!");
return;
}
for (i = 0; i < (rom_total + rom_ctl); i++) {
if(error=SearchRom(0xf0)) break;
}
}