Show Posts
Pages: [1]
1  Forum 2005-2010 (read only) / Syntax & Programs / Re: where to declare variables and mem impact on: July 08, 2010, 12:10:13 pm
Excellent stuff... thanks everyone for the responses!  this really helps!
2  Forum 2005-2010 (read only) / Syntax & Programs / where to declare variables and mem impact on: July 08, 2010, 09:50:56 am
Hi all!

  While I have *some* background in C/C++ I am relatively new to embedded programming thus now need to pay more attention to memory utilization.

   My main question regards where to declare certain variables and what the impact on memory overhead is...

   If I declare variables *inside* the functions that use them, is this memory "free" when that function exits?  i.e. is it a good practice to declare as many variables as possible within functions so as to maximize your available memory *outside* the function scope?  If this is correct, what is the downside of declaring variables within a function? (other than of course not being able to access them globally)

I'd also appreciate any insight into how the "volatile" keyword plays into this... thanks!

3  Forum 2005-2010 (read only) / Interfacing / Re: 7 Segment Display using SPI on: July 23, 2010, 11:17:41 am
Stumbling upon this thread... here is my code for interfacing with the Sparkfun 4 digit, 7-segment LCD via SPI.  It demonstrates some of the various command bytes and wraps up most of the functionality into a single function call (once initialized).

I do have one bug yet to be worked out...  on initial reset the display usually reads fine, however upon a subsequent reset the display usually turns to "junk" (random elements lit).  Pressing the reset button again doesn't fix it but closing and reopening the Serial Monitor always does fix it.  I'm guessing it has something to do with the power up sequence of the LCD uC?  Any help with this would be greatly appreciated.  

Here is my code:

Code:
//this project demonstrates SPI control of sparkfun 4 digit, 7-segment LCD
//Setup section demonstrates commanding the LCD via SPI using byte commands.  
//Loop section demonstrates a counting timer uses SendToLCD() function to simplify
//
//project by Scott Josselyn
//
//note that there are different firmware versions for the sparkfun part that may not support all functions
//Part Product Page: http://www.sparkfun.com/commerce/product_info.php?products_id=9230
//Part Reference Manual:  http://www.sparkfun.com/datasheets/Components/LED/SFE-0012-DS-7segmentSerial-v3.pdf

#define MOSI        11     //SPI MOSI Pin definition - arduino pin 11
#define SCLK        13     //SPI CLOCK Pin definition - arduino pin 13
#define LCD_CS      10     //LCD chip select -arduino pin 10

char outBuff[5];  //output buffer
int lastOutToLCD, outToLCD = 0;
byte decimalBitCode = 0;

void setup() {
  
  //configure serial port
  Serial.begin(9600);
  Serial.println("Intializing...");
  
  //the following configures the lcd
  delay(200);  //not sure if this is required or not but want to make sure the LCD uC has finished loading after powerup
  //configure the pins for SPI communication
  pinMode(MOSI, OUTPUT);
  pinMode(SCLK, OUTPUT);
  pinMode(LCD_CS, OUTPUT);
  digitalWrite(LCD_CS, LOW); //this should be moved to inside the function

  /* Enable SPI, Master, set clock rate fck/128 */
  SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0)|(1<<SPR1);  //SPE=enables SPI, MSTR=arduino is master, DORD=MSB first(default)
  //SPR0 =1, SPR1 =1 SPI2x = 0 => SCK Freq fosc/128
  
  //now reset the LCD and have it display "9999"
  SPI_MasterTransmit(0x76);  //send a reset
  delay(200);  //not sure if this helps or not
  SPI_MasterTransmit(9);  //send a 9
  SPI_MasterTransmit(9);  //send a 9
  SPI_MasterTransmit(9);  //send a 9
  SPI_MasterTransmit(9);  //send a 9
  SPI_MasterTransmit(0x77); //send decimal command byte
  SPI_MasterTransmit(0);   //clear all decimals
  Serial.println("LCD should show 9999");
  delay(2000);
  //now dim display
  Serial.println("Dimming Display...");
  SPI_MasterTransmit(0x7a);   //send brightness command byte
  SPI_MasterTransmit(0xfe);   //send min brightness value
  delay(2000);
  Serial.println("Brightening Display...");
  SPI_MasterTransmit(0x7a);   //send brightness command byte
  SPI_MasterTransmit(0x01);   //send max brightness value  
  delay(2000);
  
  for (byte i = 0;i<8; i++){
    decimalBitCode = 1 << i;
    SPI_MasterTransmit(0x77); //send decimal command byte
    SPI_MasterTransmit(decimalBitCode);   //update the decimal position
    delay(1000);    
  }
  
  delay(3000);  //delay 3 secs
  
  Serial.println("Now display a min:sec counter...");
}

