C/C++

SPI MCP3002 Bananian

3 1435
swipon  
Edited by swipon at May 23, 2016 09:15

I want to write in C simple explaination to read analog value from mcp3002 beacause i've lost a lot of time to understand it.

First of all, i used last version of bananian 16.04 https://dl.bananian.org/releases/bananian-latest.zip

Login as root user

Install the compiler
  1. apt-get install build-essential
Copy the Code



Then i've add the spidev and spi-sun7i in /etc/modules then reboot the system.

  1. echo "spidev" >> /etc/modules
  2. echo "spi-sun7i" >> /etc/modules
  3. modprobe spidev
  4. modprobe spi-sun7i
Copy the Code



I installed the wiringbp pack and verify it with

  1. cd /usr/src
  2. git clone https://github.com/LeMaker/WiringBP.git -b bananapi
  3. cd WiringBP
  4. chmod +x ./build
  5. ./build
  6. cd /root
  7. gpio reset
  8. gpio readall
Copy the Code

Check if spi work with wiringbp
  1. gpio load spi
Copy the Code



Check if spi work with wiringbp mcp3002 library
  1. gpio -x mcp3002:70:0 readall
Copy the Code



Then if all ok is possible to use SPI with C code "spiexample.c" where you can find my comments to understand step by step:


  1. <p>// Compile with gcc -o spiexample.bin spiexample.c –lwiringPi –lpthread
  2. // This program read value from a MCP3002 (http://ww1.microchip.com/downloads/en/DeviceDoc/21294C.pdf)

  3. #include <wiringPi.h>
  4. #include <wiringPiSPI.h>
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <signal.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <unistd.h>

  12. // spi channel 0 or 1
  13. #define CHSPI 0

  14. // Buffer from 1 byte (8 Bit) -> [0-255] or in hex [0x00-0xFF]
  15. uint8_t buffer[2];

  16. // declaration of variable and function
  17. unsigned int value = 0;
  18. unsigned int readSpi(int channel);//function to read the spi value
  19. unsigned int readSpi(int channel) {
  20. //write the buffer
  21. buffer[0] = 0x60 + channel; // value 96+channel = Binary value 01100000 or 01100001
  22. buffer[1] = 0x00;     // value 0
  23. //read the buffer from mcp(sobstitute the original sended buffer)
  24. wiringPiSPIDataRW(CHSPI, buffer, 2);  
  25. // The operation it's in binary code
  26. // we send 4 bit but it return 10 bit
  27. // '& 0x03' consider only last 2 bit
  28. // '<< 8' add 8 zeros on right
  29. printf("Buffer0 max 768: %d\n",((buffer[0] & 0x03) << 8))
  30. printf("Buffer1 max 255: %d\n",buffer[1])
  31. // +buffer[1] adds the two parts of buffer
  32. return ((buffer[0] & 0x03) << 8) + buffer[1];
  33. }void main(int argc, char** argv) {
  34. if (wiringPiSetup() == -1)
  35. {
  36.   printf(“Error: wiringPi setup failed.\n”);
  37.   return 1;
  38. } else {
  39.   if (wiringPiSPISetup(CHANNEL, 1000000) < 0) {
  40.    fprintf (stderr, "SPI Setup failed: %s\n", strerror (errno));
  41.    exit(errno);
  42.   }
  43. }

  44. // Assuming total maximum value 1111111111 = 0x3FF = 1023
  45. value = readSpi(0);
  46. // Print Value in unsigned decimal
  47. printf("Value=%d\n", value);
  48. }
Copy the Code

Now compile the code with the correct linking to the library
  1. gcc -o spiexample.bin spiexample.c –lwiringPi –lpthread
Copy the Code


Launch the binary and read the value:
  1. ./spiexample.bin
Copy the Code



And it is all
Luca
Thanks@swipon!
geometry dash game.

nike hyperdunk
lacoste outlet
longchamp outlet
adidas pure boost
lebron 15
cheap jordans
yeezy boost 350
air yeezy
longchamp handbags
yeezy boost 350
adidas zx flux
off white clothing
nike roshe
lebron james shoes
nike zoom
yeezy boost 350 v2
nike air max
yeezy boost 350
adidas outlet
kyrie 3
basketball shoes
yeezy shoes
yeezy shoes
pandora jewelry
van cleef arpels
adidas nmd runner
longchamps
chrome hearts
longchamp bags
hogan outlet
yeezys
russell westbrook shoes
cheap basketball shoes
new england patriots jersey
adidas tubular shadow
nike zoom running shoe
http://www.kobeshoes.uk
lebron 13
nmd
damian lillard shoes
cartier bracelet
michael kors handbags
nike zoom
kobe shoes
kobe basketball shoes
red bottoms
true religion outlet
burberry outlet
supreme clothing
balenciaga shoes
iniki
yeezy boost 350 v2
curry 4 shoes
goyard handbags
ferragamo belt
birkin bag
basketball shoes
nike flyknit
nike free
jordan 12
cheap jordans
golden goose sneakers
yeezy boost 350
adidas yeezy boost
off white
nhl jerseys
yeezy boost 350 v2
hermes handbags
asics gel kayano
nike cortez men
nike foamposite
christian louboutin
michael kors outlet
yeezy shoes
mlb jerseys
jordan 6
nike polo shirts
kyrie shoes
falcons jersey
nike air huarache
kobe 9
michael kors
paul george shoes
kyrie 4
hermes belt
golden goose
converse outlet store
links of london
adidas nmd
adidas stan smith
yeezys
yeezy boost 350
adidas yeezy
yeezy boost
longchamp
retro jordans
nike sneakers for men
cat boots
michael kors outlet
nike huarache

You have to log in before you can reply Login | Sign Up

Points Rules