Advertisement
Guest User

Untitled

a guest
Aug 25th, 2013
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.07 KB | None | 0 0
  1. /*
  2.  * Controlling MCP3208 ADC by SPI (using spidev driver)
  3.  *
  4.  * Copyright (c) 2007  MontaVista Software, Inc.
  5.  * Copyright (c) 2007  Anton Vorontsov <avorontsov@ru.mvista.com>
  6.  *
  7.  * Modifications 2012 by Claus Khnel <info@ckuehnel.ch>
  8.  *
  9.  * This program is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License.
  12.  *
  13.  * Compile on Raspberry Pi by gcc mpc3208 -o mpc3208
  14.  */
  15.  
  16. #include <stdint.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <getopt.h>
  21. #include <fcntl.h>
  22. #include <sys/ioctl.h>
  23. #include <linux/types.h>
  24. #include <linux/spi/spidev.h>
  25.  
  26. #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
  27.  
  28. #define DEBUG 1
  29.  
  30. // Configuration bits for single-ended channel selection
  31. #define CHANNEL0 0x060000   // Channel 0
  32. #define CHANNEL1 0x064000   // Channel 1
  33. #define CHANNEL2 0x068000   // Channel 2
  34. #define CHANNEL3 0x06C000   // Channel 3
  35. #define CHANNEL4 0x070000   // Channel 4
  36. #define CHANNEL5 0x074000   // Channel 5
  37. #define CHANNEL6 0x078000   // Channel 6
  38. #define CHANNEL7 0x07C000   // Channel 7
  39.  
  40. static const char *device = "/dev/spidev0.0";
  41. static uint8_t mode;
  42. static uint8_t bits = 8;
  43. static uint32_t speed = 500000;
  44. static uint16_t delay;
  45.  
  46. uint32_t channel;
  47.  
  48. static void pabort(const char *s)
  49. {
  50.     perror(s);
  51.     abort();
  52. }
  53.  
  54. static void transfer(int fd)
  55. {
  56.     uint16_t adc;
  57.     uint8_t ret,i;
  58.  
  59.     uint8_t tx[3];      // 3 bytes will be sent to MPC3208
  60.     uint8_t rx[ARRAY_SIZE(tx)] = {0, };
  61.    
  62.     for (i = 0; i < ARRAY_SIZE(tx); i++)
  63.     {
  64.         tx[ARRAY_SIZE(tx)-i-1] = channel%256;
  65.         channel = channel >> 8;
  66.     }
  67.  
  68.     printf("Sent bytes    : ");
  69.     for (i=0; i < ARRAY_SIZE(tx); i++)
  70.         printf("%02X ", tx[i]);
  71.     printf("\n");
  72.  
  73.     struct spi_ioc_transfer tr =
  74.     {
  75.         .tx_buf = (unsigned long)tx,
  76.         .rx_buf = (unsigned long)rx,
  77.         .len = ARRAY_SIZE(tx),
  78.         .delay_usecs = delay,
  79.         .speed_hz = speed,
  80.         .bits_per_word = bits,
  81.     };
  82.  
  83.     ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr);
  84.     if (ret < 1)
  85.         pabort("can't send spi message");
  86.  
  87.     printf("Received bytes: ");
  88.     for (i = 0; i < ARRAY_SIZE(tx); i++)
  89.         printf("%02X ", rx[i]);
  90.     printf("\n");
  91.  
  92.     adc = (rx[1] << 8) + rx[2];
  93.     adc &= 0xFFF;
  94.     printf("ADC = %5X (hex)\n", adc);
  95.     printf("ADC = %5d (dec)\n", adc);
  96.     printf("ADC Voltage = %g mV\n", (float) adc *3300/4096);
  97. }
  98.  
  99. int main(int argc, char *argv[])
  100. {
  101.     int ret = 0;
  102.     int fd;
  103.      
  104.     fd = open(device, O_RDWR);
  105.     if (fd < 0)
  106.         pabort("can't open device");
  107.  
  108.      // max speed hz
  109.     ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
  110.     if (ret == -1)
  111.         pabort("can't set max speed hz");
  112.  
  113.     ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed);
  114.     if (ret == -1)
  115.         pabort("can't get max speed hz");
  116.  
  117. #if DEBUG
  118.     printf("spi device: %s\n", device);
  119.     printf("spi mode: %d\n", mode);
  120.     printf("bits per word: %d\n", bits);
  121.     printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
  122. #endif
  123.  
  124.     channel = CHANNEL7;  // we use channel 7 here
  125.  
  126.     transfer(fd);
  127.  
  128.     close(fd);
  129.  
  130.     return ret;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement