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

📄 ds1820drv.c

📁 dallas单总线温度传感器驱动程序
💻 C
📖 第 1 页 / 共 2 页
字号:
    DS1820_Delay(300);  //about delay 750 us
    
    _EINT();
    WDTCTL = WDT_ARST_1000;    
       
    //读取数据位线, 60--240us
    (*PXDIR) &= ~DataBit;
    for(chRslt = 0; chRslt < 100; chRslt++)
    {
        if((*PXIN) & DataBit)//在线
        {
            
        }
        else
        {
            chValue = 0;
        }   
    }
    DS1820_Delay(200);  //about delay 500 us    

    return chValue;
}

//功能: 对某个通道上所有温度传感器写寄存器值,
//参数: chMCUPort : 通道所在MCU上的端口号
//      DataBit : 数据位线
//返回: 无
void DS1820_Write_Reg_MultiChannel(unsigned char chMCUPort, unsigned int DataBit)
{
     //要判断复位成功
     DS1820_Reset_MultiChannel(chMCUPort, DataBit);
    
     //写命令
     DS1820_Write_Byte_MultiChannel(chMCUPort, DataBit, 0xCC);//skip rom 针对所有通道!
     DS1820_Write_Byte_MultiChannel(chMCUPort, DataBit, 0x4E);//write scratchpad  
     //写数据
     DS1820_Write_Byte_MultiChannel(chMCUPort, DataBit, Mark_FF);//Hign address locations 2 to 4,but don't wtite 4
     DS1820_Write_Byte_MultiChannel(chMCUPort, DataBit, Mark_00);//Low
}

//功能: 某一个通道开始温度采样转换
//参数: chMCUPort : 通道所在MCU上的端口号
//      DataBit : 数据位线
//返回: 无
void DS1820_StartConvert_MultiChannel(unsigned char chMCUPort, unsigned int DataBit)
{   
     //要判断复位成功         
    DS1820_Reset_MultiChannel(chMCUPort, DataBit);
    
    DS1820_Write_Byte_MultiChannel(chMCUPort, DataBit, 0xCC);//skip rom 针对所有通道!
    DS1820_Write_Byte_MultiChannel(chMCUPort, DataBit, 0x44);//convert T 最好等待 750 毫秒
}

//读 板上温度  
unsigned char DS1820_Read_Temperature_Board(unsigned char chMCUPort, unsigned int DataBit)
{
    //要判断复位成功
    unsigned char  i;
    unsigned char  chTempData[4];
    DS1820_Reset_MultiChannel(chMCUPort, DataBit);
    DS1820_Write_Byte_MultiChannel(chMCUPort, DataBit, 0xCC);//只有一个 广播
    DS1820_Write_Byte_MultiChannel(chMCUPort, DataBit, 0xBE);//Read Scratchpad 
    for (i = 0; i < 4; i++)//读 4 个 即可!
    {
        chTempData[i] = DS1820_Read_Byte_MultiChannel(chMCUPort, DataBit);
    }
    if ( (chTempData[2] == Mark_FF) && (chTempData[3] == Mark_00) ) //如果寄存器写读失败,认为温度传感器故障
    {
        if((chTempData[1] & 0xF8) == 0xF8)   //温度为零下
        {
            chTempData[1] = ~chTempData[1];
            chTempData[0] = ~chTempData[0];
            i = ((chTempData[1] * 256 + chTempData[0] + 1) / 8) * 0.5;   
            if(i < 60)
            {
                i = 60 - i;
            }
            else
            {
                i = 1;
            }
        }
        else   //温度为正
        {
            i = 60 + ((chTempData[1] * 256 + chTempData[0]) / 8) * 0.5;
        }
    }
    else
    {
        i = 255;    
    }
    return i;
}


