Project Documentation. Motion Sensor LIS302DL accelerometer

Project Documentation Motion Sensor – LIS302DL accelerometer Siemens Technik Akademie Berlin Written by: Jörg Sablottny Benito Gerasch Instructor:...
6 downloads 0 Views 3MB Size
Project Documentation Motion Sensor – LIS302DL accelerometer

Siemens Technik Akademie Berlin

Written by:

Jörg Sablottny Benito Gerasch

Instructor:

Marcel Langner

Closing Date:

February 26, 2010

Jörg Sablottny Benito Gerasch 3rd Semester Project

1/27 Siemens Technik Akademie Berlin Microcomputer Technology

Table of Contents 1. Project Overview............................................................................................................. 3 1.1 Topic........................................................................................................................... 3 1.2 Goals of the Project ................................................................................................. 3 1.3 Equipment ................................................................................................................. 3 2. Programmer’s Notepad ................................................................................................. 6 3. Connect and configure the Hardware ......................................................................... 9 3.1 Unbreakable Board & LIS302DL ......................................................................... 11 3.1.1 SPI (unbreakable board-Atmel Atmega32) ................................................ 11 3.1.2 SPI (motion sensor LIS302DL)..................................................................... 13 3.1 Communication to a PC over Serial Port using USART.................................. 15 Attachments....................................................................................................................... 21

Jörg Sablottny Benito Gerasch 3rd Semester Project

2/27 Siemens Technik Akademie Berlin Microcomputer Technology

1. Project Overview 1.1 Topic You surely know today’s mobile phones like iPhone etc. But what you maybe ask yourself is how it can handle those applications like skipping pictures or driving cars in games only by moving your phone? Well the answer is quiet simple: Inside your Phone there is a little motion sensor which is configured to handle your movements. For our project we also use a motion sensor which is connected to a microcontroller in order to send the results of our movements to a connected PC.

1.2 Goals of the Project The goal of the project is to prepare a helpful documentation, so that anybody is able to work with the motion sensor and the microcontroller for some advanced future projects without having to read all datasheets. On the other hand this project helps us improving our knowledge in programming and working together in teams.

1.3 Equipment For our project we use different equipment: 1. 2. 3. 4.

Motion Sensor LIS302DL Unbreakable Board (with Atmel Atmega32 ) Bread Board with different components (voltage divider) PC with RS232-serial interface

1. The LIS302DL is a very small but powerful, configurable triple axis accelerometer. As the name says this motion sensor is built up with 3 different axis: x, y and z. In order to handle all movements in a 3-dimensional environment this is very important. Jörg Sablottny Benito Gerasch 3rd Semester Project

3/27 Siemens Technik Akademie Berlin Microcomputer Technology

LIS302 accelerometer 2. The unbreakable board is based on the Atmel Atmega32 microcontroller. It is called “unbreakable” because it has an incorrect input signal protection. The board also has a serial port interface available which can be connected to RS232-serial interface of a PC. After you have connected your board to the PC your Atmel Atmega32 can be programmed in C/C++ using the “Programmer’s Notepad” which is a great environment for programming microcontroller.

Unbreakable Board based on Atmel Atmega32

3. The breadboard is directly connected to the LIS302DL. To prevent that the motion sensor breaks down in case of to high voltages we connected all pins of it with voltage dividers on the breadboard. The following shows a schematic of the breadboard:

Jörg Sablottny Benito Gerasch 3rd Semester Project

4/27 Siemens Technik Akademie Berlin Microcomputer Technology

Bread Board schematic designed with EAGLE

4. The last component we need is a PC that offers an RS232-serial interface in order to program the microcontroller or to read/write values over this port. The following schematic should give you a little overview about the interfacing and connection: Serial Port

Laptop

Serial Cable

µController

Jörg Sablottny Benito Gerasch 3rd Semester Project

5/27 Siemens Technik Akademie Berlin Microcomputer Technology

