Go Down

Topic: Arduio Due Serial.print Data Are Always=255 (Read 116 times) previous topic - next topic

morris861211

May 09, 2019, 04:36 pm Last Edit: May 09, 2019, 05:29 pm by morris861211
Hi there,

I used Arduino Uno to get the pixel values from ADNS-9800 and it works pretty well.
However, when I use Arduino Due to do the same thing, the serial.print data are always 255. 
I use the baudrate 115200. And if I increase the baudrate over 115200, the serial.print data will turn into error.

I have changed the SPI pins' position according to  https://www.arduino.cc/en/reference/SPI
And I also changed the level voltage from 5V to 3.3V.

Does the problem come up because of the different clock frequency between these two boards?
(Clock frequency is 16MHz for Uno and 84MHz for Duo)
According to the ADNS-9800 datasheet, it is said the Serial Port Clock Frequency of ADNS-9800 is 2MHz.

I'm using Arduino Uno and Due to run the program as below.
Code: [Select]
#include <SPI.h>
#include <avr/pgmspace.h>

/*
 * The circuit:
 * Connect these to the appropriate pins for you board
 * AG - Analog ground
 * DG - Digital ground
 * VI - Voltage in
 * MO - Master out slave in (MOSI)
 * MI - Master in slave out (MISO)
 * SS - Slave Select (ncs)
 */

// Registers
#define REG_Product_ID                           0x00
#define REG_Revision_ID                          0x01
#define REG_Motion                               0x02
#define REG_Delta_X_L                            0x03
#define REG_Delta_X_H                            0x04
#define REG_Delta_Y_L                            0x05
#define REG_Delta_Y_H                            0x06
#define REG_SQUAL                                0x07
#define REG_Pixel_Sum                            0x08
#define REG_Maximum_Pixel                        0x09
#define REG_Minimum_Pixel                        0x0a
#define REG_Shutter_Lower                        0x0b
#define REG_Shutter_Upper                        0x0c
#define REG_Frame_Period_Lower                   0x0d
#define REG_Frame_Period_Upper                   0x0e
#define REG_Configuration_I                      0x0f
#define REG_Configuration_II                     0x10
#define REG_Frame_Capture                        0x12
#define REG_SROM_Enable                          0x13
#define REG_Run_Downshift                        0x14
#define REG_Rest1_Rate                           0x15
#define REG_Rest1_Downshift                      0x16
#define REG_Rest2_Rate                           0x17
#define REG_Rest2_Downshift                      0x18
#define REG_Rest3_Rate                           0x19
#define REG_Frame_Period_Max_Bound_Lower         0x1a
#define REG_Frame_Period_Max_Bound_Upper         0x1b
#define REG_Frame_Period_Min_Bound_Lower         0x1c
#define REG_Frame_Period_Min_Bound_Upper         0x1d
#define REG_Shutter_Max_Bound_Lower              0x1e
#define REG_Shutter_Max_Bound_Upper              0x1f
#define REG_LASER_CTRL0                          0x20
#define REG_Observation                          0x24
#define REG_Data_Out_Lower                       0x25
#define REG_Data_Out_Upper                       0x26
#define REG_SROM_ID                              0x2a
#define REG_Lift_Detection_Thr                   0x2e
#define REG_Configuration_V                      0x2f
#define REG_Configuration_IV                     0x39
#define REG_Power_Up_Reset                       0x3a
#define REG_Shutdown                             0x3b
#define REG_Inverse_Product_ID                   0x3f
#define REG_Motion_Burst                         0x50
#define REG_SROM_Load_Burst                      0x62
#define REG_Pixel_Burst                          0x64

byte initComplete=0;
volatile byte xydat[4];
int16_t * x = (int16_t *) &xydat[0];
int16_t * y = (int16_t *) &xydat[2];

volatile byte movementflag=0;
const int ncs = 10;   

extern const unsigned short firmware_length;
extern const unsigned char firmware_data[];

void setup() {
  Serial.begin(115200);
  pinMode(ncs, OUTPUT);
  SPI.begin();
  SPI.setDataMode(SPI_MODE3);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(8);
  delay(1);
 
  performStartup();
  delay(100);
}


void adns_com_begin(){
  digitalWrite(ncs, LOW);
}

void adns_com_end(){
  digitalWrite(ncs, HIGH);
}


byte adns_read_reg(byte reg_addr){
  adns_com_begin();
  SPI.transfer(reg_addr & 0x7f);
  delayMicroseconds(100);
  byte data = SPI.transfer(0);
 
  delayMicroseconds(1);
  adns_com_end();
  delayMicroseconds(19);

  return data;
}

void adns_write_reg(byte reg_addr, byte data){
  adns_com_begin();
 
  //send adress of the register, with MSBit = 1 to indicate it's a write
  SPI.transfer(reg_addr | 0x80 );
  //sent data
  SPI.transfer(data);
 
  delayMicroseconds(20); // tSCLK-NCS for write operation
  adns_com_end();
  delayMicroseconds(100); // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound
}

void adns_upload_firmware(){

  adns_write_reg(REG_Configuration_IV, 0x02); // bit 1 = 1 for 3k mode, other bits are reserved
 
  adns_write_reg(REG_SROM_Enable, 0xd);
 
  delay(10); // assume that the frame rate is as low as 100fps... even if it should never be that low
 
  adns_write_reg(REG_SROM_Enable, 0x8);
 
  adns_com_begin();
  SPI.transfer(REG_SROM_Load_Burst | 0x80); // write burst destination adress
  delayMicroseconds(15);
 
  unsigned char c;
  for(int i = 0; i < firmware_length; i++){
    c = (unsigned char)pgm_read_byte(firmware_data + i);
    SPI.transfer(c);
    delayMicroseconds(15);
  }
  adns_com_end();
  }

void performStartup(void){
  adns_com_begin(); // ensure that the Serial port is reset
  adns_com_end(); // ensure that the Serial port is reset
  adns_com_begin();
  adns_write_reg(REG_Power_Up_Reset, 0x5a); // force reset
  delay(50);
  adns_read_reg(REG_Motion);
  adns_read_reg(REG_Delta_X_L);
  adns_read_reg(REG_Delta_X_H);
  adns_read_reg(REG_Delta_Y_L);
  adns_read_reg(REG_Delta_Y_H); 
  adns_upload_firmware();
  delay(10);
 
  byte laser_ctrl0 = adns_read_reg(REG_LASER_CTRL0);
  adns_write_reg(REG_LASER_CTRL0, laser_ctrl0 & 0xf0 );
  delay(1);
}

void frameCapture(){
  digitalWrite(ncs, LOW);
  delayMicroseconds(1);
  SPI.transfer(REG_Frame_Capture | 0x80);
  SPI.transfer(0x93);
  SPI.transfer(REG_Frame_Capture | 0x80);
  SPI.transfer(0xc5);

  digitalWrite(ncs, HIGH);

  delayMicroseconds(200); // assuming a very slow frame rate ~200Hz
  digitalWrite(ncs, LOW);
  delayMicroseconds(100); //TsRAD
  byte readys = 0;
  while(readys == 0){
    SPI.transfer(REG_Motion & 0x7f);
    delayMicroseconds(100); // tSRAD
    readys = SPI.transfer(0);
    readys = readys & 1;
    delayMicroseconds(20);
  }

//byte v=bitRead(REG_Motion,0);
//Serial.println(v);


    // prepare to read the pixel burst register continuously.
  SPI.transfer(REG_Pixel_Burst & 0x7f);
  delayMicroseconds(100); // tSRAD

   
 
  int i;
  for(i = 0; i < 900; i++){
    byte output = 0;
    output = SPI.transfer(0);
    // deliver frame pixels.
    Serial.print(0);
    Serial.print(" ");
    Serial.println(output);
    Serial.print(" ");   
    delayMicroseconds(15);   
  }

  digitalWrite(ncs, HIGH);
  delayMicroseconds(4);
  delayMicroseconds(100);
  Serial.println("");
}
  int numCaptured = 0;

  void loop() {
   
        frameCapture();   
   
  }

ard_newbie

#1
May 09, 2019, 04:55 pm Last Edit: Jun 25, 2019, 07:18 pm by ard_newbie
SPI.setClockDivider(42); would be a better option to obtain 2 MHz, isn't it ?

morris861211

I just tried, but serial.print are still 255. :smiley-confuse:  :smiley-confuse:



Go Up