20 #include <avr/pgmspace.h>
21 #include <util/delay.h>
22 #include <avr/interrupt.h>
23 #include <util/atomic.h>
28 uint8_t err=ONEWIRE_OK;
30 ONEWIRE_PORT &= ~ONEWIRE_MASK;
31 ONEWIRE_DDR |= ONEWIRE_MASK;
33 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
34 ONEWIRE_DDR &= ~ONEWIRE_MASK;
36 if( (ONEWIRE_PIN & ONEWIRE_MASK)) {
37 err = ONEWIRE_NO_PRESENCE;
42 if( !(ONEWIRE_PIN & ONEWIRE_MASK)) {
43 err = ONEWIRE_GND_SHORT;
50 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
52 ONEWIRE_PORT &= ~ONEWIRE_MASK;
53 ONEWIRE_DDR |= ONEWIRE_MASK;
55 ONEWIRE_DDR &= ~ONEWIRE_MASK;
58 ONEWIRE_PORT &= ~ONEWIRE_MASK;
59 ONEWIRE_DDR |= ONEWIRE_MASK;
61 ONEWIRE_DDR &= ~ONEWIRE_MASK;
70 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
71 ONEWIRE_PORT &= ~ONEWIRE_MASK;
72 ONEWIRE_DDR |= ONEWIRE_MASK;
74 ONEWIRE_DDR &= ~ONEWIRE_MASK;
76 rebit = ONEWIRE_PIN & ONEWIRE_MASK;
103 for (i=0; i<8; i++) {
112 memset(buffer, 0, 8);
125 uint8_t mask, i, j, bit, rom_tmp;
126 uint8_t max_conf_zero=0;
127 static uint8_t max_conf_old;
128 uint8_t branch_flag=0;
130 if (buffer == NULL) {
136 return ONEWIRE_NO_PRESENCE;
143 for(j=0; j<64; j++) {
149 if (j < max_conf_old) {
150 if (rom_tmp & mask) {
157 }
else if (j == max_conf_old) {
174 return ONEWIRE_SCAN_ERROR;
200 max_conf_old = max_conf_zero;
203 return ONEWIRE_CRC_ERROR;
204 }
else if (branch_flag) {
207 return ONEWIRE_LAST_CODE;
219 for (i=0; i<8; i++) {
246 for (i=0; i<8; i++) {
251 return ONEWIRE_CRC_ERROR;
289 static const uint8_t crc_table[16] PROGMEM =
290 {0x00, 0x9D, 0x23, 0xBE, 0x46, 0xDB, 0x65, 0xF8,
291 0x8C, 0x11, 0xAF, 0x32, 0xCA, 0x57, 0xE9, 0x74};
302 for(; cnt>0; cnt--) {
306 crc = (crc >> 4) | (tmp << 4);
307 crc ^= pgm_read_byte(&crc_table[i]);
310 crc = (crc >> 4) | (tmp & 0xF0);
311 crc ^= pgm_read_byte(&crc_table[i]);
uint8_t onewire_reset(void)
OneWire reset.
uint8_t onewire_match_rom(const uint8_t rom[8])
select device on bus
void onewire_write_byte(uint8_t data)
write one byte
uint8_t onewire_read_rom(uint8_t rom[8])
read ROM ID of device
void onewire_search_init(uint8_t buffer[8])
init rom search buffer and internal variables
uint8_t onewire_crc(const uint8_t *data, uint8_t cnt)
calculate CRC over data array, fast version, 0.3ms for 8 bytes @1MHz
void onewire_write_bit(uint8_t wrbit)
write one bit to bus
uint8_t onewire_search(uint8_t buffer[8], uint8_t cmd)
scan OneWire bus for normal ROM or alarm search
uint8_t onewire_skip_rom(void)
select device on bus
uint8_t onewire_read_bit(void)
read one bit from bus
uint8_t onewire_read_byte(void)
read one byte
uint8_t onewire_alarm_search(uint8_t buffer[8])
scan OneWire bus for devices with active alarm flag
uint8_t onewire_search_rom(uint8_t buffer[8])
scan OneWire bus for ROMs