This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SPI and a *pointer

In my application, Main is an opcode handler which calls one of 10 functions based on a serial 0 opcode recieved.

Main also defines an u_char InputArray[4] = {0xE2,0xFF,0xAA, 0xCC).

One of the Opcodes calls a function which takes as an argument, a pointer to the above array. The function is supposed to send the 4 values in the array over the SPI as a master.

What comes out instead is -> 0xE2, 0xAA, 0xCC, 0xCC.

Here is the pertinent part of MAIN and the SPI handler function:

******************************************************************************/
#define _MSC1210_Recieve_
/*****************************************************************************  Include     */
#include "MSC1210_SPI.h"
/*****************************************************************************  Defines     */
/*****************************************************************************  Globals     */
/*****************************************************************************  Prototypes  */
//	Function Prototypes in MSC1210_SPI.h
/*****************************************************************************  MSC1210_SPI */
extern void MSC1210_SPI(unsigned char * pArrayStart)
{
	ES = 0;							// Disable Serial0 Interrupts
	P1DDRH = 0x75;					// P1.4,5,7 = output; P1.6 = input

	PDCON &= 0xFE;					// Turn on SPI
	SPIInit();

	SPIDATA = *pArrayStart++;
	SPIDATA = *pArrayStart++;
	SPIDATA = *pArrayStart++;
	SPIDATA = *pArrayStart;

	while((AIE&0x04)!=0x04)
	{
		;//Nothing, wait for SPIrx
	}

	PDCON ^= 0x01; 					// Turn off SPI
	ES = 1;

}/* MSC1210_SPI */
/*****************************************************************************  Functions   */
// Function: SPIInit(void)
//
// Description:  Setup portions of the SPI interface
//				which will not change when using different
//				Port1 functions
//
void SPIInit(void)
{
	unsigned char data ClearFlags = 0x00;

	//SPICON ->  111x0110 = tclk/256,FIFO on, MSB first, Master, Valid on Edge, Idle Low SCLK
	SPIDATA = ClearFlags;
	ClearFlags = SPIDATA;

	SPICON = 0xF6;
	SPIRCON = 0x82;					// Flush RXbuffer, 4 byte int
	SPITCON = 0xAA;					// Flush TXbuffer, set SCLK drive, 4 byte int
	SPISTART = 0x00;
	SPIEND = 0x04;

}/* SPIInit */


//##############################################################################################
//##############################################################################################

/*****************************************************************************  Globals     */
unsigned char data OpCode = 0x00;
bit ChangeOp = 1;
/*****************************************************************************  Prototypes  */
//  Contained in MSC1210_MAIN.h
/*****************************************************************************  MAIN        */
void MAIN(void)
{
	static unsigned char data InputArray[] = {0xE2,0xF0,0xAA,0xCC};

	const unsigned char data * pArray = &InputArray[0];

	CKCON &= 0xf8; 							// 0 MOVX cycle stretch, timer1 clk/4, timer0 = clk/12
	USEC= 10;								// 	11.8MHz Clock
    MSECH=0x07;          	 			   	// one millisecond high
	HMSEC=0x63;             				// one hundred millisecond interrupt

	MSC1210_Startup();						// Some basic Starup Initialization

	TI_0 = 0;
	EA = 1;									// Enable Global Int

	while(1)								// EOS Super Loop
	{										// Running the Serial Interupt Opcode Handler
		switch(OpCode)
		{
		.
		.
		case 0x37:						// "7"
			ChangeOp = 1;
			MSC1210_SPI(pArray);
			OpCode = 0x00;
			break;
		.
		.
		default:
			break;
		}
	}


I have tried the above shown way with the FIFO. I have also tried double buffered using a for loop to
increment the pointer, and a while to watch for the interupt flags between byte transfers - same result.

I'm not familiar enough with C51 to really sort through the listing. The compiler uses ?C?CLDPTR and I'm
not sure where to find its definition. If anyone would like to see the listing for the complete code, I will
post it. Another note, the same pointer is used to send the same array over Serial 1 using a for loop and while,
and it works fine.

Thanks

0