MCP3202 and spi problem.

MCP3202 and spi problem.

Postby Claudione » Thu Jun 04, 2015 7:40 pm

Hi Guys.

I got a problem using MCP3202 , the circuit use a pic18F 3 DA and one MCP41010 and finally the MCP3202 , all devices are working properly , the SPI line is ok ,however MCP3202 wont work , i show strange value . Here is my code :
Code: Select all
unsigned int getADC(unsigned char channel) { // Returns 0..4095

unsigned int ch=0;
unsigned int datahigh,datalow;
CSD = 1;
if(channel==0){ch=0xA0;}else{ch=0xE0;}
OpenSPI(SPI_FOSC_64, MODE_00, SMPEND);
CSD = 0;
WriteSPI(0x01);
WriteSPI(ch);
datahigh = ReadSPI();
WriteSPI(0x00);
datalow = ReadSPI();
CSD = 1;
return ((datahigh << 8) | datalow);
}


Someone can help me ?
Last edited by ric on Fri Jun 05, 2015 3:31 am, edited 1 time in total.
Reason: Placed "code" tags around the code.
User avatar
Claudione
 
Posts: 3
Joined: Thu Jun 04, 2015 7:15 pm
Location: Italy
PIC experience: Professional 1+ years with MCHP products

Re: MCP3202 and spi problem.

Postby AussieSusan » Fri Jun 05, 2015 3:56 am

The data sheets for the MCP41010 and MCP3202 show that they have differences in the SPI interfaces: the MCP41010 uses an 8-bit command and an 8-bit data word which means that the PIC18F will "naturally" do the right thing.
However the MCP3202 uses a shorter command and longer data value that make up 17 bits (as shown in 5-1 of its data sheet). You need to read section 6.1 of the MCP3202's data sheet to see how to use an 8-bit SPI peripheral with this ADC.
Also, I don't know the internal operation of the WriteSPI and ReadSPI functions (although they are library functions, they operate differently on different families of devices - I try to avoid them entirely as the SPI peripheral is very simple to use and then you know exactly what is going on) but the general rule is that you should always read the received value from an SPI exchange, even if you don't use it - it stops you reading "old" values if nothing else.
Also, why are you calling the OpenSPI function each time, but not closing it again. I would open it once in my initialisation section and then just use it as needed; there is no value in opening and closing it all the time.
It would help if we knew a bit more about the values you are receiving than they are "strange". Are they shifted by 1 bit; do they not match the incoming bit stream or totally random or what?
Susan
AussieSusan
Verified identity
 
Posts: 93
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: MCP3202 and spi problem.

Postby Claudione » Fri Jun 05, 2015 2:07 pm

Hi Susan Thank you so much for your answer
You are right i missed closeSPI
Ok , as you see i take care (so i hope) of data sheet and i send start bit then i ask for 0xA0 as single channel 0 with msb first , then i read it , i send again a request (bit dont care) as 0x00 the i read LSB , i also added a string like datahigh = datahigh & 0x0F; to put 0 all other bit . Like this :

Code: Select all
uunsigned int getADC(char channel) { // Returns 0..4095

      unsigned int ch;

    CSD = 1;
    BUZ = 1;
      if (channel == 0) {
          ch = 0xA0;
      } else {
          ch = 0xE0;
      }
    OpenSPI(SPI_FOSC_64, MODE_00, SMPEND);
    CSD = 0;
    WriteSPI(0x01); //start bit
   // ReadSPI();
    WriteSPI(ch);
    datahigh = ReadSPI();
    WriteSPI(0x00);
    datalow = ReadSPI();
    CSD = 1;
    CloseSPI();
    datahigh = datahigh & 0x0F; //ignoro i 4 bit piu alti
    return ((datahigh << 8) | datalow);
}


The value returned is sometimes stable but incorrect , sometimes seems random.

Can you explain better about the bit sfited because i suspect is here the problem. The initialisation is showed in code to let you know what i did.
Last edited by ric on Mon Jun 08, 2015 11:44 pm, edited 1 time in total.
Reason: Added code tags. Did NOT edit what appears to be a typo at the start.
User avatar
Claudione
 
Posts: 3
Joined: Thu Jun 04, 2015 7:15 pm
Location: Italy
PIC experience: Professional 1+ years with MCHP products

Re: MCP3202 and spi problem.

Postby ric » Mon Jun 08, 2015 11:50 pm

Note, the same question (and many of the same answers) are at http://www.microchip.com/forums/m874261.aspx
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 454
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: MCP3202 and spi problem.

Postby AussieSusan » Tue Jun 09, 2015 4:36 am

You also missed the more important point: open the SPI peripheral once at the start of the program and close it once at the end if that is actually needed. Don't keep opening it and closing it again.
However I see the issue was resolved in the Microchip forum.
Susan
AussieSusan
Verified identity
 
Posts: 93
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: MCP3202 and spi problem.

Postby Bryan Wilcutt » Fri Jan 29, 2016 8:06 pm

According to pp 15 of the MCP3202 manual (DS21034F), the first 4 bits going on are grouped.

You are sending 0x01 0xa0
It wants: Start:SGL/DIFF:ODD/SIGN:MSBF
1 1:0 1:0 1:0

Try sending out 0x0a instead.
Bryan Wilcutt
 
Posts: 1
Joined: Fri Jan 29, 2016 7:59 pm
PIC experience: Professional 5+ years with MCHP products

Re: MCP3202 and spi problem.

Postby ric » Sun Jan 31, 2016 11:44 am

It's a good idea to read all the replies before adding your own. Then you'd see this problem was fixed back in June. :)
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 454
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products


Return to SSP (IIC, SPI)

Who is online

Users browsing this forum: No registered users and 2 guests