2. Programmer’s Notepad The “Programmer’s Notepad” is a software which is helpful for programming microcontrollers like the Atmel Atmega family. To use it you only have to connect your microcontroller over the RS232-serial interface to a corresponding PC. After that you can start programming it. The following steps are based on the software package MCT_All_in_One.zip which we got from our instructor in the field of microcomputer technology. At first we start the program and choose number 3 for the Atmel AVR microcontrollers which are based on a RS232 boot loader:

Enter the start setup Now you should see your editor where you can choose the programming language. In our case we decide for C/C++.

Jörg Sablottny Benito Gerasch 3rd Semester Project

6/27 Siemens Technik Akademie Berlin Microcomputer Technology

Programmer’s Notepad To write your programm to the Atmega board you have to make sure that you use the right files: important are the “makefile” which depends on your Atmel chip and the “main.c” which is already in the software package as an example.

Makefile and Main.c Copy those two files in a new folder you create first. Then open the main.c in your editor and write your code. Jörg Sablottny Benito Gerasch 3rd Semester Project

7/27 Siemens Technik Akademie Berlin Microcomputer Technology

After you have written your source code you have to get the program onto your controller. Therefore open “tools” and click on “Make All” first. You should get some errors if you have mistakes in your program.

Programming the AVR controller If everything is fine you click on “Make Program”. Be sure that you reset your microcontroller directly after clicking! The program should be written to your controller. You can work with it now.

Later we will explain you how to read values from the serial port. But this should be enough as a little introduction to the software.

Jörg Sablottny Benito Gerasch 3rd Semester Project

8/27 Siemens Technik Akademie Berlin Microcomputer Technology

3. Connect and configure the Hardware After everything is prepared you can start to connect the hardware components together. As you know from part one of the documentation the LIS302DL is already connected to the breadboard. The next step is to connect the breadboard with the unbreakable board. In case of that we want the unbreakable board to communicate with the LIS302DL we have to connect the wires of the LIS302DLbreadboard connection to the SPI interface of the unbreakable board. The pins of the SPI interface are located on the bottom side of the unbreakable board.

The wires have to be connected as the following table shows. For the location of the pins you also have to take a look into the datasheet of the unbreakable board. LIS302DL VCC GND SCL MOSI MISO CS

Unbreakable Board VCC - 2 GND - 6 SCK (PB7) - 3 MOSI (PB5) - 4 MISO (PB6) - 1 OUT_1 (PD4) Connection Table

Jörg Sablottny Benito Gerasch 3rd Semester Project

9/27 Siemens Technik Akademie Berlin Microcomputer Technology

You really have to take care that the Master-Out-Slave-In (MOSI) of the LIS302DL is connected to the MOSI pin of the unbreakable board. This also counts for MISO. The CS pin of the motion sensor is located on the top of the board.

6 5 Vcc GND SCL MOSI MISO CS LIS302DL

4 3 2 1 Unbreakable Board – SPI ( see Connection Table)

Final hardware connection

Jörg Sablottny Benito Gerasch 3rd Semester Project

10/27 Siemens Technik Akademie Berlin Microcomputer Technology

3.1 Unbreakable Board & LIS302DL We need to configure two different interfaces on the unbreakable board. On the one hand the Serial Peripheral Interface (SPI) for the communication between unbreakable board and LIS302DL and on the other hand the Universal Synchronous and Asynchronous serial Receiver and Transmitter (USART) for the communication between unbreakable board and PC via a serial cable.

3.1.1 SPI (unbreakable board-Atmel Atmega32) First of all the SPI interface should be configured in master mode. Therefore take a look into the ATMega32.pdf datasheet on page 134. The following pages of this datasheet do also show all the registers you need to configure for the SPI interface. We configured them like followed: SPI Control Register (P. 134/135) SPCR = 0x53; Here you can see that we have set bit 6, 4, 1 and 0 to configure this register. Bit 6: enable SPI operations Bit 4: Master SPI mode Bit 1/0: setting SPI clock rate (fosc/128), choose the slowest one for test reasons SPI Status Register (P. 136) SPSR = 0; There is nothing special that needs to be configured.

SPI Data Register (P. 136) SPDR = ..; For SPDR you do not have to set specific bits for configuration because it is the read/write register for data transfer. You should write the register information for the motion sensor into this data register in order to read the information from it or

