Advertisement
pleasedontcode

**ADC Reading** rev_01

Mar 5th, 2025
194
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 Reading**
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2025-03-05 11:48:36
  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. /* START CODE */
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <SPI.h>  // Include SPI library
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. // Define SPI pins for Arduino Mega
  34. #define SELPIN 10 // Chip select (CS)
  35. #define DATAOUT 51 // MOSI (Master Out, Slave In)
  36. #define DATAIN  50 // MISO (Master In, Slave Out)
  37. #define SPICLOCK  52 // SCK (Serial Clock)
  38.  
  39. // Function to initialize SPI and serial communication
  40. void setup() {
  41.     pinMode(SELPIN, OUTPUT);
  42.     digitalWrite(SELPIN, HIGH); // Deselect ADC
  43.    
  44.     SPI.begin(); // Initialize SPI
  45.     SPI.setClockDivider(SPI_CLOCK_DIV16); // Set SPI clock speed
  46.     SPI.setDataMode(SPI_MODE0); // MCP3208 uses SPI Mode 0
  47.     SPI.setBitOrder(MSBFIRST); // MCP3208 expects MSB first
  48.    
  49.     Serial.begin(57600); // Start serial communication
  50. }
  51.  
  52. int read_adc(int channel) {
  53.     if (channel < 0 || channel > 7) return -1; // Invalid channel check
  54.  
  55.     digitalWrite(SELPIN, LOW); // Enable MCP3208
  56.    
  57.     // MCP3208 requires a 5-bit command: Start (1), Single-ended (1), 3-bit channel, followed by 7 extra bits
  58.     byte command = 0b00000110 | (channel >> 2); // Start bit + Single-ended + 3-bit channel
  59.     byte msb = (channel & 0b11) << 6; // Remaining 2 channel bits in upper byte
  60.  
  61.     // Send command and receive response
  62.     byte highByte = SPI.transfer(command);
  63.     byte lowByte = SPI.transfer(msb);
  64.     byte extraByte = SPI.transfer(0x00); // Read remaining bits
  65.  
  66.     digitalWrite(SELPIN, HIGH); // Deselect ADC
  67.    
  68.     // Combine bytes into a 12-bit result
  69.     int adcValue = ((highByte & 0x0F) << 8) | lowByte;
  70.  
  71.     return adcValue;
  72. }
  73.  
  74. void loop() {
  75.     Serial.println("---------------");
  76.    
  77.     for (int i = 0; i < 8; i++) {
  78.         int value = read_adc(i);
  79.         Serial.print("Chan ");
  80.         Serial.print(i);
  81.         Serial.print(":   ");
  82.         Serial.println(value);
  83.     }
  84.  
  85.     delay(1000);
  86. }
  87.  
  88. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement