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

📄 ds1820.c

📁 Pic Nic 16f877/18f452控制 rtl8019as 原代码
💻 C
字号:
/*
 * ds1820.c
 *
 * Interface with the One Wire Sensor (DS1820)
 *
 */

//Redundand checksum computation
int calc_crc(int *dados, int quantidade) {
   int shift_reg=0, data_bit, sr_lsb, fb_bit, i, j;

   for (i=0; i<quantidade; i++) {
      for(j=0; j<8; j++) {
         data_bit = (dados[i]>>j)&0x01;
         sr_lsb = shift_reg & 0x01;
         fb_bit = (data_bit ^ sr_lsb) & 0x01;
         shift_reg = shift_reg >> 1;
         if (fb_bit) {
            shift_reg = shift_reg ^ 0x8c;
         }
      }
   }
   return(shift_reg);
}

//Function that returns the read temperature
float getTemp(void) {

   int buffer[9], conta;
   float temp;

   reset_1w ();

	escreve_byte_1w (0xcc); 	// Skip ROM
	escreve_byte_1w (0x44); 	// Send a command to start temperature conversion
   delay_ms(200);
	reset_1w ();			      // Reset Device

		escreve_byte_1w (0xcc);	// Skip ROM
		escreve_byte_1w (0xbe); // Send a command to read the scratch memory - where the resultant temperature is

		//Get 9 byte from the ic sensor
		for (conta = 0; conta<9; conta++)
			buffer[conta]=le_byte_1w ();

      #ifdef DEBUG
   		printf ("Temp LSB = %u\r\n",buffer[0]);
   		printf ("Temp MSB = %u\r\n",buffer[1]);

   		printf ("TH = %u\r\n",buffer[2]);
   		printf ("TL = %u\r\n",buffer[3]);

   		printf ("Remain Count = %u\r\n",buffer[6]);
   		printf ("Cout per celcius degree = %u\r\n",buffer[7]);
   		printf ("Read CRC = %u\r\n",buffer[8]);
   		printf ("Computed CRC = %u\r\n", calc_crc (buffer,8));
      #endif

//		reset_1w ();

		escreve_byte_1w (0xcc);
		escreve_byte_1w (0xb4);

      #ifdef DEBUG
   		if (le_bit_1w())
            printf ("Powered Mode\r\n");
         else
            printf ("Parasite Mode\r\n");

      printf ("Temperatura = %Ld",((long)(buffer[1]<<8) + buffer[0])>>1);
		if (bit_test(buffer[0],0))
         printf (".5");
		printf (" degress (less precision)\r\n");
      #endif

      temp = (long) (buffer[1]<<8) + buffer[0];
		temp = (temp / 2) - 0.25 + (float) ((buffer[7]-buffer[6])/buffer[7]);

      #ifdef DEBUG
   		printf ("Temp = %3.2f graus celsius\r\n", temp);
      #endif

      return temp;
}

⌨️ 快捷键说明

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