Jörg Sablottny Benito Gerasch 3rd Semester Project

11/27 Siemens Technik Akademie Berlin Microcomputer Technology

you can write information into SPDR so that the registers of the motion sensor gets configured by setting corresponding bits. Of course you also need to configure the ports and pins where the SPI interface of the unbreakable board is connected internally. Therefore take a look at the datasheet of the unbreakable board: unbreakable_docu.pdf

PORTB/PIN7-4 SPI interface

PORTD/PIN4

unbreakable board schematic Configure Output Ports: DDRD |= 0x10; // SET Port D Bit 4 as output DDRB |= 0xB0; // SET Port B Bit 7,5,4 as output Configure Port Pins: PORTD &= ~0x10; // Port D 2^4 = off (low active) // LIS302DL: chip select enable (0) PORTB |= 0x10; // set SS Pin – not affected PORTB |= 0x80; // set Clock PORTB |= 0x20; // set MOSI (MasterOut-SlaveIn)

Jörg Sablottny Benito Gerasch 3rd Semester Project

12/27 Siemens Technik Akademie Berlin Microcomputer Technology

Now the SPI interface for our unbreakable board is configured and ready to interact with the motion sensor.

3.1.2 SPI (motion sensor LIS302DL) For the configuration of the SPI interface and the registers to communicate with the unbreakable board you should use the corresponding datasheet: LIS302DL.pdf First of all we defined all the possible registers of LIS302DL with their related addresses. The most important once are the following: #define #define #define #define

CTRL_REG1 0x20 OUT_X 0x29 OUT_Y 0x2B OUT_Z 0x2D

You need those addresses to read from the motion sensor or to write data to its registers for configuration. For the reason that it is more easy to work with the names of the registers and not with their addresses, we use the #define – function in Ansi-C. Control Register 1 (P. 26): MS_write(CTRL_REG1, 0x47); This is an example for writing to the register CTRL_REG1. The write-function will be explained later in this chapter. What is important at the moment is to know, that you can give information to the corresponding register with this function. So you are now able to configure it. The hex-value 0x47 tells you what bits where set: Bit 7: 0 – Data rate selection in normal mode Bit 6: 1 – Power down control in normal mode Bit 5: 0 – Self test selection in normal mode Bit 4/3: 0 – Self test enable in normal mode Bit 2: 1 – Z axis enable Bit 1: 1 – Y-axis enable Bit 0: 1 – X-axis enable

Jörg Sablottny Benito Gerasch 3rd Semester Project

13/27 Siemens Technik Akademie Berlin Microcomputer Technology

Output Data of X/Y/Z-axis (P.29/30): char xaxis, yaxis, zaxis; xaxis = MS_read(OUT_X); yaxis = MS_read(OUT_Y); zaxis = MS_read(OUT_Z); In order to get the value for the related axis when you move the motion sensor, you have can use the read-function by setting the register you want to read as parameter. Also this function will be explained later in this chapter. To work with the value you have read out, you should save it into a variable you declared before.

Read-Function: char MS_read(char Reg) { PORTD|=0x10; Reg |= 0x80; SPDR=Reg; while(!(SPSR & 0x80)); SPDR=0x00; while(!(SPSR & 0x80)); PORTD&=~0x10; return SPDR; } Within this function you can see how the information of the motion sensor is written to the SPI data register (SPDR) of the Atmel Atmega32. So you can communicate now for the first time. First you should turn on the chip select by setting it to 0. You have to know that you have to set it, in order to reset the value to 0. The MS-read-function is a normal Ansi-C function that returns the information which was written to SPDR. This happens by sending the register information (parameter for the function) to SPDR. When you have done this, you wait until the transmission was completed. The next step is that you have to send some “pseudo information” to SPDR and wait again for a completed transmission. You can have a look to the datasheet where you can see why this is necessary (P. 20 22). After you have sent all your information, you turn off the chip select and return the information that is stored in SPDR now (Register information).

Jörg Sablottny Benito Gerasch 3rd Semester Project

14/27 Siemens Technik Akademie Berlin Microcomputer Technology

Write-Function: void MS_write(char Reg, char Inf) { PORTD|=0x10; Reg &= 0x7F; SPDR=Reg; while(!(SPSR & 0x80)); SPDR=Inf; while(!(SPSR & 0x80)); PORTD&=~0x10; } The write-function is also really important to enable the motion sensor or get it work by writing to its specific registers (configuration). Of course the write-function has the return type void because it does not have to return some values; it is more important to write information to the registers of LIS302DL in order to configure them. The parameters of this function are the register information (like in the readfunction) and the additional information which you can use to configure the corresponding registers by setting predefined bits. All in all this function is quite similar to the read-function: you also have to set chip select at the beginning, then you set the write flag instead of the read flag (P. 22) and write your information into SPDR. After this you wait again for the transmission complete and send the configuration information to SPDR. Wait again and finally turn off chip select.

3.1 Communication to a PC over Serial Port using USART After your configuration of the SPI interface and the communication between the unbreakable board and LIS302DL motion sensor has been done, you can start to configure your serial port interface for communication between your PC and the unbreakable board. All the important configuration registers of the Atmel Atmega32 you can find again in the datasheet: ATMega32.pdf (P. 138 - 166) So let’s start to initialize the serial communication device: USART. The following registers need to be configured correctly: UBRRH UBRRL

Jörg Sablottny Benito Gerasch 3rd Semester Project

15/27 Siemens Technik Akademie Berlin Microcomputer Technology

UCSRA UCSRB UCSRC USART Baud Rate Registers – UBRRL and UBRRH For the reason that we work only with 8 bits the register UBRRH is written to 0. In order to configure UBRRL correctly you have to read the pages 162 – 166 of your datasheet, to make sure that you choose the right values for the baudrate with regard to the internal chrystal of your microcontroller. The internal chrystal of our unbreakable board which is based on the Atmel Atmega32 is 14.74 Mhz! In our case we used a baudrate of 9600 bps, so UBRRL gets the value 95. UBRRH=0; // only use 8 bits

This picture shows you a table of available baudrates, you can choose. Be sure that you look for the correct chrystal and that U2X is configured right for your choice (as 0 or 1)  bit 1 of UCSRA register. UBRRL=95; // baudrate 9600

Jörg Sablottny Benito Gerasch 3rd Semester Project

14.74 Mhz chrystal

16/27 Siemens Technik Akademie Berlin Microcomputer Technology

USART Control and Status Register A – UCSRA UCSRA=0xC0; //reset receive/transmit-complete P. 158/159 The following bits are set (write logic 1) for the configuration: Bit 7: receive complete: data in receive buffer Bit 6: transmit complete: shift out data Bit 1: U2X = 0: synchronous operation

USART Control and Status Register B – UCSRB UCSRB=0x08; //transmit enabled now P. 159/160 The following bits are set (write logic 1) for the configuration: Bit 3: transmit enable: enable USART transmitter

USART Control and Status Register C – UCSRC UCSRC=0x86; P. 160/162 The following bits are set (write logic 1) for the configuration: Bit 7: select registers Bit 2: select registers Bit 1: 8 bit character size

USART I/O Data Register – UDR UDR = 65; // 65 = “A” [ASCII] while(!(UCSRA & 0x20)); //empty transmit buffer This register is important for serial communication because the data you want to receive or transmit over the serial port has to be written into UDR. This example shows how to send an “A” to the PC. Please make sure that you always wait for an empty transmit buffer, when you want to write data into UDR. Therefore you have to check the UDRE-flag of the UCSRA register. (P. 157/158) As you can see we write a 65 to UDR, what means that we want to send an “A”. The reason for this is that we have to use ASCII-character encoding when writing or reading this register. Jörg Sablottny Benito Gerasch 3rd Semester Project

17/27 Siemens Technik Akademie Berlin Microcomputer Technology

After you have configured USART correctly you can prove your program by using the internal RS232 Monitor of the Programmer’s Notepad for reading values from your serial port. As an alternative you can also use Windows Hyper Terminal which you can find in the menu “Accessories”.

Open RS232 Monitor

A new tty should open now, where you can configure the COM port, baudrate, parity etc. To connect to the COM1 go to the menu “File”  “Connect”

Jörg Sablottny Benito Gerasch 3rd Semester Project

18/27 Siemens Technik Akademie Berlin Microcomputer Technology

Connection to serial port

Now you are able to read the values, your microcontroller is sending over the serial port to the PC.

Jörg Sablottny Benito Gerasch 3rd Semester Project

19/27 Siemens Technik Akademie Berlin Microcomputer Technology

Finally you can play around with the motion sensor and watch the values changing. You will recognize that it is really stable and works absolutely exact. Attached you can find a program example for testing LIS302DL: main.c

Testing the motion sensor LIS302DL If everything is working well, you can see your movements on the screen:

The Z-Axis always has a standard value of about 54d without moving the motion sensor! The reason for this is the Earth’s Gravity.

Reading values from LIS302DL Jörg Sablottny Benito Gerasch 3rd Semester Project

20/27 Siemens Technik Akademie Berlin Microcomputer Technology

Attachments Bread Board Schematic EAGLE - Copyright (c) 1988-2006 CadSoft

Jörg Sablottny Benito Gerasch 3rd Semester Project

21/27 Siemens Technik Akademie Berlin Microcomputer Technology

Source Code (Example): main.c Programmer’s Notepad

//LIS302DL motion sensor #include #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define #define

WHO_AM_I 0x0F CTRL_REG1 0x20 CTRL_REG2 0x21 CTRL_REG3 0x22 HP_FILTER_RESET 0x23 STATUS_REG 0x27 OUT_X 0x29 OUT_Y 0x2B OUT_Z 0x2D FF_WU_CFG_1 0x30 FF_WU_SRC_1 0x31 FF_WU_THS_1 0x32 FF_WU_DURATION_2 0x33 FF_WU_CFG_2 0x34 FF_WU_SRC_2 0x35 FF_WU_THS_2 0x36 FF_WU_DURATION_3 0x37 CLICK_CFG 0x38 CLICK_SRC 0x39 CLICK_THSY_X 0x3B CLICK_THSZ 0x3C CLICK_TimeLimit 0x3D CLICK_Latency 0x3E CLICK_Window 0x3F

char MS_read(char Reg); void MS_write(char Reg, char Inf); int main (void) { //configure outputs DDRD |= 0x10; // SET Port D Bit 4 as output PORTD &= ~0x10; // Port D 2^4 = off (low active) DDRB |= 0xB0; // Output, set SS Pin high for SPI System // not affected PORTB |= 0x10; // set SS Pin PORTB |= 0x80; // set Clock PORTB |= 0x20; // set MOSI (MasterOut-SlaveIn) //configuring SPI interface on ATMEGA32 SPCR = 0x53; //SPI Control Register Page 134 ff. SPSR = 0; /*configuration___initialize USART*/ UBRRH=0; // only use 8 bits UBRRL=95; // baudrate 9600 14.74 Mhz chrystal

Jörg Sablottny Benito Gerasch 3rd Semester Project

22/27 Siemens Technik Akademie Berlin Microcomputer Technology

UCSRA=0xC0; //reset receive/transmit-complete UCSRB=0x08; //transmit enabled now UCSRC=0x86; // 8 bit character //char test; char xaxis, yaxis, zaxis; char d100x, d10x, d1x, d100y, d10y, d1y, d100z, d10z, d1z; unsigned char counter = 0; unsigned char arr[] = {'X','-','A','x','i','s',' ','M','O','V','E','S',':',' '}; //14 characters unsigned char arr1[] = {'Y','-','A','x','i','s',' ','M','O','V','E','S',':',' '}; //14 characters unsigned char arr2[] = {'Z','-','A','x','i','s',' ','M','O','V','E','S',':',' '}; //14 characters

while(1){ MS_write(CTRL_REG1, 0x47); //set x-/y-/z-enable, Power-Down //Control active mode /*test = MS_read(CTRL_REG1); --> value 071 is shown on hyper terminal --> configuration was successfully*/ //X-AXIS for(counter=0;counter