Show Posts
Pages: 1 ... 54 55 [56] 57 58 ... 110
826  Using Arduino / Microcontrollers / Re: Solution to PCREL error for avr25. on: November 27, 2012, 07:54:59 am
The problem this thread covers only applies to ATTiny's. Atmega328s are a totally different breed of avr.

The issue you are facing is not due to the compiler, so you should start a new thread asking for help.
827  Using Arduino / General Electronics / Re: what do you mean by biasing? on: November 25, 2012, 06:09:45 am
Your english is good enough to ask here.
Just thought I'd point out that you get 1.7 million google hits if you search 'biasing in transistor', and the first hit has information on questions 1-3.

Is it just me or does this sound like a school/uni assignment.
828  Using Arduino / Microcontrollers / Re: SPI read write for 32bits data on: November 23, 2012, 07:55:52 am
It looks like there is more to this than meets the eye. For example there are two types of write - one which writes commands, one which writes registers. As such there are now two write functions.

Secondly, it looks like you need to send a reset command before you can use it.

Thirdly you wanted continuous conversion mode, so that needs to be set.

As for which mode it is, the datasheet seems totally ambiguous. In the diagrams on page 12, compare SDI Write and SDO Read diagrams, in the first you will see the additional falling edge I mentioned, in the second you will see none. So who knows which mode it is - could be 0 could be 3 - try both.

Finally make sure that the MODE pin on the IC is connected to ground.

Code:
#include <SPI.h>

union FourByte{
    struct {
      unsigned long value:24; //24bit register values go in here
      byte command:8; //8bit command goes in here.
    };
    byte bit8[4]; //this is just used for efficient conversion of the above into 4 bytes.
};

void setup(){
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE3); //I believe it to be Mode3
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  pinMode(SS, OUTPUT); //not really necessary as it is done by the SPI library.
  digitalWrite(SS, HIGH);  
  
  Serial.begin(9600);
  
  unsigned long check = SPI_read(0x00); //read the config register.
  Serial.print("Config = ");
  Serial.println(check);
  
  //Software reset:
  SPI_writeCommand(0x80);
  
  unsigned long status;
  do {
    status = SPI_read(0b00011110); //read the status register
    status &= (1UL<<23);
  } while (!status); //keep checking until DRDY bit gets set.

  SPI_writeCommand(0xE8); //Set continuous conversion mode  
  
  
  check = SPI_read(0x00); //read the config register.
  Serial.print("Config = ");
  Serial.println(check);

/*
  //example of writing a register
  union FourByte data;
  data.command = 0b01000000; //Write to config register
  data.value = 1; //This is the default value from datasheet, just using it as an example.
  SPI_writeRegister(data);*/
}

void loop(){
  delay(1000);
    //example of reading data
  unsigned long voltage = SPI_read(4);//Instantaneous Voltage Channel 1
  Serial.print("Voltage = ");
  Serial.println(voltage);
}

void SPI_writeCommand(byte command) {
  digitalWrite(SS,LOW); //SS goes low to mark start of transmission
  union FourByte data = {0xFEFEFE,command}; //generate the data to be sent, i.e. your command plus the Sync bytes.
  for(char i = 3; i >= 0; i--){
    SPI.transfer(data.bit8[i]); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
  }
  digitalWrite(SS,HIGH);
}
void SPI_writeRegister(union FourByte data) {
  digitalWrite(SS,LOW); //Using CS pin, so sync1/sync0 bytes not needed
  for(char i = 3; i >= 0; i--){
    SPI.transfer(data.bit8[i]); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
  }
  digitalWrite(SS,HIGH);
}

unsigned long SPI_read(byte command){
  digitalWrite(SS,LOW); //SS goes low to mark start of transmission
  union FourByte data = {0xFEFEFE,command}; //generate the data to be sent, i.e. your command plus the Sync bytes.
  for(char i = 3; i >= 0; i--){
    data.bit8[i] = SPI.transfer(data.bit8[i]); //send the data whilst reading in the result
  }
  digitalWrite(SS,HIGH); //SS goes high to mark end of transmission
  return data.value; //return the 24bit value recieved.
}

Could you try running that with:
Code:
SPI.setDataMode(SPI_MODE3);

And with
Code:
SPI.setDataMode(SPI_MODE0);

In each case, what value is returned on the Serial Monitor for the line "Config = "
829  Using Arduino / Microcontrollers / Re: SPI 32-bits Fetching data for IC on: November 22, 2012, 04:09:38 pm
I think this thread has spun off from this one:
http://arduino.cc/forum/index.php/topic,133133.15.html
In which case in my original code, this line:
Code:
data.bit8 = SPI.transfer(data.bit8);

was:
Code:
data.bit8[i] = SPI.transfer(data.bit8[i]);

In which four special bytes are sent to recieve 3 bytes in return (the first of the four recieved bytes gets ignored when the function returns the 24bit field in the struct)

similarly in the write function, this:
Code:
    SPI.transfer(data.bit8); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
was/should be
Code:
    SPI.transfer(data.bit8[i]); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
830  Using Arduino / Microcontrollers / Re: SPI read write for 32bits data on: November 22, 2012, 02:47:47 pm
I also got it from vendor smiley.All I do is read the data sheet and managing for the code based on the data sheet.But I'm also still on the way so we can help each other out.

@Tom BTW What's this line also...Thanks a lot

Code:
data.value = 1; //This is the default value from datasheet, just using it as an example.

Basically that is setting the value you want to write to a register.

The bit of code relavent to that snippit is:
Code:
 //example of writing data
  union FourByte data;
  data.command = 0b01000000; //Write to config register
  data.value = 1; //This is the default value from datasheet, just using it as an example.
  SPI_write(data);

In the code, the command is set to write to the config register (0b01000000 from the datasheet - page 26), and then the value to write to that register is 1 (if you look at the list of commands in the datasheet it shows that the default value for the config register is 1, this was just an example of usage, not a requirement).

When you are reading a register, the lower three bytes should be 0xFEFEFE as you pointed out. This is done in the SPI_Read() function for you.



@Paradigm yes,I'm on the same boat as you.  smiley and I used Energy IC diagram as in the data sheet page 42 to join the Arduino Uno....working out on the program..

@Tom  Thanks for the correction.. How can I print the voltage?I mean when I print out,the voltage value remains at 0.Should this line be in the loop?
Code:
unsigned long voltage = SPI_read(4);//Instantaneous Voltage Channel 1


That is an example of how to read a register, it doesn't actually do anything with the returned value. You could do something like this:

Code:
void loop() {
  delay(1000);
  unsigned long voltage = SPI_read(4);//Instantaneous Voltage Channel 1
  Serial.println(voltage); //print the voltage to the serial monitor (you will need to have called Serial.begin(...) in the setup() for it to work)
}


Quote
Also, it is definitely MODE_3. The datasheet talks about the SDI setup time before rising edge which is indicative of MODE_3 which the timing diagrams confirm.
How do you figure that? The way I see it, the timing diagram shows the capture is on the first rising edge after the CS goes LOW. That timing specification is marked (on the write timing) as "t3" (time required between the CS Falling and SCLK Rising) (edit: and "t6" on the read timing).

The read timing shows this best. SCLK is idle LOW with clock pulses going HIGH. That is SPI Mode 0.
http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus

Are we having fun yet?  smiley


The way I see the timing diagrams, it doesn't matter where the clock IDLEs in SPI. The clock is high when/as the SS line goes low. So we have:

SCLK is high.
SS drops low.
SCLK drops low.
MOSI is set to the correct value
SCLK rises high -> data is clocked in on the rising edge

All of that is indicative of MODE3.

Another way of putting it is that there is always a falling edge of SCLK before the first rising edge on which data is clocked - this is MODE3.
831  Using Arduino / Microcontrollers / Re: SPI read write for 32bits data on: November 22, 2012, 05:49:09 am
You are correct that the clock for the IC should be 4MHz. However at 4MHz, the serial clock frequency for its SPI interface is limited to a maximum of 2MHz, which is specified on page 11 of the datasheet.

As the Uno runs at 16MHz, then
SPI_CLOCK_DIV2 = 8MHz,
SPI_CLOCK_DIV4 = 4MHz,
SPI_CLOCK_DIV8 = 2MHz,
SPI_CLOCK_DIV16 = 1MHz.

As such you will see this line in the code in my last post:
  SPI.setClockDivider(SPI_CLOCK_DIV16);

So that the SPI interface will run at 1MHz. It is usually best to run below the maximum value which is why I suggest using 1MHz not 2MHz.
832  Using Arduino / Microcontrollers / Re: SPI read write for 32bits data on: November 22, 2012, 05:04:52 am
Right, all of the code appears to be doing what is expected, however I have just had it up on the oscilloscope and the default SCLK rate (8MHz) is far far faster than the chips absolute maximum SCLK rate (2MHz).

Also, it is definitely MODE_3. The datasheet talks about the SDI setup time before rising edge which is indicative of MODE_3 which the timing diagrams confirm.