void loop(){//begin loop
        
      int mins = millis()/60000;   //at some point need to add code to watch for wrap around at 99 min1!1 or whenever millis overflows
      long secs = (millis()/1000 % 60);
      outToLCD = mins*100 + secs;
      decimalBitCode = B00010000;  //code to enable the colon between hundreds and thousands place
      
      //now send it to the LCD (but only if display needs to be changed since last time)
      if (outToLCD != lastOutToLCD) {//begin check for new display data
        SendToLCD( outToLCD, decimalBitCode);
        //send to terminal for debugging
        Serial.print(mins, DEC); Serial.print(":"); Serial.println(secs, DEC);
      }; //end if check for new display data
    
      //update our variable that keeps track of what it was last time.
      lastOutToLCD = outToLCD;  

}//end loop


void SendToLCD( int n, byte decimalCode)
{
  //takes an unsigned int, converts to bcd and outputs the least 4 sig digits to the LCD and places
  //decimals on display are defined defined by decimalCode
  
  //digit1 - digit2 - digit3 - digit4
  //bit 0 = decimal between digit 1 and 2
  //bit 1 = decimal between digit 2 and 3
  //bit 2 = decimal between digit 3 and 4
  //bit 3 = top of colon
  //bit 4 = bottom of colon
  //bit 5 = above and right of digit 3
  
 
        //  Most of the following code section is from http://www.cs.uiowa.edu/~jones/bcd/decimal.html
        uint8_t d4, d3, d2, d1, d0, q = 0;

        if (n < 0) {
            putchar( '-' );
            n = -n;
        }

        d1 = (n>>4)  & 0xF;
        d2 = (n>>8)  & 0xF;
        d3 = (n>>12) & 0xF;

        d0 = 6*(d3 + d2 + d1) + (n & 0xF);
        q = (d0 * 0xCD) >> 11;
        d0 = d0 - 10*q;

        d1 = q + 9*d3 + 5*d2 + d1;
        q = (d1 * 0xCD) >> 11;
        d1 = d1 - 10*q;

        d2 = q + 2*d2;
        q = (d2 * 0x1A) >> 8;
        d2 = d2 - 10*q;

        d3 = q + 4*d3;
        d4 = (d3 * 0x1A) >> 8;
        d3 = d3 - 10*d4;
    
 
      //Now send SPI bytes to LCD
      SPI_MasterTransmit(d3);
      SPI_MasterTransmit(d2);
      SPI_MasterTransmit(d1);
      SPI_MasterTransmit(d0);
      SPI_MasterTransmit(0x77);  //command byte for decimal
    
      SPI_MasterTransmit( decimalCode );  //now place decimal

}


void SPI_MasterTransmit(char cData)
{
  /* Start transmission */
  SPDR = cData;
  /* Wait for transmission complete */
  while(!(SPSR & (1<<SPIF)))
;
}
 
