Create Encoder Pattern Using Excel

I recently have been working with quadrature encoders. I created several programs using an encoder to control the speed of a motor using pulse width modulation. These encoders are of mechanical design and for best results require a resistor and capacitor filter on the inputs in order to compensate for switch bounce.

The final result worked very well. However, these encoders have mechanical contacts which are subject to wear and are generally limited to a maximum number of cycles.

Optical encoders can be used to overcome these limitations. They are quite a bit more expensive than mechanical encoders.

I decided I wanted to experiment with making my own optical encoder. The necessary artwork is not that complex, simply an alternating pattern of black and white segments. I did not have a suitable drawing program other than my PCB artwork program – which was unable to produce a suitable encoder design.

In studying the problem, I suddenly realised that the encoder artwork resembled a circular pie chart. Since I did have the spreadsheet program Excel and I was already familar with how it works, I decided to try using it to make a circular pie chart with equal alternating black and white segments.

It turned out to fairly easy. The requirements are simple: You want an even number of black and white segments with the total evenly divisable into 360 degrees. I chose to start with an encoder wheel with a total of 24 segments. Since 360 divided by 24 is equal to 15, you simply need to enter the value of 15 into 24 number cells in Excel. The easy way is to enter 15 into the first cell and then drag the value down into the next lower 23 cells (24 total cells).

Once this is done, you create a simple pie chart and then format each cell with an alternating pattern of black and white colors by clicking on each value in the legend and selecting the correct color. I suggest saving the final pattern as a PDF document.

The only other variable is the size of the artwork. This is important as the encoder output must be in the correct pattern in order work correctly.

The most basic encoder typically has two outputs called “A” and “B” and since the outputs are 90 degrees out of phase they are referred to as a quadrature output encoder. Having two outputs gives the ability to determine the direction of rotation either clockwise or counter-clockwise.

The ultimate size will be dependent on the size and type of optical encoders that are going to be used. I plan on experimenting with the Sharp GP1S53VJ000F transmissive photointerrupter.

I found I could easily adjust the final print size of the encoder using the printer software and entering a percentage of the normal print size. I ultimately did this by trial and error.

I eventually plan to use the artwork to create a circuit board with the encoder pattern.

Sharp OptoInterrupter

 

24 Segment Encoder

 

Forty Segment Encoder Pattern

 

 

End Post

 

Surface Mount Test Clips

Any one working with electronics, even at the hobby level, will at some point be forced to work with surface mount components. It is a fact of life these days that more and more integrated circuits are released only in surface mount packages.

This fact should not discourage anyone from working with SMT. Sure, they can be more difficult to handle and solder. However, equipped with the proper tools, which are readily available, the use of SMT can be done by the hobbyist and they do have some advantages. Two such advantages are a more compact circuit board design, and if you make your own circuit boards, fewer holes that have to be drilled for through hole components.

Some tools that are necessary are a good quality 5x magnifying glass, a fine soldering iron tip and some static free tweezers to handle the parts with. A flux pen and some soldering paste are also a good idea to have on hand – both are available in water washable formulas.

A problem that I recently ran into is the ability to connect test equipment such as a oscilloscope to a SMT device.

It started when I made a simple test PCB in order to run some operational tests on a PCA9685 Led driver. This device is only available in SMT. The easiest package to work with is the TSSOP28 with a pin pitch of 0.65mm – yes, quite small.

When I designed the PCB, I neglected to add test points for any of the outputs or the I2C pins. Fortunately, I found that there is a solution for this.

A company called TPI USA makes an excellent line of surface mount test clips – Nano Clips and Micro Clips. The Nano Clips are designed for 0.3mm pitch leads and also work on 0.65mm pitch leads as well. The Micro Clips will work on a 1.27mm pin pitch. Both clips have gold plating for good conductivity, and are available in a number of different colors. They connect using a 0.65mm x 4mm long pin located on the clip body.

I purchased two of the Nano Clips from DigiKey Electronics, and although they are not inexpensive, they do work very well.

I connected them to my oscilloscope using a wire whip I made from some 30 awg stranded wire I had on hand. I soldered a small female connector removed from a transistor socket to one end and a 0.65 mm square pin to the other end. I covered both with some small diameter heat shrink tubing.

Problem solved!

Here is a picture of them in use.

 

 

end post

 

MSP430 Long Timer Delay

