Pages: [1]   Go Down
Author Topic: Newbie Code for Proofreading  (Read 875 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all, I posted a topic in the project guidance section regarding my control scheme for a digital power supply.  I have most of the code written, but I wanted to present it to the community to see if there are any optimizations I can make, or any syntax that I may have misused or that is lacking.
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#define ENC1A A0
#define ENC1B A1
#define ENC2A A2
#define ENC2B A3
#define LDAC 9
#define RANGE 10
LiquidCrystal_I2C lcd(0x20,16,2);
//Encoder 1, channel A
int EA1 = LOW;
//Encoder 1, channel B
int EA2 = LOW;
//Encoder 2, channel A
int EB1 = LOW;
//Encoder 2, channel B
int EB2 = LOW;
//Raw encoder count value for each encoder
int ENCV;
int ENCI;
//Variable to store up/down count
int ENCPOS1 = 0;
int ENCPOS2 = 0;
//Variables for previous and new values to check for change
int VSET = 0;
int ISET = 0;
int VOLD;
int IOLD;
//Flag for LCD update
int NEW = 0;
//Variable to set coarse or fine adjustment
int A;

void setup()  {
  lcd.init();
  lcd.backlight();
  //SPI communication:  AD5504 DAC interface.
  SPI.begin();
  //Serial communication for initial debugging
  Serial.begin(9600);
  //Setting all pins connected to the encoders as inputs.
  pinMode(ENC1A, INPUT);
  pinMode(ENC1B, INPUT);
  pinMode(ENC2A, INPUT);
  pinMode(ENC2B, INPUT);
  //Setting output pins
  pinMode(LDAC, OUTPUT);
  //turning on pull-up resistors for the encoder pins.
  digitalWrite(ENC1A, HIGH);
  digitalWrite(ENC1B, HIGH);
  digitalWrite(ENC2A, HIGH);
  digitalWrite(ENC2B, HIGH);

}


void loop()  {
  //Check for coarse or fine adjustment
  if (digitalRead(RANGE) == LOW)  {
    A = 64;
  }
  else  {
    A = 1;
  }
  ENCV = ENC1();
  ENCI = ENC2();
  //Adding the configuration bits to the 16 bit instruction
  VSET = (4096 + ENCV);
  ISET = (8192 + ENCI);
  //Check for changes
  if (VSET != VOLD)  {
    //Set flag for LCD update
    NEW = 1;
    //Sending the first 8 bits
    SPI.transfer(highByte(VSET));
    //Second 8 bits
    SPI.transfer(lowByte(VSET));
    //Update the output from register
    digitalWrite(LDAC, LOW);
    delayMicroseconds(1);
    digitalWrite(LDAC, HIGH);
    //Setting the old value to check for changes
    VOLD = VSET;
    Serial.println(ENCV);
    delay(0);
  } 
  else  {
  }
  //Check for changes
  if (ISET != IOLD)  {
    //Set flag for LCD update
    NEW = 1;
    //Sending the first 8 bits
    SPI.transfer(highByte(ISET));
    //Second 8 bits
    SPI.transfer(lowByte(ISET));
    //Update the output from register
    digitalWrite(LDAC, LOW);
    delayMicroseconds(5);
    digitalWrite(LDAC, HIGH);
    //Setting the old value to check for changes
    IOLD = ISET;
    //Serial communication for debugging
    Serial.println(ENCI);
    delay(0);
  } 
  else  {
  }
  delay(2);
  if (NEW == 1)  {
    LCDWrite();
  }

}

//Read encoder 1
int ENC1()  {
  //Read channel A
  EA1 = digitalRead(ENC1A);
  if ((EA2 == LOW) && (EA1 == HIGH))  {
    if (digitalRead(ENC1B) == LOW) {
      ENCPOS1 -= A;
    } 
    else  {
      ENCPOS1 += A;
    }
  }
  if (ENCPOS1 >= 4095)  {
    ENCPOS1 = 4095;
  } 
  else if  (ENCPOS1 <= 0)  {
    ENCPOS1 = 0;
  } 
  else  {
  }
  EA2 = EA1;
  return ENCPOS1;
}

//Read encoder 2
int ENC2() {
  EB1 = digitalRead(ENC2A);
  if ((EB2 == LOW) && (EB1 == HIGH))  {
    if (digitalRead(ENC2B) == LOW) {
      ENCPOS2 -= A;
    } 
    else  {
      ENCPOS2 += A;
    }
  }
  if (ENCPOS2 >= 4095)  {
    ENCPOS2 = 4095;
  }
  else if  (ENCPOS2 <= 0)  {
    ENCPOS2 = 0;
  }
  else  {
  }
  EB2 = EB1;
  return ENCPOS2;

}


//Write to LCD
void LCDWrite()  {
  lcd.setCursor(0,0);
  lcd.print("Set");
  lcd.setCursor(4,0);
  lcd.print(ENCPOS1 * 7.326);
  Serial.println(ENCPOS1 * .007326);
  NEW = 0;
}

Any comments and questions are very welcome and appreciated.  All code is open source under whatever license is appropriate, I'm unsure of how to do that part.  Thank you for looking!

Graham
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
to see if there are any optimizations I can make,
Does it run fast enough?

Syntax?
The compiler will spot that for you.
Logged

Per Arduino ad Astra

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

Theoretically I have no idea if it will run fast enough.  I'm checking, double checking, and triple checking my code before I build the board, since I have no spares to use.  I only have enough of the parts to build it once currently.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Theoretically I have no idea if it will run fast enough
And without performance criteria, neither do we.
Logged

Per Arduino ad Astra

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

Well, I need it to write SPI data to the DAC fast enough that it won't cause any problems with that, and I'm debating whether or not I'll have enough pins to simply abandon the I2C code and directly drive the LCD via parallel 4 bit mode.  Would running the LCD directly slow the code down?  How much lag does there have to be before the SPI starts acting in an unexpected way?  Is there any way that I can write the code more efficiently?  The basic idea is that the uC reads a rotary encoder and turns that into a 12 bit value for a high voltage DAC, which controls a quad op-amp, setting the output voltage and current limit of the power supply.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I need it to write SPI data to the DAC fast enough that it won't cause any problems with that
Nope, you're still not helping.
How fast would that need to be?
Logged

Per Arduino ad Astra

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

There's a timing diagram in the datasheet, but I have no idea how to read this information.  I'm using an AD5504 from Analog Devices, datasheet here. http://www.analog.com/static/imported-files/data_sheets/AD5504.pdf
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think I'm still missing any performance criteria.
Logged

Per Arduino ad Astra

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

I'm not sure what you mean by that.  I just don't want to screw up my $12 DAC or $15 opamp.  I want my interface to be responsive and to make sure that the DAC doesn't get loaded with erroneous data and try to output something I didn't tell it to.  I'm not sure what you want for performance criteria.
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want my interface to be responsive
I want my employer's payroll system to be responsive and pop out a bank transfer once a month.
On the other hand, I'd probably want my ABS brakes to operate on a slightly shorter timescale.

It is all relative, see?

Quote
make sure that the DAC doesn't get loaded with erroneous data and try to output something I didn't tell it to.
How would it do that?
« Last Edit: February 19, 2011, 03:19:37 pm by Groove » Logged

Per Arduino ad Astra

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

Ah, I see what you mean.  Well, I'm unsure of what time lags would be perceptible to a user.  I understand that there will be some amount of time for the encoder to be read, the serial data to be calculated and transmitted, and the DAC to update, however I don't know how much time this will take or how long it has to take before the user feels like it's updating overly slowly.  Like playing a video game and having to wait for loading screens.  I want it to be as smooth and seamless as possible, while being slow enough that there is little chance for mistakes.  What kind of time difference from user input to measurable output would seem sluggish?
Logged

Pages: [1]   Go Up
Jump to: