SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By Hobby_Elec
#79302
This is my first Arduino project and need lot of help..

My goal for now is to be able to read data from LIS302 into Arduino Pro mini.

For this I have soldered header pins to Arduino board and LIS302DL board. Next, I made wire connections between Arduino and LIS302DL board.

I found a sketch to read LIS302 data and tried to load it, when I ran into several problems.

1) Tried loading sketch (atmega168+LIS302)..taking forever to load..

"Uploading to I/O Board.."

Red messages appear in the bottom window..looks like something is not right.

Question: How to verify sketch is correctly loaded ?

2) LIS302DL board does not have SCA pin at all !
It does have MISO, MOSI, SCL, VCC, GND

Question: Is MOSI pin same as SCA ?

3) Do I need LCD board as well ? or can I display x, y, z acceralation values on laptop/PC monitor ? which is easier (i.e., between LCD display vs. PC monitor )

Requirement is: Would like to see x, y, z values visually when the LIS302 is moved

Appreciate any help.. Thanks.
User avatar
By leon_heller
#79304
SCA is an I2C signal, MOSI etc. are SPI. The LIS302 can use either I2C or SPI, IIRC.

Leon
By Hobby_Elec
#79312
leon_heller wrote:SCA is an I2C signal, MOSI etc. are SPI. The LIS302 can use either I2C or SPI, IIRC.

Leon
ok..thanks.

Since I am using I2C, does this mean I have to use MOSI pin as SCA ?

Where can I find datasheet for this board ?
User avatar
By leon_heller
#79313
See the LIS302 data sheet.

Leon
By Hobby_Elec
#79315
leon_heller wrote:See the LIS302 data sheet.

Leon
Ok, i'll check out the datasheet.

Would you be able to comment on the last part my question as well ?

>>3) Do I need LCD board as well ? or can I display x, y, z acceralation values on laptop/PC monitor ? which is easier (i.e., between LCD display vs. PC monitor )

>Requirement is: Would like to see x, y, z values visually when the LIS302 is moved

Regards.
By lehmanna
#79325
You won't need an extra LCD since you can just as well use serial communication.

What kind of Arduino do you have? USB or bluetooth? What operating system are you using?

The USB Arduino comes with an on-board FTDI-chip, which basically converts your uC's serial output to USB packets and transfers them to the PC. On your PC, that connection is represented by a virtual COM port. Under Mac OS X (and I assume Linux, too), the serial port is named something like /dev/tty.usb*, and you can easily access it with terminal programs like GNU screen or whatever. As far as Windows is concerned, you'd probably want to use HyperTerminal.

However, since you've written that programming takes forever and/or goes horribly wrong, I suggest that you re-check your FTDI driver installation first.
By macegr
#79345
The Arduino software comes with a terminal that can see data you send from the Arduino using the Serial.print() command.

On another note, do you realize there is an huge official forum for the Arduino, with thousands of discussions, examples, and Arduino experts?

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl
By tz
#79437
I would start with just using a short sketch with a begin and a send/receive for the 0x0f register which should return 0x3b. Until that happens the hardware (or something strange with the software like the address) isn't configured right. Something like:
Code: Select all
    uint8_t addr = 0x1c, val; // or 0x1d
    Serial.begin(57600);
    Wire.begin();
    Wire.beginTransmission(addr);
    Wire.send(0x0f); // who am i
    Wire.endTransmission();
    Wire.requestFrom(addr, (uint8_t) 1);
    // omitting Wire.available();
    val = Wire.receive();
    Seriall.println(val, HEX);
This is for the ADXL345, which is a similar part, but you can see how I'm doing it.

I wrote a sketch that outputs at 57600 - to the serial monitor, or whatever port the FTDI breakout (or whatever) is connected to. Read the Wire lib info for what the calls do.

It uses the "pull to ground" address of 0x1d. I.e. CS and AD are grounded, SCK goes to A5, SDA to A4, with 3.3 and ground to the arduino pins.

(I originally had trouble with a stand-alone, I missed a few things including apparently you need to turn on pullups on the AVR - A checklist would help).
Code: Select all
#include <Wire.h>

const uint8_t addr = 0x1d;

void setup()
{
    Serial.begin(57600);
    Wire.begin();
    Serial.println("Wire");

    Wire.beginTransmission(addr);
    Wire.send(0x2d);
    Wire.send(0x08);
    Wire.endTransmission();

    Wire.beginTransmission(addr);
    Wire.send(0x2c);
    Wire.send(0x08);
    Wire.endTransmission();

    Wire.beginTransmission(addr);
    Wire.send(0x31);
    Wire.send(0x0b);
    Wire.endTransmission();

    Wire.beginTransmission(addr);
    Wire.send(0x38);
    Wire.send(0x9f);
    Wire.endTransmission();

}

void loop()
{
    int count = 0;
    int i, j, k;
    byte val, val1;

    Wire.beginTransmission(addr);
    Wire.send(0x39); // fifo stat, how many samples
    Wire.endTransmission();
    Wire.requestFrom(addr, (uint8_t) 1);
    // i = Wire.available(); // should be 1
    count = Wire.receive();
    if (!count)
        return;
    //    Serial.print(count, DEC);    
    //    Serial.print(":");
    Wire.beginTransmission(addr);
    Wire.send(0x32);
    Wire.endTransmission();
    // could do 7 instead of 6 and repeat if fifo full
    Wire.requestFrom(addr, (uint8_t) 6);

    // i = Wire.available(); // should be 6
    for (j = 0x32; j < 0x38; j++) { // read these registers
        val = Wire.receive();
        if (!(j & 1)) {  // save off LSB
            val1 = val;
            continue;
        }
        i = val;
        i <<= 8;
        i |= val1;
        Serial.print(i, DEC);
        Serial.print(" ");
    }
    Serial.println();
}
By tz
#79702
This talks to the AD345XL accelerometer using interrupts without using the arduino library.

It prints a "K" (kinetic, since I'm adding it to other code), then X,Y,Z as three hex digits each.
Code: Select all
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>

#define TXBUFLEN (256)
static unsigned char txbuf[TXBUFLEN];
static unsigned char txhead;
static unsigned char txtail;

ISR(USART_UDRE_vect)
{
    if (txhead == txtail)
        UCSR0B &= ~0x20;
    else
        UDR0 = txbuf[txtail++];
}

void txenqueue(unsigned char *dataOut, unsigned length)
{
    // should try to prevent overruns?
    while (length--)
        txbuf[txhead++] = *dataOut++;
    UCSR0B |= 0x20;
}

unsigned char hobuf[8];
static void send2(unsigned char prefix, unsigned char val1, unsigned char val)
{
    unsigned char *h = hobuf, i;

    *h++ = prefix;
    i = val1 & 15;
    *h++ = i > 9 ? 'A' - 10 + i : '0' + i;
    i = val >> 4;
    i &= 15;
    *h++ = i > 9 ? 'A' - 10 + i : '0' + i;
    i = val & 15;
    *h++ = i > 9 ? 'A' - 10 + i : '0' + i;
    txenqueue(hobuf, h - hobuf);
}
#if 0
static void send(unsigned char val)
{
    unsigned char *h = hobuf, i;

    i = val >> 4;
    i &= 15;
    *h++ = i > 9 ? 'A' - 10 + i : '0' + i;
    i = val & 15;
    *h++ = i > 9 ? 'A' - 10 + i : '0' + i;
    txenqueue(hobuf, h - hobuf);
}
#endif

#define CXXX ((1<<TWINT)|(1<<TWEN)|(1<<TWIE)) // 0x80 4 1
#define CSTR (CXXX|(1<<TWSTA)) // 0x20
#define CXFN CXXX
#define CXFA (CXXX|(1<<TWEA)) // 0x40
#define CSTP (CXXX|(1<<TWSTO)) // 0x10

#define SSTR 0x08
#define SRPS 0x10
#define SAWA 0x18
#define SAWN 0x20
#define SDWA 0x28
#define SDWN 0x30
#define LARB 0x38
#define SARA 0x40
#define SARN 0x48
#define SDRA 0x50
#define SDRN 0x58

struct twist {
    unsigned char state; // expected
    unsigned char data;
    unsigned char nextcmd;
    unsigned char flag;
};

struct twist statetab[] = {
    {0x00, 0x00, CSTP, 0 },
    // 1 read 0x39
    {SSTR, 0x3a, CXFN, 1 },
    {SAWA, 0x39, CXFN, 1 },
    {SDWA, 0x00, CSTR, 0 },
    {SRPS, 0x3b, CXFN, 1 },
    {SARA, 0x00, CXFN, 0 },
    {SDRN, 0x00, CSTP, 2 },
    // 7 - Read 6 bytes from 0x32
    {SSTR, 0x3a, CXFN, 1 },
    {SAWA, 0x32, CXFN, 1 },
    {SDWA, 0x00, CSTR, 0 },
    {SRPS, 0x3b, CXFN, 1 },
    {SARA, 0x00, CXFA, 0 },
    {SDRA, 0x00, CXFA, 2 },
    {SDRA, 0x00, CXFA, 2 },
    {SDRA, 0x00, CXFA, 2 },
    {SDRA, 0x00, CXFA, 2 },
    {SDRA, 0x00, CXFN, 2 },
    {SDRN, 0x00, CSTP, 2 },
    // 18 - write register to val
    {SSTR, 0x3a, CXFN, 1 },  //17
    {SAWA, 0x00, CXFN, 4 },
    {SDWA, 0x00, CXFN, 4 },
    {SDWA, 0x00, CSTP, 0 },
    // 22
};

unsigned char twistate, twireturn;
unsigned char twibuf[10], *twiptr;
ISR(TWI_vect) {
    if ((TWSR & 0xf8) == statetab[twistate].state ) {
        if ( statetab[twistate].flag & 2 )
            *twiptr++ = TWDR;
        if ( statetab[twistate].flag & 1 )
            TWDR = statetab[twistate].data;
        if ( statetab[twistate].flag & 4 )
            TWDR = *twiptr++;
        if( CSTP != statetab[twistate].nextcmd ) {
            TWCR = statetab[twistate++].nextcmd;
            return;
        }
    }
    twireturn = twistate;
    TWCR = CSTP;
    twistate = 0;
}

void xfertwi(unsigned char cmd)
{
    twistate = cmd;
    twiptr = twibuf;
    twireturn = 0;
    TWCR = CSTR;
    while( twistate )
        sleep_mode();
}

void writereg(unsigned char reg, unsigned char val)
{
    twibuf[0] = reg;
    twibuf[1] = val;
    xfertwi(18);
    return;
}

int main(void)
{
    DDRB = 0x20;
    PORTB &= ~0x20;             // LED

    // Crystal based: xtal/16 or xtal/8 for clk / baud - 1
#define BAUD (57600)
    UBRR0 = 1000000 / BAUD - 1;

    UCSR0C = 0x06;              //8N1 (should be this from reset)
    UCSR0A = 0xE2;              // clear Interrupts, UART at 2x (xtal/8)
    UCSR0B = 0x18;              // oring in 0x80 would enable rx interrupt
    txhead = txtail = 0;



    PORTC |= 0x30;
    TWCR = CSTP;
    TWAMR = TWDR = TWAR = 0;
    TWSR = 0;
    TWBR = 2;//32;//2



    sei();

    writereg(0x2d,8);
    writereg(0x31,0xb);
    writereg(0x38,0x9f);
    // writereg(0x2c,0x0a);   // rate, default 100hz/A
    writereg(0x2c,0x0c);   // rate, default 100hz/A

    for(;;) {


        if( !twistate ) { // data available
            if( twireturn == 17 ) {
                // print grabbed data
                send2( 'K', twibuf[1] , twibuf[0] );
                send2( ',', twibuf[3] , twibuf[2] );
                send2( ',', twibuf[5] , twibuf[4] );
                txenqueue( (unsigned char *) "\r\n", 2);
                // fallthrough instead of contiue will keep it busy,
                // twireturn = 0;
                // else something else needs to break the next sleep
            }
            if( twireturn != 6 || !twibuf[0] ) {
                twibuf[0] = 0;
                twistate = 1;
            }
            else // data ready, grab it
                twistate = 7;
            twiptr = twibuf;
            twireturn = 0;
            TWCR = CSTR;
        }
        
        sleep_mode();

    }

    sleep_mode();
}