Advertisement
Guest User

Untitled

a guest
Jul 20th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.02 KB | None | 0 0
  1. /*
  2.    Plik ds1820.c
  3. */
  4.  
  5. #include <avr/io.h>
  6. #include <util/delay.h>
  7. #include "ds1820.h"
  8.  
  9.  
  10.  
  11. unsigned char ds1820_ConvertT(void)
  12. {
  13.   if (!OneWireReset()) return 0;
  14.  
  15.   OneWireWriteByte(0xcc); // SKIP ROM
  16.   OneWireWriteByte(0x44); // CONVERT T
  17.  
  18.   return -1;
  19. }
  20.  
  21. /***********************************************************/
  22.  
  23. int ds1820_Read(unsigned char scratchpad[])
  24. {
  25.   unsigned char i;    
  26.  
  27.   if (!OneWireReset()) return 0;
  28.  
  29.   OneWireWriteByte(0xcc); // SKIP ROM
  30.   OneWireWriteByte(0xbe); // READ SCRATCHPAD
  31.  
  32.   for(i=0; i<9; i++) scratchpad[i] = OneWireReadByte();
  33.  
  34.   return 1;
  35. }
  36.  
  37. /**********************************************************/
  38.  
  39. void OneWireStrong(char s)
  40. {
  41.   if(s)
  42.   {
  43.      SET_ONEWIRE_PORT;
  44.      SET_OUT_ONEWIRE_DDR;
  45.   }
  46.   else
  47.   {
  48.      SET_IN_ONEWIRE_DDR;
  49.   }
  50. }
  51.  
  52. /**********************************************************/
  53.  
  54. unsigned char OneWireReset()
  55. {
  56.   CLR_ONEWIRE_PORT;
  57.  
  58.   if (!(IS_SET_ONEWIRE_PIN)) return 0;  
  59.  
  60.   SET_OUT_ONEWIRE_DDR;
  61.   _delay_us(500);
  62.   SET_IN_ONEWIRE_DDR;
  63.   _delay_us(70);
  64.  
  65.   if(!(IS_SET_ONEWIRE_PIN))
  66.   {
  67.     _delay_us(500);
  68.     return(1);
  69.   }
  70.  
  71.   _delay_us(500);
  72.  
  73. return(0);
  74. }
  75.  
  76. /**********************************************************/
  77.  
  78. void OneWireWriteByte(unsigned char byte)
  79. {
  80.    unsigned char i;
  81.    CLR_ONEWIRE_PORT;
  82.    for (i=0; i<8; i++)
  83.    {
  84.      SET_OUT_ONEWIRE_DDR;
  85.      if (byte & 0x01)
  86.      {
  87.        _delay_us(7);
  88.        SET_IN_ONEWIRE_DDR;
  89.        _delay_us(70);
  90.      }
  91.      else
  92.      {
  93.         _delay_us(70);
  94.         SET_IN_ONEWIRE_DDR;
  95.         _delay_us(7);
  96.      }
  97.  
  98.      byte >>= 1;
  99.    }
  100. }
  101.  
  102. /***********************************************************/
  103.  
  104. unsigned char OneWireReadByte(void)
  105. {
  106.   unsigned char i, byte = 0;
  107.  
  108.   SET_IN_ONEWIRE_DDR;
  109.  
  110.   for (i=0; i<8; i++)
  111.   {
  112.      SET_OUT_ONEWIRE_DDR;
  113.      _delay_us(7);
  114.      SET_IN_ONEWIRE_DDR;
  115.      _delay_us(7);
  116.      byte >>= 1;
  117.      
  118.      if(IS_SET_ONEWIRE_PIN) byte |= 0x80;
  119.  
  120.      _delay_us(70);
  121.   }
  122.  
  123.   return byte;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement