Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F97J60 with MCP3301 ADC

Status
Not open for further replies.

Pheetuz

Full Member level 3
Joined
Feb 12, 2010
Messages
162
Helped
21
Reputation
42
Reaction score
19
Trophy points
1,298
Location
Brighton
Activity points
2,835
Hi folks,
I am using a microchip PIC18F97J60 along with a 13bit MCP3301 A2D which I have connected up to my PICs SPI port, I am sure that the hardware wiring is correct, the input to this A2D is in the form of two unity gain OP amps which are outputting well within the range of the A2D.
Whenever I read the A2D I keep getting strange values, either 0b00010000 0b00000000 or 0b00011111 0b11111111 which looks to me like I am actually getting data out of the A2D as only 13bits seem to be being used, I don't currently have access to a scope so I cant see what is really going on.

The code I am using is shown below:

Code:
#include <P18F97J60.h>

void sampleADC (void);

void init_spi(void)
{
	/* Configure the SPI port pins */
	TRISDbits.RD6 = 0x00;


	/* Configure control register */
	SSP2CON1 = 0x00;
	/* Slave mode with SS2 pin disabled */
	SSP2CON1bits.SSPM1 = 0x01;
	//SSP2CON1bits.SSPM2 = 0x01;
	/* Clock idles low */
	SSP2CON1bits.CKP = 0x01;	

	/* Configure status register */
	SSP2STAT = 0x00;
	SSP2STATbits.CKE = 0x00;	/* Transmit data on falling edge. */
	SSP2STATbits.SMP = 0; 	  	/* Input sampled at end of data output time. */
	SSP2STATbits.BF = 0; 		/* Clear receive buffer */

	/* Clear buffer */
	SSP2BUF = 0x00;

	/* CS idles high */
	LATCbits.LATC7 = 1;

	/* Make SPI CS an output */
	TRISCbits.RC7 = 0;

	/* We need to disable the UART so we can use the pin as GPIO */
	RCSTA1bits.SPEN = 0;

	/* Enable SPI port */
	SSP2CON1bits.SSPEN = 0x01;	

	/* Need to reset the CS line */
	PORTCbits.RC7 = 0;
	PORTCbits.RC7 = 1;
	sampleADC();
}


void sampleADC (void)
{
	unsigned char temp[2] = {0,0};
	unsigned int cnt = 0;

	while(1)
	{
		PORTCbits.RC7 = 0;
		SSP2BUF = 0x01;
		while(SSP2STATbits.BF != 1);

		temp[0] = SSP2BUF;
		SSP2STATbits.BF = 0;
		SSP2BUF = 0x01;
		while(SSP2STATbits.BF != 1);
		temp[1] = SSP2BUF;
		SSP2BUF = 0;
		SSP2STATbits.BF = 0;		
		PORTCbits.RC7 = 1;

		while(++cnt);
	}

	while(1);

}

Please ignore most of the comments as they dont acurately reflect the code in its current state (they did before I started tinkering to get it to work properly!)

Any help is much appreciated.

Cheers,
Pheetuz.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top