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

📄 xds1820.c

📁 this is source file mcs51 interface with ds1820 use keil
💻 C
字号:
/*  Filename     XDS1820.C
    Description  EXAMPLE PROGRAM FOR DS1820 (TEMPERATURE)
    Hardware     JAZZ-31 + DS1820 (DQ -> P1.0)
    Compiler     Keil CA51 v5.0
    Engineer	 Kriangsak B.
    Company      Sila Research Co.,Ltd. 

    Compile      Project ... XDS1820.C JAZZ31.A51
		 Option/BL51/Size ...  Code address = 8100 
*/

#include <reg51.h>
#include <absacc.h>
#include <ctype.h>
#include <intrins.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/********** I/O PORT **********/

#define SEGM   XBYTE [0xf800]
#define DIGIT  XBYTE [0xf801]
sbit TMDAT = P1^0;

/********** INT-RAM WORKING AREA **********/

unsigned char DISBUF[8];
unsigned char TEMBUF[2];

/********** FUNCTION **********/

void dmsec (unsigned int count) {      // mSec Delay 11.0592 Mhz 
    unsigned int i;		       // Keil v5.2 
    while (count) {
        i = 115; while (i>0) i--;
        count--;
    }
}

void tmreset (void) {                  // Reset TX 
    unsigned int i;
    TMDAT = 0;
    i = 103; while (i>0) i--;          // Approx 900 uS 
    TMDAT = 1;
    i = 4; while (i>0) i--;
}

void tmpre (void) {                    // Wait for Presence RX 
    unsigned int i;
    while (TMDAT);
    while (~TMDAT);
    i = 4; while (i>0) i--;
}

bit tmrbit (void) {                    // read one bit 
    unsigned int i;
    bit dat;
    TMDAT = 0; i++;
    TMDAT = 1; i++; i++;
    dat = TMDAT;
    i = 8; while (i>0) i--;            // Approx 65 uS 
    return (dat);
}

unsigned char tmrbyte (void) {         // read one byte 
    unsigned char i,j,dat;
    dat = 0;
    for (i=1;i<=8;i++) {
        j = tmrbit ();
        dat = (j << 7) | (dat >> 1);
    }
    return (dat);
}

void tmwbyte (unsigned char dat) {               // write one byte 
    unsigned int i;
    unsigned char j;
    bit testb;
    for (j=1;j<=8;j++) {
        testb = dat & 0x01;
        dat = dat >> 1;
        if (testb) {
            TMDAT = 0;                           // Write 1 
            i++; i++;                            // Approx 4 uS 
            TMDAT = 1;
            i = 8; while (i>0) i--;              // Approx 65 uS 
        }
        else {
            TMDAT = 0;                           // Write 0 
            i = 8; while (i>0) i--;              // Approx 65 uS 
            TMDAT = 1;
            i++; i++;                            // Approx 4 uS 
        }
    }
}

void tmstart (void) {                  // ds1820 start convert 
    tmreset ();
    tmpre ();
    dmsec (1);
    tmwbyte (0xcc);                    // skip rom 
    tmwbyte (0x44);                    // convert 
}

void tmrtemp (void) {                  // read temp 
    unsigned char a,b;
    TEMBUF[0] = 0;
    TEMBUF[1] = 0;
    tmreset ();
    tmpre ();
    dmsec (1);
    tmwbyte (0xcc);                    // skip rom 
    tmwbyte (0xbe);                    // convert 
    a = tmrbyte ();                    // LSB 
    b = tmrbyte ();                    // MSB 
    if (b==1) return;                  // don't care negative temp 
    TEMBUF[1] = a & 0x1;               // 0=x.0 1=x.5 
    a = a >> 1;                     
    TEMBUF[0] = a + 1;                 // adjust for thailand 
}

unsigned char code SEGTAB[16] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
                                 0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
                                            // standard segment code 
unsigned char htosx (unsigned char hex) {   // change hex to segment 
    return (SEGTAB[hex]);
}

void clear (void) {                    // clear DISBUF 
    unsigned char i;
    for (i=0;i<=7;i++) {
        DISBUF[i] = 0;
    }
}

void scand (void) {                    // scan display & get key 
    unsigned char i;               
    for (i=0;i<=7;i++) {
        DIGIT = (DIGIT & 0xf8) | i;
        SEGM = DISBUF[i];
        dmsec (1);
        SEGM = 0;
    }
}

void ctos (unsigned char dat) {        // change char to segment 
    DISBUF[3] = htosx (dat % 10); 
    dat = dat / 10;
    DISBUF[2] = htosx (dat % 10); 
}

/********** MAIN **********/

void main (void) {
    unsigned int i;
    clear ();                          // clear display
    DISBUF[2] = 0x40;
    DISBUF[3] = 0x40;
    DISBUF[4] = 0x40;
    DISBUF[5] = 0x63;                  // 'C
    DISBUF[6] = 0x39;
    while (1) {
        tmstart ();                    // ds1820 start convert
        i = 150;                       // scan display & delay approx 1 sec
        while (i>0) {
            scand ();
            i--;
        }
        tmrtemp ();                    // read temperature
        ctos (TEMBUF[0]);              // change to segment xx
        if (TEMBUF[1]==1)              // display .0 or .5 
            DISBUF[4] = 0x3f; 
        else 
            DISBUF[4] = 0x6d;
        DISBUF[3] = DISBUF[3] | 0x80;  // display dot xx.x
    }
}

⌨️ 快捷键说明

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