機(jī)電之家資源網(wǎng)
單片機(jī)首頁|單片機(jī)基礎(chǔ)|單片機(jī)應(yīng)用|單片機(jī)開發(fā)|單片機(jī)文案|軟件資料下載|音響制作|電路圖下載 |嵌入式開發(fā)
培訓(xùn)信息
贊助商
程序范例-ds18b20 c51源代碼
程序范例-ds18b20 c51源代碼
 更新時(shí)間:2008-7-27 16:00:38  點(diǎn)擊數(shù):4
【字體: 字體顏色
#include "reg51.h"#include "INTRINS.H"#include "LCD.h"#define CLR_RI  (RI=0)#define CLR_TI  (TI=0)                                unsigned char code ID[2][8]={ 0x28,0x1D,0x25,0x1D,0x00,0x00,0x00,0x80,                              0x28,0x0e,0x9e,0x1c,0x00,0x00,0x00,0x32};unsigned char currSensorNo=0;sbit  TMDAT   =  P1^7;sbit  RUN_LED =  P1^6;/*------------------------------------------------**------------------------------------------------*/void serial_initial(){     TMOD=0X20;     SCON=0X50;     PCON=0X00;     TL1=TH1=0XE8;     TR1=1;}/*------------------------------------------------**------------------------------------------------*/void  send(unsigned char count,unsigned char SEND_Buf[]){  unsigned char  i; for(i=0;i<count;i++){SBUF=SEND_Buf;while(!TI);CLR_TI;}}/*----------------------------------------------------*   delay N ms*----------------------------------------------------*/void Delay_ms (unsigned int Nms ){unsigned char i;while(Nms--)for(i=0; i<125; i++) ;}/*----------------------------------------------------*   delay N count*----------------------------------------------------*/void Delay_Count (unsigned char Count ){    while(Count>0) Count--;}/*----------------------------------------------------*   start Reset Pulse----------------------------------------------------*/void tmreset(void){    TMDAT=0;    Delay_Count(103);    TMDAT=1;    Delay_Count(4);}/*----------------------------------------------------*        ACK*----------------------------------------------------*/void tmpre(void){    while(TMDAT);    while(~TMDAT);    Delay_Count(4);}/*----------------------------------------------------*   Read a bit from 1820*----------------------------------------------------*/bit tmrbit(void){    int i=0;    bit dat;    TMDAT=0;i++;        TMDAT=1;i++;i++;    dat = TMDAT;        Delay_Count(8);    return dat;}/*----------------------------------------------------*   Read a byte from 1820*----------------------------------------------------*/unsigned char tmrbyte(void){unsigned char i,j,dat=0;    for(i=1;i<=8;i++)    {        j=tmrbit();        dat=(j<<7)|(dat>>1);    }    return dat;}/*----------------------------------------------------*   Read a byte from 1820*----------------------------------------------------*/void tmwbyte(unsigned char dat){    signed char   i=0;    unsigned char j;    bit testb;    for(j=1;j<=8;j++)    {        testb=dat & 0x01;        dat = dat>>1;        if(testb)        {            TMDAT=0;            i++;i++;            TMDAT=1;            Delay_Count(8);        }        else        {            TMDAT=0;            Delay_Count(8);            TMDAT=1;            i++;i++;        }    }}/*----------------------------------------------------*   send convert command to  1820*----------------------------------------------------*/void tmstart(void){unsigned char i;tmreset();    tmpre();    Delay_ms(1);/*  tmwbyte(0xcc);*/    tmwbyte(0x55);    for(i=0;i<8;i++)    tmwbyte(ID[currSensorNo]);    tmwbyte(0x44);}/*----------------------------------------------------*   Read tempreature from  1820*----------------------------------------------------*/unsigned int tmrtemp_all(void){unsigned char a,b;unsigned int y3;unsigned char i;tmreset();    tmpre();    Delay_ms(1);    /*tmwbyte(0xcc);*/    tmwbyte(0x55);    for(i=0;i<8;i++)    tmwbyte(ID[currSensorNo]);              tmwbyte(0xbe);    a = tmrbyte();    b = tmrbyte();    y3 = ((unsigned int)b) << 8;       return ((y3+a) & 0x7ff) ;}/*----------------------------------------------------*   Start Test tempreature, All**----------------------------------------------------*/void Display_AllTemp(void ){unsigned int last;unsigned char i,Dot;RUN_LED=0;Part=0;LED_DISPLAY();Delay_ms(1);tmstart();Delay_ms(800);last=tmrtemp_all();RUN_LED=1;Dot= (last & 0x0f)*6.25 ;Digit[0]= Dot%10;Digit[1]=Dot/10;last=(last>>4) & 0x7f ;if( (last == 0x7f) )   // erro{    for(i=0;i<5;i++)  Digit=16; }    else{for(i=2;i<5;i++){Digit = last %10;last = last/10;}if(Digit[4]==0)Digit[4]=17;Part=1; }Digit[5]=currSensorNo;         LED_DISPLAY();}/*----------------------------------------------------**----------------------------------------------------*//*  void  Read_Id()  {   unsigned char i,id[8];    tmreset();    tmpre();    Delay_ms(1);    tmwbyte(0x33);   for(i=0;i<8;i++)    id=tmrbyte();      send(8,id);}*//*----------------------------------------------------**    Main*----------------------------------------------------*/void main(void){unsigned char id[8]={1,2,3,4,5,6,7,8};serial_initial();send(8,id);    for(;;)    {RUN_LED=!RUN_LED;/*    Read_Id();*/TX_C =!TX_C ;currSensorNo=(currSensorNo==1)?0:1;Display_AllTemp();      /*Change();*/send(6,Digit);Delay_ms(1000);         _nop_();   }}
  • 上一篇: 51的4乘4鍵盤鎖液晶時(shí)鐘程序
  • 下一篇: 24c02讀寫程序絕對(duì)好用的
  • 發(fā)表評(píng)論   告訴好友   打印此文  收藏此頁  關(guān)閉窗口  返回頂部
    熱點(diǎn)文章
     
    推薦文章
     
    相關(guān)文章
    網(wǎng)友評(píng)論:(只顯示最新5條。)
    關(guān)于我們 | 聯(lián)系我們 | 廣告合作 | 付款方式 | 使用幫助 | 機(jī)電之家 | 會(huì)員助手 | 免費(fèi)鏈接

    點(diǎn)擊這里給我發(fā)消息66821730(技術(shù)支持)點(diǎn)擊這里給我發(fā)消息66821730(廣告投放) 點(diǎn)擊這里給我發(fā)消息41031197(編輯) 點(diǎn)擊這里給我發(fā)消息58733127(審核)
    本站提供的機(jī)電設(shè)備,機(jī)電供求等信息由機(jī)電企業(yè)自行提供,該企業(yè)負(fù)責(zé)信息內(nèi)容的真實(shí)性、準(zhǔn)確性和合法性。
    機(jī)電之家對(duì)此不承擔(dān)任何保證責(zé)任,有侵犯您利益的地方請(qǐng)聯(lián)系機(jī)電之家,機(jī)電之家將及時(shí)作出處理。
    Copyright 2007 機(jī)電之家 Inc All Rights Reserved.機(jī)電之家-由機(jī)電一體化網(wǎng)更名-聲明
    電話:0571-87774297 傳真:0571-87774298
    杭州濱興科技有限公司提供技術(shù)支持

    主辦:杭州市高新區(qū)(濱江)機(jī)電一體化學(xué)會(huì)
    中國行業(yè)電子商務(wù)100強(qiáng)網(wǎng)站

    網(wǎng)站經(jīng)營許可證:浙B2-20080178-1