时间:2009-04-28 11:50
人气:
作者:admin


| Example | Description |
| DS3900.BoardPresent() | Returns TRUE if the DS3900 is detected during the CdsPic class initialization code, returns FALSE if the DS3900 is not detected. |
| DS3900.Write1(true) | Pin P1 is set to an output and forces its high level. Returns TRUE if no errors are detected during communications. |
| DS3900.Write1(false) | Pin P1 is set to an output and forces its low level. Returns TRUE if no errors are detected during communications. |
| DS3900.Read1(state) | Pin P1 is set to an input, read, and P1's input level is returned to a Boolean variable "state." Returns TRUE if no errors are detected during communications. |
| DS3900Read4(state) | Pin P4 is set to an input, read, and P4's input level is returned to a Boolean variable "state." Returns TRUE if no errors are detected during communications. |
// TOD Add extra initialization here <-Developer Studio Comments
if(DS3900.BoardPresent()) // <- BoardPresent() checks for DS3900
{ // <-If found path
m_sEDIT_Status= "DS3900 Found!"; // new status message, all systems go
DS3900.Write1(false); // initialize clock
DS3900.Write2(false); // initialize reset
OnRead(); // read pots and update edit boxes
}
else
{ // <-If not found path
m_sEDIT_Status = "DS3900 not found!@#$"; // new status message, error detect
UpdateData(FALSE); // Update Dialog Values
MessageBox("DS3900 Not Found
Check Power and Serial Cable
Restart
Applicaion","DS3900 Error");
}
void OnRead()
{
//Variables used by subroutine
int success;
bool bit;
unsigned char mask=0x80;
unsigned char pot0=0;
unsigned char pot1=0;
if(DS3900.BoardPresent()) // Only Read if DS3900 found
{
success = DS3900.Write2(true); // Pull reset high
if(success) // Abort Read if comm fail.
{
success += DS3900.Read5(bit); // Read Cout (stack bit first)
success += DS3900.Write4(bit); // Copy Read Contents to DQ
success += DS3900.Write1(true); // Clock bit
success += DS3900.Write1(false); // Clock bit
m_RADIO_Stack = bit; // <-Update Dialog Box Variable
if(success == 5) // Abort Read if comm fail.
{
for(int x = 0; x <8 ; x++) // Pot 1 Read Loop
{
success += DS3900.Read5(bit); // Read Cout (stack bit first)
success += DS3900.Write4(bit); // Copy Read Contents to DQ
success += DS3900.Write1(true); // Clock bit
success += DS3900.Write1(false); // Clock bit
if(bit) // If bit set, set bit in Pot variable
pot1 |= mask;
mask = mask >> 1; // Adjust Mask for next pass
}
m_ucEDIT_Pot1 = pot1; // <-Update Dialog Box Variable
mask=0x80; // Reset Mask
for(int y = 0; y <8 ; y++) // Pot 0 Read Loop
{
success += DS3900.Read5(bit); // Read Cout (stack bit first)
success += DS3900.Write4(bit); // Copy Read Contents to DQ
success += DS3900.Write1(true); // Clock bit
success += DS3900.Write1(false); // Clock bit
if(bit) // If bit set, set bit in Pot variable
pot0 |= mask;
mask = mask >> 1; // Adjust Mask for next pass
}
m_ucEDIT_Pot0 = pot0; // <-Update Dialog Box Variable
}
}
success += DS3900.Write2(false); // Pull reset low
if(success == 70) // Determine if comm has failed
m_sEDIT_Status = "Successful Read"; // Success Message
else
m_sEDIT_Status = "Read Failed"; // Fail Message
}
UpdateData(FALSE); // <-Triggers Dialog Box Update
}
Notice the last if statement of the read algorithm updates the status message of the dialog box. This will overwrite the "DS3900 Found!" message of the initialization with "Successful Read" before the dialog box is displayed if the DS3900 is initialized successfully.void OnWrite()
{
UpdateData(TRUE); //Read values of Dialog Box
//variables used by subroutine
int success;
unsigned char mask = 0x80;
unsigned char pot0 = m_ucEDIT_Pot0;
unsigned char pot1 = m_ucEDIT_Pot1;
bool bit = false;
if(m_RADIO_Stack) // place stack select bit into "bit" variable.
bit = true;
if(DS3900.BoardPresent()) // Only Write if DS3900 Found
{
success = DS3900.Write2(true); // Pull reset high
if(success) // Write abortion if comm. fail
{
success += DS3900.Write4(bit); // write stack select bit
success += DS3900.Write1(true); // Clock bit
success += DS3900.Write1(false); // Clock bit
if(success == 4) // Write abortion if comm. fail
{
for(int x = 0; x <8 ; x++) // Loop for 8 bits of pot 1
{
if(pot1 & mask) // Read next DQ value with mask
success += DS3900.Write4(true);
else
success += DS3900.Write4(false);
success += DS3900.Write1(true); // Clock bit
success += DS3900.Write1(false); // Clock bit
mask = mask >> 1; // Adjust mask to next position
}
mask = 0x80; // Reset mask
for(int y = 0; y <8 ; y++) // Loop for 8 bits of pot 0
{
if(pot0 & mask) // Read next DQ value with mask
success += DS3900.Write4(true);
else
success += DS3900.Write4(false);
success += DS3900.Write1(true); // Clock bit
success += DS3900.Write1(false); // Clock bit
mask = mask >> 1; // Adjust mask to next position.
}
}
}
success += DS3900.Write2(false); // Pull reset low
}
if(success == 53) // Comm. Pass/Fail notification.
m_sEDIT_Status = "Successful Write"; // Pass Message
else
m_sEDIT_Status = "Write Failed"; // Fail Message
UpdateData(FALSE); // <-Trigger Dialog Update
}