/* Initialize pointers to DS1WM addresses. Base address of DS1WM is 0x20000000 */
unsigned char* Command1WM = (unsigned char*)0x20000000; /* Address of Command Register */
unsigned char* Data1WM = (unsigned char*)0x20000001; /* Address of Data Register */
unsigned char* Int1WM = (unsigned char*)0x20000002; /* Address of Interrupt Register */
unsigned char* Clock1WM = (unsigned char*)0x20000004; /* Address DS1WM Clock Divider */
/* The clock divider must be programmed before 1-Wire communication can take place */
/* Refer to the DS1WM datasheet page 4 for the proper programming value for you system clock */
*Clock1WM = (unsigned char)0x12; /* Setup for 32MHz Clock */
/* Reset will generate a reset on the 1-Wire bus. If no presence detect was seen, it will return a 1, */
/* otherwise it returns 0. */
int Reset(void)
{
unsigned char result;
int loop;
int DELAY = 30000; /* Time to Poll for Completion */
/* Choose a length to Poll that is slightly greater than */
/* the maximum possible time to complete the operation */
*Command1WM=0x01; /* Send 1WR Reset */
for( loop=0; loop < DELAY; loop++ ) /* Poll INT Register for command completion */
{
result=*Int1WM;
if(result&0x01) break;
}
if((result&0x02) != 0x00) return 1; /* No Presence Detect Found, Return Error */
return 0;
}
/* Send a byte on the 1-Wire Bus. Poll for completion afterwards */
/* Return 1 if operation timed out, 0 if successful */
int WriteByte(char Data)
{
unsigned char result;
int loop;
int DELAY = 30000; /* Time to Poll for Completion */
/* Choose a length to Poll that is slightly greater than */
/* the maximum possible time to complete the operation */
*Data1WM = Data; /* Transmit Data Byte on the Bus */
for( loop=0; loop < DELAY; loop++ ) /* Poll INT Register for command completion */
{
result=*Int1WM;
if(result&0x0C) == 0x0C ) break; /* Poll TBE & TEMT until both are empty */
}
if(loop == DELAY) return 1; /* Operation Timed Out */
return 0;
}
/* Read a byte from the 1-Wire Bus. Write a 0xFF to create read time slots and poll for receive */
/* buffer full before moving the result to the location pointed to by *Data. Returns a 1 if either */
/* the write 0xFF or read times out, 0 otherwise. */
int ReadByte(char *Data)
{
unsigned char result;
int loop;
int DELAY = 30000; /* Time to Poll for Completion */
/* Choose a length to Poll that is slightly greater than */
/* the maximum possible time to complete the operation */
if(WriteByte((char)0xFF)) return 1; /* Generate 1-Wire read timeslots */
for( loop=0; loop < DELAY; loop++ ) /* Poll INT Register for command completion */
{
result=*Int1WM;
if( result&0x10) == 0x10 ) break; /* Poll RBF until set */
}
if(loop == DELAY) return 1; /* Operation Timed Out */
*Data = *Data1WM; /* Retrieve data that was returned */
return 0;
}