ds18b20.c~

来自「F:程序DS18B20.rar索读取温度」· C~ 代码 · 共 73 行

C~
73
字号
#include <mega16.h>

// 1 Wire Bus functions
#asm
   .equ __w1_port=0x12 ;PORTD
   .equ __w1_bit=3
#endasm
#include <1wire.h>

// DS1820 Temperature Sensor functions
#include <ds18b20.h>
#include <delay.h> 
#include<ds18b20_add.h>

// maximum number of DS1820 devices
// connected to the 1 Wire bus
#define MAX_DS1820 8
// number of DS1820 devices
// connected to the 1 Wire bus
unsigned char ds1820_devices;
// DS1820 devices ROM code storage area,
// 9 bytes are used for each device
// (see the w1_search function description in the help)
unsigned char ds1820_rom_codes[MAX_DS1820][9];

// Standard Input/Output functions
#include <stdio.h>

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here

}

// Declare your global variables here

void main(void)
{
// Declare your local variables here
unsigned char i,j;
int temp; 
 

// Global enable interrupts
#asm("sei")
 init_M16();
 
 for (i=0;i<ds1820_devices;)
    if (!ds18b20_init(&ds1820_rom_codes[i++][0],20,30,DS18B20_12BIT_RES))
       {
       printf("Init error for\ndevice #%u",i);
       while (1); /* stop here if init error */
       };
while (1)
      {
      // Place your code here
       for (i=0;i<ds1820_devices;)
          {
          temp=ds18b20_temperature(&ds1820_rom_codes[i][0])*10;
          j='+';
          if (temp<0)
             {
             j='-';
             temp=-temp;
             };
          printf("t%u=%u.%u^C",++i,temp/10,temp%10);
          delay_ms(1800);
          };
      };

}

⌨️ 快捷键说明

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