This demo shows how to communicate with the DS2482 1-Wire interface chip, using the nz_ds2482.h and nz_ds2482.c driver. It uses non-blocking (asynchronous) DS2482 functions. These functions are more complex to implement than the blocking version, but never cause delays blocking all other code from executing. All blocking function names end with "WATE" (WAiT and Error processing). For example, the blocking version of the owReadByte() function is owReadByteWATE().
It also flashes the system LED. To add DS2482 support to a project, the following must be done:
This project is located in the "src/demos/1wire/ow_ds2482_async_debug" folder of the Netcruzer Download. To compile for Netcruzer Board, open this project in MPLAB X, and select the "Project Configuration" for desired board. For example "SBC66ZL_R1" for the SBC66ZL Revision 1 board. For details click here
2013-10-31, David H. (DH):
#define THIS_IS_MAIN_FILE //Uniquely identifies this as the file with the main application entry function main()
#include "HardwareProfile.h"
#if !defined(DEBUG_CONF_MAIN)
#define DEBUG_CONF_MAIN DEBUG_CONF_DEFAULT //Default Debug Level, disabled if DEBUG_LEVEL_ALLOFF defined, else DEBUG_LEVEL_ERROR
#endif
#define MY_DEBUG_LEVEL DEBUG_CONF_MAIN
#define DS18B20_PARASITIC_POWER
DS2482_INFO objDS2482;
WORD tmrFlashLed;
#define DS2482 (&objDS2482)
BYTE ds18b20_task(DS2482_INFO* pObj);
int main(void) {
DEBUG_PUT_STR(DEBUG_LEVEL_INFO,
"\nThis is a test debug message from ow_ds2482_async_debug");
DIR_SYSLED = OUTPUT_PIN;
ds2482_init(DS2482, 1, DS2482_ADDRESS_00);
delay_ms(20);
while(1)
{
ds18b20_task(DS2482);
ds2482_task(DS2482);
LAT_SYSLED = !LAT_SYSLED;
}
}
}
BYTE ds18b20_task(DS2482_INFO* pObj) {
#define MAX_DEVICES 4
static BYTE deviceAdr[MAX_DEVICES][8];
static BYTE get[10];
static BYTE devices;
static WORD_VAL temp;
static WORD tmrDelay;
static BYTE iRd=0;
static BYTE idx=0;
double fTemp;
typedef enum SM_PRINT_DS18B20_ {
SM_PRTDS_IDLE = 0,
SM_PRTDS_SEARCH,
SM_PRTDS_SEARCH_DONE,
SM_PRTDS_NEXT_DEVICE,
SM_PRTDS_NEXT_DEVICE_SELECT,
SM_PRTDS_NEXT_DEVICE_START_CONVERSION,
SM_PRTDS_NEXT_DEVICE_START_CONVERSION_DELAY,
SM_PRTDS_NEXT_DEVICE_SELECT2,
SM_PRTDS_NEXT_DEVICE_RD_SCRATCH_PAD,
SM_PRTDS_READ_DS18B20_BYTES,
SM_PRTDS_READ_DS18B20_BYTES2,
SM_PRTDS_DONE,
} SM_PRINT_DS18B20;
static SM_PRINT_DS18B20 sm_prtds = SM_PRTDS_IDLE;
#define SM_PRTDS_WAIT_FOR_TXION 0x80 //Wait for DS2482 Transmission - use upper bit 7
#define SM_PRTDS_SM_MASK 0x7F //Mask out all bits above
if (sm_prtds & SM_PRTDS_WAIT_FOR_TXION) {
if (ds2482_isBusy(pObj) == TRUE)
return 1;
if (ds2482_getStatus(pObj) != 0) {
DEBUG_PUT_STR(DEBUG_LEVEL_WARNING,
"\nprintAllAsync_DS18B20 Error!");
sm_prtds = SM_PRTDS_IDLE;
return 0;
}
sm_prtds = sm_prtds & ~SM_PRTDS_WAIT_FOR_TXION;
}
switch (sm_prtds & SM_PRTDS_SM_MASK) {
case SM_PRTDS_IDLE:
devices = 0;
owSearchReset(pObj);
sm_prtds++;
case SM_PRTDS_SEARCH:
if (owSearch(pObj) != 0)
goto PRINT_ALL_ASYNC_DS18B20_ERROR;
sm_prtds = SM_PRTDS_SEARCH_DONE | SM_PRTDS_WAIT_FOR_TXION;
break;
case SM_PRTDS_SEARCH_DONE:
if (owGetAdr64(pObj) != 0) {
if (pObj->adr64[0] == 0x28) {
for (iRd=0; iRd<8; iRd++) {
deviceAdr[devices][iRd]=pObj->adr64[iRd];
}
if (devices == (MAX_DEVICES-1)) {
DEBUG_PUT_STR(DEBUG_LEVEL_INFO,
"\nFound maximum supported number of devices");
break;
}
devices++;
}
else {
#if (MY_DEBUG_LEVEL >= DEBUG_LEVEL_INFO)
for (iRd=0; iRd<8; iRd++) {
}
#endif
}
sm_prtds = SM_PRTDS_SEARCH;
break;
}
idx = 0;
sm_prtds++;
break;
case SM_PRTDS_NEXT_DEVICE:
if(idx++ < devices) {
if (owReset(pObj) != 0)
goto PRINT_ALL_ASYNC_DS18B20_ERROR;
sm_prtds = SM_PRTDS_NEXT_DEVICE_SELECT | SM_PRTDS_WAIT_FOR_TXION;
break;
}
sm_prtds = SM_PRTDS_DONE;
break;
case SM_PRTDS_NEXT_DEVICE_SELECT:
if (owSelect(pObj, &deviceAdr[idx-1][0]) != 0)
goto PRINT_ALL_ASYNC_DS18B20_ERROR;
sm_prtds = SM_PRTDS_NEXT_DEVICE_START_CONVERSION | SM_PRTDS_WAIT_FOR_TXION;
break;
case SM_PRTDS_NEXT_DEVICE_START_CONVERSION:
#if defined(DS18B20_PARASITIC_POWER)
if (owWriteBytePower(pObj, 0x44) != 0) {
#else
if (owWriteByte(pObj, 0x44) != 0) {
#endif
goto PRINT_ALL_ASYNC_DS18B20_ERROR;
}
sm_prtds = SM_PRTDS_NEXT_DEVICE_START_CONVERSION_DELAY | SM_PRTDS_WAIT_FOR_TXION;
break;
case SM_PRTDS_NEXT_DEVICE_START_CONVERSION_DELAY:
if (owReset(pObj) != 0)
goto PRINT_ALL_ASYNC_DS18B20_ERROR;
sm_prtds = SM_PRTDS_NEXT_DEVICE_SELECT2 | SM_PRTDS_WAIT_FOR_TXION;
break;
}
break;
case SM_PRTDS_NEXT_DEVICE_SELECT2:
if (owSelect(pObj, &deviceAdr[idx-1][0]) != 0)
goto PRINT_ALL_ASYNC_DS18B20_ERROR;
sm_prtds = SM_PRTDS_NEXT_DEVICE_RD_SCRATCH_PAD | SM_PRTDS_WAIT_FOR_TXION;
break;
case SM_PRTDS_NEXT_DEVICE_RD_SCRATCH_PAD:
if (owWriteByte(pObj, 0xBE) != 0)
goto PRINT_ALL_ASYNC_DS18B20_ERROR;
sm_prtds = SM_PRTDS_READ_DS18B20_BYTES | SM_PRTDS_WAIT_FOR_TXION;
iRd = 0;
break;
case SM_PRTDS_READ_DS18B20_BYTES:
if (owReadByte(pObj) != 0)
goto PRINT_ALL_ASYNC_DS18B20_ERROR;
sm_prtds = SM_PRTDS_READ_DS18B20_BYTES2 | SM_PRTDS_WAIT_FOR_TXION;
break;
case SM_PRTDS_READ_DS18B20_BYTES2:
get[iRd] = owGetByte(pObj);
if(iRd++ < 9) {
sm_prtds = SM_PRTDS_READ_DS18B20_BYTES;
break;
}
temp.byte.LB = get[0];
temp.byte.HB = get[1];
fTemp = (temp.Val & 0x07f0)>>4;
fTemp += 0.0625 * (temp.byte.LB & 0x0f);
printf(
"\nTempC= %f degrees C\n", fTemp);
sm_prtds = SM_PRTDS_NEXT_DEVICE;
break;
case SM_PRTDS_DONE:
idx = 0;
sm_prtds = SM_PRTDS_NEXT_DEVICE;
}
break;
}
return (sm_prtds!=SM_PRTDS_DONE);
PRINT_ALL_ASYNC_DS18B20_ERROR:
sm_prtds = SM_PRTDS_DONE;
DEBUG_PUT_STR(DEBUG_LEVEL_WARNING,
"\nprintAllAsync_DS18B20 Error!");
return 0;
}
#if defined(HAS_NZ_DEBUGGING)
void debugService(void) {
}
ds2482_reset(DS2482);
}
owReset(DS2482);
if (ds2482_getStatusWait(DS2482) != 0) {
}
}
DEBUG_PUT_STR(DEBUG_LEVEL_INFO,
"\nReceived unknown debug message");
}
}
}
#endif