One Wire Library
Twenty two functions and procedures.
The three basic functions are open.
Now is simple and easy to write for "mutidrop" 1-wire Devices.
Language:
mBasic and mPascal.
Until the moment I don't know if mC shares lib done by mB language,
in case he shares, this lib it can also be used by mC.
Capability:
For Family P12, 16 and P18. Clock from 3.8 up to 40Mhz or higher.
Now can be re-compiled with any of these clock frequency.
Work with any PORT-PIN.
Short Descriptions:
Functions:
Sub Function Ow_Reset_Dev() as Boolean
Issue a reset command.
Sub Function Ow_MatchRom(Dim ByRef RomArray[8] as Byte) as Byte
Address a specific device in the net.
Sub Function Ow_Pwr_Type(Dim ByRef RomArray[8] as Byte) as Byte
Issue a command that get the type of power.
Sub Function Ow_PUPdetect() as boolean
Issue a command that return if a pull-up is connected or not on 1-wire pin.
Sub Function Ow_DetectDevice(Dim ByRef RomArray[n][8] as Byte) as Byte
Issue the command SerchRom and get all devices in the 1-wire net inner an
array mutidimentional.
Sub Function Ow_CountDevice() as Byte
Issue a command to read the amount present devices in the 1-wire net.
Sub Function Ow_Serach(Dim ByRef RomArray[8], Dim pLastDiscrepancy, pCmd as Byte) as Boolean
Issue a command SearchRom or SearchAlarm for devices in the net.
Sub Function Ow_Read_Byte() as Byte
Issue the command to read a byte.
Sub Function Ow_ReadBit() as Byte
Issue the command read a bit
Sub Function Ow_ChkAlarm( Dim pTemp, pTHi, pTLo as Byte) as Boolean
This Function compares the actual temp with THi and TLo set.
Sub Function Ow_ChkCRC(Dim ByRef RomArray[], Dim pCRCidx as Byte) as Boolean
Make a CRC check for ROM and Scratchpad of a device.
Procedures:
Sub Procedure Ow_WriteBit(Dim BitVal as Byte)
This command Write a bit on device.
Sub Procedure Ow_Config(Dim ByRef pPORT as Byte, Dim Ow_Pin as Byte)
Set's the Port and pin to work with 1-wire.
Sub Procedure Ow_Issue_Cmd_Val(Dim pCmdVal as Byte)
Issue any command or value to 1-wire device.
Sub Procedure Ow_ReadSpad(Dim ByRef pSpad as Byte[])
Issue the command Read Scratchpad.
Sub Procedure Ow_ReadRom(Dim ByRef RomArray as Byte[])
Issue the command Read Rom.
Sub Procedure Ow_RcallE2Prom(Dim ByRef RomArray as Byte[])
Issue the command Recall EEPROM.
Sub Procedure Ow_WriteSpad12(Dim ByRef RomArray as Byte[], Dim pTHi, pTLo, ConfByte as Byte)
Issue the command Write Scratchpad for devices with 12 bit’s resolution.
Sub Procedure Ow_WriteSpad9(Dim ByRef RomArray as Byte[], Dim Thi, Tlo as Byte)
Issue the command Write Scratchpad for devices with 9 bit’s resolution.
Sub Procedure Ow_ConvTemp(Dim ByRef RomArray as Byte[])
Issue the command Convert temperature.
Sub Procedure Ow_Copy_Spad(Dim ByRef RomArray as Byte[])
Issue the command Copy Scratchpad into EEPROM.
Sub Procedure Ow_Write_Byte(Dim ByteCmd as Byte)
This command Write a byte on device.
So that you have a better idea get the complete description and examples
in the link:
ftp://ttelecom.com.br next go to /onewire/examples/onewire.zip
Later, for to have a better comfort to use these lib's, you can copy the files
that are inside the folder OneWire/mBasic/lib/p12_16/, to uses/p16/ from mE's,
and the files that are inside the folder OneWire/mBasic/lib/p18/, to uses/p18/ from mE's.
Reopen the program.
The above is valid for mPascal too.
Good luck and good work!!
1-WIRE LIBRARY FOR P12,16 AND 18 SINGLE AND MULTIDROP DEV.
1-WIRE LIBRARY FOR P12,16 AND 18 SINGLE AND MULTIDROP DEV.
Working with you, for you!
MAN
MAN
one wire library
I tried using the library with 16F876A and DS18S20 but got compile errors not found: _MPORT
not found: _pinMask
not found: _MTRIS
in ow_romcmd16_lib.c
using mikroC v8.0.0.0 and the onewirelib.zip is 349kb downloaded today.
Regards
not found: _pinMask
not found: _MTRIS
in ow_romcmd16_lib.c
using mikroC v8.0.0.0 and the onewirelib.zip is 349kb downloaded today.
Regards
Abnk 18 Not found
Hi,
Also tried using your library.
With my 18f4550 it came back with a linker error:
I'm using mBasic
Bank 18 not found
Any suggestions ?
tnx
Erwin
Also tried using your library.
With my 18f4550 it came back with a linker error:
I'm using mBasic
Bank 18 not found
Any suggestions ?
tnx
Erwin
Keep the black side under ...
one wire library
Still had trouble using the libraries from MAN (ftp://ttelecom.com.br ), my original compile problem was not using Ow_Config first before calling any of the other routines
However, still could not get things to work (not sure why, hardware test setup works OK with mikroC library for a single device), so, resorted to writing/translating the routines (based upon the code in APPLICATION NOTE 187 from Maxim), and some info in the CCS C-compiler forum). I've copied the resultant mikroC code below for those that are interested. It is still fairly 'raw' and there are bits and pieces of 'testing' stuff left within it, some more error checking/catching should be included here and there, and can be made more efficient/elegant etc, but it works. Checked using one-wire bus for multi-drop (tested with PIC16F876A, 4Mhz clock, and 2 x DS1820's on RC5 on homemade board). I used the routines from MAN (ftp://ttelecom.com.br ) for output via RS232/hyperterminal for testing the code, so modify this as needed for your own output device.
If making modifications/having trouble, please consult the application note for the DS1820 from Dallas as the one-wire system is very specific about timing/protocols etc (had a little trouble with this, the settings for delay_us() within the various routines work for my setup - but have not checked how robust they are). Also, note that many of the routines are for only a single-device (just for testing what I was doing along the way), and you will need to use the example from parts of main() where Ow_MatchRom is used for multi-drop.
Hope this helps somebody, as many of the other posts/info in the forum have helped me in the past.
However, still could not get things to work (not sure why, hardware test setup works OK with mikroC library for a single device), so, resorted to writing/translating the routines (based upon the code in APPLICATION NOTE 187 from Maxim), and some info in the CCS C-compiler forum). I've copied the resultant mikroC code below for those that are interested. It is still fairly 'raw' and there are bits and pieces of 'testing' stuff left within it, some more error checking/catching should be included here and there, and can be made more efficient/elegant etc, but it works. Checked using one-wire bus for multi-drop (tested with PIC16F876A, 4Mhz clock, and 2 x DS1820's on RC5 on homemade board). I used the routines from MAN (ftp://ttelecom.com.br ) for output via RS232/hyperterminal for testing the code, so modify this as needed for your own output device.
If making modifications/having trouble, please consult the application note for the DS1820 from Dallas as the one-wire system is very specific about timing/protocols etc (had a little trouble with this, the settings for delay_us() within the various routines work for my setup - but have not checked how robust they are). Also, note that many of the routines are for only a single-device (just for testing what I was doing along the way), and you will need to use the example from parts of main() where Ow_MatchRom is used for multi-drop.
Hope this helps somebody, as many of the other posts/info in the forum have helped me in the past.
Code: Select all
#define ow_DQPin PORTC.F5 // One Wire Bus pin assignment
#define ow_Reg TRISC.F5
#define DS1820_Tvalue 0 //Temperature LSB (AAh)
#define DS1820_TSign 1 //Temperature MSB (00h) EEPROM
#define DS1820_Thigh 2 //TH Register or User Byte 1
#define DS1820_Tlow 3 //TL Register or User Byte 2
#define DS1820_CntRemain 6 //OUNT REMAIN (0Ch)
#define DS1820_CntPerC 7 //COUNT PER °C (10h)
#define DS1820_CRC 8 //CRC check value
unsigned char TxtBuffer[20]; //buffer for USART output
char i,j; //general purpose counter etc
char ByteTxt[4]; //general purpose to convert byte to txt for display
char scratchPad[9]; //scratchPad memory of DS1820
char DS1820_ROM[8]; //
char temp; //temperature after conversion from DS1820
int rslt,cnt;
//only temporary for using mikroC library
char byteC;
#include "C:\_DOCS\Electronics\MikroC\Headers\SerOut16.h"
#include "C:\_DOCS\Electronics\MikroC\Headers\ByteToHex18.h"
void NewLine() {
Usart_Write(13); Usart_Write(10);
}
void ow_Pull_Low(){
ow_Reg = 0;
ow_DQPin = 0;
}
void ow_Float_High() {
ow_Reg = 1;
ow_DQPin = 1;
}
//******************************************************************************/
// Read bit on one wire bus
char read_bit(void) {
ow_Pull_Low();//output_low(DQ);
delay_us(1); // 1uS min. Original code relied on 8051 being slow
ow_Float_High();//output_float(DQ);
delay_us(10); // Wait at least 15mS from start of time slot ******************************************
if (ow_DQPin == 0) {
return 0;
} else {
return 1;
}
//return(input(DQ)); // Delay to finish time slot (total 60 to 120uS)
} // must be done next.
/******************************************************************************/
void write_bit(char bitval)
{
ow_Pull_Low();//output_low(DQ);
if(bitval == 1) {
delay_us(1); // 1uS min. Original code relied on 8051 being slow
ow_Float_High();//output_float(DQ);
}
delay_us(60); // Wait for end of timeslot
ow_Float_High();//output_float(DQ);
}
char read_byte(void)
{
char i;
char val = 0;
for(i=0;i<8;i++)
{
if(read_bit()) val |= (0x01 << i);
delay_us(51); // To finish time slot
}
return val;
}
/******************************************************************************/
void write_byte(char val)
{
char i;
char temp;
for (i=0;i<8;i++)
{
temp = val >> i;
temp &= 0x01;
write_bit(temp);
}
//delay_us(105);
}
/******************************************************************************
* Function: ow_Init()
* Dependencies: ow_Pull_Low(); ow_Float_High()
* Input: #define for the ow_DQPin, ow_Reg
* Output: returns 1=Init OK (presense pulse detected); 0=Init Failed
* Overview:
* Notes: From the source info, this initialisation will only work for
* a single permanently connected device
* also, interrupts will possibly interfer with the timing needed
* interacting with the DS1820, .: should be turned off at start
* of routine, and then back on afterward
*****************************************************************************/
char Ow_Init() {
Ow_Pull_Low(); // pull 1-wire low for reset pulse
delay_us( 500 ); // Wait > 480us
Ow_Float_High(); // Release data pin (set to input for high) float 1-wire high
delay_us( 80 ); // Wait > 60us for presence pulse, allowing for device variation
if (ow_DQPin == 0) {
//usart_write_constStr(TxtBuffer,"Init OK");newline();
delay_us(420); // wait-out remaining initialisation window, end of presence pulse
return 1; //Init OK
} else {
//usart_write_constStr(TxtBuffer,"Init Fail");newline();
delay_us(420); // wait-out remaining initialisation window, end of presence pulse
return 0; //Init Fail
}
}
/******************************************************************************
* Function: ow_Read_PwrSupply()()
* Dependencies: write_byte(); read_byte()
* Input: #define for the ow_DQPin, ow_Reg
* Output: returns 0=parasite power supply; 1=external power supply
* Overview:
* Notes:
*****************************************************************************/
char Ow_Read_PwrSupply(){
char i;
Ow_Init(); //
write_byte(0xCC); // Issue command SKIP_ROM
write_byte(0xB4); // Issue command READ_POWER_SUPPLY
i=read_byte();
if (i==0) {
return 0; //Parasite
} else {
return 1; //External"
}
}
/******************************************************************************
* Function: ow_Read_ScratchPad(char sPad[8])
* Dependencies: write_byte(); read_byte()
* Input: #define for the ow_DQPin, ow_Reg
* Output:
* Overview:
* Notes:
*****************************************************************************/
void Ow_Read_ScratchPad(char sPad[8]){
char i;
Ow_Init(); //
write_byte(0xCC); // Issue command SKIP_ROM
write_byte(0xBE); // Issue command READ_SCRATCHPAD
for (i=0;i<8;i++) {
sPad[i] = read_byte();
}
}
/******************************************************************************
* Function: ow_Read_ROM(char ROM[8])
* Dependencies:
* Input: #define for the ow_DQPin, ow_Reg
* Output:
* Overview:
* Notes: This function is only useful for a single device on the one-
* bus - otherwise data collision.
*****************************************************************************/
void Ow_Read_ROM(char DS_ROM[8]){
char i;
Ow_Init(); //
write_byte(0x33); // Issue command READ_ROM
DS_ROM[0] = read_byte(); // device family code
for (i=1;i<7;i++) { // device serial number
DS_ROM[i] = read_byte();
}
DS_ROM[7] = read_byte(); // CRC
}
/******************************************************************************
* Function: Ow_Set_Temp_Alarms()
* Dependencies:
* Input: #define for the ow_DQPin, ow_Reg
* Output:
* Overview:
* Notes:
*****************************************************************************/
void Ow_Set_Temp_Alarms(char Thigh, char Tlow){
char i;
char sPad[8];
Ow_Init(); // Master issues reset pulse.
write_byte(0xCC); // Master issues Skip ROM command
write_byte(0x4E); // Master issues Write Scratchpad command.
write_byte(Thigh); // Master sends two data bytes to scratchpad (TH and TL)
write_byte(Tlow);
Ow_Init(); // Master issues reset pulse
write_byte(0xCC); // Master issues Skip ROM command
Ow_Read_ScratchPad(sPad); // Master issues Read Scratchpad command.
// still need to implement the following
// The master then recalculates the CRC of the first eight data bytes from the
// scratchpad and compares the calculated CRC with the read CRC (byte 9).
// If they match, the master continues; if not, the read operation is repeated.
// this will require setting a flag, and using some sort of counter for how many
// tries before returning temp alarms failed to set
Ow_Init(); // Master issues reset pulse
write_byte(0xCC); // Master issues Skip ROM command
write_byte(0x48); // Master issues Copy Scratchpad command.
// Copies TH and TL data from the scratchpad to EEPROM.
Ow_Float_High(); // DQ line held high by strong pullup
delay_us(15); // > 10ms while copy operation is in progress.
}
/******************************************************************************
* Function: Ow_Read_Temp(char sPad[8])
* Dependencies:
* Input: #define for the ow_DQPin, ow_Reg
* Output:
* Overview:
* Notes: The temperature data is read from the DS1820 and returned in
* the variable sPad - the actual display of the temperature data
* is left to the calling routine (manipulate first byte of sPad etc)
* as the display is likely to be specific depending upon output
* device, USART, LCD etc.
*****************************************************************************/
void Ow_Read_Temp(char sPad[8]){
char i;
Ow_Init(); // Master issues reset pulse.
write_byte(0xCC); // Master issues Skip ROM command
write_byte(0x44); // Master issues Convert T command
// if in parasite power mode, DQ line held high by strong pullup
// Master applies strong pullup to DQ for the duration of the conversion (tconv).
// however, not needed if in external power
// this needs to be implemented using call to power_type
Ow_Read_ScratchPad(sPad);
// still need to implement the following
// The master then recalculates the CRC of the first eight data bytes from the
// scratchpad and compares the calculated CRC with the read CRC (byte 9).
// If they match, the master continues; if not, the read operation is repeated.
// The master also calculates the TEMP_READ value and stores the contents of the COUNT
// REMAIN and COUNT PER °C registers.
// this will require setting a flag, and using some sort of counter for how many
// tries before returning temp alarms failed to set
Ow_Init(); // Master issues reset pulse
// still need to implement the following
// CPU calculates extended resolution temperature using the equation in the
// OPERATION — MEASURING TEMPERATURE section of this datasheet.
}
void display_scratchPad_data(){
temp = scratchPad[DS1820_Tvalue]>>1; //right bit shift to get rid of fractional temp, bit 0
usart_write_constStr(TxtBuffer,"T= ");
ByteToStr(temp,ByteTxt);
Usart_Write_Str(ByteTxt);
usart_write_constStr(TxtBuffer,".");
if (scratchPad[DS1820_Tvalue] & 0x0001) { //bit 0 = 1 then fractional = 0.5 else = 0.0
usart_write_constStr(TxtBuffer,"5");
} else {
usart_write_constStr(TxtBuffer,"0");
} // scratchPad[DS1820_TSign] = 1 indicates negative temp, =0 indicates positive temp
usart_write_constStr(TxtBuffer,"°C ");
usart_write_constStr(TxtBuffer,"Counts Remain= ");
ByteToStr(scratchPad[DS1820_CntRemain],ByteTxt);
Usart_Write_Str(ByteTxt);
// usart_write_constStr(TxtBuffer," Counts/°C ");
// ByteToStr(scratchPad[DS1820_CntPerC],ByteTxt); //should always be 0x10 for DS1820
// Usart_Write_Str(ByteTxt);
usart_write_constStr(TxtBuffer," T High Alarm= ");
ByteToStr(scratchPad[DS1820_Thigh],ByteTxt);
Usart_Write_Str(ByteTxt);
usart_write_constStr(TxtBuffer," T Low Alarm= ");
ByteToStr(scratchPad[DS1820_Tlow],ByteTxt);
Usart_Write_Str(ByteTxt);
newLine();
}
//************************************************************************************************************************
//************************************************************************************************************************
// global search state
unsigned char ROM_NO[8];
int LastDiscrepancy;
int LastFamilyDiscrepancy;
int LastDeviceFlag;
unsigned char crc8;
unsigned char Found_ROMs[8][8]; //**************** MINE ADDED
const unsigned char dscrc_table[] = {
0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};
//--------------------------------------------------------------------------
// Calculate the CRC8 of the byte value provided with the current
// global 'crc8' value.
// Returns current global crc8 value
//
unsigned char docrc8(unsigned char value) {
// See Application Note 27
crc8 = dscrc_table[crc8 ^ value];
return crc8;
}
//--------------------------------------------------------------------------
// 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;
unsigned char 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 (!Ow_Init()) { //OWReset()) {
// reset the search
LastDiscrepancy = 0;
LastDeviceFlag = 0;//FALSE;
LastFamilyDiscrepancy = 0;
return 0;//FALSE;
}
// issue the search command
write_byte(0xF0); //OWWriteByte(0xF0);
// loop to do the search
do {
// read a bit and its complement
id_bit = read_bit(); //OWReadBit();
cmp_id_bit = read_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
search_direction = (id_bit_number == LastDiscrepancy);
}
// 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
write_bit(search_direction); //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 = 1;//TRUE;
search_result = 1;//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 = 0;//FALSE;
LastFamilyDiscrepancy = 0;
search_result = 0;//FALSE;
}
return search_result;
}
//--------------------------------------------------------------------------
// Find the 'first' devices on the 1-Wire bus
// Return TRUE : device found, ROM number in ROM_NO buffer
// FALSE : no device present
//
int OWFirst() {
// reset the search state
LastDiscrepancy = 0;
LastDeviceFlag = 0;//FALSE;
LastFamilyDiscrepancy = 0;
return OWSearch();
}
//--------------------------------------------------------------------------
// Find the 'next' devices on the 1-Wire bus
// Return TRUE : device found, ROM number in ROM_NO buffer
// FALSE : device not found, end of search
//
int OWNext() {
// leave the search state alone
return OWSearch();
}
//--------------------------------------------------------------------------
// Verify the device with the ROM number in ROM_NO buffer is present.
// Return TRUE : device verified present
// FALSE : device not present
//
int OWVerify() {
unsigned char rom_backup[8];
int i,rslt,ld_backup,ldf_backup,lfd_backup;
// keep a backup copy of the current state
for (i = 0; i < 8; i++)
rom_backup[i] = ROM_NO[i];
ld_backup = LastDiscrepancy;
ldf_backup = LastDeviceFlag;
lfd_backup = LastFamilyDiscrepancy;
// set search to find the same device
LastDiscrepancy = 64;
LastDeviceFlag = 0;//FALSE;
if (OWSearch()) {
// check if same device found
rslt = 1;//TRUE;
for (i = 0; i < 8; i++) {
if (rom_backup[i] != ROM_NO[i]) {
rslt = 0;//FALSE;
break;
}
}
} else {
rslt = 0;//FALSE;
}
// restore the search state
for (i = 0; i < 8; i++)
ROM_NO[i] = rom_backup[i];
LastDiscrepancy = ld_backup;
LastDeviceFlag = ldf_backup;
LastFamilyDiscrepancy = lfd_backup;
// return the result of the verify
return rslt;
}
//--------------------------------------------------------------------------
// Setup the search to find the device type 'family_code' on the next call
// to OWNext() if it is present.
//
void OWTargetSetup(unsigned char family_code) {
int i;
// set the search state to find SearchFamily type devices
ROM_NO[0] = family_code;
for (i = 1; i < 8; i++)
ROM_NO[i] = 0;
LastDiscrepancy = 64;
LastFamilyDiscrepancy = 0;
LastDeviceFlag = 0;//FALSE;
}
//--------------------------------------------------------------------------
// Setup the search to skip the current device type on the next call
// to OWNext().
//
void OWFamilySkipSetup() {
// set the Last discrepancy to last family discrepancy
LastDiscrepancy = LastFamilyDiscrepancy;
LastFamilyDiscrepancy = 0;
// check for end of list
if (LastDiscrepancy == 0)
LastDeviceFlag = 1;//TRUE;
}
// Sends Match ROM command to bus then device address
char Ow_MatchRom(unsigned char ROM_SerialNum[8]) {
int i;
if (Ow_Init()) { // Master issues reset pulse.
write_byte(0x55); // Master issues Match ROM command
for (i=0;i<8;i++){
write_byte(ROM_SerialNum[i]); // Send ROM code
};
return 1;
} else {
return 0;
}
}
void main() {
// Initialise LCD
ADCON1 = 0x06; // set all PORT A to digital I/O
TRISA = 0;
Lcd_Config(&PORTA,0,1,0,5,4,3,2);
Lcd_Cmd(LCD_TURN_OFF);
//Initializes USART
Usart_Init(2400);
Usart_Write_ConstStr(TxtBuffer,"--- my routines ---");NewLine();
// if (ow_Read_PwrSupply()==0) {
// usart_write_constStr(TxtBuffer,"Parasite");
// } else {
// usart_write_constStr(TxtBuffer,"External");
// }
// NewLine();
//
// Ow_Read_ROM(DS1820_ROM);
// usart_write_constStr(TxtBuffer,"Family Code ");
// if (DS1820_ROM[0]==0x10) {
// Usart_Write_ConstStr(TxtBuffer,"DS1820");
// } else {
// ByteToStr(DS1820_ROM[0],ByteTxt);
// Usart_Write_Str(ByteTxt);
// }
// newLine();
// usart_write_constStr(TxtBuffer,"Serial #");
// for (i=1;i<7;i++) {
// Usart_Write_ConstStr(TxtBuffer," ");
// ByteToStr(DS1820_ROM[i],ByteTxt);
// Usart_Write_Str(ByteTxt);
// }
// newLine();
//
// // this call demonstrates using Ow_Read_ScratchPad and displaying tthe contents
// Ow_Read_ScratchPad(scratchPad);
// display_scratchPad_data();
//
// // this call sets the actual alarm points, need to re-read the scratchPad and
// // then re-display to see the results if desired
// Ow_Set_Temp_Alarms(20, 15);
// do { // continually monitor the temperature
// Ow_Read_Temp(scratchPad);
// display_scratchPad_data();
// delay_ms(1000); // give a little delay between readings
// } while (1);
// find ALL devices
newLine(); usart_write_constStr(TxtBuffer,"List all devices"); newLine();
cnt = 0;
rslt = OWFirst();
while (rslt) {
// print device found
usart_write_constStr(TxtBuffer,"Device #");
IntToStr(cnt++,ByteTxt);
usart_write_Str(ByteTxt);
usart_write_constStr(TxtBuffer," ROM ");
for (i=0;i<8;i++) {
Found_ROMs[cnt][i]= ROM_NO[i];// store found ROM serial
ByteToStr(ROM_NO[i],ByteTxt);
Usart_Write_Str(ByteTxt);
usart_write_constStr(TxtBuffer,",");
}
newline();
rslt = OWNext();
}
usart_write_constStr(TxtBuffer,"Total Found ");
IntToStr(cnt,ByteTxt);
usart_write_Str(ByteTxt);
newline();
do {
for (i=1;i<=cnt;i++){
for (j=0;j<8;j++) ROM_NO[j]=Found_ROMs[i][j];
Ow_Init(); // Master issues reset pulse.
if (Ow_MatchRom(ROM_NO)) {
write_byte(0x44); // Master issues Convert T command
delay_ms(500);
// if parasite powered, would need delay 500ms here
Ow_Init(); // Master issues reset pulse.
if (Ow_MatchRom(ROM_NO)) {
write_byte(0xBE); // Issue command READ_SCRATCHPAD
for (j=0;j<8;j++) {
scratchPad[j] = read_byte();
}
usart_write_constStr(TxtBuffer,"Dev#");
ByteToStr(i,ByteTxt);
usart_write_Str(ByteTxt);
usart_write_constStr(TxtBuffer," ");
//display_scratchPad_data();
temp = scratchPad[DS1820_Tvalue]>>1; //right bit shift to get rid of fractional temp, bit 0
usart_write_constStr(TxtBuffer,"T= ");
ByteToStr(temp,ByteTxt);
Usart_Write_Str(ByteTxt);
usart_write_constStr(TxtBuffer,".");
if (scratchPad[DS1820_Tvalue] & 0x0001) { //bit 0 = 1 then fractional = 0.5 else = 0.0
usart_write_constStr(TxtBuffer,"5");
} else {
usart_write_constStr(TxtBuffer,"0");
}
newLine();
}
}
}
usart_write_constStr(TxtBuffer,"--------------");
newLine();
delay_ms(1000);
} while (1);
// //mikroC routine
// usart_write_constStr(TxtBuffer,"--- mikroC routines ---");
// newline();
// if (Ow_Reset(&PORTC,5)==0) { //0 if DS1820 is present, and 1 if not present
// Usart_Write_ConstStr(TxtBuffer,"reset OK");
// } else {
// Usart_Write_ConstStr(TxtBuffer,"reset fail");
// }
// NewLine();
//
// //Ow_Reset(&PORTC,5); // Onewire reset signal -> 'uses' the prior reset
// Ow_Write(&PORTC,5,0xCC); // Issue command SKIP_ROM
// Ow_Write(&PORTC,5,0xB4); // READ POWER SUPPLY
// i=Ow_Read(&PORTC,5);
// if (i==0) {
// usart_write_constStr(TxtBuffer,"Parasite");
// } else {
// usart_write_constStr(TxtBuffer,"External");
// }
// NewLine();
//
// Ow_Reset(&PORTC,5); // Onewire reset signal
// Ow_Write(&PORTC,5,0x33); // Issue command READ_ROM
// DS1820_ROM[0] = Ow_Read(&PORTC,5);// family code
// for (i=1;i<7;i++) {
// DS1820_ROM[i] = Ow_Read(&PORTC,5);// serial #
// }
// DS1820_ROM[7] = Ow_Read(&PORTC,5);// CRC
//
// if (DS1820_ROM[0]==0x10) {
// Usart_Write_ConstStr(TxtBuffer,"DS1820"); newline();
// }
// Usart_Write_ConstStr(TxtBuffer,"Serial # ");
// for (i=1;i<7;i++) {
// ByteToStr(DS1820_ROM[i],ByteTxt);
// Usart_Write_Str(ByteTxt);
// Usart_Write_ConstStr(TxtBuffer," ");
// }
// newline();
//do { // main loop
// Ow_Reset(&PORTC,5); // Onewire reset signal
// Ow_Write(&PORTC,5,0xCC); // Issue command SKIP_ROM
// Ow_Write(&PORTC,5,0x44); // Issue command CONVERT_T
// Delay_us(120);
// Ow_Reset(&PORTC,5);
// Ow_Write(&PORTC,5,0xCC); // Issue command SKIP_ROM
// Ow_Write(&PORTC,5,0xBE); // Issue command READ_SCRATCHPAD
// Delay_ms(400);
//
// for (i=0;i<8;i++) {
// scratchPad[i] = Ow_Read(&PORTC,5);
// }
// Ow_Reset(&PORTC,5); // Onewire reset signal
//
// Usart_Write_ConstStr(TxtBuffer,"Cnt = ");
// ByteToStr(scratchPad[6],ByteTxt);
// Usart_Write_Str(ByteTxt);
//
// Usart_Write_ConstStr(TxtBuffer," T1 = ");
// byteC=scratchPad[0]>>1;
// ByteToStr(byteC,ByteTxt);
// Usart_Write_Str(ByteTxt);
// byteC = scratchPad[0] & 0x0001;
// if (byteC) {
// Usart_Write_ConstStr(TxtBuffer,".5"); // Print fractional part
// } else {
// Usart_Write_ConstStr(TxtBuffer,".0");
// }
// newline();
// Delay_ms(500);
//} while (1);
}//~!
Thanks
Thanks vlady for your input.
As stated we're looking for the romsearch routines in mB.
I'll try to convert your routine into Basic.
We're using USB communications for temperature logging. We now playing with the options of reading the romcodes from the pc, as it a static setup.
As stated we're looking for the romsearch routines in mB.
I'll try to convert your routine into Basic.
We're using USB communications for temperature logging. We now playing with the options of reading the romcodes from the pc, as it a static setup.
Keep the black side under ...
One wire help
I've downloaded the libraries but i dont know how to use them, i put them all in ....\mikroC\Uses\P16 and P18, i`m using a P16f877A, i have to put any #include in the file?, or only with puting that files in uses i can uses the functions?.. cause when i try to compile my program it says : Undeclared identifier[ow_config]., internal error, ; expexted but ) found.
This is my program, and i cant get it work :oops:
This is my program, and i cant get it work :oops:
Code: Select all
void Main() {
TRISA = 0;
ADCON1 = 0x07;
ADCON0 = 0;
Ow_Config(&PORTA,5);
}
Problem
Thank you for this great job!
I have little problem to get compiled the microC code.
ftp://ttelecom.com.br/onewire/examples/1-wirelib_mc.zip
PIC18F877A
Missing functions
Usart_Write_Str
usart_write_constStr
Can somebody place them?
Project there
http://infoside.lt/~pranas/p16F877A.rar
Thank you
Pranas
I have little problem to get compiled the microC code.
ftp://ttelecom.com.br/onewire/examples/1-wirelib_mc.zip
PIC18F877A
Missing functions
Usart_Write_Str
usart_write_constStr
Can somebody place them?
Project there
http://infoside.lt/~pranas/p16F877A.rar
Thank you
Pranas
- nikola.kostic
- mikroElektronika team
- Posts: 433
- Joined: 11 Aug 2009 12:12
-
- Posts: 1
- Joined: 05 Jul 2009 09:34
Re: 1-WIRE LIBRARY FOR P12,16 AND 18 SINGLE AND MULTIDROP DE
Hi
it is an older post but if sombody need the one wire lib for higher osc there are some modification for above code necessary:
i tested the code with 8 and 20mhz 18f4550
The difference is the delay_us(1); after pull the dataline high or low
it is an older post but if sombody need the one wire lib for higher osc there are some modification for above code necessary:
i tested the code with 8 and 20mhz 18f4550
The difference is the delay_us(1); after pull the dataline high or low
Code: Select all
//******************************************************************************/
// Read bit on one wire bus
char read_bit(void) {
int ret;
ow_Pull_Low();//output_low(DQ);
delay_us(1); //important if osc > 4mHz
delay_us(15);
ow_Float_High();//output_float(DQ);
[b] delay_us(1); [/b]
if (ow_DQPin == 0) {
ret= 0;
} else {
ret= 1;
}
delay_us(45);
return(ret);
}
/******************************************************************************/
void write_bit(char bitval)
{
ow_Pull_Low();//output_low(DQ);
delay_us(1);
if(bitval == 1) {
delay_us(15);
ow_Float_High();//output_float(DQ);
delay_us(1);
delay_us(45);
} else { //bit 0
delay_us(60);
ow_Float_High();
delay_us(1);
}
char read_byte(void)
{
char i;
char val = 0;
for(i=0;i<8;i++)
{
if(read_bit()) val |= (0x01 << i);
//delay_us(51); // To finish time slot
}
return val;
}
/******************************************************************************/
void write_byte(char val)
{
char i;
char temp;
for (i=0;i<8;i++)
{
temp = val >> i;
temp &= 0x01;
write_bit(temp);
}
//delay_us(105);
}
}
Re: 1-WIRE LIBRARY FOR P12,16 AND 18 SINGLE AND MULTIDROP DE
Hi Man
Do you happen to have a copy of the onewire library in this thread as I followed the link and get nothing, as these are the libraries I need to read and work with more than one temp sensor, fingers crossed heres hoping, I'm working with Mickrobasic Pro V6.4
Do you happen to have a copy of the onewire library in this thread as I followed the link and get nothing, as these are the libraries I need to read and work with more than one temp sensor, fingers crossed heres hoping, I'm working with Mickrobasic Pro V6.4