HC12: Problem passing values between routines

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

HC12: Problem passing values between routines

Jump to solution
1,495 Views
NZ_Design
Contributor I
Why does the value being returned from my routine different from the value in the routine sending it?
Im using MC9S12DP256 processor.
 
here is the routine doing the sending
 
uchar is declared as #define uchar unsigned char
 
/*------------------------------------------------------------*
| Name: SPI_Proc. uchar SPI_Proc(cValue);
|
| Description: Read port
*/
uchar SPI_Proc (/*uchar cPort,*/ uchar cValue)
{
 uchar cData
/*  while (!(*(char*)SPI_STATUS[cPort] & SPI_TEF));         // Wait for SPTEF set
 *(char*)SPI_DATA[cPort] = cValue;
 while (!(*(char*)SPI_STATUS[cPort] & SPI_IF));          // Wait for SPIF set
 return *(char*)SPI_DATA[cPort];*/
 
 while ((SPI0SR & 0x20) != 0x20);         // Wait for SPTEF set
 SPI0DR /**(char*)SPI_DATA[cPort]*/ = cValue;
 while ((SPI0SR & 0x20) != 0x20);         // Wait for SPTEF set
 cData = SPI0DR;
 Disp_8b_Dec(cData);
 return cData; /**(char*)SPI_DATA[cPort];*/
}
 
Data is 154 in this routine
 
Next routine in the link is in a different file
 
decleared as extern uchar SPI_Proc(uchar cValue);
 
/*------------------------------------------------------------*
| Name: MAX7301_Read. uchar MAX7301_Read(cCommand);
|
| Description: Read port
*/
uchar MAX7301_Read(/*uchar cDevice,*/ uchar cCommand)
{
 uchar cData;
 
 MAX7301_Write(/*cDevice,*/cCommand | 0x80,0);
 PTS /**(char*)MAX7301_SS_Port[cDevice]*/ &= ~ 0x80; //MAX7301_SS_SET[cDevice];
 (void)  SPI_Proc(/*MAX7301_Port[cDevice],*/0);
 cData = SPI_Proc(/*MAX7301_Port[cDevice],*/0);
 PTS /**(char*)MAX7301_SS_Port[cDevice]*/ |= 0x80; //MAX7301_SS_SET[cDevice];
 Disp_8b_Dec(cData);
 return cData;
}
 
cData is 154
 
the start of the link is
 
void MAX7301_Config_Port (/*uchar cDevice,*/ uchar cCommand, uchar cDir, uchar cBit)
{
 uchar cData;
 
//  Disp_8b_Dec(cCommand);
//  Disp_8b_Dec(cDir);
//  Disp_8b_Dec(cBit);
 cData = MAX7301_Read(/*cDevice,*/cCommand);
//  cData = cRetData;
 Disp_8b_Dec(cData);
 cDir = cDir << (cBit * 2);
 cData &= ~(0x03 << (cBit * 2));
 cData |= cDir;
 MAX7301_Write (/*cDevice,*/cCommand, cData);
//  Disp_8b_Dec(cCommand);
//  Disp_8b_Dec(cData);
 Disp_8b_Dec(255);
}
cData when read fromMAX7301_Read is 48
 
Why is this?
 
 


 

Message Edited by CrasyCat on 2007-04-13 11:51 AM

Labels (1)
Tags (1)
0 Kudos
1 Solution
424 Views
bigmac
Specialist III
Hello,
 
The function SPI_proc()  (as you have modified it), does not wait a sufficient period for the completion of the return data byte.  You need to wait until the SPIF flag is set, to ensure the return data from the SPI slave is valid (what the code you commented out appears to do, in principle).  The following code should do as you intend.
 
uchar SPI_Proc (uchar cValue)
{
  uchar cData;
 
  while ((SPI0SR & 0x20) != 0x20);   // Wait for SPTEF set
  SPI0DR = cValue;
  while ((SPI0SR & 0x80) != 0x80);   // Wait for SPIF set
  cData = SPI0DR;
  Disp_8b_Dec(cData);
  return cData;
}
 
I do not know whether there might also be other problems in communicating with the MAX7301 device.  The CPOL and CPHA settings will need to match its requirements.
 
A secondary comment about your "commenting out" part of the code in a single line - it will make the code easier to read, and less prone to error if you comment out the whole original line, and enter the code that you require in the following line.
 
Regards,
Mac
 

Message Edited by bigmac on 2006-11-2112:17 PM

View solution in original post

0 Kudos
1 Reply
425 Views
bigmac
Specialist III
Hello,
 
The function SPI_proc()  (as you have modified it), does not wait a sufficient period for the completion of the return data byte.  You need to wait until the SPIF flag is set, to ensure the return data from the SPI slave is valid (what the code you commented out appears to do, in principle).  The following code should do as you intend.
 
uchar SPI_Proc (uchar cValue)
{
  uchar cData;
 
  while ((SPI0SR & 0x20) != 0x20);   // Wait for SPTEF set
  SPI0DR = cValue;
  while ((SPI0SR & 0x80) != 0x80);   // Wait for SPIF set
  cData = SPI0DR;
  Disp_8b_Dec(cData);
  return cData;
}
 
I do not know whether there might also be other problems in communicating with the MAX7301 device.  The CPOL and CPHA settings will need to match its requirements.
 
A secondary comment about your "commenting out" part of the code in a single line - it will make the code easier to read, and less prone to error if you comment out the whole original line, and enter the code that you require in the following line.
 
Regards,
Mac
 

Message Edited by bigmac on 2006-11-2112:17 PM

0 Kudos