In the previous two posts I described how to use a timer interrupt in order to create precision time delays. This time I describe a method to create much longer time delays. These time delays are useful in looping main “c” functions that require periodic updating.

Similar to a previous post that described a timer interrupt with a M430G2452 mcu, we will be programming a longer time delay using a timer interrupt and the timer will be clocked using a high accuracy external oscillator.

In this example the delay will be contained in a function that can be called any where in the main program loop. Also it would be easy to create different time delay functions by simply changing a couple of variables.

The function works by using a fixed time delay and then looping this delay the required number of times. A timer interrupt is used to increment the loop counter. By changing the value in the loop counter, long accurate time delays can be created.

MSP430 LONG TIME DELAY PROGRAM

 

 

TIMER OUTPUT WAVEFORMS

ONE SECOND TIME DELAY

 

FIVE SECOND TIME DELAY

 

end post

MSP430 Precision Timer Interrupt

This is a post similar to the previous one that described a timer interrupt with a M430G2452 mcu with the exception that we will be programming a M430G2553 mcu with a timer interrupt and the timer will be clocked using a high accuracy external oscillator.

The main difference between the M430G2553 and the M430G2452 is that the M430G2553 has two timer A’s and has a USCI communications module instead of the USI module in the M430G2452.

Both of these mcu’s directly support an external low frequency 32 kHz crystal. However, neither support the use of a high frequency crystal only. Instead, if higher accurracy is required, either mcu can be connected to an external 3.3 vdc crystal oscillator that has a CMOS or TTL compatable output.

Crystal Oscillator

Before working with timer A, review the section on interrupt vector addresses located in the data sheet and the timer A section in the user guide.

1.) Data sheet for the M430G2553 (TI document #SLAS735J).
2.) User guide for the M430G2553 (TI document #SLAU144).
3.) In Code Composer Studio open the header file (msp430g2553.h) for the M430G2553.

M430G2553 TIMER A DESCRIPTION

The M430G2553 contains a two 16 bit timers designated as Timer0_A3 and Timer1_A3 with the three indicating the number of combination capture /compare registers available. Here we will be using a single compare register. Both timers have four operating modes and also contain two timer interrupt vectors. We will be using Timer0 only.

M430G2553 Timer0 Interrupt Description

Two interrupt vectors are associated with the 16-bit Timer0_A3 module. Compare register TA0CCR0 is an interrupt vector for TA0CCR0 CCIFG, and TAIV is an interrupt vector for the remaining two CCIFG flags and TAIFG. In compare mode, any CCIFG flag is set if TAR counts to the associated TA0CCRx register value. The TAIFG flag is set when the timer (TAR) completes counting down to zero. Software is able to set or clear any CCIFG flag.

All CCIFG flags request an interrupt when their corresponding CCIE bit and the GIE bit are set. The TA0CCR1 CCIFG, TA0CCR2 CCIFG, and TAIFG flags are prioritized and combined to source a single interrupt vector. A program read of the interrupt vector register TAIV is used to determine which flag requested an interrupt. In the code example below, I used an “if” statement to assign interrupt #10 to the output. A “switch/case” statement can also be used.

In addition to controlling the timer delay using the value in the TA0CCR0 compare register, the IDx value in the TA0CTL register can also be changed to create other timer delays. Check the header file for more information.

Note 1: When using any interrupt, the code should be kept as short as possible. Calling a function inside an interrupt and the nesting of interrupts should be avoided if possible. Use an interrupt to check bit status or to update variables is best. Don’t forget to enable all maskable interrupts by setting the GIE bit in the status register.

 

PRECISION TIMER INTERRUPT PROGRAM

 

PRECISION TIMER INTERRUPT WAVEFORMS

100uS TIMER INTERRUPT OUTPUT

 

100mS TIMER INTERRUPT OUTPUT

 

end post

 

 

 

MSP430 Timer Interrupts

In this post I am going to describe, as simply as possible, how to configure a Texas Instruments MSP430G2452 mcu to create precise time periods using the Timer A interrupt. The MSP430G2452 is one of the mcu’s provided with the low cost EXP430G2 LaunchPad.

The EXP430G2 LaunchPad

Even though I have been using the LaunchPad for only a couple of months, I have found it to be an excellent way to learn about microcontrollers along with the variety of low cost development boards and free software tools (i.e. Code Composer Studio) that TI has made available. Also, the LaunchPad has a built-in flash programmer – no other external hardware is required. Although I have not yet tried, you should be able to use the LaunchPad as an external programmer to program an mcu installed on a breadboard using only four wires – two of them would be Vcc and Ground plus TEST and RST (jumpers are installed at these locations). The LaunchPad also comes with two onboard LEDs and pushbuttions.