//功能: 在通道上读取温度传感器的ID, 一般要总线上只接了一个温度传感器
//参数: chMCUPort : 通道所在MCU上的端口号
//      DataBit : 数据位线
//      pDS1820ID : 存放读取到的ID
//返回: 无
void DS1820_Read_ID_MultiChannel(unsigned char chMCUPort, unsigned char DataBit, unsigned char *pDS1820ID)
{
    unsigned char i;

     //要判断复位成功       
    DS1820_Reset_MultiChannel(chMCUPort, DataBit);
    DS1820_Write_Byte_MultiChannel(chMCUPort, DataBit, 0x33);//假定只有一个从机--读 8 位 ID 号  
    for (i = 0; i < 8; i++)
    {
        pDS1820ID[i] = DS1820_Read_Byte_MultiChannel(chMCUPort, DataBit);
    }
}



////////////////////////////////////////////////////门禁系统部分//////////////////////////////////////////////////////////////////////
//写 电子纽扣的信息
void Write_DoorID(unsigned char *chP)//固定 四 位
{
        unsigned char i;
        unsigned char chData[8];
        //先不判断
        DS1820_Reset_MultiChannel(DoorChannelPort,DoorChannelDataBit);
        //写命令
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0xCC);//skip rom 针对所有通道!
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0x5A);//Read Subkey
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0x00);//Read first Subkey address
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0xFF);//Read first Subkey address 的补码
        //读8 Byte subkey
        for (i = 0; i < 8; i++)
        {
            chData[i] = DS1820_Read_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit);
        }
        //写 8 Byte old subkey
        for (i = 0; i < 8; i++)
        {
            DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit,chData[i]);//
        }
        //写 8 Byte new subkey
        for (i = 0; i < 4; i++)
        {
            DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, *(chP + i));//
        }
        for (i = 0; i < 4; i++)
        {
            DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, *(chP + i));//
        }
        //写 8 Byte passwore
        for (i = 0; i < 8; i++)
        {
            DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, *(unsigned char *)(Door_Password_Info + i));//
        }
        //马上写信息
        //先不判断
        DS1820_Reset_MultiChannel(DoorChannelPort,DoorChannelDataBit);
        //写命令
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0xCC);//skip rom 针对所有通道!
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0x99);//Read Subkey
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0x10);//Read first Subkey address
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0xEF);//Read first Subkey address 的补码
        //读8 Byte subkey
        for (i = 0; i < 8; i++)
        {
            chData[i] = DS1820_Read_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit);
        }
        //写 8 Byte passwore
        for (i = 0; i < 8; i++)
        {
            DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, *(unsigned char *)(Door_Password_Info + i));//
        }
        //写8 Byte message
        for (i = 0; i < 4; i++)
        {
            DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, *(unsigned char *)(Door_Password_Info + i + 4));//
        }
        for (i = 0; i < 4; i++)
        {
            DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, *(chP + i));//
        }
}
//读 电子纽扣的信息
unsigned char Read_DoorID(void)
{
    {
        unsigned char i;
        unsigned char chData[8],chIDData[8];
        //先不判断
        DS1820_Reset_MultiChannel(DoorChannelPort,DoorChannelDataBit);
        //写命令
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0xCC);//skip rom 针对所有通道!
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0x66);//Read Subkey
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0x10);//Read first Subkey address
        DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, 0xEF);//Read first Subkey address 的补码
        //读8 Byte subkey
        for (i = 0; i < 8; i++)
        {
            chIDData[i] = DS1820_Read_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit);
        }
        //写 8 Byte passwore
        for (i = 0; i < 8; i++)
        {
            DS1820_Write_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit, *(unsigned char *)(Door_Password_Info + i));//
        }
        //读8 Byte message
        for (i = 0; i < 8; i++)
        {
            chData[i] = DS1820_Read_Byte_MultiChannel(DoorChannelPort, DoorChannelDataBit);
        }
        {
            unsigned char chShowReadID[] = "ID:xxxxxxxx M:xxxxxxxx\n\r";
            unsigned char chI;
            for(chI = 0; chI < 8; chI++)
            {
                chShowReadID[chI + 3] = chIDData[chI];
            }
            for(chI = 0; chI < 8; chI++)
            {
                chShowReadID[chI + 14] = chData[chI];
            }
            InsertDebugSendBuf((unsigned char *)chShowReadID, sizeof(chShowReadID) - 1);    
        }
        return 1;
    }
}

⌨️ 快捷键说明

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