Demo board MC9S08DZ60, global declarations

cancel
Showing results for 
Search instead for 
Did you mean: 

Demo board MC9S08DZ60, global declarations

284 Views
Johnny_x
Contributor I

Hi, I am fairly new to the Freescale line of microcontrollers and not too experienced with ANSI C overall.  What I am trying to do on this demo board is to read in 52 bits from a clocked pin.  The data is in ultimatley in 12x 4-bit words.  I can get this running using a array of char[52].  This is obviosly a huge waste of memory.  This array is declared globally so it can be populated in an interrupt triggered by the clock pin, and manipulated in the ProcessorExpert main() into a char array where I can at least handle the 4-bit words.  I can create a boolean array in the ProcessorExpert main(), but if I try it as an extern bool array[] the compiler throws an error (C2450 Expected: ; = ,) I think this would be an ideal way to collect the bits, either that or make up two long structures.  Any sugguestions?

 

Thanks in advance!

John

Labels (1)
0 Kudos
6 Replies

79 Views
rocco
Senior Contributor II

In addition to Mac's suggestions, it is also possible to do it in hardware.

 

Since your serial bit-stream is clocked, you could have the SPI programmed as a slave and feed it into the SPI. It would only work if:

1) your data was byte-aligned, and

2) it is shifting in MSB first.

 

Since you are working with 4-bit data, you could efficiently manipulate it with the NSA instruction (nibble-swap accumulator).

 

just a thought . . .

0 Kudos

79 Views
kef
Specialist I

rocco,

 

SPI in current Freescale MCUs is configureable MSB first or LSB first. Look for LSBFE bit in SPIC1 register. LSBFE is available at least in S08D.

0 Kudos

79 Views
rocco
Senior Contributor II

Thanks Kef.

 

I knew that was true on some SPI modules, so I looked at the block diagram "Figure 13-2. SPI System Connections" on page 275, and saw only MSB first. But had I looked further to "Figure 13-3. SPI Module Block Diagram" on page 277, I would have seen the LSBFE bit.

0 Kudos

79 Views
bigmac
Specialist III

Hello,

 

The OP seems to be considering 52 clock pulses, with an odd number of nybbles.  For SPI slave to work would require that the master generate 56 clock pulses, so that the final nybble can be read.

 

The high nybble in each byte would need to be extracted with -

hi_nybble = array[i] >> 4;

 

Whether NSA instruction is used will depend on the compiler.

 

Regards,

Mac

 

0 Kudos

79 Views
bigmac
Specialist III

Hello John,

 

The usual way of handling the incoming serial data is to shift each bit value into a multiple byte array - in this case the array could be of byte type, with one nybble of each byte occupied, to represent the final form of the data.  Thus, the array size would be 13 bytes.

 

With assembly code, the shifting through multiple bytes is simple because of direct access to the carry flag.  With C code, the process is somewhat more complex.  With the following untested code snippet, I have assumed that MSB of the data is sent first, so that there is a left shift of each data bit into the last nybble of the array.  However, before this can be done, the rest of the array needs to be rotated left to make room for the new bit value.

 

byte array[13];     // Global array for 13 data nybbles (52 bits)byte bitcount = 52; // Value is decremented as each bit is received// Local variable:byte i;for (i = 0; i < 13; i++) {  // Commence with first nybble  array[i] <<= 1;           // Left shift nybble  array[i] &= 0x0F;         // Mask nybble value  if (i < 12) {             // Not last nybble    if (array[i+1] & 0x08)      array[i] |= 0x01;     // Carry bit from next nybble  }  else {                    // Last nybble of array    if (PTAD_PTAD0)         // Serial input data at PTA0 is assumed      array[12] |= 0x01;  }}bitcount--;                 // Decrement countif (bitcount == 0)          // Test for end of data sequence...

 

Each element of the array should ultimately contain a value within the limits 0-15 decimal.

 

Regards,

Mac

 

0 Kudos

79 Views
bigmac
Specialist III

Hello John,

 

A more efficient version of the shift process than above, where room is made for the incoming data by shifting the next nybble to the previous nybble location within the array.

 

byte array[13];     // Global array for 13 data nybbles (52 bits)byte bitcount = 52; // Value is decremented as each bit is received// Local variable:byte i;if ((bitcount & 0x03) == 0) {  for (i = 0; i < 12; i++)    array[i] = array[i+1];  // Transfer whole nybble  array[12] = 0x00;         // Ready for next serial data nybble}array[12] <<= 1;if (PTAD_PTAD0)  array[12] |= 0x01;bitcount--;                 // Decrement countif (bitcount == 0)          // Test for end of data sequence...

 

Regards,

Mac

 

0 Kudos