/** Odczytuje dane ze pamięci czujnika. @param buffer bufor, do którego zapisywane są dane */ void temp_read_scratchpad(uint8_t* buffer) { char i; onewire_write_byte(DS18B20_READ_SCRATCHPAD); for(i=0; i<9; i++) { *buffer++= onewire_read_byte(); } }
void immob_check_state(struct ecudata_t* d) { uint8_t i = 0, crc = 0; uint8_t key[8]; if (!(d->param.bt_flags & _BV(BTF_USE_IMM))) return; //immibilizer was not activated onewire_save_io_registers(); if (!onewire_reset()) goto lock_system; //not device present, lock the system! //Read 64-bit key onewire_write_byte(OWCMD_READ_ROM); for(; i < 8; ++i) key[i] = onewire_read_byte(); //validate CRC8, all bytes except CRC8 byte for(i = 0; i < 7; ++i) crc = update_crc8(key[i], crc); if (crc != key[7]) goto lock_system; //crc doesn't match, lock the system! //validate read key, skip family code and CRC8 bytes if (!validate_key(d, key+1, IBTN_KEY_SIZE)) goto lock_system; //read and stored keys don't match, lock the system! onewire_restore_io_registers(); return; //ok, system is unlocked lock_system: onewire_restore_io_registers(); d->sys_locked = 1; //set locking flag }
// Read a DS18xx sensor void read_DS18xxx(unsigned char register_id) { unsigned char i, pinmask = register_pinmask_map[register_id]; // Reset and read the temperature if (onewire_reset(pinmask)) { send_onewire_rom_commands(register_id); onewire_write_byte(CMD_READ_SCRATCHPAD, pinmask); for (i = 0; i < 9; i++) { ow_buf[i] = onewire_read_byte(pinmask); } if (ow_buf[8] == calculate_onewire_crc(ow_buf, 8) && ow_buf[7] == 0x10) { temperatures[register_id] = ow_buf[0] | (ow_buf[1] << 8); // If result needs scaling up then scale it up if (register_identification[register_id][SCALE_POSITION] == SCALE_TEMP) scale_DS18B20_result(register_id); } else { temperatures[register_id] = (signed int) ONEWIRE_TEMP_FAIL; } } }
uint8_t onewire_read_temp (struct onewire_temp_sensor *sensor) //Temperatur eines DS18x20 auslesen und umrechnen { uint8_t spad[9]; uint8_t i; uint8_t crc = 0; float temp_f; //einzelnen Sensor ansprechen onewire_reset(); if (onewire_presence_pulse == 0) { return 0; } onewire_send_byte(match_rom); for (i=0; i<8; i++) { onewire_send_byte(sensor->rom[i]); } //Scratchpad auslesen onewire_send_byte(read_spad); for (i=0; i<9; i++) { spad[i] = onewire_read_byte(); } //CRC Check for (i=0; i<9; i++) { crc = _crc_ibutton_update(crc, spad[i]); } //Temperatur berechnen if (crc == 0) { //0,5°C Bit löschen spad[temperature_lsb] &= ~(1<<0); //Temperatur umrechnen if (spad[temperature_msb] == 0) { temp_f = spad[temperature_lsb]/2.0; } else { i = ~(spad[temperature_lsb]); temp_f = i+1; temp_f = (temp_f*(-1))/2.0; } //Nachkommaberechnung temp_f = temp_f-0.25+(16.0-spad[count_remain])/16.0; //Umrechnung, um Kommazahl als Int zu speichern und abspeichern sensor->temperature = temp_f*100; return 1; } return 0; }
void onewire_read_rom(uint8_t *rom) { //Lesen der 64-bit Adresse onewire_reset(); onewire_send_byte(read_rom); /*Schleifenzähler läuft rückwärts, weil der Slave mit dem letzten Byte (Family-Code) anfängt und so der ROM-Code richtig herum in "adresse" steht*/ for (uint8_t i=8; i>0; i--) { *rom = onewire_read_byte(); rom++; } }
__interrupt void Timer0_A0(void) { switch (__even_in_range(TA0IV, 6)) { case TA0IV_NONE: { // TA0CCR0 P1OUT &= ~(CONTROL); // relais on bis if (i++ == 0) { // start reading onewire_reset(&ow); onewire_write_byte(&ow, 0xcc); // skip ROM command onewire_write_byte(&ow, 0x44); // convert T command onewire_line_high(&ow); } if (i >= 14) { // read values 16 onewire_reset(&ow); onewire_write_byte(&ow, 0xcc); // skip ROM command onewire_write_byte(&ow, 0xbe); // read scratchpad command for (i = 0; i < 9; i++) scratchpad[i] = onewire_read_byte(&ow); temp = ((scratchpad[1] << 8) + scratchpad[0]) * 0.0625; // temp control if (temp < 32 & TA0CCR1 < 48000) { // increase pwm //TA0CCR1 = TA0CCR1 +1000; } if (temp > 32 & TA0CCR1 > 1000) { // decrease pwm //TA0CCR1 = TA0CCR1 -1000; } i = 0; } break; } case TA0IV_TACCR1: { // TA0CCR1 P1OUT |= CONTROL; // relais off break; } case TA0IV_TACCR2: // TA0CCR2 { break; } case 6: break; // TA0CCR3 default: break; } }
float ds1820_read_temperature(int fd, uint64_t devcode) { int i; unsigned char data[9]; if(onewire_reset(fd)) { onewire_address_command(fd, devcode); onewire_write_byte(fd, DS1820_READ_SCRATCHPAD); // read scratch for(i = 0; i < 9; i++) { data[i] = onewire_read_byte(fd); } int ds1820_temperature = data[1] & 0x0f; ds1820_temperature <<= 8; ds1820_temperature |= data[0]; return ((float)ds1820_temperature) * 0.0625f; } return 211; }
void operate_device(void) { // Create messaging variables unsigned char response_opcode = RESPONSE_UNDEFINED, p, pinmask; bool got_message; // The main loop of the device while (TRUE) { // Operate main device functions operate_onewire_temp_measurement(); got_message = get_device_message(); #if 0 /* * Watch communication activity on bus and reset the device outputs * if no communication is seen whithin timeout */ if (!got_message) { if (timeout_occured(BUS_COMMUNICATION_WATCHDOG_TIMER, BUS_COMMUNICATION_TIMEOUT_MS)) { // Re-initialize the device - shut every output down device_specific_init(); init_device_comm(HOST_ID, COMM_SPEED_9600_H); // Reset the timer reset_timeout(BUS_COMMUNICATION_WATCHDOG_TIMER); } } else { reset_timeout(BUS_COMMUNICATION_WATCHDOG_TIMER); } #endif // Take care of messaging if (got_message && !process_generic_messages()) { // Set response opcode to undefined to ensure issue identification response_opcode = RESPONSE_UNDEFINED; switch (message_buffer.content[OPCODE]) { case SET_REGISTER: // p holds register to write p = message_buffer.content[PARAMETER_START]; // Preset response opcode to success response_opcode = COMMAND_SUCCESS; if(p>0 && p<=3) { /* Address 1: External temp sensor * Address 2: Living temp sensor * Address 3: Upstairs temp sensor */ // Read-only temp registers - command fails response_opcode = COMMAND_FAIL; } else if( p<=5 ) { /* Address 4: Living floor valve * Address 5: Upstairs floor valve */ pinmask = register_pinmask_map[p-1]; if (onewire_reset(pinmask)) { // If the value read and the value got on the bus do not equal then toggle the value of the DS2405 switch if((message_buffer.content[PARAMETER_START + 1] > 0) != ReadDS2405(register_rom_map[p-1], pinmask)) { if(onewire_reset(pinmask)) { send_onewire_rom_commands(p-1); } else { response_opcode = COMMAND_FAIL; } } } else { response_opcode = COMMAND_FAIL; } } else { /* Any other write addresses fail */ response_opcode = COMMAND_FAIL; } message_buffer.index = PARAMETER_START-1; break; case READ_REGISTER: // p holds register to write p = message_buffer.content[PARAMETER_START]; // Preset response opcode to success response_opcode = COMMAND_SUCCESS; if(p>0 && p<=3) { /* Address 1: External temp sensor * Address 2: Living temp sensor * Address 3: Upstairs temp sensor */ message_buffer.content[PARAMETER_START] = temperatures[p - 1] & 0x00ff; message_buffer.content[PARAMETER_START + 1] = (temperatures[p- 1] >> 8) & 0x00ff; message_buffer.index = PARAMETER_START + 1; } else if( p<=5 ) { /* Address 4: Living floor valve * Address 5: Upstairs floor valve */ pinmask = register_pinmask_map[p-1]; if (onewire_reset(pinmask)) { message_buffer.content[PARAMETER_START] = ReadDS2405(register_rom_map[p-1], pinmask); message_buffer.index = PARAMETER_START; } else { response_opcode = COMMAND_FAIL; message_buffer.index = PARAMETER_START-1; } } else if (p == 6){ /* Test address to read a single onewire device's ROM code */ pinmask = register_pinmask_map[0]; onewire_reset(pinmask); onewire_write_byte(CMD_READ_ROM, pinmask); for (p = 0; p < 8; p++) message_buffer.content[PARAMETER_START+p] = onewire_read_byte(pinmask); message_buffer.index = PARAMETER_START+7; } else { /* Any other read addresses fail */ response_opcode = COMMAND_FAIL; message_buffer.index = PARAMETER_START-1; } break; default: response_opcode = COMMAND_FAIL; break; }
int main() { onewire_t ow; int i; uint8_t scratchpad[9]; uint8_t id[8]; WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer BCSCTL1 = CALBC1_8MHZ; DCOCTL = CALDCO_8MHZ; uart_setup(); ow.port_out = &P1OUT; ow.port_in = &P1IN; ow.port_ren = &P1REN; ow.port_dir = &P1DIR; ow.pin = BIT7; printf("start\n"); search(&ow, id, 0, 1); printf("done\n"); onewire_reset(&ow); onewire_write_byte(&ow, 0xcc); // skip ROM command onewire_write_byte(&ow, 0x44); // convert T command onewire_line_high(&ow); // DELAY_MS(850); // at least 750 ms for the default 12-bit resolution // while __delay_cycles(>100000) give the __delay_cycles max value error: for (i = 0; i < 65; i++) __delay_cycles(100000); onewire_reset(&ow); onewire_write_byte(&ow, 0xcc); // skip ROM command onewire_write_byte(&ow, 0xbe); // read scratchpad command for (i = 0; i < 9; i++) scratchpad[i] = onewire_read_byte(&ow); for (i = 0; i < 9; i++) printf("%02x", scratchpad[i]); //meas = scratchpad[0]; // LSB //meas |= ( (uint16_t)scratchpad[1] ) << 8; // MSB int j=0; uint16_t temp = 0; for(j = 16; j > 0; i--){ temp >>= 1; if (onewire_read_bit(&ow)) { temp |= 0x8000; } } if(temp<0x8000){ return(temp*0.0625); } else { temp=(~temp)+1; return(temp*0.0625); } printf("%02x\n", temp); printf("%02x\n", scratchpad[2]); printf("%02x°C\n"); _BIS_SR(LPM0_bits + GIE); return 0; }
/* * To send a PING: * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,07,01,01,36,05,37,cf} * * To identify Register 4 (expected response as per the above identification data): * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,08,01,01,36,02,04,2c,d8} * * To identify Register 1 (expected response as per the above identification data): * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,08,01,01,36,02,01,99,32} * * To Read Register 1 * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,08,01,01,36,01,01,8f,66} * * To Write Register 3 (PWM), with low pin 2-2 sec * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,0b,01,01,35,00,03,14,14,00,04,d8} * */ void operate_device(void) { // Create messaging variables unsigned char response_opcode = RESPONSE_UNDEFINED, p, pinmask; // The main loop of the device while (TRUE) { // Operate main device functions operate_onewire_temp_measurement(); // Take care of messaging if (get_device_message() && !process_generic_messages()) { switch (message_buffer.content[OPCODE]) { case SET_REGISTER: response_opcode = COMMAND_FAIL; break; case READ_REGISTER: // p holds register to read p = message_buffer.content[PARAMETER_START]; // Branch based on register number if (p == 1) { message_buffer.content[PARAMETER_START] = temperatures[p - 1] & 0x00ff; message_buffer.content[PARAMETER_START + 1] = (temperatures[p - 1] >> 8) & 0x00ff; message_buffer.index = PARAMETER_START + 1; response_opcode = COMMAND_SUCCESS; } else if (p == 2) { pinmask = register_pinmask_map[0]; onewire_reset(pinmask); onewire_write_byte(CMD_READ_ROM, pinmask); p = 0; do { message_buffer.content[PARAMETER_START + p] = onewire_read_byte(pinmask); } while (++p < 8); message_buffer.index = PARAMETER_START + 7; if (message_buffer.content[PARAMETER_START + p] == calculate_onewire_crc( message_buffer.content + PARAMETER_START, 8)) response_opcode = COMMAND_SUCCESS; else response_opcode = COMMAND_FAIL; } else { response_opcode = COMMAND_FAIL; } break; default: response_opcode = COMMAND_FAIL; break; } send_response(response_opcode); }
/* * To send a PING: * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,07,01,01,36,05,37,cf} * * To identify Register 4 (expected response as per the above identification data): * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,08,01,01,36,02,04,2c,d8} * * To identify Register 1 (expected response as per the above identification data): * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,08,01,01,36,02,01,99,32} * * To Read Register 1 * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,08,01,01,36,01,01,8f,66} * * To Write Register 3 (PWM), with low pin 2-2 sec * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,0b,01,01,35,00,03,14,14,00,04,d8} * */ void operate_device(void) { // Messaging variables unsigned char response_opcode = RESPONSE_UNDEFINED, p; bool got_message; // The main loop of the device while (TRUE) { // Operate main device functions operate_onewire_temp_measurement(); got_message = get_device_message(); /* * Watch communication activity on bus and reset the device outputs * if no communication is seen whithin timeout */ if (!got_message) { if (timeout_occured(BUS_COMMUNICATION_WATCHDOG_TIMER, BUS_COMMUNICATION_TIMEOUT_MS)) { // Re-initialize the device - shut every output down device_specific_init_phase1(); init_device_comm(HOST_ID, COMM_SPEED_9600_H); device_specific_init_phase2(); // Reset the timer reset_timeout(BUS_COMMUNICATION_WATCHDOG_TIMER); } } else { reset_timeout(BUS_COMMUNICATION_WATCHDOG_TIMER); } // Take care of messaging if (got_message && !process_generic_messages()) { // Set response opcode to undefined to filter response opcode programming issues response_opcode = RESPONSE_UNDEFINED; switch (message_buffer.content[OPCODE]) { case SET_REGISTER: // p holds register to write p = message_buffer.content[PARAMETER_START]; // Preset response opcode to success response_opcode = COMMAND_SUCCESS; if ( p>0 && p<=4 ) { /* Address 1: HW temp sensor * Address 2: Basement temp sensor * Address 3: Return temp sensor * Address 4: Hidr Shift temp sensor */ response_opcode = COMMAND_FAIL; } else if( p <= 12 ) { /* * Address 5: Buffertop valve - Contact 2 * Address 6: Return valve - Contact 3 * Address 7: Radiator pump - Contact 4 * Address 8: Floor pump - Contact 5 * Address 9: Hidraulic Shifter pump - Contact 6 * Address 10: HW pump - Contact 7 * Address 11: Basement radiator valve Contact 8 * Address 12: Basement floor valve - Contact 9 */ write_extender(p-5, message_buffer.content[PARAMETER_START + 1] > 0); } else if(p == 13) { /* Address 13: Heater relay P3_5 - Heater contact */ HEATER_RELAY_PIN = message_buffer.content[PARAMETER_START + 1] > 0; } else if(p <= 15) { /* * Temp wipers - expected data format: * Byte 1 & 2 - 9 bits of data holdiong the desired wiper setting * Byte 3 - bool flag - is volatile * Address 14: HW Wiper * Address 15: Heating Wiper */ if (!write_wiper( (message_buffer.content[PARAMETER_START+1] << 8) | message_buffer.content[PARAMETER_START+2] , message_buffer.content[PARAMETER_START + 3]>0, p == 14 ? WIPER_HW : WIPER_HEAT)) response_opcode = COMMAND_FAIL; } else { /* Any other address fails */ response_opcode = COMMAND_FAIL; } message_buffer.index = PARAMETER_START-1; break; case READ_REGISTER: // p holds register to read p = message_buffer.content[PARAMETER_START]; // Preset response opcode to success response_opcode = COMMAND_SUCCESS; if ( p>0 && p<=4 ) { /* Address 1: HW temp sensor * Address 2: Basement temp sensor * Address 3: Return temp sensor * Address 4: Hidr Shift temp sensor */ message_buffer.content[PARAMETER_START] = temperatures[p - 1] & 0x00ff; message_buffer.content[PARAMETER_START + 1] = (temperatures[p- 1] >> 8) & 0x00ff; message_buffer.index = PARAMETER_START + 1; } else if( p <= 12 ) { /* * Address 5: Buffertop valve - Contact 2 * Address 6: Return valve - Contact 3 * Address 7: Radiator pump - Contact 4 * Address 8: Floor pump - Contact 5 * Address 9: Hidraulic Shifter pump - Contact 6 * Address 10: HW pump - Contact 7 * Address 11: Basement radiator valve Contact 8 * Address 12: Basement floor valve - Contact 9 */ message_buffer.content[PARAMETER_START] = get_extender_value(p-5); message_buffer.index = PARAMETER_START; } else if(p == 13) { /* Address 13: Heater relay P3_5 - Heater contact */ message_buffer.content[PARAMETER_START] = HEATER_RELAY_PIN; message_buffer.index = PARAMETER_START; } else if(p <= 15) { /* * Temp wipers - expected data format: * Byte 1 - bool flag - is volatile * Address 14: HW Wiper * Address 15: Heating Wiper */ read_wiper((unsigned int*)(message_buffer.content+PARAMETER_START), message_buffer.content[PARAMETER_START+1]>0, p == 14 ? WIPER_HW : WIPER_HEAT); message_buffer.index = PARAMETER_START+1; } else if(p == 16) { /* Test address to read rom on onewire bus - a single device should be connected to the bus in this case */ onewire_reset(0x04); onewire_write_byte(CMD_READ_ROM, 0x04); for (p = 0; p < 8; p++) message_buffer.content[PARAMETER_START+p] = onewire_read_byte(0x04); message_buffer.index = PARAMETER_START+7; } else { response_opcode = COMMAND_FAIL; message_buffer.index = PARAMETER_START-1; } break; // Any other message code fails default: response_opcode = COMMAND_FAIL; message_buffer.index = PARAMETER_START-1; break; }
void onewire_example(void) { unsigned char i, buf[9], type, pinmask = 0x04; int dly, temperature; // printf("1-wire Temperature Test:\r\n"); if (onewire_reset(pinmask)) { // printf("presence pulse ok\r\n"); // It isn't really necessary to read the ROM if // you know which chip is connected. A lot of // the code below can be removed if you only // use one type of sensor. /// printf("ROM: "); onewire_write_byte(CMD_READ_ROM, pinmask); for (i=0; i<8; i++) { buf[i] = onewire_read_byte(pinmask); // printf("%x ", buf[i]); } // printf(" CRC=%s\r\n", onewire_crc_check(buf, 8); type = buf[0]; // printf("Chip ID: "); /* switch (type) { case 0x10: printf("DS18S20\r\n"); break; case 0x28: printf("DS18B20\r\n"); break; case 0x22: printf("DS1822\r\n"); break; default: printf("unknown\r\n"); break; } */ // if it's a configurable resolution, set it for 12 bits if ((type == 0x28 || type == 0x22) && (buf[4] & 0x60) != 0x60) { onewire_write_byte(CMD_WRITE_SCRATCHPAD, pinmask); onewire_write_byte(0, pinmask); onewire_write_byte(0, pinmask); onewire_write_byte(0x7F, pinmask); // config } // printf("Begin temperature measurement\r\n"); onewire_reset(pinmask); onewire_write_byte(CMD_SKIP_ROM, pinmask); onewire_write_byte(CMD_CONVERT_T, pinmask); //STRONG_PULLUP_PIN = 0; // turn on strong pullup transistor for (dly=0; dly<1563; dly++) { delay_480us(); } //STRONG_PULLUP_PIN = 1; // turn off strong pullup onewire_reset(pinmask); // Temperature measurement is complete, now read it onewire_write_byte(CMD_SKIP_ROM, pinmask); onewire_write_byte(CMD_READ_SCRATCHPAD, pinmask); // printf("Data: "); for (i=0; i<9; i++) { buf[i] = onewire_read_byte(pinmask); // printf("%x ", buf[i]); } // printf(" CRC=%s\r\n", // onewire_crc_check(buf, 9); temperature = buf[0] | (buf[1] << 8); // if it's DS18S20, scale up to 12 bit resolution if (type == 0x10) { temperature *= 8; if (buf[7] == 0x10) { temperature &= 0xFFF0; temperature += 12; temperature -= buf[6]; } else { // printf("Err: count remain not 0x10\r\n"); } } #ifdef NICE_OUTPUT // printf("Temperature: %.2f Celsius\r\n", // (float)temperature * 0.0625; // printf("Temperature: %.2f Fahrenheit\r\n", // (float)temperature * 0.1125 + 32.0; #else // result is deg_C * 16 printf("Temperature: %d / 16\r\n", temperature); #endif } else { // printf("no 1-wire devices present\r\n"); } // printf_fast("\r\n\r\n\r\npress any key to reboot"); // pm2_cin(); // pm2_restart(); /* ESC to quit */ }