The EXP430G2 LaunchPad is breadboard friendly and has a number of “BackPacks” that can be added to enhance its capability. Also, while it seems that the number of microcontrollers that are in DIP packages decreases every year, TI still offers a fair number of microcontrollers in DIP packages. I consider this very important as it allows you to easily replace the mcu (the mcu on the Launchpad is in a socket) should it become damaged as mistakes can happen to anybody – especially someone just starting out.

Code Composer Studio

I am using Code Composer Studio (CCS, Version 6.2) to write my programs in C and then use it to program the flash memory on the mcu. The 16k code limited version of Code Composer Studio is a free download from TI and is a very versitile IDE. It comes with a “Resource Explorer” which allows you to easily find and download code examples and other necessary documentation, including viewing online videos that helps you to get started. TI also provides periodic free automatic updates to CCS.

Of course, you still need to have a basic understanding of electronics, the C programming language or assembly if you so desire, digital logic, and the hexadecimal system. Also, there is a bit of an initial learning curve to get through, especially for beginners – also true for any new IDE. That said, Texas Instruments has made a tremendous amount of resources and community support available for all of its development boards and devices.

Using the Timer Interrupt

Before working with timer A, review the section on interrupt vector addresses located in the data sheet and the timer A section in the user guide.
1.) Data sheet for the M430G2452 (TI document #SLAS722G).
2.) User guide for the M430G2452 (TI document #SLAU144).
3.) In Code Composer Studio open the header file (msp430g2452.h) for the M430G2452.

M430G2452 Timer A Description

The M430G2452 contains a single 16 bit timer designated as Timer0_A3 with the three indicating the number of combination capture /compare registers available. Here we will be using a single compare register. This timer has four operating modes and also contains two timer interrupt vectors.

M430G2452 Timer A Interrupt Description

Two interrupt vectors are associated with the 16-bit Timer0_A3 module. Compare register TACCR0 is an interrupt vector for TACCR0 CCIFG and TAIV is an interrupt vector for the remaining two CCIFG flags and TAIFG. In compare mode, any CCIFG flag is set if TAR counts up to the associated TACCRx register value. The TAIFG flag is set when the timer (TAR) completes counting down to zero. Software is able to set or clear any CCIFG flag.

All CCIFG flags request an interrupt when their corresponding CCIE bit and the GIE bit are set. The TACCR1 CCIFG, TACCR2 CCIFG, and TAIFG flags are prioritized and combined to source a single interrupt vector. A program read of the interrupt vector register TAIV is used to determine which flag requested an interrupt. In the code example below, I used an “if” statement to assign interrupt #10 to the output. A “switch/case” statement can also be used.

In addition to controlling the timer delay using the value in the TA0CCR0 compare register, the IDx value in the TA0CTL register can also be changed to create other timer delays. Check the header file for more information.

Note; When using any interrupt, the code should be kept as short as possible. Calling a function inside an interrupt and the nesting of interrupts should be avoided if possible. Use an interrupt to check bit status or to update variables is best. Don’t forget to enable all maskable interrupts by setting the GIE bit in the status register.

How to determine the correct vector address

1.) In the data sheet, determine the timer interrupt address for the interrupt source that you want to use.

2.) In Code Composer Studio, locate the timer interrupt address in the header file. Then use the interrupt vector name shown in the header file in the interrupt function definition in your program. See table below.

M430G2452 Interrupt Vector Table
Interrupt SourceInterrupt FlagInterrupt AddressHeader File Definition
Timer0_A3TACCR0 CCIFG0FFF2hTIMER0_A0_VECTOR
Timer0_A3TACCR1 CCIFG0FFF0hTIMER0_A1_VECTOR
Timer0_A3TACCR2 CCIFG0FFF0hTIMER0_A1_VECTOR
Timer0_A3TAIFG0FFF0hTIMER0_A1_VECTOR

 

Timer Interrupt Code Example

 

OUTPUT WAVEFORMS

1 mS POSITIVE AND NEGATIVE PULSES USING TIMER INTERRUPT

 

250 mS POSITIVE AND NEGATIVE PULSES USING TIMER INTERRUPT

 

End Post