Continue to Site

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.

  • 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.

interfacing ATMega168 with depth sensor and accelerometer

Status
Not open for further replies.

galed

New Member
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
 

Attachments

  • ADXL345.pdf
    481.8 KB · Views: 645
  • depth sensor - ms5541c.pdf
    277.7 KB · Views: 806
  • ATMega168.pdf
    4.5 MB · Views: 706
  • circuit.png
    circuit.png
    20.8 KB · Views: 3,710
Last edited:
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:
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.
 
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
 

Attachments

  • Screenshot.png
    Screenshot.png
    22.5 KB · Views: 992
  • circuit.png
    circuit.png
    25.7 KB · Views: 1,355
Last edited:
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.
 
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.
 

Attachments

  • circuit.png
    circuit.png
    28.3 KB · Views: 989
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
 
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
 

Attachments

  • circuit.png
    circuit.png
    29.5 KB · Views: 1,142
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:
// 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:
Particular care must be taken when connecting the device to power supply: A 47μF tantalum capacitor must be
placed as close as possible of the MS5541C's VDD pin.
 
Last edited:
good catch on the vdd cap.. i missed that one.

thanks for the code too, that really helps clarify what's happening
 
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?
 
I guess Galed died diving, because he has not been here since september 2010. So much for opening ancient threads.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top