4  Forum 2005-2010 (read only) / Interfacing / Re: Need help troubleshooting 3.3v SPI on: July 16, 2010, 12:53:19 pm
I'll try to upload a schematic tonight.  I'll certainly consider a hex converter and I have a sparkfun logic level shifter I could try.  That aside, I'm still interested in finding out why my existing circuit won't work.  I have very little electronics background but having reviewed the forums, I'm wondering if this could be an impedance issue or if perhaps the capacitance of my breadboard is a factor (I'm using a breadboard for the voltage divider circuit as well as the inline resistors - the successful 5v supplied circuit did not use the breadboard... I just directly wired the accelerometer's pins to the relevant arduino pins).
5  Forum 2005-2010 (read only) / Interfacing / Need help troubleshooting 3.3v SPI on: July 16, 2010, 09:37:19 am

Hi everyone…  I'm having trouble interfacing with a LIS3LV02DQ 3-axis accelerometer via SPI.  I am using the breakout board from Sparkfun which sadly has been discontinued (the schematic for the breakout board is located here:  http://www.sparkfun.com/datasheets/Sensors/LIS3LV02DQ-Breakout.pdf)

and here is the datasheet link:

http://www.st.com/stonline/products/literature/ds/11115/lis3lv02dq.pdf

This is a 3.3v device but there have been many successful attempts to interface via I2C or SPI at 5v (references below)

http://www.nearfuturelaboratory.com/2006/09/22/arduino-and-the-lis3lv02dq-triple-axis-accelerometer/

http://interactive.usc.edu/members/jbleecker/archives/006915.html

http://www.kerrywong.com/2010/04/17/interfacing-lis3lv02dl-using-spi-ii/

I have indeed been successful at interfacing with this at 5v (good “WHO_AM_I” value of 0x3A and clearly good data, but so far have been unsuccessful using 3.3v (“WHO_AM_I” of 0xFF or sometimes 0x3F and bad data).  I have tried using the 3.3v output from the arduino as well as using a voltage divider on the 5v output, bringing it down to 3.4v or so… neither worked (with nothing else changed from the wiring that worked when I ran the 5v output directly to power the breakoutboard).  

I have also tried using the inline resistor method covered at sparkfun (http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=65) to interface between the 5v arduino and 3.3v accelometer without success.  It is worth noting that this same circuit worked perfectly for me when interfacing with a 3.3v SCP1000 pressure sensor breakout board.  I pulled that board out and popped the accelerometer into the same circuit but no worky.  

There are no _CS conflicts on the bus (the accelerometer is currently the only sensor on the bus)

Can anyone explain why I can interface fine at 5.0v but not at 3.3v (as it was designed for?!)

Any suggestions on further troubleshooting?  I really want to get this working at 3.3v as I plan on additional 3.3v sensors on the SPI bus that are not as overvolt tolerant as the LIS3LV02DQ apparently is.  If this can't be solved, can anyone suggest a safe way to interface with both 3.3v and 5.0v sensors on the same SPI bus?

Unfortunately I do not have a logic analyzer…  (if anyone can recommend a cheap one I would appreciate it!)

Thanks in advance

6  Forum 2005-2010 (read only) / Interfacing / Power Supply Conditioning Question on: July 08, 2010, 09:45:16 am
Hi all,
  
   I'm working on a project that integrates multiple sensors.  I'd  like to power each of the sensors from the Arduino's 5v and/or 3.3v power pins.  The spec sheets for the sensors recommend power filtering caps (usually 1uF and .01uF).  My question is, does each sensor need its own filter caps physically "close" to the component or can I get by with just placing the caps anywhere on the power bus? Thanks in advance!

7  Forum 2005-2010 (read only) / Interfacing / Re: Hall Effect Sensor on: May 26, 2010, 08:30:50 am
What type of Hall Effect sensor are you using?  Make sure it is a Hall Effect *Switch* and not a Linear Hall Effect sensor.  I had both in my kit and accidentally used the linear part instead of the switch and the linear sensor never outputted enough signal to trip to a logic high.  Swapped to the switch and it worked fine.  
8  Forum 2005-2010 (read only) / Interfacing / Re: Wattage meter for Cycling on: May 26, 2010, 08:35:01 am
Correct me if I am wrong... but the problem with using alternator/generator or brake solutions is that the measurement sensor then adds resistance that the rider must overcome on top of whatever effort he is exerting to maintain his speed.  This isn't desirable...  the other methods sense the power output affecting the bike/rider system.
9  Forum 2005-2010 (read only) / Interfacing / Re: Wattage meter for Cycling on: May 25, 2010, 04:05:20 pm
I've actually been working on this conceptually for several years (since hearing about the iBike) and initially started to try to implement one on an MSP430 before jumping to Arduino around last Christmas.  

Curious if anyone else has made any progress.  I've made a very accurate cycling computer and am working on interfacing additional sensors now.

I've also simulated the setup in MATLAB to determine what some of the sensor tolerances would need to be to be useful for training.  This is the part that hurts... the wind speed (dynamic pressure) sensor needs to be extremely sensitive and I've had a hard time finding a good cheap part to do this... my first attempt only got me to about 1 mph or wind resolution although I think I can get to .1mph or resolution with oversampling.  Also, for grade effects you need a tilt sensor accurate to at least 1/1000 of a g.  

There are of course other ways to go... I also had thought about the instrumenting my pedals or shoes but the above mentioned type of strain gauges won't work (wrong plane) and pressure transducers are notoriously inaccurate (although never thought about using whatever is inside a scale!  Anyone done this?) Also, a small but measurable amount of pedaling torque is produced by the upgoing leg... not sure how you'd measure that with a pressure trandsducer.

I'd also thought about using strain gauges on the chain stays.  By measuring the forces in all the rear triangle structural members and doing some vector math you can back out the tension in the drive chain.    Once you have chain tension you multiply it by chain speed to get power.

Alternately you could go the Polar route and measure the vibration of the chain which also correlates directly with tension. (I actually did this using a guitar pick-up as a sensor feeding and FFTed it...worked great on tuning forks an inch or so away but I just got "noise" near a vibrating chain... maybe I should re-look into this).  You could also use a laser diode to measure the vibrating chain.. but you'd have to be able to track the chain as it moves across the different gears.

In summary - there are several ways to skin this cat.  You can go the iBike route and measure the everything *except* the power and then back out power, or you can try to get at power directly by measuring force/tension in the chain, pedals, cranks, crank shaft of hub.  The later two may be the hardest as you'd need to get a strain gauge into a rotating bearing.  Now that I think about it, strain gauges on the cranks measuring their flexing might be the easiest way to go if you can figure out how to get the signal off each crank (arduino mini's plus bluetooth?)  Anyone have any expertise on the art of affixing strain gauges to structures?  Do you need to glue them down or something?

I'd like to document what I've done so far...can anyone recommend a good package for documenting projects on a blog or website?

Feel free to PM me...
10  Forum 2005-2010 (read only) / Exhibition / Re: SD/MMC From the ground up on: July 25, 2010, 07:44:31 pm
Hate to reply to my own post but adding intrigue... found some relevant info over on the sparkfun forums... seems others have had problems interfacing SCP1000 with other slaves on an SPI bus.  

http://forum.sparkfun.com/viewtopic.php?f=14&t=19485&start=0

Can anyone suggest any mitigation strategies that don't involved software SPI?  I'm going to comb through the SD library and look for dummy reads/writes and try changing their values - I know there is one in SDCard:Init... any other suggestions?  Thanks in advance!

11  Forum 2005-2010 (read only) / Exhibition / Re: SD/MMC From the ground up on: July 25, 2010, 09:16:46 am
Hi all... I'm running into exactly the same problem that BLP had in Reply #16... I can't get the library to work with the Sparkfun SCP1000 pressure sensor via SPI.  Again, as with BLP, the SCP1000 code works fine if I comment out the FAT stuff, and the FAT stuff works weather or not I comment out the SCP1000 code (but the sensor returns all zeros).   I wrote my own implementation so unless he and I made the exact same mistake I'm thinking this is something in the FAT library.  

I'm wondering if indeed the SD code is responding even when the SD CS_ is HIGH.  Can anyone suggest any troubleshooting ideas?  (I *don't* have a logic analyzer).  SoftSPI is not an option ... Thanks in advance!
Pages: [1]