⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds2482-100.c

📁 ds2482-100是IIC协议转1-wire协议的接口芯片。本程序包利用ds2482-100操作温度传感器ds18s20。注意:ds18s20初始化后温度值为85摄氏度
💻 C
字号:
#include "iic_core.h"
#include "ds1820.h"
#include "hs_lcd.h"
#include "main.h"

#define DS2482 0x30   //DS2482-100  IIC器件地址
#define AT24C02 0xA0

//DS2482-100命令字
#define Device_Reset 0xf0
#define Set_Read_Pointer 0xe1      //参数: F0(状态),E1(数据),C3(配置)
#define Write_Configuration 0xd2   //参数:configuration byte
#define One_Wire_Reset  0xb4
#define One_Wire_Write_Byte 0xa5   //参数: Data Byte
#define One_Wire_Read_Byte  0x96
#define One_Wire_Triplet  0x78
//DS18S20命令字
#define Read_Rom 0x33
#define Match_Rom 0x55
#define Skip_Rom 0xcc
#define Search_Rom 0xf0
#define Alarm_Search 0xec
#define Temp_Change 0x44
#define Read_Temp 0xbe

tByte temp[5];
tByte temp2[8];
tWord temperature;
tByte String[9];   //用于lcd显示

void delay(tLong Delay)
{
  while(Delay--)
  {
    tByte j=10;
    while(j--);
  }
}

//return 0: 1 wire not busy
//return 1: 1 wire busy
bit Check_Busy(void)
{
   tByte i;
   temp[0]=Set_Read_Pointer;
   temp[1]=0xf0;
   I2C_Send_String2(DS2482,temp,2);
   I2C_Recv_String2(DS2482,temp,1);
   //处理数据,检查1 wire busy 位
   return (temp[0]&0x01);
}


void Reset_DS2482(void)
{
  temp[0]=Device_Reset;
  I2C_Send_String2(DS2482,temp,1);
}

void Reset_1wire(void)
{
  temp[0]=One_Wire_Reset;
  I2C_Send_String2(DS2482,temp,1);
}


void Write_1wire_Command(tByte Command)
{
  temp[0]=One_Wire_Write_Byte;
  temp[1]=Command;
  I2C_Send_String2(DS2482,temp,2);
}

//入口参数:将获得的DS18s20的8字节序列id
//存储到AT24C02的(num-1)*8~(num-1)*8+7单元中
void Get_1Wire_ID(tByte num)
{
   tByte i;
   Reset_1wire();
   delay(10);
   while(Check_Busy()); //检查1线器件忙否

   Write_1wire_Command(Read_Rom);

   for(i=0;i<8;i++)
   {
     //从1wire器件读取数据
     temp[0]=One_Wire_Read_Byte;
     I2C_Send_String2(DS2482,temp,1);
     delay(5);
     //设置指针
     temp[0]=Set_Read_Pointer;
     temp[1]=0xe1;
     I2C_Send_String2(DS2482,temp,2);
     delay(10);

     I2C_Recv_String2(DS2482,temp,1);//将data register中数据读出
     I2C_Send_String(AT24C02,(num-1)*8+i,temp,1); //同时将数据存到AT24C02中
   }
}


//入口参数:总线上挂的第num(>=1)个ds18s20
tWord Read_Temperature(tByte num)
{
   tByte i;
   Reset_1wire();
   delay(20);
   Write_1wire_Command(Skip_Rom);    //跳过ROM
   while(Check_Busy());
   Write_1wire_Command(Temp_Change); //启动所有温度转换
   while(Check_Busy());
   delay(100); //等待转换完毕

   //从AT24C02中取出ds18s20序列号至temp2[]中.
   I2C_Recv_String(AT24C02,(num-1)*8,temp2,8);

   Reset_1wire();
   delay(20);

   Write_1wire_Command(Match_Rom);  //匹配ROM
   while(Check_Busy());
   delay(5);
   for(i=0;i<8;i++)
   {
     temp[0]=One_Wire_Write_Byte;
     temp[1]=temp2[i];
     I2C_Send_String2(DS2482,temp,2);
     while(Check_Busy());
     delay(5);
    }

   Write_1wire_Command(Read_Temp); //读取温度
   while(Check_Busy());
   delay(5);
   //读两字节温度
   //低
   temp[0]=One_Wire_Read_Byte;
   I2C_Send_String2(DS2482,temp,1);
   delay(5);
   while(Check_Busy());
   temp[0]=Set_Read_Pointer;       //设置DS2482指针
   temp[1]=0xe1;
   I2C_Send_String2(DS2482,temp,2);

   I2C_Recv_String2(DS2482,temp,1);//将data register中数据读出
   temp2[0]=temp[0];
   //高
   temp[0]=One_Wire_Read_Byte;
   I2C_Send_String2(DS2482,temp,1);
   while(Check_Busy());
   delay(5);
   temp[0]=Set_Read_Pointer;       //设置DS2482指针
   temp[1]=0xe1;
   I2C_Send_String2(DS2482,temp,2);

   I2C_Recv_String2(DS2482,temp,1);//将data register中数据读出
   temp2[1]=temp[0];

   Reset_1wire();
   return(temp2[1]*256+temp2[0]);
}


//在x,y(>=1)坐标初显示温度字符串
void Show_Temperature(tByte x,tByte y)
{
  temperature= Read_Temperature(1);
  if(temperature>0x8000)
  {
    temperature=65535-temperature+1;
    String[0]='-';
  }
  else String[0]='+';

  if(temperature&0x0001) String[5]='5';//半摄氏度
  else String[5]='0';

  temperature=(temperature&0xff)/2;
  String[1]=(temperature/100)+0x30;
  String[2]=((temperature/10)%10)+0x30;
  String[3]=(temperature%10)+0x30;
  String[4]='.';
  String[6]=' ';
  String[7]='C';
  String[8]='\n';
  LCD_Text_Display(x,y,String);
  LCD_Graphic_Display((x-1)*8+1,y+6,0x06);
  LCD_Graphic_Display((x-1)*8+2,y+6,0x09);
  LCD_Graphic_Display((x-1)*8+3,y+6,0x09);
  LCD_Graphic_Display((x-1)*8+4,y+6,0x06);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -