时间:2009-04-30 14:15
人气:
作者:admin

long* ChipSelectRegister = (long*)0xFFE00008; /* Address of Chip Select 2 Register */ *ChipSelectRegister = 0x2000203E; /* Base Address of 0x20000000 BA = 0x200 */ /* Chip Select 2 Enabled CSEN = 1 */ /* Write Byte Access Enabled BAT = 0 */ /* Data Float Time Zero TDF = 0 */ /* Page Size of 1 Meg PAGES = 0 */ /* Wait State Enabled WSE = 0 */ /* 8 Standard Wait States NWS = 0x7 */ /* 8 bit Data Bus Width DBW = 0x2 */Once the chip enable has been initialized, communication to the DS1WM can begin. The following ANSI C code shows how to set pointers to the DS1W M's registers, initialize the bus clock, and how to perform the three major bus operations—read, write, and reset. The code assumes that the DS1WM's base address is 0x20000000h and the system clock is 32MHz as before. The Clock Divider register should be programmed as soon as communication to the DS1WM has been established and before any communication on the 1-Wire bus takes place. The WriteByte, ReadByte, and Reset functions can then be called whenever needed. The Write, Read, and Reset functions in this example poll the Interrupt register waiting for the command to be completed. The user's functions, especially in a time sensitive application, should use this time to perform other tasks.
/* 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;
}
1-Wire is a registered trademark of Maxim Integrated Products, Inc.