ICACHE_FLASH_ATTR void ds18b20_run(void) { int temp_low, temp_high; struct Temp temp; if (ds18b20_reset() == ONEWIRE_PRESENT) { ds18b20_putc(Skip_ROM); ds18b20_putc(Convert_T); os_delay_us(DS18B20_CONV_TIME); ds18b20_reset(); ds18b20_putc(Skip_ROM); ds18b20_putc(Read_scratchpad); temp_low = ds18b20_getc(); temp_high = ds18b20_getc(); ConvertTemp(temp_high, temp_low, &temp); os_sprintf(temperature_value, "%c%u.%02u", temp.tsign, temp.tint, temp.tfloat / 100); os_printf("%s: Temperature: %s C.\n", __FUNCTION__, temperature_value); espconn_gethostbyname(&thingspeak_conn, thingspeak_host, &thingspeak_ip, dns_done); } else { os_printf("%s: %s\n", __FUNCTION__, "ds18b20 1-wire signal is absent..."); } }
/* * get temperature */ double ds18b20_gettemp() { uint8_t temperature_l; uint8_t temperature_h; double retd = 0; #if DS18B20_STOPINTERRUPTONREAD == 1 cli(); #endif ds18b20_reset(); //reset ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); //start temperature conversion while(!ds18b20_readbit()); //wait until conversion is complete ds18b20_reset(); //reset ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); //read scratchpad //read 2 byte from scratchpad temperature_l = ds18b20_readbyte(); temperature_h = ds18b20_readbyte(); #if DS18B20_STOPINTERRUPTONREAD == 1 sei(); #endif char buffer[10]; sprintf(buffer,"%d:%d\n\r",temperature_h,temperature_l); USART1_sendStr(buffer); //convert the 12 bit value obtained retd = ( ( temperature_h << 8 ) + temperature_l ) * 0.0625; return retd; }
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); }
double ds18b20_gettemp(void){ uint8_t temperature_l; uint8_t temperature_h; double retd = 0; #if DS18B20_StopInterruptOnRead == 1 cli(); #endif ds18b20_reset(); ds18b20_writebyte(DS18B20_SkipROM); ds18b20_writebyte(DS18B20_ConvertTemp); while(!ds18b20_readbit()); ds18b20_reset(); ds18b20_writebyte(DS18B20_SkipROM); ds18b20_writebyte(DS18B20_RScratchPad); temperature_l = ds18b20_readbyte(); temperature_h = ds18b20_readbyte(); #if DS18B20_StopInterruptOnRead==1 sei(); #endif retd = ((temperature_h << 8) + temperature_l) * 0.0625; return retd; }
/* * get temperature */ double ds18b20_gettemp() { uint8_t temperature[2]; int8_t digit; uint16_t decimal; double retd = 0; ds18b20_reset(); //reset ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); //start temperature conversion while(!ds18b20_readbit()); //wait until conversion is complete ds18b20_reset(); //reset ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); //read scratchpad //read 2 byte from scratchpad temperature[0] = ds18b20_readbyte(); temperature[1] = ds18b20_readbyte(); ds18b20_reset(); //reset //store temperature integer digits digit = temperature[0]>>4; digit |= (temperature[1]&0x7)<<4; //store temperature decimal digits decimal = temperature[0]&0xf; decimal *= DS18B20_DECIMALSTEPS; //compose the double temperature value and return it retd = digit + decimal * 0.0001; return retd; }
/* * get temperature */ double ds18b20_gettemp() { uint8_t scratchpad[SCRATCHPAD_SIZE]; uint8_t i; double temp_value = DS18B20_ERR; cli(); // reset if (ds18b20_reset()) { sei(); return DS18B20_ERR; } ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_WSCRATCHPAD); //write to scratchpad ds18b20_writebyte(0x00); //alarm trigger TH ds18b20_writebyte(0x00); //alarm trigger TL ds18b20_writebyte(DS18B20_RES); //conversion resolution // reset if (ds18b20_reset()) { sei(); return DS18B20_ERR; } ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); //start temperature conversion sei(); while(!ds18b20_readbit()); //wait until conversion is complete cli(); // reset if (ds18b20_reset()) { sei(); return DS18B20_ERR; } ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); //read scratchpad //read scratchpad for (i=0; i<SCRATCHPAD_SIZE; i++) scratchpad[i] = ds18b20_readbyte(); sei(); if (crc8(scratchpad, SCRATCHPAD_SIZE - 1) == scratchpad[SCRATCHPAD_CRC]) //convert the value obtained temp_value = ((scratchpad[SCRATCHPAD_TEMP_H] << 8) + scratchpad[SCRATCHPAD_TEMP_L]) * 0.0625; return temp_value; }
//============================================================================= static int dev_ds18b20(int argc, char* argv[]) { //... ds18b20 <pin> <lun> if(argc != 3) { fprintf(stderr, "Usage: ... %s <pin> <lun>\n", argv[0]); return ERR_ARGC; } fprintf(stderr, "ds18b20===>>\n"); int pin = lib_piob_from_pin(atoi(argv[1])); if(pin < 0) return ERR_PIN; int mask = 1 << pin; union CMD_BUFF data = { .sd.cmd = CMD_SDATA, .sd.dev = DEV_DS18B20, .sd.lun = atoi(argv[2]) }; io_base = lib_open_base(0xFFFFF000); PIOB_PER = mask; PIOB_PPUER = mask; //enable pull up if(ds18b20_reset(mask)) { fprintf(stderr, "DS18B20 not detected.\n"); return 0; } float old = -999, temp; while(g_run && !ds18b20_read_temp(&temp, mask)) { if(temp - old < 0.1 && old - temp < 0.1) continue; data.sd.time = time(0); data.sd.val = temp; write(1, &data, sizeof(data)); old = temp; } fprintf(stderr, "ds18b20===<<\n"); lib_close_base(io_base); return 0; }
/*------------------------------------------------------- * 我的电路中只有一个器件DS18B20,所以不需要多个器件的ID 识别,跳过之后,启动温度转换,但在启动后,用户应等待几百个 毫秒,才能读到这次的转换值,这是DS18B20的数据手册规定的。因为温度转换是需要时间的嘛!(^_^) */ void convert_T(void) { if(ds18b20_reset()==SUCC) { // 如果复位成功 ds18b20_write(0xcc); // 跳过多器件识别 ds18b20_write(0x44); // 启动温度转换 } }
static int ds18b20_read_temp(float* temp, int mask) { int res = 0; //start conversion PIOB_ODR = mask; //disable output ds18b20_reset(mask); w1_write_byte(mask, DS18B20_SKIP_ROM); w1_write_byte(mask, DS18B20_CONVERT_T); res = usleep(750000); //reading ds18b20_reset(mask); w1_write_byte(mask, DS18B20_SKIP_ROM); w1_write_byte(mask, DS18B20_READ_SCRATCHPAD); *temp = (w1_read_byte(mask) | (w1_read_byte(mask) << 8)); (*temp) /= 16; //skip reading the rest of scratchpad bytes PIOB_OER = mask; //enable output PIOB_CODR = mask; //level low return res; }
/*------------------------------------------------------- * 读取转换后的温度值 * 我假定DS18B20一定是正确的,所以没有返回有关状态。当你故意把DS18B20从电路中拔下而能让程序告诉你出错时,你可以自己修 改这段代码! */ INT16U read_T(void) { INT16U value=0; if(ds18b20_reset()==SUCC) { // 如果复位成功 ds18b20_write(0xcc); // 跳过多器件识别 ds18b20_write(0xbe); // 读暂存器 value = (INT16U)ds18b20_read(); // 低字节 value += (INT16U)(ds18b20_read())<<8; // 高字节 } return(value); }
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; }
static int mars_ds18b20_open(struct inode *inode, struct file *filp) { int flag = 0; flag = ds18b20_reset(); if (flag & 0x01) { printk(KERN_WARNING "[%s] ds18b20 is failed at line %d!\n", __FUNCTION__, __LINE__); return -1; } printk(KERN_NOTICE "%s successfully!!\n", __FUNCTION__); return 0; }
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; }
uint8_t ds18b20_set_precision(uint8_t prec){ if(!ds18b20_reset()){ return 0; } ds18b20_write_byte(DS18B20_SKIP_ROM); ds18b20_write_byte(DS18B20_WRITE_SCRATCHPAD); ds18b20_write_byte(0); ds18b20_write_byte(0); ds18b20_write_byte((prec<<5)|0x1f); //check crc if(!ds18b20_check_crc()){ return 0; } return 1; }
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; }
void ds18b20_init(void) { gpio_set_mode(DS18B20_PORT, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, DS18B20_PIN); ds18b20_reset(); }
int main (void) { int64_t id = 0; uint8_t LD =0 ; //lastDeviation, uart_tx_init (); //UART initialization //_delay_ms(250); uart_print("\e[1;1H\e[2J"); char buffer[50]; do { LD = ow_enumerate(&id, LD); if (LD==0xff) { uart_print("\n\rFailed\n\r"); break; } else { uart_print("\n\r"); for(int i=0; i<64; i++) { if (id & ((int64_t)1<<(63-i))) uart_print("1"); else uart_print("0"); } } snprintf(buffer, 50, "\n\rLD = %d\n\r", LD); uart_print(buffer); } while (LD); uart_print("\n\r"); while (1) { #if _DEBUG_ == 1 uint8_t ow_status = ds18b20_reset(); if (ow_status){ uart_print("OneWire: Error.\n\r"); } else { uart_print("OneWire: OK\n\r"); } #endif // _DEBUG_ int temp_raw = ds18b20_readtemp(); //double temp = (double)temp_raw/16; //because 4 less significant bits mean fractions of degree char s_whole_degrees[10]; snprintf(s_whole_degrees, 10, "%d", (int)temp_raw>>4); //uart_print("\e[1;1H\e[2J"); //clear terminal window uart_print("Current temperature: "); uart_print(s_whole_degrees); uart_print("\r"); /* uint8_t low = temp_raw & 0xff; uint8_t high = (uint8_t)(temp_raw >> 8); uart_send_byte(high); uart_send_byte(low); uart_send_byte(0xff); */ _delay_ms(500); } return 0; }