1wire.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "1wire.h"
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4. #include <avr/wdt.h>
  5. #ifdef SEARCHROM
  6. #ifdef INITST
  7. unsigned char initst;
  8. #endif
  9. #endif
  10. unsigned char _1WireInit(void){
  11. unsigned char out=0;
  12. _1WPORT &= ~_1WL;
  13. _1WLOW();
  14. _delay_us(480);
  15. cli();
  16. _1WHIGH();
  17. _delay_us(70);
  18. if(!_1WISHIGH()) out++;
  19. sei();
  20. _delay_us(410);
  21. if(!_1WISHIGH()) out++;
  22. return out; // 0 - zwarcie, 1 - ok, 2 - rozwarcie
  23. }
  24. void _1WireWriteByte(unsigned char data){
  25. register unsigned char i;
  26. for(i=0;i<8;i++){
  27. _1WireWriteSlot(data&1,0);
  28. data >>= 1;
  29. }
  30. }
  31. void _1WireWriteBytePR(unsigned char data){ //natychmiast po poleceniu włącz zasilanie
  32. register unsigned char i;
  33. for(i=0;i<7;i++){
  34. _1WireWriteSlot(data&1,0);
  35. data >>= 1;
  36. }
  37. _1WireWriteSlot(data,1);
  38. _1WPORT |= _1WL;
  39. _1WDDR |= _1WL;
  40. sei();
  41. if(!_1WISHIGH()) _1WirePoweroff();
  42. }
  43. void _1WireWriteSlot(unsigned char x, unsigned char pr){
  44. cli();
  45. _1WLOW();
  46. if(x) _delay_us(6); else _delay_us(60);
  47. _1WHIGH();
  48. if(x) _delay_us(64); else _delay_us(10);
  49. if(!pr) sei();
  50. _delay_us(1);
  51. }
  52. unsigned char _1WireReadByte(void){
  53. register unsigned char i=1;
  54. unsigned char data=0;
  55. while(i){
  56. if(_1WireReadSlot()) data |= i;
  57. i <<= 1;
  58. }
  59. return data;
  60. }
  61. unsigned char _1WireReadSlot(void){
  62. unsigned char out=0;
  63. cli();
  64. _1WLOW();
  65. _delay_us(6);
  66. _1WHIGH();
  67. _delay_us(9);
  68. if(_1WISHIGH()) out=1;
  69. sei();
  70. _delay_us(55);
  71. return out;
  72. }
  73. /*
  74. void _1WirePoweroff(void){ //wyłacz zasilanie
  75. _1WDDR &= ~_1WL;
  76. _1WPORT &= ~_1WL;
  77. }*/
  78. #ifdef SEARCHROM
  79. unsigned char _1WireSearch(unsigned char rv, unsigned char *buf){
  80. unsigned char i,a,b,mask=1,lz=0;
  81. #ifdef INITST
  82. if((initst = _1WireInit())!=1) return 0xff;
  83. #else
  84. if(_1WireInit()!=1) return 0xff;
  85. #endif
  86. _1WireWriteByte(0xf0);
  87. for(i=1;i<65;i++){
  88. a = _1WireReadSlot();
  89. b = _1WireReadSlot();
  90. if(a && b) return 0xff;
  91. if(a==b) {
  92. if(i>rv) a = 0;
  93. else if(i==rv) a = 1;
  94. else a = !!((*buf)&mask);
  95. if(!a) lz=i;
  96. }
  97. _1WireWriteSlot(a,0);
  98. if(a) (*buf)|=mask; else (*buf)&=~mask;
  99. mask<<=1;
  100. if(!mask){
  101. mask = 1;
  102. buf++;
  103. }
  104. }
  105. rv=lz;
  106. return rv;
  107. }
  108. void _1WireSendRom(unsigned char *rom){
  109. register unsigned char i;
  110. for(i=0;i<8;i++){
  111. _1WireWriteByte(rom[i]);
  112. }
  113. }
  114. #endif
  115. /*
  116. unsigned char tablica_z_adresami[MAX_NUM][8];
  117. unsigned char rom[8];
  118. unsigned char i=0, rv=0;
  119. do {
  120. rv = _1WireSearch(rv, rom);
  121. memcpy(tablica_z_adresami[i], rom, 8);
  122. i++;
  123. } while(!rv || i<MAX_NUM);*/