static ssize_t mars_ds18b20_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos) { int flag; unsigned char msByte = 0, lsByte = 0; unsigned int temp; flag = ds18b20_reset(); if (flag & 0x01) { printk(KERN_WARNING "ds18b20 init failed\n"); return -1; } ds18b20_write_byte(0xcc); ds18b20_write_byte(0x44); flag = ds18b20_reset(); if (flag & 0x01) return -1; ds18b20_write_byte(0xcc); ds18b20_write_byte(0xbe); lsByte = ds18b20_read_byte(); // LSB of the temperature msByte = ds18b20_read_byte(); // MSB of the temperature temp = msByte; temp <<= 8; temp |= lsByte; if (copy_to_user(buf, &temp, sizeof(temp))) return -EFAULT; return sizeof(temp); }
uint16_t ds18b20_read(void) { uint8_t busy = 0; uint8_t temp1, temp2; uint16_t temp; ds18b20_reset(); /* ds18b20_write_byte(0xCC);*/ ds18b20_write_byte(0x44); /* while(busy==0) busy = ds18b20_read_byte(); ds18b20_reset(); ds18b20_write_byte(0xCC);*/ ds18b20_write_byte(0xBE); temp1 = ds18b20_read_byte(); temp2 = ds18b20_read_byte(); temp = ((uint16_t)temp1 << 8) | temp2; delay_us(200); gpio_toggle(GPIOB, GPIO9); return temp; }
uint8_t ds18b20_check_crc(){ if(!ds18b20_reset()){ return 0; } ds18b20_write_byte(DS18B20_SKIP_ROM); ds18b20_write_byte(DS18B20_READ_SCRATCHPAD); uint8_t crc=0; for(int8_t i=0;i<9;i++){ _crc_ibutton_update(crc,ds18b20_read_byte()); } return !crc; }
uint8_t ds18b20_read_temp(double* temp, uint8_t prec){ if(!ds18b20_set_precision(prec)){ return 0; } if(!ds18b20_reset()){ return 0; } ds18b20_write_byte(DS18B20_SKIP_ROM); ds18b20_write_byte(DS18B20_CONVERT_T); // provide power SET_OUTPUT(DS18B20_DDR, DS18B20_DQ); SET_HIGH (DS18B20_PORT, DS18B20_DQ); for(int8_t i=0;i<(1<<prec);i++){ _delay_ms(93.75); } if(!ds18b20_reset()){ return 0; } ds18b20_write_byte(DS18B20_SKIP_ROM); ds18b20_write_byte(DS18B20_READ_SCRATCHPAD); uint8_t crc=0; uint8_t l=ds18b20_read_byte(); _crc_ibutton_update(crc,l); uint8_t h=ds18b20_read_byte(); _crc_ibutton_update(crc,h); for(int8_t i=0;i<7;i++){ _crc_ibutton_update(crc,ds18b20_read_byte()); } if(crc){ return 0; } int16_t t=(h<<8)+l; *temp=t*0.0625; return 1; }