1wire.c 2.4 KB

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