Pages: [1]   Go Down
Author Topic: Serial over usb & SPI output  (Read 763 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi there

Is it possible to input serial to the arduino uno, over usb, then convert this to SPI so an SPI enabled device can function with it?
Any help would be appreciated.

Thanks
Jack
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8662
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Did you try something simple like this:

Code:
// the sensor communicates using SPI, so include the library:
#include <SPI.h>

// pins used for the connection with the sensor
// the other you need are controlled by the SPI library):
const int chipSelectPin = 7;

void setup() {
  Serial.begin(9600);
  SPI.begin();

  pinMode(chipSelectPin, OUTPUT);
}

void loop() {
  uint8_t MOSIbyte, MISObyte;
  if (Serial.available() > 0)
  {
    // take the chip select low to select:
    digitalWrite(chipSelectPin, LOW);

    // Loop till out of input so we don't de-select after every byte
    while (Serial.available() > 0)
    {
      MOSIbyte = Serial.read();

      MISObyte = SPI.transfer(MOSIbyte);
      Serial.write(MISObyte);
    }

    // take the chip select high to de-select:
    digitalWrite(chipSelectPin, HIGH);
  }
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have tried your code, but nothing can be seen on the serial monitor!
I want to take in data through SPI ports and see that on serial monitor, I am using ADIS16210 sensor and Arduino UNO!
Please let em know your suggestions!!!

Thanks
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8662
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You should start by reading the device's datasheet:

http://www.analog.com/static/imported-files/data_sheets/ADIS16210.pdf

You have to send commands to the device to read the values.  The values are in binary.  If you want something human-readable you will need to interpret the data some before printing the values to your serial monitor.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Singapore
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello John,

I have tried many things but couldn't get any data from the sensor and display it on the serial monitor. Need your serious guidance to resolve this issue asap. I am using the code below.



Code:
//Add the SPI library so we can communicate with the ADIS16210 sensor
#include <SPI.h>

//Assign the Chip Select signal to pin 10.
int CS=10;

//This is a list of some of the registers available on the ADIS16210.
//To learn more about these and the rest of the registers on the ADIS16210, read the datasheet!


#define BIT4         0x0010
#define FLASH_CNT 0x00 // Diagnostics, flash write counter (16-bit binary)
#define SUPPLY_OUT 0x02 // Output, power supply
#define XACCL_OUT 0x04 // Output, x-axis acceleration
#define YACCL_OUT 0x06 // Output, y-axis acceleration
#define ZACCL_OUT 0x08 // Output, z-axis acceleration
#define TEMP_OUT 0x0A // Output, internal temperature
#define XINCL_OUT 0x0C // Output, ±180° x-axis inclination
#define YINCL_OUT 0x0E // Output, ±180° y-axis inclination
#define ZINCL_OUT 0x10 // Output, ±180° z-axis inclination
#define XACCL_NULL 0x12 // Calibration, x-axis acceleration offset null
#define YACCL_NULL 0x14 // Calibration, y-axis acceleration offset null
#define ZACCL_NULL 0x16 // Calibration, z-axis acceleration offset null
#define ALM_MAG_X 0x20 // Alarm, x-axis amplitude threshold
#define ALM_MAG_Y 0x22 // Alarm, y-axis amplitude threshold
#define ALM_MAG_Z 0x24 // Alarm, z-axis amplitude threshold
#define ALM_MAG_S 0x26 // Alarm, system alarm threshold
#define ALM_SMPL_X 0x28 // Alarm, x-axis sample period
#define ALM_SMPL_Y 0x2A // Alarm, y-axis sample period
#define ALM_SMPL_Z 0x2C // Alarm, z-axis sample period
#define ALM_CTRL 0x2E // Operation, alarm control
#define GPIO_CTRL 0x32 // Operation, general I/O configuration and data
#define MSC_CTRL 0x34 // Operation, orientation mode
#define DIO_CTRL 0x36 // Operation, digital I/O configuration and data
#define AVG_CNT 0x38 // Operation, decimation filter configuration
#define SLP_CNT 0x3A // Operation, sleep count
#define DIAG_STAT 0x3C // Diagnostics, system status register
#define GLOB_CMD 0x3E // Operation, system command register
#define LOT_ID1 0x52 // Lot identification, Code 1
#define LOT_ID2 0x54 // Lot identification, Code 2
#define PROD_ID 0x56 // Production identification number
#define SERIAL_NUM 0x58 // Serial Number


void setup()

{
 
  //Configure the SPI connection for the ADIS16210.
 
  //Initiate an SPI communication instance.
  SPI.begin();
 
  SPI.setDataMode(SPI_MODE3);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV32);
 
  //Create a serial connection to display the data on the terminal.
  Serial.begin(9600); // send info to serial port
  Serial.println("Start ADIS16210 Accelorometer Sensor Demo....");
 
 // initialize ADIS16210
//    sensor_init();
   
    while(1)
{
// read sample from device and print to uart
loop();
}
   
   
  }


/**
void sensor_init(void)
{

  // set average count / sample rate to 1Hz (512Hz/(2^9)
write_reg(AVG_CNT,0x09);

        // data ready enabled on DIO1, busy indicator enabled on DIO2
write_reg(DIO_CTRL,0x37);

}
**/


void loop(void)
{
short x_accl, y_accl, z_accl, serialID;

// read accelaration
x_accl = read_reg(XACCL_OUT);
y_accl = read_reg(YACCL_OUT);
z_accl = read_reg(ZACCL_OUT);
        serialID=read_reg(SERIAL_NUM);
       


Serial.print(x_accl);
Serial.print('\n');
delay(1000);


}

/**
void command(unsigned char val)
{
// send command
write_reg(GLOB_CMD,val);
}

**/


/**
void write_reg(unsigned char address, unsigned char value)
{
digitalWrite(CS, LOW); // Enable sensor /CS           // lower !CS pin
SPI.transfer(0x80 | 0x04);
SPI.transfer(value);
digitalWrite(CS, HIGH); // disable sensor /CS              // raise !CS pin

delay(45);   // stall time is 40us for ADIS16210
}
**/

/***

void write_reg_int(unsigned char address, unsigned short data)
{
write_reg(address,data);
write_reg(address+1,data>>8);
}


***/






short int read_reg(unsigned char address)
{
short int data;
short int hi_byte, lo_byte;

digitalWrite(CS, LOW);           // lower !CS pin

// send address (1st two bytes)
SPI.transfer(XACCL_OUT);
SPI.transfer(0x00);

delay(45);   // stall time is 40us on adis16210 with normal mode

// send dummy bytes for data
hi_byte = SPI.transfer(0x00);
lo_byte = SPI.transfer(0x00);

digitalWrite(CS, HIGH);             // raise !CS pin

data = (hi_byte << 8) | lo_byte;

delay(45);   // stall time is 40us on adis16210 with normal mode

// return data read from device at 'address'
//return data;
}
Logged

Pages: [1]   Go Up
Jump to: