Advertisement
pleasedontcode

ADC Reader rev_02

Mar 5th, 2025
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: ADC Reader
  13.     - Source Code compiled for: Arduino Mega
  14.     - Source Code created on: 2025-03-05 11:51:26
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* i need to interface the arduino mega with the */
  21.     /* MCP3208 ADC in order to read voltages */
  22. /****** END SYSTEM REQUIREMENTS *****/
  23.  
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <SPI.h>  // Include SPI library
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. // Define SPI pins for Arduino Mega
  35. #define SELPIN 10 // Chip select (CS)
  36. #define DATAOUT 51 // MOSI (Master Out, Slave In)
  37. #define DATAIN  50 // MISO (Master In, Slave Out)
  38. #define SPICLOCK  52 // SCK (Serial Clock)
  39.  
  40. // Function to initialize SPI and serial communication
  41. void setup() {
  42.     pinMode(SELPIN, OUTPUT);
  43.     digitalWrite(SELPIN, HIGH); // Deselect ADC
  44.    
  45.     SPI.begin(); // Initialize SPI
  46.     SPI.setClockDivider(SPI_CLOCK_DIV16); // Set SPI clock speed
  47.     SPI.setDataMode(SPI_MODE0); // MCP3208 uses SPI Mode 0
  48.     SPI.setBitOrder(MSBFIRST); // MCP3208 expects MSB first
  49.    
  50.     Serial.begin(57600); // Start serial communication
  51. }
  52.  
  53. int read_adc(int channel) {
  54.     if (channel < 0 || channel > 7) return -1; // Invalid channel check
  55.  
  56.     digitalWrite(SELPIN, LOW); // Enable MCP3208
  57.    
  58.     // MCP3208 requires a 5-bit command: Start (1), Single-ended (1), 3-bit channel, followed by 7 extra bits
  59.     byte command = 0b00000110 | (channel >> 2); // Start bit + Single-ended + 3-bit channel
  60.     byte msb = (channel & 0b11) << 6; // Remaining 2 channel bits in upper byte
  61.  
  62.     // Send command and receive response
  63.     byte highByte = SPI.transfer(command);
  64.     byte lowByte = SPI.transfer(msb);
  65.     byte extraByte = SPI.transfer(0x00); // Read remaining bits
  66.  
  67.     digitalWrite(SELPIN, HIGH); // Deselect ADC
  68.    
  69.     // Combine bytes into a 12-bit result
  70.     int adcValue = ((highByte & 0x0F) << 8) | lowByte;
  71.  
  72.     return adcValue;
  73. }
  74.  
  75. void loop() {
  76.     Serial.println("---------------");
  77.    
  78.     for (int i = 0; i < 8; i++) {
  79.         int value = read_adc(i);
  80.         Serial.print("Chan ");
  81.         Serial.print(i);
  82.         Serial.print(":   ");
  83.         Serial.println(value);
  84.     }
  85.  
  86.     delay(1000);
  87. }
  88.  
  89. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement