1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

interfacing ATMega168 with depth sensor and accelerometer

Discussion in 'AVR' started by galed, Sep 27, 2009.

  1. galed

    galed New Member

    Joined:
    Sep 27, 2009
    Messages:
    32
    Likes:
    0
    Location:
    Boulder, CO
    Hi all, new here, first post, and I apologize if this is in the wrong place. Let me jump right in:

    I'm working on a dive computer that uses a depth sensor, accelerometer, micro and (eventually) an LCD or OLED screen. I've just started this and I'm trying to get my parts talking to eachother. The schematic I have is attached, along with the info about the ADXL345, ATMega168 and MS5541 depth sensor.

    What I'm going for is reading every second or half-second from the depth sensor and doing some number crunching. Whenever the ADXL345 puts out either its single tap or double tap interrupts, the micro needs to get data from it about which direction the taps were in, then do some more number crunching.

    First of all, is this even remotely right, hardware-wise? This is the first embedded design i've built from scratch and I'm really not sure if I have the connections set up properly.

    Second, is the 168 gonna be able to run this AND drive an OLED screen or should I be looking for a beefier processor?

    Third, I'm not really sure what to do with the crystals. The depth sensor runs at 32kHz, but the micro much faster. Should the XTAL 1,2 pins (Q1 in my schematic) be the 32kHz crystal? The ATMega will put that out on the XCK pin, right?

    Any general feedback? Thanks in advance :D

    edit: whoops, older version of the schematic... the newer one is up now
     

    Attached Files:

    Last edited: Sep 27, 2009
  2. Sceadwian

    Sceadwian Banned

    Joined:
    Oct 27, 2006
    Messages:
    14,047
    Likes:
    141
    Location:
    Rochester, US
    That seems like a really weird layout for caps and everything.
    Why is the mega running at 32khz you said it's running faster? If you're running the chip on the internal oscillator the crystal is wasted space because the clkout option only runs from the selected oscillator if I'm not mistaken.
     
    Last edited: Sep 27, 2009
  3. Noggin

    Noggin Member

    Joined:
    Jul 21, 2003
    Messages:
    595
    Likes:
    9
    Location:
    Austin, TX
    I didn't pull the datasheets on the parts to verify that they are connected properly, but I see a few things that you need to do right off the bat...

    1. Don't tie the VCC and AVCC pins to power through a resistor. They should connect directly to the power supply. The reset pin should be pulled up through a resistor though, about 10k.
    2. Add an ISP port, it'll make your life WAAAY easier to program it while it is in circuit.
    3. Add RS-232 chip (such as a MAX232) to go from the RX and TX pins to a DB9 connector so you can connect this to a PC to help debug it.
    4. Replace the 32 KHz crystal with a 7.3728 MHz crystal to run the AVR at a decent speed.
    5. Set up a PWM output on the AVR to drive the depth sensor at 32.768 KHz

    If the display you want is something like a 64x128 mono display with a serial data input, this MCU I think will be fine. Just make sure your fonts fit with code space left for other stuff.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. galed

    galed New Member

    Joined:
    Sep 27, 2009
    Messages:
    32
    Likes:
    0
    Location:
    Boulder, CO

    OK, i fixed a couple things and cleaned up the schematic a little bit.

    I'm not really sure what's going on with the crystal. I attached a picture from the depth sensor datasheet that has an example implementation. If you look at that, it has the 32kHz crystal on the micro and the depth sensor is getting its MCLK signal from the micro... which led to my current confusion

    is ISP short for an in-circuit serial programmer? "ISP port" on google turns up all sorts of stuff, mostly related to the internet... i don't have the slightest idea how to implement one of those.

    A PWM would be the best way to run the 32kHz signal you think?

    I'll work on the RS232... and if i'm looking at a slightly heftier screen than that, how much more processing umph do i need?

    edit: ok, the rs232 is on there and it's cleaned up a little. I still need to address the PWM... attached image of updated schematic
     

    Attached Files:

    Last edited: Sep 27, 2009
  6. Noggin

    Noggin Member

    Joined:
    Jul 21, 2003
    Messages:
    595
    Likes:
    9
    Location:
    Austin, TX
    Much better.

    1. Pins 7 & 8 on the MCU now need around 18pf to 22pf ceramic caps on them to ground.
    2. You need to add pin numbers to your depth sensor.
    3. The ISP pins can be found in the ATMega's datasheet on page 298. In addition to the 3 pins listed, you will need to bring out Vcc, GND, and reset. I suggest you bring it out to a 0.1" header, 2 rows, 3 pins per row. Look at the datasheet for an AVRISP-MKII to get the pinout for the header. Purchase an AVRISP-MKII from Digikey for $30 and use it as your programmer.
    4. DATAO on your accelerometer doesn't appear to go anywhere

    I think PWM to run the depth sensor is a good way to do it. This will let your MCU run quickly and output a 0-3.3v signal to drive the depth sensor. The depth sensor schematic would have the MCU running at a speed of 32 KHz, which might be fast enough to do your number crunching, but probably not everything you want to do. The 7.xxx crystal is evenly divisible by a number to get 32768, so PWM will give you a perfect 32.768KHz signal output to drive the depth sensor. Just connect it to OC1A, set up the hardware in the init section of your MCU, and letter rip. Never have to check on it or mess with it again. It'll just run on its own forever.

    What is it you want to display on the screen? I've driven mono GLCD's with just simple 8 lines of text. This micro would be fine for that. If you want to do simple graphics like a bar chart, this should also be little trouble. Should even be able to do a simple x y graph. If you want full color graphics, I would suggest a lot more umph. I use a PIC32 (32 bit MCU with 512k of flash at 80 MHz) to drive a 320x240 16-bit color GLCD and sometimes even that can feel sluggish when accessing the SD card and updating the screen at the same time.
     
  7. galed

    galed New Member

    Joined:
    Sep 27, 2009
    Messages:
    32
    Likes:
    0
    Location:
    Boulder, CO
    So here's an even more cleaned up version. I decided to use usb instead of rs232 since it should be just as easy and is what I want to end up with anyway. The ISP's in there too.
     

    Attached Files:

  8. Noggin

    Noggin Member

    Joined:
    Jul 21, 2003
    Messages:
    595
    Likes:
    9
    Location:
    Austin, TX
    Lookin pretty good now :)

    Got a few more things for ya though.

    1. Put 10k pull up resistors on both chip select lines on your sensors. This will keep them from getting selected while the ATMega is held in reset while being programmed.
    2. Put a 0.1uF cap on the 3v3OUT on the USB<->Serial adapter
    3. Put a power connector on the board
    4. I find that a couple of LED's goes a long way to help debug. One health LED blinking at 1 or 2 Hz rate will make you feel a LOT better about whether or not the MCU is running when it seems like it is refusing to do anything.
    5. Add the 18 to 22pF caps to ground on each side of the crystal
    6. It has been over a year since I've used an AVR, but I think you want MCLK on PB1 as that is your PWM (Output Compare) capable pin. Double check the datasheet
     
  9. galed

    galed New Member

    Joined:
    Sep 27, 2009
    Messages:
    32
    Likes:
    0
    Location:
    Boulder, CO
    1. What are you referring to when you say chip select lines?
    2. I'll get to a power connector, eventually
    3. I think I'll use LEDs for the USB chip to know that it's actually doing something. I also like the idea of having one blinking at one or two hz just to know the processor is doing something
    4. I think the 168 has numerous PWM pins, I need to double check though

    Lastly, I'm still not really sure what's happening with the serial interface on the pressure sensor, MS5541C...

    new and improved atttached
     

    Attached Files:

  10. Noggin

    Noggin Member

    Joined:
    Jul 21, 2003
    Messages:
    595
    Likes:
    9
    Location:
    Austin, TX
    Hmm, I assumed that both devices were SPI. Big boo-boo on my part. The pressure sensor is not SPI and it does not have a chip select. The CS pin on the accelerometer though is a chip select and it is used to tell the device that it is about to be written to. So, pull that one high with a 10k resistor. The MS541 doesn't have a CS pin, it uses a special sequence of clocks and DIN to know when to start doing something. Put a 10k pulldown on its SCLK pin so that it doesn't get accidentally activated while the AVR is being programmed.

    From looking at the datasheet on the MS5541, it looks like you should separate the DIN and DOUT lines on that device. Some SPI devices will allow you to tie the lines together like you did, but it looks like this one does not. Page 13 of the data sheet shows an example of how to read D1 (pressure measurement).

    When idle, SCLK and DIN (DIN is the sensors receive line) should be low. You don't have to use the following code, but it would be close to what I would do:
    Code (text):
    // ms5541c.c
    Uint8 MS5541_Send_Hi_Bit( void )
    {
    Uint8 read_value;

       set DIN high
       wait 100 uS
       Set SCLK high
       wait 100 uS
       if DIN is high read_value = 1
       Set SCLK low

       return read_value
    }

    Uint8 MS5541_Send_Lo_Bit( void )
    {
    Uint8 read_value;

       set DIN low
       wait 100 uS
       Set SCLK high
       wait 100 uS
       if DIN is high read_value = 1
       Set SCLK low

       return  read_value
    }

    Uint8 MS5541_Read_Bit( void )
    {
       return MS5541_Send_Lo_Bit()
    }

    void MS5541_Send_Start_Bit( void )
    {
       MS5541_Send_Hi_Bit()
       MS5541_Send_Hi_Bit()
       MS5541_Send_Hi_Bit()
    }

    void MS5541_Send_Stop_Bit( void )
    {
       MS5541_Send_Lo_Bit()
       MS5541_Send_Lo_Bit()
       MS5541_Send_Lo_Bit()
    }

    Uint8 MS5541_Read_D1( Uint16 *data)
    {
    Uint16 shift_value;
    Uint8 i;

       MS5541_Send_Start_Bit()
       
       MS5541_Send_Hi_Bit()
       MS5541_Send_Lo_Bit()
       MS5541_Send_Hi_Bit()
       MS5541_Send_Lo_Bit()
       
       MS5541_Send_Stop_Bit()
       
       // at this point, DOUT should be high which signals and ACK
       if (DOUT == 0)
          return MS5541_ERR_NAK  // no acknowledgement

        MS5541_Send_Lo_Bit()
        MS5541_Send_Lo_Bit()

       while (DOUT == 1)
          ; // wait for DOUT to go low to signify that the device is ready to be read

       for (i = 0; i < 16, i++)
          shift_value = (shift_value << 1) | MS5541_Read_Bit()

       *data = shift_value;

       return MS5541_SUCCESS;
    }
    Showing code is the best way I can describe how the data works.

    Edit: Just saw this in the datasheet:
     
    Last edited: Sep 29, 2009
  11. galed

    galed New Member

    Joined:
    Sep 27, 2009
    Messages:
    32
    Likes:
    0
    Location:
    Boulder, CO
    good catch on the vdd cap.. i missed that one.

    thanks for the code too, that really helps clarify what's happening
     
  12. buton

    buton New Member

    Joined:
    May 3, 2012
    Messages:
    2
    Likes:
    0
    Did you make it work?

    i want to make a depth logger, i bought the sensor and a arduino uno... i will mess with it this weekend.

    any pointers?
     
  13. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,487
    Likes:
    186
    Location:
    Prague, Czechia (not Chechnya)
    I guess Galed died diving, because he has not been here since september 2010. So much for opening ancient threads.
     
  14. buton

    buton New Member

    Joined:
    May 3, 2012
    Messages:
    2
    Likes:
    0
    Well,

    I will start working on that and let you know...
     

Share This Page