Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.3 kB
2
Indexable
Never
//----- Circuit Pin Recommendations -----
// PWM pin: 16
// Multiplexer Select Pins: 17, 5, 18, 19
// SPI pins for MCP3008 ADC chip (sck, mosi, miso, cs): (14, 13, 12, 25)

//----- TODO: Include library for MCP3008 -----
#include <Adafruit_MCP3008.h>
Adafruit_MCP3008 adc;

//----- TODO: Multiplexer input pins (for ESP32) -----
#define GPIO_PIN 16
#define S0 19
#define S1 18
#define S2 5
#define S3 17


//----- TODO: Number of receiver (RX) and transmitter (TX) lines -----
int numRx = 0;
int numTx = 0;

int MHz = 1000000;
int channel_number = 1;
int duty_resolution = 2;
int frequency = 20;  //MHz
int duty_cycle = 2;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  //----- TODO: Identify the GPIO pin and assign PinMode -----
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);



  //----- TODO: Setup PWM Channel -----
  setupPWM();

  //----- TODO: Initialize MCP3008 chip with SPI -----
  adc.begin(14, 13, 12, 25);  // for ESP32, use (14, 13, 12, 25)
}

void loop() {
  // put your main code here, to run repeatedly:
  //----- TODO: Read through the receiver pins periodically -----
  //selectChannelOut(1);
  for (int chan = 0; chan < 4; chan++) {
    selectChannelOut(chan);
    Serial.print(adc.readADC(chan));
    Serial.print("\t");
  }
}


//----- TODO: Setup PWM Channel -----
void setupPWM() {
  ledcSetup(channel_number, frequency * MHz, duty_resolution);
  ledcAttachPin(GPIO_PIN, channel_number);
  ledcWrite(channel_number, duty_cycle);
}

//----- TODO: Set Select Pin Values -----
void selectChannelOut(int channel) {
  if (channel & 1 == 1) {
    digitalWrite(S0, HIGH);
  } else {
    digitalWrite(S0, LOW);
  }

  if (channel & 2 == 1) {
    digitalWrite(S1, HIGH);
  } else {
    digitalWrite(S1, LOW);
  }

  if (channel & 3 == 1) {
    digitalWrite(S2, HIGH);
  } else {
    digitalWrite(S2, LOW);
  }

  if (channel & 4 == 1) {
    digitalWrite(S3, HIGH);
  } else {
    digitalWrite(S3, LOW);
  }


  // leave this delay at the end of the function,
  // this is for mux to stabilize after each select pin values assignment
  // you can adjust the delay value
  delay(2);
}