Echoing SPI by UART

Hello!

I need to send by SPI the bit sequence '1000' to a MCP3208 ADC and then receive and send the received data by UART. I'm using the following code:

#include <SPI.h>

void setup (void) {
   Serial.begin(115200); //set baud rate to 115200 for usart
   digitalWrite(SS, HIGH); // disable Slave Select
   SPI.begin ();
   SPI.setClockDivider(SPI_CLOCK_DIV8);//divide the clock by 8
}

void loop (void) {
   char c;
   digitalWrite(SS, LOW); // enable Slave Select
   // send test string
   for (const char * p = "1000" ; c = *p; p++) {
      SPI.transfer (c);
      Serial.print(c);
   }
   digitalWrite(SS, HIGH); // disable Slave Select
   delay(250);
}

But I'm not very sure If this is doing what it supposed to do, specially with the SPI.transfer(c) method. How can I fix my code to send the '1000' and receive the data?

Thanks in advance

      Serial.print(c);
      Serial.println(SPI.transfer (c));

You should send something like this
SPI.transfer(0b1000);
followed by 2 bytes of anything to receive the analog value.

Looking at figures 6-1 and 6-2 it will become more complicated to shift the transmitted and received bits into place.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.