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

Interfacing LPC2378 with MCP3208 ADC using SPI protocol

Can anyone provide me the working code of interfacing of any ADC (preferably MCP3208) and LPC2378 or LPC1248 using SPI?
My below code is not working

this is my main pgm:

int main()
{
Init_2378(); //Initialisation of internal peripherals
while(1)
{
  IOCLR0 = CS_ADC; //CS Enable
  spi0_send_Byte(0x064);/*12 Bit of Data with start bit, configuration bits (D2 D1 D0)*/
  spi0_send_Byte(0x000); //Dont care bits
  ADC_Value = spi0_receive_Byte(); /*reads value from SPI bus SOSPDR*/
  IOSET0 = CS_ADC; //CS disable

  putchUART0(ADC_Value>>8); //output on UART
  putchUART0(ADC_Value);

 }
}

This is my SPI Initialisation code

void SPI0_Init()
{
// set up SPI0
//P0.15 (SCK), P0.16 (SSEL), P0.17 (MISO), P0.18(MOSI)
PCONP |= (1 << 8); //On SPI

PINSEL0|= 0xC0000000; //SCK P0.15
PINSEL1|= 0x0000003F; //SSEL, MISO, MOSI

IOSET0=CS_ADC ;/*disabled SSEL =1 Chip Select is active low hence set*/

S0SPCR = 0x0C24; // master mode, CPHA = 0 CPOL = 0, MSB first, 12 bits of data transfer
S0SPCCR = 0x0C; // this makes the SPI0 bus 1Mhz
// (PCLK/12 (0x0C) = 12Mhz/12=1.0)

//on reset SPI is enabled
}

void spi0_send_Byte(unsigned int buf)
{
unsigned int  dummy;
S0SPDR = buf;
while ((S0SPSR & 0x80) == 0); /*wait till SPIF (7th bit) is 0, when SPIF=1 SPI Data transfer is complete*/
dummy = S0SPDR; //dummy read
}

unsigned int spi0_receive_Byte()
{
 unsigned int value;
 S0SPDR = 0xFFFF;/* write dummy byte out to generate clock, then read data from MISO */

while ( !(S0SPSR & SPIF) );
value=S0SPDR;
return(value);
}

The above code transmits max. value of ADC continuously i.e 0F FF (4095)
I am using Channel 1 of ADC and i have connected a 10K pot at input. I dont get any variation in the output of ADC.

  • Hey guys my ADC started working!!
    I was doing one mistake....!
    I was defining P0.16 as SSEL (Slave Select Pin), but it is required to define this pin as a GPIO Pin (declared as output) and not as SPI SSEL pin.
    The above code is same with only few changes as below-

    PINSEL1|= 0x0000003C; //GPIO, MISO, MOSI
    

    and in the main program, no need to send Dont care bits for ADC -

    int main()
    {
    Init_2378(); //Initialisation of internal peripherals
    
    while(1)
     {
     Delay_ADC(10000);
     IOCLR0 = CS_ADC;
     spi0_send_Byte(0x064);//12 Bit of Data
    
     ADC_Value = spi0_receive_Byte();
     IOSET0 = CS_ADC;
    
     putchUART0(ADC_Value>>8);
     putchUART0(ADC_Value);
     }
    
    }