📄 1820new.c
字号:
#include "reg52.h"
#include "intrins.h"
#define uchar unsigned char
#define uint unsigned int
#define Set_Bit(Address,Bit) ((Address) |= (1 << Bit))
sbit dq = P1^0; //define 18b20 data port
sbit En_485T = P1^1; //enable 485 T
sbit En_485R = P0^2; //enable 485 R
bit flag;
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//定义与IAP有关的特殊功能寄存器
sfr ISP_DATA = 0xe2;
sfr ISP_ADDRH = 0xe3;
sfr ISP_ADDRL = 0xe4;
sfr ISP_CMD = 0xe5;
sfr ISP_TRIG = 0xe6;
sfr ISP_CONTR = 0xe7;
/* 定义命令 */
#define READ_AP_and_Data_Memory_Command 0x01 /* 字节读数据存储区 */
#define PROGRAM_AP_and_Data_Memory_Command 0x02 /* 字节编程数据存储区 */
#define SECTOR_ERASE_AP_and_Data_Memory_Command 0x03 /* 扇区擦除数据存储区 */
/* 定义常量 */
#define ERROR 0
#define OK 1
#define WAIT_TIME 0x02
#define DELAY_CONST 60000
#define DEBUG_AP_Memory_Begin_Sector_addr 0x8000
#define DEBUG_AP_Memory_End_Sector_addr 0x8200
#define DEBUG_Data_Memory_Begin_Sector_addr 0x8400
#define USED_BYTE_QTY_IN_ONE_SECTOR 128
void ISP_IAP_enable(void);
void ISP_IAP_disable(void);
uchar temp_buff[9]; //存储读取的字节,read scratchpad为9字节,read rom ID为8字节
uchar aa,bb,iii;
uint tt;
uchar xdata data_temp[320];
uchar xdata data_temp1[320];
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//ROM SEARCH DEFINE
unsigned char xdata code1[40][8]; // 传感器特征码
uchar Available;
// 传感器个数
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//comm command define
uchar address_num_485;
uchar addr_temper_flag=0; // 传感器序列码检测标志
uchar addr_temper_flag1=0; // 传感器温度检测标志
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
/************************************************************
*Function:延时处理
*parameter:
*Return:
*Modify:
*************************************************************/
void TempDelay (uchar us)
{
while(us--);
}
/************************************************************
*Function:18B20初始化
*parameter:
*Return:
*Modify:
*************************************************************/
void Init18b20 (void)
{
dq=1;
_nop_();
dq=0;
TempDelay(86); //delay 530 uS//80
_nop_();
dq=1;
TempDelay(7); //delay 100 uS//14
_nop_();
_nop_();
_nop_();
if(dq==0)
flag = 1; //detect 1820 success!
else
flag = 0; //detect 1820 fail!
TempDelay(30);
_nop_();
_nop_();
dq = 1;
}
/************************************************************
*Function:向18B20写入一个字节
*parameter:
*Return:
*Modify:
*************************************************************/
void WriteByte(uchar wr) //单字节写入
{
uchar i;
for (i=0;i<8;i++)
{
dq = 0;
_nop_();
dq=wr&0x01;
TempDelay(5); //delay 45 uS //5
_nop_();
_nop_();
dq=1;
wr >>= 1;
}
}
/************************************************************
*Function:向18B20写入一位
*parameter:
*Return:
*Modify:
*************************************************************/
void WriteBit (uchar wr) //位操作
{
dq = 0;
_nop_();
dq=wr&0x01;
TempDelay(5); //delay 45 uS //5
_nop_();
_nop_();
dq=1;
}
/*void WriteByte(uchar wr)
{
uchar i=0;
for(i=0;i<8;i++)
{
WriteBit(wr);
wr>>=1;
}
}*/
/************************************************************
*Function:读18B20的一个字节
*parameter:
*Return:
*Modify:
*************************************************************/
uchar ReadByte(void) //读取单字节
{
uchar i,u=0;
for(i=0;i<8;i++)
{
dq = 0;
u >>= 1;
dq = 1;
if(dq==1)
u |= 0x80;
TempDelay (4);
_nop_();
}
return(u);
}
/************************************************************
*Function:读18B20的一位
*parameter:
*Return:
*Modify:
*************************************************************/
uchar ReadBit (void) //读取一位
{
uchar u=0;
dq = 0;
_nop_();
dq = 1;
if(dq==1)
u |= 0x80;
TempDelay (4);
_nop_();
if(!u)
return 0;
else
return 1;
}
/*uchar ReadByte(void)
{
uchar i=0;
uchar Value=0;
for(i=0;i<8;i++)
{
if ( ReadBit () ) Set_Bit (Value, i);
}
return Value;
}*/
/************************************************************
*Function:读取温度高低位
*parameter:
*Return:
*Modify:
*************************************************************/
unsigned int ReadT(void)
{
uchar a=0;
uchar b=0;
uint t=0;
float tt=0;
Init18b20();
WriteByte(0xCC); // 跳过读序号列号的操作
WriteByte(0x44); // 启动温度转换
Init18b20();
WriteByte(0xCC); //跳过读序号列号的操作
WriteByte(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadByte();
b=ReadByte();
t=b;
t<<=8;
t=t|a;
return(t);
}
//void SearchRom(void)
void SearchRom()
{
unsigned int flag=0;
unsigned char i=0;
unsigned char j=0;
unsigned char k=0;
unsigned char a=0; // first read
unsigned char b=0; // complement read
unsigned char Lconf=0; // last conflict bit of last search
unsigned char Tconf=64; // last conflict bit of this search
unsigned char scode[64];
for(i=0;i<64;i++) // initialize search code
scode[i]=0x00;
while(Tconf != Lconf)
{
Lconf=Tconf;
Init18b20();
WriteByte(0xF0); // send SearchROM command
for(i=0;i<64;i++)
{
a=ReadBit();
b=ReadBit();
b |= a<<1;
code1[j][i/8] >>= 1;
switch(b)
{
case 0x00: // devices attached, conflict bits
{
if(i==Lconf)
{
WriteBit(1);
scode[i] = 0x01;
for(k=i+1;k<64;k++)
scode[k] = 0x00;
code1[j][i/8] |= 0x80;
}
else
{
if(i<Lconf)
{
WriteBit(scode[i]);
code1[j][i/8] |= scode[i]<<7;
if(scode[i]==0x00)
Tconf=i;
}
else // i>Lconf
{
WriteBit(0);
code1[j][i/8] |= 0x00;
Tconf=i;
}
}
break;
}
case 0x01: // all 0 in bits
{
WriteBit(0);
code1[j][i/8] |= 0x00;
break;
}
case 0x02: // all 1 in bits
{
WriteBit(1);
code1[j][i/8] |= 0x80;
break;
}
case 0x03: // no device
break;
}
}
j++;
}
if(b==0x03)
j=0;
Available = j;
}
/////////匹配ROM////////////////
void Send_MatchRom(void)
{
uchar i;
Init18b20();
WriteByte(0x55); //ROM match command
for(i=0;i<8;i++)
{
WriteByte(code1[iii][i]);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
/************************************************************
*Function:FLASH Erase
*parameter:
*Return:
*Modify:
*************************************************************/
/* 扇区擦除 */
uchar sector_erase(uint sector_addr)
{
uint get_sector_addr = 0;
get_sector_addr = (sector_addr & 0xfe00); // 1111,1110,0000,0000; 取扇区地址
ISP_ADDRH = (uchar)(get_sector_addr >> 8);
ISP_ADDRL = 0x00;
ISP_CMD = ISP_CMD & 0xf8; // 1111,1000
ISP_CMD = ISP_CMD | SECTOR_ERASE_AP_and_Data_Memory_Command;
ISP_IAP_enable();
ISP_TRIG = 0x46; /* 触发ISP_IAP命令 */
ISP_TRIG = 0xb9; /* 触发ISP_IAP命令 */
_nop_();
ISP_IAP_disable();
return OK;
}
/************************************************************
*Function:FLASH READ
*parameter:
*Return:
*Modify:
*************************************************************/
/* 字节读 */
uchar byte_read(uint byte_addr)
{
ISP_ADDRH = (uchar)(byte_addr >> 8);
ISP_ADDRL = (uchar)(byte_addr & 0x00ff);
ISP_CMD = ISP_CMD & 0xf8;
ISP_CMD = ISP_CMD | READ_AP_and_Data_Memory_Command;
ISP_IAP_enable();
ISP_TRIG = 0x46;
ISP_TRIG = 0xb9;
_nop_();
ISP_IAP_disable();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -