frasmacon
Posts: 45
Joined: Thu Jan 24, 2013 9:22 pm

Re: SPI ADC was working fine last week and now it's not

Sat Apr 20, 2013 2:46 pm

OK, here's some C code that doesn't seem to work either for some reason... It's also attached (ZIP'ed as I can't attach C.

Code: Select all

//MCP3004 code thing by Fraser May
//Adapted from the examples

#include <bcm2835.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    // If you call this, it will not actually access the GPIO
// Use for testing
//        bcm2835_set_debug(1);

      if (!bcm2835_init())
	return 1;

    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default

    char buf[] = {0x01, 0x80, 0x0};
    bcm2835_spi_transfern(buf, sizeof(buf));
    int adc = ((buf[1]&3) << 8) + buf[2];
    printf("%02X \n", adc);
    bcm2835_spi_end();
    bcm2835_close();
    return 0;
}
Attachments
mcp3004.zip
MCP3004 Code
(626 Bytes) Downloaded 92 times

notro
Posts: 695
Joined: Tue Oct 16, 2012 6:21 pm
Location: Drammen, Norway

Re: SPI ADC was working fine last week and now it's not

Sun Apr 21, 2013 11:57 am

Disclaimer: I haven't tried this communicating with a chip. Only testet it againts spidev and had a look at the spidev source code.

You can also send data on the SPI bus direcly from the shell:

Code: Select all

echo -ne '\x00\x01\x02' > /dev/spidev0.0
This is the function in spidev which is called when writing to the device:
https://github.com/raspberrypi/linux/bl ... dev.c#L192
In case you want to see what it's doing.

Downside: you can't set transfer characteristics like speed, mode etc, as you can with using the ioctl's.
And you can't read at the same time as writing.

But it should be good for some testing.

Ref: http://www.acmesystems.it/spi

frasmacon
Posts: 45
Joined: Thu Jan 24, 2013 9:22 pm

Re: SPI ADC was working fine last week and now it's not

Sun Apr 21, 2013 5:17 pm

OK, I have tried to rethink this and it's really confusing. Basically I'm just trying to work out what is wrong.

So code-wise I have
  • Used my own code
  • Used the C example from BCM_2835.h
  • Used the Python bitbang from Adafruit
and it still doesn't work

I have checked, double-checked, triple-checked and quadruple-checked the wiring. All 10 times I've rewired it. And the problem is there with two identical ADCs which I ordered as a set.

I don't know what else could be wrong. Especially since I have no real way of testing each thing. I now have a multimeter so I may be able to find problems with that.

Return to “Interfacing (DSI, CSI, I2C, etc.)”