Try this:
Code:
#include <SPI.h>

union FourByte{
    struct {
      unsigned long value:24; //24bit register values go in here
      byte command:8; //8bit command goes in here.
    };
    byte bit8[4]; //this is just used for efficient conversion of the above into 4 bytes.
};

void setup(){
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE3); //I believe it to be Mode3
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  pinMode(SS, OUTPUT); //not really necessary as it is done by the SPI library.
  digitalWrite(SS, HIGH);
  //Page
  //example of reading data
  unsigned long voltage = SPI_read(4);//Instantaneous Voltage Channel 1
  
  //example of writing data
  union FourByte data;
  data.command = 0b01000000; //Write to config register
  data.value = 1; //This is the default value from datasheet, just using it as an example.
  SPI_write(data);
}

void loop(){
  
}

void SPI_write(union FourByte data) {
  digitalWrite(SS,LOW); //Using CS pin, so sync1/sync0 bytes not needed
  for(char i = 3; i >= 0; i--){
    SPI.transfer(data.bit8[i]); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
  }
  digitalWrite(SS,HIGH);
}

unsigned long SPI_read(byte command){
  digitalWrite(SS,LOW); //SS goes low to mark start of transmission
  union FourByte data = {0xFEFEFE,command}; //generate the data to be sent, i.e. your command plus the Sync bytes.
  for(char i = 3; i >= 0; i--){
    data.bit8[i] = SPI.transfer(data.bit8[i]); //send the data whilst reading in the result
  }
  digitalWrite(SS,HIGH); //SS goes high to mark end of transmission
  return data.value; //return the 24bit value recieved.
}
833  Using Arduino / Microcontrollers / Re: SPI read write for 32bits data on: November 22, 2012, 04:33:55 am
@Jay

You are correct about the changes from byte to char in the for loops, that was a typo on my end. Also setting SS high at the beginning is necessary (though setting it to an output is done by the SPI library).

One thing I have noticed is that you are trying to use SPI mode 0, and I believe the IC to be SPI mode 3 (data is read on the first rising edge after a falling one according to the timing diagrams).


@Paradigm

The union works by placing an array of 4 bytes in the same memory location as the struct. The struct contains a 24bit variable and a byte variable.
 Due to the way unions work this basically means that the byte variable 'command' is stored at the same place as the element 3 in the array. Similarly the variable 'value' is stored in the same memory space as the three other bytes in the array (elements 0,1,2).
Basically what this means is that you can convert between the array and the struct without doing any calculations, making it a very efficient method.

I will have a play with the code to see if I can find anything which may be causing a problem.
834  Using Arduino / Project Guidance / Re: LED Gurus? on: November 20, 2012, 07:23:24 pm
For maximum power density, you would need to start looking at rechargeable Li-Po batteries like those used for RC planes and helicopters.

As a gauge for how long they last, a battery rated for 1100mAH would supply 1100mA for an hour, or in your case 140mA for 1100mAH/140mA = 7.9Hours.
835  Using Arduino / Project Guidance / Re: LED Gurus? on: November 20, 2012, 05:51:51 pm
Yup, Forward Current (aka If) = 140 [mA] = 0.14 [A]. Forward Voltage (aka Vf) is somewhere in the region of 1.4 [V] to 1.5 [V], so I would work on the theory that it is 1.45 [V] (the actual voltage will depend on the LED, temperature, current), but this approximate figure should be good enough.

If we do the math, 5 diodes in series x 1.45 [V] each = 7.25 [V] dropped across the diodes. For a nominal 9 [V] supply, this leaves 9-7.25=1.75 [V] across your resistor. V=IR, so R = V/I = 1.75/0.14 = 12.5 [Ohm]. You will need an minimum P=IV = 0.14*1.75 = 0.245 [W] rated resistor.

However... A 9V battery supplying 140mA will run flat very very very quickly. You would be lucky if you got half an hour out of it. The other thing is that you would be wasting a lot of energy in that resistor.
836  Using Arduino / Microcontrollers / Re: SPI read write for 32bits data on: November 20, 2012, 04:27:18 pm
Modified my example from before based on the datasheet:
Code:
#include <SPI.h>

union FourByte{
    struct {
      unsigned long value:24; //24bit register values go in here
      byte command:8; //8bit command goes in here.
    };
    byte bit8[4]; //this is just used for efficient conversion of the above into 4 bytes.
};

void setup(){
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE3); //I believe it to be Mode3
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  pinMode(SS, OUTPUT); //not really necessary as it is done by the SPI library.
  digitalWrite(SS, HIGH);
  //Page
  //example of reading data
  unsigned long voltage = SPI_read(4);//Instantaneous Voltage Channel 1
  
  //example of writing data
  union FourByte data;
  data.command = 0b01000000; //Write to config register
  data.value = 1; //This is the default value from datasheet, just using it as an example.
  SPI_write(data);
}

void loop(){
  
}

void SPI_write(union FourByte data) {
  digitalWrite(SS,LOW); //Using CS pin, so sync1/sync0 bytes not needed
  for(char i = 3; i >= 0; i--){
    SPI.transfer(data.bit8[i]); //transfer all 4 bytes of data - command first, then Big Endian transfer of the 24bit value.
  }
  digitalWrite(SS,HIGH);
}

unsigned long SPI_read(byte command){
  digitalWrite(SS,LOW); //SS goes low to mark start of transmission
  union FourByte data = {0xFEFEFE,command}; //generate the data to be sent, i.e. your command plus the Sync bytes.
  for(char i = 3; i >= 0; i--){
    data.bit8[i] = SPI.transfer(data.bit8[i]); //send the data whilst reading in the result
  }
  digitalWrite(SS,HIGH); //SS goes high to mark end of transmission
  return data.value; //return the 24bit value recieved.
}
837  Using Arduino / Microcontrollers / Re: SPI read write for 32bits data on: November 19, 2012, 01:36:39 pm
You can use a 'union' to efficiently and quickly convert a long to a series of four bytes or 2 ints or vice versa:
Code:
#include <SPI.h>

union FourByte{
    unsigned long bit32;
    unsigned int bit16[2];
    unsigned char bit8[4];
};

void setup(){
  SPI.begin();
 
  //save a 32bit value
  unsigned long someValue = 0xFFFFFFFEUL;
  //transmit 32bit value:
  transfer(someValue);
 
  //read 32bit value
  someValue = transfer(0);
 
  //read and write at the same time.
  someValue = transfer(someValue);

  //concatinate ints:
  FourByte bob;
  bob.bit16[0] = 10;
  bob.bit16[1] = 20;
 
  transfer(bob.bit32); //sends 1310730 = 20*65536+10

  //concatinate bytes:
  bob.bit8[0] = 0x1; //1
  bob.bit8[1] = 2; //2
  bob.bit8[2] = 0b11; //3
  bob.bit8[3] = B100; //4
 
  transfer(bob.bit32); //transfers 0x04030201
}

unsigned long transfer(unsigned long value){
  FourByte data = {value};
  for(byte i = 0; i < 4; i++){
    data.bit8[i] = SPI.transfer(data.bit8[i]);
  }
  return data.bit32;
}
838  Using Arduino / Programming Questions / Re: Arduino capacitor reformer on: November 18, 2012, 05:20:00 am
Code:
  if (state = 1) int charge ();
  if (state = 2) int wait (hold);
  if (state = 3) int discharge ();
  if (state = 4) int saftey ();

isn't right.  Are you expecting to call some functions rather than declare them?

Your code sounds like it can simply be linear and not need a state variable (or are you doing something else in the sketch at the same time?)
Not only that, but they should all be the equality operator ('==') not the assignment operator ('=').
839  Using Arduino / Programming Questions / Re: what's means about "PINB |= 1<<5" on: November 17, 2012, 03:53:03 pm
I believe your understanding is correct.


A bit of background (I am using I/O to mean a physical pin on the chip to avoid confusion with the PIN register).

There are 3 registers that control any given I/O on the chip:

PIN = input register
PORT = output register
DDR = direction register.

The direction register controls whether an I/O is an input or an output. Behind the scenes pinMode(pin); uses this register.
The input register is what you use to read the current state of the I/O. If the I/O is an input PIN returns the value of whatever is connected, if it is an output, PIN returns the value in PORT.
The output register is what you use to set the state of an I/O. If the I/O is an output, PORT sets it to be a 1 or a 0. If the I/O is an input, PORT sets whether or not the internal pullup is enabled.

If the DDR register is set to Output, and you try to write to the PIN register, the I/O toggles such that if the output state is a 1, it becomes a zero and vice versa.
840  Using Arduino / Programming Questions / Re: What is wrong with this code? on: November 17, 2012, 03:40:36 pm
You may have to type cast it:
Code:
strsep ((char**)&data, "M");

or if you don't want to type cast you could do:

Code:
char data[] = "whatever";
char* ptr = data;
strsep(&ptr,"M");
Pages: 1 ... 54 55 [56] 57 58 ... 110