Translate

Showing posts with label olimex. Show all posts
Showing posts with label olimex. Show all posts

Saturday, December 21, 2013

Build fast a simple tachometer

The goal : measure the RPM of an home-made electric motor for a school experiment.

Requirements


The kind of motor built for the experiment is slow by definition, so the RPM meter (also called tachometer - http://en.wikipedia.org/wiki/Tachometer) must be capable to measure and display low revolutions per minute.
The other goal is to build the tool easily and in short time, possibly spending the less possible.

Acquiring Methods


The first problem to solve is how to acquire the rotor revolutions, i.e. how to translate a movement in an electric signal.
Usually there are two methods :
  • magnetic
  • optical 

Magnetic

Since the rotor has already magnets glued on it, it is relatively easy to add a magnetic sensor capable to generate a pulse every time a magnet pass close to it.
The sensor can be a reed switch or, typically, a Hall sensor. The Hall sensor is better than the Reed switch because is not mechanical, so is not generating false signals and is faster.
If an Hall sensor is used, it must be unipolar.
There are two kind of Hall sensors, unipolar and bypolar.
The bypolar requires to "see" a change pole. i.e. a change from South pole to North pole and viceversa. Every change, triggers the change of the electric signal.
The unipolar sensor change the state of the electric signal every time a South pole is close to the sensor.
Here a graph to show the two kind of Hall sensors :


The upper part shows a bipolar Hall sensor.
When the South pole is close to the sensor, the output signal goes down. Only when the North pole is close to the sensor, the output signal goes back high.
The lower part of the graph shows a unipolar Hall sensor.
When the South pole is close to the sensor, the output goes down and it goes up as soon as the South pole is going away from the sensor.

Optical

The other common way to acquire the revolution, is optical.
Typically on the rotor is glued a reflective surface in a spot. A Infrared ray is sent toward the rotor and a circuit can detect the reflection of the infrared ray when bounced back from the reflective surface.
As alternative is possible to attach a disc with holes to the rotor and have a light beam (or infrared beam) interrupted by such disc.
A third optical alternative is to attach to the rotor a "black and white" pattern and illuminate it with a stroboscopic light.
Changing the frequency of the light until the pattern is "stable" (or visible as it would be stopped) will give the RPM.

Measurement


We have a series of pulses generated by the sensor.
Let assume we are using the magnetic acquiring method, with a unipolar Hall sensor.

Depending how many magnets we have on the rotor we can have "n" pulses for every revolution.
i.e if we have two magnets on the rotor, we'll have two pulse every revolution. 4 magnets will generate 4 pulses and so on.

The "intuitive algorithm is quite easy. We need to count the number of pulses in a minute, then divide the number for the number of magnets and the result will be our RPM.

Unfortunately this method has a drawback. We need to wait a minute before to see the result and after that, we need to wait another minute before to see changes.
So we need to reduce the acquiring time and then estimate the RPM from the minor number of readings.
For example we can count the pulses for 1 second, then divide the number of pulses by the number of magnets. The number will represent the number of revolution for second. If we multiply that number for 60, will have the number of revolution per minute (1 minute = 60 seconds).

This can work well assuming the motor rotates at a constant rate and that rate is not too slow.
For very slow rotations, it is necessary to increase the counting time, maybe to two or three seconds (and then multiply accordingly)

The problem with that is the "visualization" of the measurement. More time to count the pulses means more time between the visualization of the result.

Building


It is possible to build the tachometer in many different ways.
Of course today is more simple to use a microcontroller rather than use more "traditional" ways, like using counters, decoders and LED display.
Any microntroller family is more than suitable for this application, especially because the measurements to do are not critical.
A microcontroller is also better than "traditional logic" because it require less components and only changing the software is possible to change the type of measurement.

Mostly because is something I already have around, the idea is to use the Texas Instrument MSP430 family of microcontroller, specifically a board with already the MSP430F169 and a Philips LCD display.


This board has everything we need to build a tachometer and we can easily change the parameters, like the number of magnet on the rotor and the acquisition time.

The software will be build using the GNU Msp430 Gcc compiler.

Note ! It is possible to use the software on different boards with few modifications.
For example the MSP430 Launchpad board is more than enough for this project.
Only two reasons did let me decide for the Olimex board :
  1. I already had this board around picking up dust
  2. This board has already a joystick and LCD display on it

Tools


In order to develop the RPM meter (or tachometer) some tools are necessary.
The software will be build under a Linux machine, using Code::Blocks as editor/IDE.
The compiler used is the open source version of the GNU Gcc for the MSP430 .
The debug system is based on the msp430-gdbserver.
An oscilloscope will be used to check the input signal from the Hall sensor.
The PC used to develop the code must have a traditional parallel port since the only GDB tool is parallel.
For the schematics, the FidoCad and Eagle programs will be used.
FidoCad allows to draw very simple schematics, Eagle is more sophisticate and can be used to produce a PCB (not necessary for this particular project though)

Hardware


As introduced before, the main hardware is the Olimex board based on the MSP430F169.
What is needed is the Hall sensor. I choose a Honeywell SS400 series Unipolar Hall sensor (SS441A ).

The sensor will be connected to an I/O pins of the board,  one capable to read the output signal from the sensor.
Since the board has plenty of available I/O Pins is not a problem.

Specifically will be used the Pin1.1 to read the signal from the Hall Sensor.


The P1.1 is capable to generate interrupts and it can be also connected to an internal timer in case a different measure approach is needed.
The sensor needs to be powered with a Vcc starting from 3.8 V. The Olimex board is operating at 3.3.V so an external regulator is needed and a 5V one is used. Because the MSP430F169 I/O pin are NOT 5V tolerant a resistor limiting the current is needed to avoid damage to the microcontroller.
The capacitors are the standard ones for the 78L05 regulators.

Here is possible to find the schematic of the Olimex board. 

Software

First draft


The software is a first simple draft to prove the concept and verify the hardware.
It is based on the original demo code coming with the board, modified for the specific needs.
The Hall sensor signal will be connected to an interrupt capable I/O in order to don't miss any "firing" of the signal.
One or more timers will be used for the time counting. The visualization will be placed in the main loop.

The algorithm is quite simple and based on two interrupts.
One interrupt is related to the I/O pin connected to the Hall sensor. Normally the signal is pulled up, so the interrupt is set to fire on the descending slope of the signal.
The interrupt function increment a counter every time the signal goes down.

The other interrupt is a timer.
A generic timer "fires" every "n" seconds (default is 1).
When the timer fires, the the counter is copied in a variable and the counter is set to zero.
A flag is set to one indicating the need to display the content.
In the main loop, the visualization loop check the variable updated by the visualization timer to show the result.

I/O Settings


Some I/O are "forced" by the nature of the board.
Here the main ones used other than the ones pre-wired on the board :


P1.1 Hall sensor input
Pulled up, the pin goes to zero when a South Pole magnetic field is close to the sensor
P2.1 Status LED - toggle at every Hal sensor signal
The LED is toggled at every sensor magnetic event
P2.2 Status clock
The pin is toggled at every timer firing. If the timer is set for 1 second, the signal will have 2 second period.
2 seconds timer will generate a 4 seconds period on the pin and so on.
P2.3 Debug pin
Generic debug pin


Clock settings


The Olimex MSP430F169 LCD board has two crystal sources.
A 32Khz crystal and a 8 Mhz crystal.
For this application one of them should be OK since the low time involved.
The minimum period needed is about 1 second, so also the slower but precise 32Khz source should be OK as clock.

In order to minimize the timer interrupts generated, the 32 Khz crystal is used in order to generate a 1 second timer used for the measurement, the rest of the board continue to use the DCO as main source of the clock.

To set the 32Khz crystal :
DOCTL = 0x60
modulation off, DCOx and RSELx ~800kHz
BCSCTL1 = 0xB7
XT2OFF = 1 - XTS = 0 - DIVAx = 3 - RSELx = 7
BCSCTL2 = 0x00
SELMx = 0 - DIVMx = 0 - SELS = 0 - DIVSx = 0 = DCOR = 0
BCSCTL3 = 0x0C
XT2Sx = 0 - LFXT1Sx = 0 - XCAPx = 12.5pF - XT2Of = 0 - LFXT1OF = 0

With this setting the ACLK is of 4096 Hz, the MCLCK/SMCLK of ~800kHz
Note : using the 32Khz for the main clock (MCLK) the display is not working properly.

Timer setting


A generic timer is handling some sub-timers used to count the Hall sensor pulse and display the value.
The timer_A 0 will be used in the up mode.
The clock source for the timer will be ACLK, i.e. 4096 Hz.
The Timer_A will be set with a /8 so that the value 512 placed in the TACCR0 will generate an interrupt every 1 second.

TACTL = TASSEL_1 + ID_3 + MC_1; /* Uses ACLK, divide by 8, count in up mode */
TACCTL0 = CCIE; /* Use TACCR0 to generate interrupt */
TACCR0 = TMRVALUE; /* 1s */

Menu

When the code starts, a menu appears.
Two choices are available :

  • Set
  • Measure

The Set allows to set up some parameters, like the number of seconds for the counter/display value or the number of magnets
The Measure starts the measurement.



Update


After few small modifications and settings, the tachometer was installed on the home-made experiment motor.
The sensor is placed on the top of the rotor using a wooden pole, so to be perfectly aligned with the rotor's magnets.





If somebody is interested to have the source code just contact me

Sunday, October 16, 2011

Olimex MSP430F169 LCD board


Olimex has different development platforms available for the MSP430.
One of these is the MSP430-169LCD. This article contains some notes about the board, tests operations and tips/tricks.

This development kit is very interesting.

FEATURES:

  • MCU: MSP430F169 with 60K Bytes Program Flash, 256 Bytes data Flash,2K Bytes RAM
  • NOKIA 3310 LCD 84x48 pixels black & white
  • Joystick with 4 directions and push button function
  • SD/MMC card connector
  • two LEDs: status and power
  • RESET switch
  • JTAG connector
  • 32 768 Hz oscillator crystal
  • 8Mhz crystall oscillator
  • power supply voltage regulators and filtering capacitor
  • extension headers for all uC pins
  • PCB: FR-4, 1.5 mm (0,062"), soldermask, white silkscreen component print
  • Dimensions: 67x66 mm (2.65x2.6")
On the Olimex website or the Sparkfun Electronics website , is possible to download the schematic of this board, a test program and the processor specific datasheet .

Linux development environment

First of all, I installed the mspgcc development system.
Since I use Ubuntu I installed it (another article will describe that ), then I installed the program msp430-gdbproxy and the libraries from the Soft-switch.org website.

This is very critical.
The msp430-gdbproxy must be copied in the directory where is installed the mspgcc environment (usually /usr/msp430/bin, but in my case /opt/msp430-gcc-4.4.3/bin) and the two libraries libHIL.so and libMSP430.so, must be copied in the directory /usr/lib.
It is vital to copy the program and libraries from the same site, i.e. compiled for the same version.
The mspgcc installation process already copy the libHIL.so and libMSP430.so but very likely these libraries are NOT in sync with the msp430-gdbproxy program.

JTAG test

Then I started to see if the Olimex board was working.
I connected a 9V battery to the board (Vin and GND) and the board become alive.
Then, removing the external power, I connected the board to the JTAG connector, already hooked to the parallel port of the PC.
The board come alive, since the JTAG connector, thru the jumper P-IN (inserted by default), bring the power.

At this point, following the instructions found on the Develissimo website and from this article on the MSP430 development environment , I started the msp430-gdbproxy in a terminal.
The first time I had strange error, so I reset the board and something started finally to work.
However using the suggested command :

#msp430-gdbproxy --port=2000 msp430

I obtained this error :

debug: MSP430_Configure()
error: msp430-gdbproxy: unable to open debugger connection. Will restart

I spent quite some time tracking this error but the only thing I realized, was that the error was somehow related to the TCP port used or about some conflict with other services or the Linux configuration.
Then I started to read the mspgcc documentation and I found some notes about the msp430-gdbproxy .
In the notes it was suggested to try the command:

#msp430-gdbproxy --debug msp430

and it worked !
Only I noted that the default port was not 2000 but 2001 !
So I modified the original command using the new port :

#msp430-gdbproxy --port=2001 msp430

and ... voila' !!!
I was able to connect the Olimex board with the JTAG.


So to recap, in order to open a debug session with the card :

  • connect the Olimex board to the Jtag (parallel version)
  • open a terminal and start the gdb proxy :
    msp430-gdbproxy --port=2001 msp430
    Note ! If is not working is possible to use sudo, but in this case the absolute path of the msp430-gdbproxy must be issued.
    For example in my case : sudo /opt/msp430-gcc-4.4.3/bin/msp430-gdbproxy msp430
  • open another terminal, or a tab in the already opened, and start the gdb :
    msp430-gdb name_program_to_load.elf
Inside the gdb digit help for generic help.
Here some common operations :
  1. connect to the board (first thing to do)
    target remote localhost:2001
  2. erase the flash memory (needed to do every time before to load the new code)
    monitor erase
  3. load a program in the flash
    load name_program_to_load.elf (or .a43)
Converting the demo code

The first test is to convert the IAR demo code available for this board, for the mspgcc.
Following the conversion rules , it is possible to compile with mspgcc the code.
To simplify the process I created a makefile. Looking at what files the IAR used to create the code, I created a simple makefile :

CC=msp430-gcc
CFLAGS=-O0 -Wall -g -mmcu=msp430x169

OBJS=main.o mmc.o system.o lcd_new.o

all: $(OBJS)
$(CC) $(CFLAGS) -o test.elf $(OBJS)

%.o: %.c
$(CC) $(CFLAGS) -c lt;

clean:
rm -fr main.elf $(OBJS)

To compile the code, simply open a terminal, go in the directory where there is the makefile and the source code and then :

  • $make clean
    to clean up the objects
  • $make
    to compile

The result is a file called test.elf that can be load directly using the procedure described above.
The code is running smoothly.




Joystick
Here some notes about the joystick on the board.
The joystick is seen as 5 different switches.
The higher 4 bit of the Port1 (set in I/O - input direction - MSB) are used to carry the state of the 4 direction switches and 1 bit of the Port 2 (set in I/o - input direction) is used to carry the state of the pushbutton.
Here a table :


Signal  Name  Description 
 P1.4 B1  Right
 P1.5 B2  Down
 P1.6 B3  Up
 P1.7 B4  Left
 P2.0 B5  Pushbutton 


The signals are pulled up, so when the switch is NOT pushed the reading will be high (1).
Here a Schematic to indicate the combinations of signals (remember, a 0 means the switch is pushed).







The continue line indicates the real signals, the dotted one the combinations of signals.
The inner circles indicates the name of the connection on the schematic.
Pushing the joystick on the left for example, will generate the combination of 0x7x (0111xxxx), i.e. masking the MSB will have 0x70 (01110000).
Pushing the joystick Up and Right will generate the masked combination of 0xA0 (10100000).







Here some other link to related resources and examples.