void DS18B20_SendID(unsigned char *u8Rom) { int i; OWWriteByte(0x55); for(i = 0; i < 8; i++) { OWWriteByte(u8Rom[i]); } }
int do_onewire(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { unsigned short val; int result; int i; unsigned char ow_id[6]; char str[32]; /* * Clear 1-wire bit (open drain with pull-up) */ val = in_be16((void*)(CONFIG_SYS_FPGA_BASE_ADDR + CONFIG_SYS_FPGA_MODE)); val &= ~CONFIG_SYS_FPGA_MODE_1WIRE; /* clear 1-wire bit */ out_be16((void*)(CONFIG_SYS_FPGA_BASE_ADDR + CONFIG_SYS_FPGA_MODE), val); result = OWTouchReset(); if (result != 0) puts("No 1-wire device detected!\n"); OWWriteByte(0x33); /* send read rom command */ OWReadByte(); /* skip family code ( == 0x01) */ for (i = 0; i < 6; i++) ow_id[i] = OWReadByte(); OWReadByte(); /* read crc */ sprintf(str, "%02X%02X%02X%02X%02X%02X", ow_id[0], ow_id[1], ow_id[2], ow_id[3], ow_id[4], ow_id[5]); printf("Setting environment variable 'ow_id' to %s\n", str); setenv("ow_id", str); return 0; }
uint8_t readTempData_ds18b20(int *temperature) { if(resetSensor() != 0){ return 1; } OWWriteByte(0xCC); //skip rom OWWriteByte(0x44); //convert T _delay_ms(800); if(resetSensor() != 0){ return 1; } OWWriteByte(0xCC); //skip rom OWWriteByte(0xBE); //read scratchpad *temperature = readScratchpad(2); //*temperature = 0xFC90; return 0; }
void DS18B20_Convert(uint8 *pID)//温度转换 { OWReset(); //复位 // OWWriteByte(0xcc); DS18B20_SendID(pID); OWWriteByte(0x44);//发出转换命令 }
unsigned int DS18B20_ReadTemperature(uint8 *pID) { unsigned int Data; OWReset(); //复位 DS18B20_SendID(pID); OWWriteByte(0xbe); //读温度 Data = OWReadByte(); Data += (unsigned int)(OWReadByte() << 8); return Data; }
// legge il codice del SINGOLO dispositivo sul bus // e lo memorizza nell'array ID passato come argomento void OWReadRom(unsigned char *ID) { OWReset(); // resetto la linea one-wire OWWriteByte(OW_READ_ROM); // invio il comando readrom, che si può usare solo se sulla linea c'è una sola sonda for(unsigned char a=0; a<8; a++)// ciclo per leggere gli 8 bytes restituiti dal dispositivo { // eseguo la lettura del byte a-esimo: ID[a]=OWReadByte(); } }
unsigned char *DS18B20_ReadID(unsigned char *u8Rom) { int i; OWReset(); //复位 OWWriteByte(0x33); for(i = 0; i < 8; i++) { u8Rom[i] = OWReadByte(); } return u8Rom; }
//----------------------------------------------------------------------------- // Read and return the page data and SHA-1 message authentication code (MAC) // from a DS2432. // int ReadPageMAC(int page, unsigned char *page_data, unsigned char *mac) { int i; unsigned short data_crc16, mac_crc16; // set the speed to 'standard' // select the device if (OWTouchReset()) // Reset the 1-Wire bus return 0; // Return if no devices found OWWriteByte(0xCC); // Send Skip ROM command to select single device // read the page OWWriteByte(0xA5); // Read Authentication command OWWriteByte((page << 5) & 0xFF); // TA1 OWWriteByte(0); // TA2 (always zero for DS2432) // read the page data for (i = 0; i < 32; i++) page_data[i] = OWReadByte(); OWWriteByte(0xFF); // read the CRC16 of command, address, and data data_crc16 = OWReadByte(); data_crc16 |= (OWReadByte() << 8); // delay 2ms for the device MAC computation // read the MAC for (i = 0; i < 20; i++) mac[i] = OWReadByte(); // read CRC16 of the MAC mac_crc16 = OWReadByte(); mac_crc16 |= (OWReadByte() << 8); // check CRC16... return 1; }
//----------------------------------------------------------------------------- // Set all devices on 1-Wire to overdrive speed. Return '1' if at least one // overdrive capable device is detected. // int OWOverdriveSkip(unsigned char *data, int data_len) { // set the speed to 'standard' // reset all devices if (OWTouchReset()) // Reset the 1-Wire bus return 0; // Return if no devices found // overdrive skip command OWWriteByte(0x3C); // set the speed to 'overdrive' // do a 1-Wire reset in 'overdrive' and return presence result return OWTouchReset(); }
int main(void) { unsigned int c; int num=0; /* * Initialize UART library, pass baudrate and AVR cpu clock * with the macro * UART_BAUD_SELECT() (normal speed mode ) * or * UART_BAUD_SELECT_DOUBLE_SPEED() ( double speed mode) */ //uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) ); /* * now enable interrupt, since UART library is interrupt controlled */ //sei(); /* * Transmit string to UART * The string is buffered by the uart library in a circular buffer * and one character at a time is transmitted to the UART using interrupts. * uart_puts() blocks if it can not write the whole string to the circular * buffer */ //uart_puts("String stored in SRAM\n"); /* * Transmit string from program memory to UART */ //uart_puts_P("String stored in FLASH\n"); /* * Transmit single character to UART */ //uart_putc('\r'); /* initialize display, cursor off */ lcd_init(LCD_DISP_ON); /* clear display and home cursor */ lcd_clrscr(); LED_INIT; LED_OFF; unsigned char Buffer_RX[32]; int i; for(;;) { /* * Get received character from ringbuffer * uart_getc() returns in the lower byte the received character and * in the higher byte (bitmask) the last receive error * UART_NO_DATA is returned when no data is available. * */ //uart_puts_P("Kiss my ass\n"); //c = uart_getc(); c=0; //uart_puts("TAREK TAHA ===>>> Wireless is Up and Running\n"); lcd_clrscr(); lcd_gotoxy(16-(num++)%16,0); lcd_puts("<<LCD TEST 1>>\n"); if (OWTouchReset() == 1) { lcd_puts("No Pulse found\n"); } else { lcd_puts("Pulse found\n"); OWWriteByte(OW_READ_ROM_CMD); for (i=0;i<=8;i++) { Buffer_RX[i] = OWReadByte(); lcd_putc(Buffer_RX[i]); } } DelayMs(100); continue; if ( c & UART_NO_DATA ) { /* * no data available from UART */ } else { /* * new data available from UART * check for Frame or Overrun error */ if ( c & UART_FRAME_ERROR ) { /* Framing Error detected, i.e no stop bit detected */ uart_puts_P("UART Frame Error: "); } if ( c & UART_OVERRUN_ERROR ) { /* * Overrun, a character already present in the UART UDR register was * not read by the interrupt handler before the next character arrived, * one or more received characters have been dropped */ uart_puts_P("UART Overrun Error: "); } if ( c & UART_BUFFER_OVERFLOW ) { /* * We are not reading the receive buffer fast enough, * one or more received character have been dropped */ uart_puts_P("Buffer overflow error: "); } /* * send received character back */ if(c=='0') { LED_OFF; uart_puts_P("--->>>Light OFF\n"); } else if(c=='1') { LED_ON; uart_puts_P("--->>>Light ON\n"); } uart_puts_P("Press 0 or 1 to toggle Light\n"); //uart_putc( (unsigned char)c ); } //delay_ms(100); uart_puts("TAREK TAHA"); } }
//-------------------------------------------------------------------------- //! Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing //! search state. //! @return TRUE : device found, ROM number in ROM_NO buffer //! FALSE : device not found, end of search // int OWSearch() { int id_bit_number; int last_zero, rom_byte_number, search_result; int id_bit, cmp_id_bit; uchar rom_byte_mask, search_direction; // initialize for search id_bit_number = 1; last_zero = 0; rom_byte_number = 0; rom_byte_mask = 1; search_result = 0; crc8 = 0; // if the last call was not the last one if (!LastDeviceFlag) { // 1-Wire reset if (!OWReset()) { // reset the search LastDiscrepancy = 0; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; return FALSE; } // issue the search command OWWriteByte(0xF0); // loop to do the search do { // read a bit and its complement id_bit = OWReadBit(); cmp_id_bit = OWReadBit(); // check for no devices on 1-wire if ((id_bit == 1) && (cmp_id_bit == 1)) break; else { // all devices coupled have 0 or 1 if (id_bit != cmp_id_bit) search_direction = id_bit; // bit write value for search else { // if this discrepancy if before the Last Discrepancy // on a previous next then pick the same as last time if (id_bit_number < LastDiscrepancy) search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0); else // if equal to last pick 1, if not then pick 0 if (id_bit_number == LastDiscrepancy) search_direction = 1; else search_direction = 0; // if 0 was picked then record its position in LastZero if (search_direction == 0) { last_zero = id_bit_number; // check for Last discrepancy in family if (last_zero < 9) LastFamilyDiscrepancy = last_zero; } } // set or clear the bit in the ROM byte rom_byte_number // with mask rom_byte_mask if (search_direction == 1) ROM_NO[rom_byte_number] |= rom_byte_mask; else ROM_NO[rom_byte_number] &= ~rom_byte_mask; // serial number search direction write bit OWWriteBit(search_direction); // increment the byte counter id_bit_number // and shift the mask rom_byte_mask id_bit_number++; rom_byte_mask <<= 1; // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask if (rom_byte_mask == 0) { docrc8(ROM_NO[rom_byte_number]); // accumulate the CRC rom_byte_number++; rom_byte_mask = 1; } } } while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 // if the search was successful then if (!((id_bit_number < 65) || (crc8 != 0))) { // search successful so set LastDiscrepancy,LastDeviceFlag,search_result LastDiscrepancy = last_zero; // check for last device if (LastDiscrepancy == 0) LastDeviceFlag = TRUE; search_result = TRUE; } } // if no device found then reset counters so next 'search' will be like a first if (!search_result || !ROM_NO[0]) { LastDiscrepancy = 0; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; search_result = FALSE; } return search_result; }
void main(){ setup_oscillator (OSC_8MHZ); setup_timer_1(T1_DISABLED); setup_adc_ports(NO_ANALOGS|VSS_VDD); setup_adc(ADC_OFF); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE); // Give indiction that the microcontroller is up and running for (i=0;i<5;i++){ blink(); } //On boot interactive mode is on so that the user can use a terminal interface interactive = 1; //Main loop starts here... while(true){ //Waits for a command to come in over the serial port in interactive mode if (interactive == 1) printf("Please enter a command (h for help):\n\r"); command = getc(); switch (command){ case 'i': //Turn off interactive mode printf("Turning Off Interactive...\n\r"); interactive = 0; printf("Done...\n\r"); break; case 'r': //Reset DS2482 if (interactive == 1){ printf("Resetting the DS2482\n\r"); } //Do the Reset result = ds2482_reset(); if (interactive == 1){ if (result == 1) printf("...Success...\n\r"); else printf ("...Fail..\n\r"); } break; case 'c': //Configure DS2482 //Active pullup should always be selected unless //there is only a single slave on the 1-Wire line. //0x01 1WS Disabled, SPU Disabled, 0 NC, APU Active Pullup (Normal Operation for more then one device on onewire bus) //0x05 1WS Disabled, SPU Active, 0 NC, APU Active Pullup (Strong pullup is commonly used with 1-Wire parasitically powered temperature sensors, eeprom, A/D converters) if (interactive == 1){ printf("Configuring the DS2482\n\r"); } //Do the Config result = ds2482_write_config(0x01); if (interactive == 1){ if (result == 1) printf("...Success...\n\r"); else printf ("...Fail..\n\r"); } break; case 'R': //Reset One Wire Bus if (interactive == 1){ printf("Resetting the One Wire\n\r"); } result = OWreset(); if (interactive == 1){ if (result == 1) printf("...Success...\n\r"); else printf ("...Fail..\n\r"); } break; case 'n': //Read the Network Address OWreset(); delay_ms(1); OWWriteByte("33"); //delay_ms(20); for(i=0; i<=7; i++) { result = OWReadByte(); printf ("%2X",result); delay_ms(1); } break; case 's': //Read the OW status register OWreset(); delay_ms(1); OWWriteByte(204); //Transmit skip Rom Command CC = 204 OWWriteByte(105); //0x69 Transmit Read RAM command OWWriteByte(1); //Transmit Read start address result=OWReadByte(); break; case 'f': //Fix the floating power switch OWreset(); OWWriteByte(0xCC); OWWriteByte(0x6C); //Write Data Command OWWriteByte(0x31); //Eeprom address but actually gets written to Shadow Ram OWWriteByte(0xE7); //Value to make PMOD1 SWEN=0 RNAOP=0 //Copy the shadow Ram written above over to actual EEPROM OWreset(); OWWriteByte(0xCC); OWWriteByte(0x48); //send the copy command OWWriteByte(0x30); //copy shadow ram to the block containing 31 break; default: //Displays help message if you get an 'h' or an unknown command printf("PyroLogger Board Found and Responding...\n\r"); printf("Firmware Version 2\n\r"); printf("Interactive Mode is ON\n\r"); printf("\n\r"); printf("\n\r"); printf("Command List:\n\r"); printf("h = System Help\n\r"); printf("i = Exit ineractive mode\n\r"); printf("r = Reset DS2482\n\r"); printf("c = Configure DS2482\n\r"); printf("R = Reset One Wire Bus\n\r"); printf("N = Print Net Address\n\r"); } } }
/** * @brief Reads temperature on a ds1820 temperature sensor * @param temperature Pointer to the measured temperature * @return 0 if temperature read successfully */ int bbb_one_wire_DS1820_read(float* temperature, string* data) { /* int get[10]; char temp_lsb,temp_msb; int k; char temp_f,temp_c; OWTouchReset(); OWWriteByte(0xCC); //Skip ROM OWWriteByte(0x44); // Start Conversion tickDelay(5); OWTouchReset(); OWWriteByte(0xCC); // Skip ROM OWWriteByte(0xBE); // Read Scratch Pad for (k=0;k<9;k++) { get[k] = OWReadByte(); } cout << "ScratchPAD DATA = " << get[8] << get[7] << get[6] << get[5] << get[4] << get[3] << get[2] << get[1] << get[0] << endl; temp_msb = get[1]; // Sign byte + lsbit temp_lsb = get[0]; // Temp data plus lsb if (temp_msb <= 0x80) { temp_lsb = (temp_lsb/2); } // shift to get whole degree temp_msb = temp_msb & 0x80; // mask all but the sign bit if (temp_msb >= 0x80) { temp_lsb = (~temp_lsb)+1; } // twos complement if (temp_msb >= 0x80) { temp_lsb = (temp_lsb/2); }// shift to get whole degree if (temp_msb >= 0x80) { temp_lsb = ((-1)*temp_lsb); } // add sign bit //cout << "TempC= " << (int)temp_lsb << " degrees C" << endl;// print temp. C *temperature = (int)temp_lsb; return 1; */ int i = 0; int a = 1; unsigned short dataCRC16; OWTouchReset(); OWWriteByte(SKIP_ROM); OWWriteByte(CONVERT_T); while(a) { tickDelay(5); if(OWReadBit()) a = 0; } OWTouchReset(); OWWriteByte(SKIP_ROM); OWWriteByte(READ_SCRATCHPAD); *temperature = OWReadByte() + (OWReadByte() << 8); return 1; /* if(OWTouchReset()) return 0; OWWriteByte(0xCC); OWWriteByte(0xA5); OWWriteByte((page << 5) & 0xFF); OWWriteByte(0); for(i = 0; i < 32; i++) *data += OWReadByte() + "-"; OWWriteByte(0xFF); */ // return result; }
//-------------------------------------------------------------------------- // The 'OWSearch' function does a general search. This function // continues from the previous search state. The search state // can be reset by using the 'OWFirst' function. // This function contains one parameter 'alarm_only'. // When 'alarm_only' is TRUE (1) the find alarm command // 0xEC is sent instead of the normal search command 0xF0. // Using the find alarm command 0xEC will limit the search to only // 1-Wire devices that are in an 'alarm' state. // // Returns: TRUE (1) : when a 1-Wire device was found and its // Serial Number placed in the global ROM // FALSE (0): when no new device was found. Either the // last search was the last device or there // are no devices on the 1-Wire Net. // int OWSearch() { int id_bit_number; int last_zero, rom_byte_number, search_result; int id_bit, cmp_id_bit; unsigned char rom_byte_mask, search_direction, status; // initialize for search id_bit_number = 1; last_zero = 0; rom_byte_number = 0; rom_byte_mask = 1; search_result = FALSE; crc8 = 0; // if the last call was not the last one if (!LastDeviceFlag) { // 1-Wire reset if (!OWReset()) { // reset the search LastDiscrepancy = 0; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; return FALSE; } // issue the search command OWWriteByte(0xF0); // loop to do the search do { // if this discrepancy if before the Last Discrepancy // on a previous next then pick the same as last time if (id_bit_number < LastDiscrepancy) { if ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0) search_direction = 1; else search_direction = 0; } else { // if equal to last pick 1, if not then pick 0 if (id_bit_number == LastDiscrepancy) search_direction = 1; else search_direction = 0; } // Perform a triple operation on the DS2482 which will perform 2 read bits and 1 write bit status = DS2482_search_triplet(search_direction); // check bit results in status byte id_bit = ((status & DS2482_STATUS_SBR) == DS2482_STATUS_SBR); cmp_id_bit = ((status & DS2482_STATUS_TSB) == DS2482_STATUS_TSB); search_direction = ((status & DS2482_STATUS_DIR) == DS2482_STATUS_DIR) ? 0x01 : 0x00; // check for no devices on 1-Wire if ((id_bit) && (cmp_id_bit)) break; else { if ((!id_bit) && (!cmp_id_bit) && (search_direction == 0)) { last_zero = id_bit_number; // check for Last discrepancy in family if (last_zero < 9) LastFamilyDiscrepancy = last_zero; } // set or clear the bit in the ROM byte rom_byte_number // with mask rom_byte_mask if (search_direction == 1) ROM_NO[rom_byte_number] |= rom_byte_mask; else ROM_NO[rom_byte_number] &= (uint8_t)~rom_byte_mask; // increment the byte counter id_bit_number // and shift the mask rom_byte_mask id_bit_number++; rom_byte_mask <<= 1; // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask if (rom_byte_mask == 0) { calc_crc8(ROM_NO[rom_byte_number]); // accumulate the CRC rom_byte_number++; rom_byte_mask = 1; } } } while(rom_byte_number < 8); // (do-while) loop until through all ROM bytes 0-7 // if the search was successful then if (!((id_bit_number < 65) || (crc8 != 0))) { // search successful so set LastDiscrepancy,LastDeviceFlag,search_result LastDiscrepancy = last_zero; // check for last device if (LastDiscrepancy == 0) LastDeviceFlag = TRUE; search_result = TRUE; } } // if no device found then reset counters so next 'search' will be like a first if (!search_result || (ROM_NO[0] == 0)) { LastDiscrepancy = 0; LastDeviceFlag = FALSE; LastFamilyDiscrepancy = 0; search_result = FALSE; } return search_result; }