Category Archives: MSP430

MSP430

Code Composer Studio Graphing Tool Tutorial

 

In this Code Composer Studio graphing tool tutorial, graphing variables will be demonstrated in Texas Instruments IDE.  The process is fairly straightforward and will be shown in a video as well as some screenshots so it can be easily replicated.

Introduction

For the demonstration a program running on the MSP430G Launchpad was used, with the MSP430G2553 fitted.  The Launchpad was connected to a small experimental PID boc I have constructed, which I call the ‘Pocket PID’ (tutorial to follow on this), the box uses a LM35DZ temperature sensor as well as a few other components.  The voltage from the LM35DZ is sampled and then two variables are generated; a variable called Result which is comprised of a raw data set of several over samples and a variable called FilteredResult, which is a digitally filtered version of the Result variable.  Both variables can be viewed side by side which allows the effectiveness of the digital filter to be observed, the box also has a small heater and fan which allows the variables rate of change to be viewed over time in a graphical format. This provides a good example of how the code composer studio graphing tool can be used.

The graphing tool can also bee seen in action on another tutorial based on the C2000 Launchpad, where it is used to view the operation of an solar MPPT, graphing the power, voltage and PWM duty cycle.  The third part of the article concerned with this can be found here and the YouTube video here.

Code Composer Studio Graphing Tool Tutorial – Video Demonstration

Code Composer Studio Graphing Tool Tutorial – Main Steps

The main steps to access the the graphing tool in code composer studio (CCS) are carried out in debug mode, so all the screenshot images below are taken from within that mode.  As with all software there are often other ways to achieve the same results, this is just a method that works for me.

The first step is to decide which variables you want to graph and add these as Watch Expressions in CCS, this is achieved by highlighting the variable and then right clicking to bring up a properties menu, from this menu you want to choose the Add Watch Expression selection. This is shown in the below image.

Code Composer Studio Graphing Tool Tutorial Add Watch Expression MSP430 Tiva C C2000

If this has been successful then the variable chosen should then appear in the Expressions window, which is shown in the next image.

Code Composer Studio Graphing Tool Tutorial Watch Expression Visible MSP430 Tiva C C2000

You can add as many watch expressions as necessary, the video demonstration shows two variables being watched and graphed, but I have successfully watched and graphed four expressions.  I am not sure if there is an upper limit, but i have noticed stability issues when many variables are being watched over a long period of time.

After adding the variables required to the watch expression window, the next step is to add breakpoint and then edit the breakpoint properties.  Adding a breakpoint can be achieved either by double clicking on the line number, or highlighting a section of code on the line number and right clicking, then selecting the Breakpoint (Code Composer Studio) and Breakpoint option.  The image below illustrates this action.

Code Composer Studio Graphing Tool Tutorial Add Breakpoint MSP430 Tiva C C2000

Once the breakpoint has been added it should be visible in the Breakpoint window, for the breakpoint to be enabled it needs to have a tick in the far left column.  The image below shows the breakpoint window with the tick in the far left column, as well as an arrow highlighting the Action column.

Code Composer Studio Graphing Tool Tutorial Breakpoint Visible MSP430 Tiva C C2000

 

A breakpoint by default will halt the program at the chosen point, for the graphing tool to work all the windows simply need to be refreshed, this then allows the Expressions window and the graph to be updated.  The next image illustrates how this is achieved by editing the breakpoint properties.  I simply right click on the breakpoint symbol shown next to the line number it will activate, this then brings up a properties list and then select the Breakpoint Properties option. The image below shows this then step.

Code Composer Studio Graphing Tool Tutorial Breakpoint Properties MSP430 Tiva C C2000

Once the Breakpoint Properties window is open there are a few options that are accessible, for this tutorial only one is of interest which is the Action property.  As already mentioned the action the breakpoint will carry out by default is half the program, for the watched expressions to update, this action simply needs to be changed to Refresh All Windows.  The next image illustrates how this is changed.

Code Composer Studio Graphing Tool Tutorial Breakpoint Properties Change Action MSP430 Tiva C C2000

 

Now that the variables have been added and a breakpoint has been placed and set-up to perform the required action, the variables to be graphed can be chosen and set-up.  To bring a graph up for a particular variable, right click on the variable in the Expressions window and then select the option Graph,  as per the image below.

Code Composer Studio Graphing Tool Tutorial Watch Expression Right Click MSP430 Tiva C C2000

Once the graph option is selected the graph should be visible in CCS, I find it usually defaults to the bottom left of the window, as shown in the next image.  The graph window can be manipulated to the required size as well as being picked and placed as required.  Additional graphing windows can be added for other variables in the same way.

Code Composer Studio Graphing Tool Tutorial Tutorial Add Graph Window MSP430 Tiva C C2000

Now the graphing windows also has various options which allow you to tailor the view for your requirements, by hovering over the symbols small tooltips will appear which give a good impression of the button’s action.  This tutorial will only cover two of the buttons which provide enough of an introduction for now.  The first button is the Graph Properties button which is shown in the image below.

Code Composer Studio Graphing Tool Tutorial Graph Properties MSP430 Tiva C C2000

By clicking this button a new window will open which displays some useful quick access properties.  I usually use the Grid Style option to add a Major Grid to the x and y axis, additionally the Display Data Size option allows you to determine how much data is viewable on the graph, before it is pushed off the edge of the screen.  For long data captures increasing the Display Data Size can be useful, I have had issue with instability here though so its compromise on other settings such as Sampling Rate Hz as well as other settings for CCS when in debug mode.  The Graph Properties window is shown in the next image.

Code Composer Studio Graphing Tool Tutorial Tutorial Graph Properties MSP430 Tiva C C2000

The next button that will prove useful is the Graph Display Properties button, this is shown in the next image (also note the Major Grid now shown on the graph window).

Code Composer Studio Graphing Tool Tutorial Graph Display Properties MSP430 Tiva C C2000

The Graph Display Properties window again has quite a few options, allowing things like colour, number formats, axis names and scale to be changed.  Some of these options are demonstrated in the video, the image below shows the Graph Display Properties window and the option for the Y axis Set Number Format option window open.

Code Composer Studio Graphing Tool Tutorial Graph Display Properties MSP430 Tiva C C2000

The final image shows a screen capture from the video, with both sets of data displayed side by side.

Code Composer Studio Graphing Tool Tutorial Video Result Capture MSP430 C2000 Tiva C

I take great care when writing all the tutorials and articles, ensuring all the examples are fully tested to avoid issues for my readers.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

MSP430 Voice Control Over Bluetooth

 

In this tutorial I will demonstrate a MSP430 voice control over Bluetooth, using a HC06 Bluetooth module.  I will be using an Android App I programmed which can be downloaded and installed, the C code for the MSP430 is also downloadable at the end of the tutorial.

**Update 18/01/2015 the Android App has been updated and now includes 23 generic commands

So before we look at everything in further detail, the video below gives a demonstration of what the simple App and basic hardware set/up can do.  Note it was filmed with an older phone, so excuse the quality and dodgy camera angles.

HC06 Bluetooth Module

This is my first foray into bluetooth, I will be looking at Bluetooth 4 (aka BLE) soon, but for now lets take a look at the HC06 bluetooth module.

MSP430 Voice Control Over Bluetooth HC06 module

The image above shows the HC06, the module is shown already soldered to a blue PCB and a connecting wire.  I purchased the whole assembly for under £3.50 including postage and packaging from Ebay.  You can also buy the module on it’s own for even cheaper, but you then need to mount it on a suitable PCB, as well as taking into account a few configuration considerations.  If you want to keep things simple then I recommend buying the same assembly as used in this tutorial.    An alternatively option if you looking for a neater Boosterpack design, is to source some parts from the 43oh website store, they sell the modules and Boosterpack PCB.  So lets look at a few specifications for the module, I have just listed the ones that are pertinent, bear in mind any module you buy may differ slightly.

  • Bluetooth protocal: Bluetooth Specification v2.0+EDR
  • Frequency: 2.4GHz ISM band
  • Emission power: ≤4dBm, Class 2 (~10 meter range)
  • Speed: Asynchronous: 2.1Mbps(Max) / 160 kbps, Synchronous: 1Mbps/1Mbps
  • Power supply: +3.3VDC 50mA (3.6V ~ 6V on-board regulator input)

The HC06 kits has six external connections, only four have header pins connected these are VCC, GND, TXD and RXD (Wakeup and State are not connected).  The TXD and RXD form the transmit and receive for a serial connection, these will be connected to the Universal Asynchronous Receiver/Transmitter (UART) on the MSP430G2553.

Hardware Set-Up

For this tutorial I kept it very simple, the demonstration uses the Launchpads on-board LED’s. The VCC and GND pins from the bluetooth module are connected to the launchpads VCC and GND pins.  TXD from the module is connected to P1.1 on the launchpad, this is the UART receive, and RXD from the module is connected to P1.2 on the launchpad, this is the UART transmit.  The image below should clarify the connections, as it shows the colour coded wire clearly.

MSP430 Voice Control Over Bluetooth HC06 module connected to Launchpad

Thats all there is to it for the hardware set-up, there was no need to add any further electronics as the potential control applications will become evident.

Software

I was originally just playing around with the UART, and had intended to write a tutorial on this but remembered I had brought the HC06 module, so decided to dig it out.  Before I connected the module I had already tested the UART using a free terminal program called Putty running on Windows.  The MSP430 UART example programs are enough to learn the basics and get you started, the code I used is based on one of the examples.  After having great success with the initial UART tests, I then connected the bluetooth module.  To test the module I needed a similar program to Putty but running on Android, you will find dozens of terminal programs for Android just by searching the Play Store.

After some initial tests this worked fine and I had an LED switching on and off via bluetooth, all for about one hour of playing around.  It was at this point I thought wouldn’t it be cool to have voice control, my Nexus 5 supports ‘Ok Google’ for voice activation and that gave me the idea. Now I have to stress my App doe’s not support ‘Ok Google’  this is the first iteration, in fact you have to hit a button first and then speak the command.  I will write more about the Android App, but first lets go through the MSP430G2553 code.

MSP430G2553 Code

I am only going to run through the code quickly, as it can be downloaded below so will just walk through the main parts.

The first block of code deals with the watchdog and clock set/up, this also ensures the clock is calibrated, the accuracy is required for the UART operation although there is still a small margin of error due to the internal oscillator tolerance.

The GPIO’s are set-up next in lines 2 and 3, P1.1 and P1.2 are used for the UART.  In order to configure these both the GPIO function select registers need to be set to 1, this configures the pins to function with the secondary peripheral module.  If you are not familir with some of the GPIO or registry configuration commands, I would recommend you take a look at my two part MSP430 Programming Tutorial Part/1 and Part/2.  Lines 4 and 5 are used to configure P1.0 and P1.6, these are connected to the on-board LED’s, and are purely used here as a simple demonstration.

The next block of code is used to configure the Universal Serial Communication Interface (USCI), this supports multiple serial communication modes.  The MSP430G2553 has two USCI modules, USCI_A and USCI_B, USCI_A is used here in UART mode.

Starting with line 2 this configures USCI_A0 control register 1 and sets the clock source as the SMCLK.  Line 3 configures the USCI_A0 baud rate control register 0, using a value of 104.  The SMCLK is running at 1MHz, therefore 1MHz divided by 104 = 9615.4, which is almost 9600 baud. Line 4 configures the USCI_A0 baud rate control register 1, this is used for clock prescaler settings more information can be found in the family guide with relation to this.  Line 5 configures the USCI_A0 modulation control register, this parameter determines the modulation pattern, in short terms this allows extra adjustment depending on the divided clock frequency and desired baud rate.  Therefore providing a mechanism to achieve the most accurate result, with as little deviation from the desired baud rate.  Line 6 configures the USCI_A0 control register 1, this time it performs a software reset which initialises the USCI state machine.  Line 7 enables the interrupt on the USCI_A0 receive buffer.  Line 8 enables low power mode 0 which turns the CPU off and interrupts.

So now that the USCI_A is configured the processor will switch off until data is received, then when the receive buffer has a new byte of information an interrupt will be called.

The code snippet above is inside the interrupt handler for the USCI_A receive buffer.  Line 1 assigns the data inside the buffer to the variable Rx_Data, line 2 is used to wake up the CPU.

The variable Rx_Data is then used in a switch case statement to determine what has been sent over bluetooth.  The App on the phone can be programmed to send a string, but this first iterations simply converts a string into a single ASCII value, which is then sent over bluetooth. In the case of the code running on the MSP430, it is using the switch case to determine the ASCII value represented by a hexadecimal number.  The ASCII value sent and shown in line 3 is 0x41 which is a capital A, and line 4 is 0x42 which is a capital B.  So when you say the word ‘On’ the phone App translates this and sends a capital A to the HC06 bluetooth module, when you say the word ‘Off’ a capital B is sent.  There are other commands and I can and will add more, the full list currently supported is further down the tutorial.

The rest of the code shown is purely for demonstration, basically line 4 and 11 are used to disable a timer interrupt.  The timer interrupt requires disabling as the flash demo is not used if the on or off commands have been given.  Line 5 and 12 are used to change the function of pin P1.6 to a GPIO function.  Line 6 is used to turn the on-board launchpad LED’s on, and line 13 is used to turn the on-board launchpad LED’s off.

Android Voice App

I am not an Android App developer so this was put together quite quickly, literally six or so hours.  I have been testing it with the launchpad and it performs well, there were a few early teething problems but now it seems pretty solid in operation.  The App can currently only be downloaded here, I was going to upload to the Play Store but Google wanted 25$ for the privilege.  So I decided not to upload at this stage, as it may not be of any use to anyone other than me…time will tell!

 

I am not running through how to code the App here, just showing some screen shots of the operation with some notes, as the video should really cover this.  I have only tested this on my Google Nexus 5, so unsure if the layout will change on other phones.  If I have left any areas unclear or you have issues, leave a comment at the end of the article and I will answer as soon as I can.

Before running the App you need to enable your Bluetooth, wasn’t sure how to do this and it does not prompt you to enable it yet.

MSP430 Voice Control Over Bluetooth HC06 module Android App Not ConnectedOk the first screen capture shows the Apps main and only window, you will notice the big red text that says ‘Not Connected’ .  To connect a bluetooth device tap the blue box with the text ‘Choose BT Device’

MSP430 Voice Control Over Bluetooth HC06 module Android App Bluetooth SelectionYou will then be taken to a screen with a list of bluetooth devices, as you can see I have the HC06 device listed.  To select the device just tap on the listed device.  The HC06 module has a small LED on the board, this flashes when not connected, becoming solid once the device is connected.  If its your first time connecting you will need to pair the devices, the most common password used for the HC06 seems to be ‘1234’.

MSP430 Voice Control Over Bluetooth HC06 module Android App ConnectedAfter a few seconds the screen will return to the main window, and you should notice the red text has now changed green and reads ‘Connected’.  You will notice directly below this text is more black text which says ‘Captured Speech = ?’, this indicates no speech has been captured. By pressing the orange button which says ‘Press Then Give Voice Command’, you will enter the speech capture mode.

MSP430 Voice Control Over Bluetooth HC06 module Android App Capture ModeThe capture mode is just Google’s Speech to Text, this as the name suggests will convert your speech to text.

MSP430 Voice Control Over Bluetooth HC06 module Android App Unaccepted Code WordThe next screen shot demonstrates the captured speech being displayed, this is not a recognised command word shown, it could be in future though.  I have left all words displayed as if Google’s application has misidentified your speech, it’s better to know where the error lies. I guess I could have the recognised word change green for a valid command and red for an invalid one, so will maybe implement that at a future date.  You can disconnect the bluetooth device any time by pressing the red button labelled ‘Disconnect BT’.

A few of the commands supported are as follows:

  • on‘ command then generates a capital A and sends over bluetooth or hex 0x41
  • off‘ command then generates a capital B and sends over bluetooth or hex 0x42
  • flash‘ command then generates a capital C and sends over bluetooth or hex 0x43
  • 1‘ command then generates the number 1 and sends over bluetooth or hex 0x31
  • 2‘ command then generates the number 2 and sends over bluetooth or hex 0x32
  • 3‘ command then generates the number and sends over bluetooth or hex 0x33
  • 4‘ command then generates the number 4 and sends over bluetooth or hex 0x34
  • 5‘ command then generates the number 5 and sends over bluetooth or hex 0x35

I will probably add forward, backward, left, right and a few other obvious command words.

MSP430G2553 C Code

The link below contains the zip file with the full example C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

MSP430 Voice Control Over Bluetooth MSP430G2553 C Code

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

Android App BT_Voice_MCU.apk

**Update 18/01/2015 the Android App has been updated and now includes some additional commands, for a full list see this Excel file, the link below has also been updated.

The link below contains the zip file with the full Android App.apk file, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.  I guess I need to put some sort of disclaimer here as this is a phone App, and untested on any other phone except my Nexus 5, but it appears 100% stable and has not caused any issues.  If you download and use this App, I am not responsible for any issues you may encounter so you do so at your own risk.

MSP430_Bluetooth_Voice_Control

*Original* – MSP430 Voice Control Over Bluetooth BT_Voice_MCU Android App

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

MSP430 Launchpad used as a programmer

 

In this MSP430 Launchpad used as a programmer tutorial, the MSP430 Launchpad board will be used to program a MSP430G2230 microcontroller.  The MSP430G2230 is an 8 pin microcontroller and comes in a Small Outline Integrated Circuit package (SOIC), so offers the developer the chance to reduce the overall footprint of the project on a budget.  As the MSP430G2230 comes in a SOIC package, for prototyping it’s often easier to increase the package size, which allows more conventional and off the shelf components to be used during the development stage.  Texas Instruments sells a Dual In-Line Package or DIP adaptor PCB, the image below shows the board and it can also be found here.

MSP430 Launchpad used as a Programmer for MSP430G2230 DIP Adaptor evaluation PCB

It is also possible to buy these from other vendors or even make them yourself, the next few images shows the MSP430G2230 soldered on to the Texas Instruments SOIC to DIP evaluation PCB.  Also shown is a PCB I manufactured for an ADS118IDGST 16bit ADC, which is a Micro Small Outline Package (MSOP-10), this was covered in an earlier Stellaris tutorial which can be found here.

MSP430 Launchpad used as a Programmer for MSP430G2230 DIP Adaptor and own MSOP-10 adaptor topMSP430 Launchpad used as a Programmer for MSP430G2230 DIP Adaptor and own MSOP-10 adaptor side

So once the MSP430G2230 is more accessible in a DIP package, it can be easily used on some vero-board or even a breadboard set-up.

The nest step is setting up the MSP430G Launchpad board so it can be used to program the MSP430G2230, this is a very simple process and involves the removal of the existing microcontroller.  There are only four pins used for programming and I used some breadboard which allowed easy wire connections.  The next image shows the four GPIO pins used and the corresponding pins on the Launchpad board.

MSP430 Launchpad used as a Programmer for MSP430G2230 pinout connections

 

As the image shows the DIP converted MSP430G2230 pins don’t directly align with the Launchpad, hence the need for the bread board.  So once the MSP430G2230 is wired up to the Launchpad board, a programme can be flashed from Code Composer Studio (CCS).  I wrote a small program to flash an LED which I connected to pin two on the MSP430G2230 or GPIO P1.2, this allowed me to verify the set-up so further development could progress.  The video below demonstrates the whole set-up in operation, the code has already been downloaded at this stage.

MSP430G2230 Simple Blinky Test Code

The link below contains the zip file with the full example C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

MSP430G2230 Test Code

 

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

Switch Debouncing Tutorial Pt/2

 

In this switch debouncing tutorial part 2 C code debounce algorithms will be looked at further, and their effectiveness.  All the software solutions shown will be demonstrated on the MSP430G Launchpad.  However the basic principle of operation shown in the examples, can be applied to all microcontrollers, particularly the last example which is based on some code found at Jack Ganssle tutorial, this can be easily implemented on any system using the C language.

MSP430 Single Switch Debounce WatchDog

The first debounce algorithm example is based on some Arduino code, which uses the millis() function.  In this case the millis second count is generated by the watchdog timer on the MSP430.  The launchpad switch connected to GPIO P1.3 is used in this test code.

The while loop on line 1 is inside the main function, line 5 AND’s port 1 with BIT3 as this is the only GPIO pin of interested.  Lines 6 to 9 will set the variable reading to a 1 if the value on pin P1.3 is a 1 i.e. not pressed, and 0 if the switch is pressed.  Lines 11 and 12 check to see if the switch has changed from it’s previous stored state, if this is true then the time when the switch was pressed is saved to the variable lastDebounceTime.  Lines 14 and 15 determine if the switches state hasn’t changed for a time equal to the variable debounceDelay, this then means that it is the current stable state of the push switch.  The stable state is assigned to the variable switchState, then lines 17 to 20 determine the if the LED connected to GPIO P1.0 is on or off.   The debounceDelay was set to 10, and the algorithm performed very well allowing fast presses of the single switch, without any issues.

The watchdog timer was used in this example as it was simple to set-up and generate an interrupt every 0.5mS.  Lines 36 to 33 show the interrupt handler, some basic statements inside the interrupt generate a 1mS count, which continuously increments.  The function Mils_Count() in lines 35 to 39, is used to obtain the current count value.  The watchdog timer is not meant to be used in this way, but it is so often disabled in many examples, yet is a resource that can be exploited.  If you were producing a production embedded system this would probably not be the case, but this adds a little extra functionality to some of the low end MSP430G devices.  The watchdog set-up shown will be used in some of the other debounce examples in this tutorial, and can easily be substituted with a standard timer.

MSP430 Single Switch Debounce WatchDog Example Code

The link below contains the zip file with the full example C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

MSP430 Single Switch Debounce WatchDog

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

MSP430 Interrupt Button Control

The second example configures the GPIO pins to trigger interrupts when a change in state is detected, which is caused by a switch being pressed.  Only the interrupt handler is shown in the code snippet below, but the entire C code can be downloaded further down the page.

There are two GPIO pins set-up to generate interrupts P1.3 and P1.7.  When a switch is pressed on either of these pins, the interrupt handler is called.  The switch case statements are used here, the port 1 interrupt flag register (P1IFG) being used as the switch.  Once the correct pin interrupt has been identified, the interrupt edge select is toggled (lines 8 and 14).  Then the corresponding LED is toggled, as shown in lines 9 and 15.  Lines 10 and 16 use a delay function which basically waits for 40mS (1MHz clock).  This produced a reasonable outcome at slow to medium rates, pressing the switch at a faster rate produced indeterminate results.  The delay function is not the best method to carry out a delay as it wastes CPU time.  This technique is also not as robust a the first algorithm, especially if the switch is pressed quickly, but it does allow a whole port of switches to be used.

MSP430 Interrupt Button Control Example Code

The link below contains the zip file with the full example C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

MSP430 Interrupt Button Control

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

  

MSP430 Multiple Switch Debounce WatchDog

The third example has two examples of switch debounce algorithms, these are split into two individual functions which can be run from the main function, simply by commenting one of them out at a a time.  The functions incorporate aspects of the previous two examples, allowing multiple switches to be debounced.  The code snippet below shows both of the functions Press_Time() and Debounce_Buttons() residing inside a while loop, which is inside the main function.

The Press_Time() function will be looked at first.

Starting with line 3, the if statement is used to ensure that the switches connected to BIT3 or BIT7 have been pressed, if not the statement is considered false.  Line 5 assigns the variable state with the AND’ed value of port 1 (P1IN) with hex value 0x88 or binary 10001000.  Line 7 assigns the current Mils_Count() value to the variable Reaction_Count.  The switch case statements are used here with the variable state being used as the switch.  If the switch on P1.7 is pressed then the case statement on line 14 will be selected.  A while loop is then entered, which waits until the current Mils_Count() minus the variable Reaction_Count is greater than the variable Button_Reaction_Delay.  This allows a tunable delay to be entered with ease, for testing a delay of 100-150mS was found to produce satisfactory results.  This method produced better results than the interrupt method, but will suffer with increased switching speed.

The second function Debounce_Buttons() is basically a copy of the first example.  The variables are just doubled up and surrounded by a if statement so the code is only executed when a switch is pressed.

This works well as per the first example but with two switches, however the code is very inefficient, due to the large number if statements that are executed each time a switch is pressed.

MSP430 Multiple Switch Debounce WatchDog Example Code

The link below contains the zip file with the full example C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

MSP430 Multiple Switch Debounce WatchDog

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

MSP430 Ganssle Switch Debounce Multiple Switches

This fourth and final example is based on sample code provided by Jack Ganssle, he has an excellent tutorial located here.  The code is based on his third example, there is also a good description of the code operation with his article.  The code snippet below shows the main body of the algorithm, I have made some modifications adding a compound bitwise AND operator, as well as adding some of his considerations regarding OR’ing the final debounced port value.

Line 6 shows a function call for rawPortData(), this function simply returns the current state of port 1 and can be seen below in the next code snippet.

The debounceSwitch() function returns the debouncedORd value, and is called in the following way.

The checkButtons() function uses switch case statements to interpret which switch or GPIO pin has changed, the nice part about this code is the debouncedORd value makes the code very intuitive.

This last example is easy to port to other microcontrollers, just by changing the code in functions checkButtons() and rawPortDate().  Needless to say this code works very well and produces excellent results with the PCB tac switches used, under fast or slow switching.

MSP430 Ganssle Switch Debounce Multiple Switches Example Code

The link below contains the zip file with the full example C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

MSP430 Ganssle Switch Debounce Multiple Switches

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

Switch Debouncing Tutorial Pt/1

 

In this switch debouncing tutorial part 1 the cause and effect of switch bounce will be explained and demonstrated, then a cost effective hardware debouncing solution will be discussed, with oscilloscope captures to demonstrate the results.  The last section of part 1 will show a simple program based on the MSP430 , this can be used to see the effects of a particular switch connected to the GPIO.  Switch deboucing tutorial part 2 of this tutorial will look at further C code debounce algorithms and their effectiveness.

All the software solutions shown will be demonstrated on the MSP430G Launchpad.  However the basic principle of operation shown in the examples, can be applied to all microcontrollers, particularly the last example which is based on some code found on Jack Ganssle tutorial, this can be easily implemented on any system using the C language.

Switch Contact Bounce

Switch contact bounce is a common issue for all mechanical switches, this includes mechanical relays.  The contact bounce occurs when the metal contacts of the switch are forced together, the property of the metals used causes the contacts to bounce apart.  How often the contacts bounce apart before finally latching shut depends on the contact type and the property of the switch.  The bouncing effect can causes multiple high frequency pulses, as opposed to a clean transition at the output.  If we take an example of a microcontroller with a switch connected to one of it’s GPIO pins, the microcontroller is able to read these high frequency pulses, misinterpreting them as legitimate presses, resulting in an undesired action.

The image below shows a basic circuit used to test switch contact bounce.

Switch Debouncing Tutorial switch circuit without debounce

With the circuit constructed on some breadboard, an oscilloscope was connected to the GPIO pin header and set to trigger when the switch was pressed, the resultant capture can be seen below.

Switch Debouncing Tutorial switch without debounce circuit poor quality switchThe oscilloscope captures shows the steady state of just over 3.3V, as the switch is pressed and released, multiple pulses are visible during this time.  The switch used for this capture was an old switch I found in a bag of spares I had, it was a small momentary touch which had a sprung button.  Many of these extra pulses would be picked up by a microcontroller, causing unexpected behaviour with your program if no debouncing was used.

The next oscilloscope capture was taken using a small PCB mounted tac switch, this was set-up in the same way as the previous test.

Switch Debouncing Tutorial switch without debounce circuit good quality

It can clearly been seen that this inexpensive PCB switch has a far superior switching action, but there is still bouncing going on, as the expanded image below shows in greater detail.

Switch Debouncing Tutorial switch without debounce circuit good quality zoomed

Hardware Solution

There are many hardware solutions to solve switch contact bounce, ranging in price from a dedicated microcontroller programmed purely to act as a debouncer, or a dedicated key encoder (MM74C923) with built in debounce, to a low end solution using just a resistor and capacitor.  This tutorial will only cover the latter option, as when combined with a suitable software algorithm, provides a cost effective solution for most small microcontroller applications.

A simple resistor capacitor switch deboucing circuit can be seen below.

Switch Debouncing Tutorial switch circuit with debounce

 

The resistor capacitor combination forms an RC circuit, which has a time constant determined by τ = R*C, therefore 47kΩ*100nF = 4.7mS.  The capacitor is considered charged after approximately 5*τ, therefore roughly 25mS.  So when the switch S2 is pressed effectively closing the switch, the voltage across the capacitor is discharged through the switch to ground.  As there is very little resistance this happens quickly, but as will be shown not instantly.  When the switch is released and becomes open, the capacitor is charged via R2 and should take approximately 25mS to charge back up to the supply voltage.  Any spikes caused by bouncing contacts are absorbed by the RC circuit, however care must be taken when selecting the values to ensure the switching action is fast enough for the project.  If the resistor or capacitor is too large the time lag may cause the system responsiveness to suffer, too small and a switch with a long bounce characteristic will still have an issue.  Capturing the switch bounce with an oscilloscope is the best way to view the problem and then take the appropriate action.  The oscilloscope capture below shows the circuit in action using the cheap PCB tac switch.

Switch Debouncing Tutorial switch with debounce

This clearly shows a huge improvement in the switching noise, the falling edge shows a clean edge, while the leading edge is curved due to C1 charging through R2.  The next image shows the falling edge of the capture on a smaller time base.

Switch Debouncing Tutorial switch with debounce falling edge

The falling edge can still be seen to show the capacitor discharge curve, this takes approximately 1uS, therefore the resistance to ground is approximately 2Ω.  The next image shows the rising edge of the capture on a smaller time base, the image clearly shows the capacitor curve levelling off around 25mS.

Switch Debouncing Tutorial switch with debounce rising edge

This circuit will work sufficiently in most situations, but it is best practice to discharge the capacitor in a more controlled fashion, especially if there are higher currents and voltages involved.  A second resistor can be used in conjunction with R2, thus ensuring C1 has a higher resistance path to ground, when the switch S2 is closed.  The image below shows a circuit using this additional resistor (R1).

Switch Debouncing Tutorial switch circuit with debounce 2nd resistor

The combination of R1 and R2 has very little impact on the original time constant, but allows a controlled discharge of the capacitor to ground.  The value of R1 would typically be less than 6.8kΩ, being dependant on the requirements of the system.  This will ultimately improve the life of the switch, as it avoids high instantaneous currents.

Before finishing part 1 of this tutorial, a basic code example is shown below which allows some of the contact bounces from a switch to be recorded on a MSP430 Launchpad.

The code snippet above is used with an external switch, connected to GPIO pin P1.0, which is configured to function with Timer0_A.  Timer0_A is set-up in capture mode and configured to trigger an interrupt on every falling edge pulse.  Every time the interrupt is triggered the variable count is incremented, therefore by running this code it is possible to determine roughly how noisy a switch is.  To see the updated count value, the code can be run then the switch pressed, the code can then be paused to check the value of Count, or a breakpoint can be set and the variable Count watched.

Test Code

The link below contains the zip file with the full example C code, there is a small advert page first via Adfly, which can be skipped and just takes a few seconds, but helps me to pay towards the hosting of the website.

MSP430 Debounce Switch Test

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

In Part 2 debounce algorithms will shown with C examples, they will all be written to run on the MSP430 but the principle of operation can be carried over to other microcontrollers.  The last code example in particular can easily be implemented on other microcontrollers.

MSP430 Programming Tutorial Pt/2

 

In this second part of the MSP430 programming tutorial examples of GPIO register settings will be shown and explained.  Additionally register examples for some of the internal peripherals will be demonstrated and explained.  The first part of the tutorial can be found here.

Changing the GPIO Registers to your desired configuration

The following examples should help to illustrate how you change the individual bits, as well as multiple bits in the GPIO registers to achieve exactly what you want.  There is a reference to BIT3 and BIT7 being defined as hexadecimal values, these values can be found in the msp430g2253.h header file inside Code Composer Studio, or the MSP430 software provided by Texas Instruments.

MSP430 Programming Tutorial GPIO register statements 1

So by using this statement we only change BIT7 of port 1 to a logic 1 or GPIO P1.7. This is very powerful as it allows individual pins to be configured, without effecting other pins on the same port.  But what if you want to adjust multiple pins to outputs, well that can be achieved quite easily, two ways are illustrated below.

MSP430 Programming Tutorial GPIO register statements 2

So turning a single registry bit or multiple bits to a logic 1 are covered, how about assigning a logic 0 to a single register bit or multiple register bits.  The following images will demonstrate how this is achieved.

MSP430 Programming Tutorial GPIO register statements 3

And for multiple bits.

MSP430 Programming Tutorial GPIO register statements 4

There is one more operator that is commonly used on GPIO pin register bits, that is the ^ or XOR bitwise operator.  This is used in many examples on the internet to toggle the LED’s on the launchpad, the example below demonstrates it’s use.

MSP430 Programming Tutorial GPIO register statements 5

Although all these examples are only used with the P1OUT register, the same principles can be applied to all the GPIO port registers.  The examples shown where multiple registers are written to, using a combined hex value will also optimise any code, saving execution time by removing additional arithmetic in the form of an addition.

  

Understanding and Changing Peripheral Registers

Understanding how to change bits inside the peripheral registers, is not a great leap in understanding from the GPIO ports.  A few examples will be shown which are based on the ADC peripheral.  The ADC10 Control Register 1 (ADC10CTL1) will be used as the example register, but all registers will follow the same principle.  The image below is extracted from the MSP430 family guide and shows the ADC10CTL1 register.

MSP430 Programming Tutorial ADC10CTL1 Control Register 1

So what we can see here is the register is a 16 bit register and the bits are split into blocks which correspond to different parameters.  I have covered what these individual blocks do in a previous tutorial dedicated to the MSP430 ADC, found here.  It can be seen that the blocks correspond to certain bits inside the register, for example INCHx occupies Bits 12-15 of the register and ADC10DIVx occupies Bits 5-7 of the register.  To illustrate this we could view the 16 bit register in binary form:

INCHx occupies the bits shown in bold 0000 0000 0000 0000
ADC10DIVx occupies the bits shown in bold 0000 0000 0000 0000

As with the GPIO port pins Texas Instruments have provided defines in the header files, so it is not necessary to remember the binary or hexadecimal values for the register settings. The names used for these register settings are shown above i.e. INCHx and ADC10DIVx.  As INCHx occupies four Bits it therefore has 16 possible combinations and ADC10DIVx has 8, hence the small x after each name.

The next two images are again extracted from the MSP430 family guide and show how the bit combinations correspond to different parameters.

MSP430 Programming Tutorial ADC10CTL1 Control Register 1 INCHx

MSP430 Programming Tutorial ADC10CTL1 Control Register 1 ADC10DIVx

So now lets look at a command to set the ADC10CTL1 register using the parameters INCHx and ADC10DIVx.

The code snippet above basically adds the two parameters together and assigns them to the register using a compound OR assignment operator.  Now lets look at this in binary form, which will help shed some light on the process.

MSP430 Programming Tutorial ADC10CTL1 INCHx + ADC10DIVxSo by using this command the register parameters can be set individually, or multiple parameters can be set in one go.  As with the GPIO port settings a hexadecimal value can be used to set the parameters as well.

The code snippet above achieves the same result as well as being more efficient, however the code becomes much more obfuscated.

That covers setting the register bits to 1, how about setting the register bits to 0.  This is achieved in the same way as with the GPIO, the image below illustrates the operation.

MSP430 Programming Tutorial ADC10CTL1 ~INCHx

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.

MSP430 Programming Tutorial Pt/1

 

In this MSP430 programming tutorial part 1 some of basic C operators used for programming the MSP430 will be looked at.  The GPIO port registers will then be looked at in greater detail.  In part 2 example code for the GPIO registers will be shown and explained, as well as examples for the the ADC peripheral register.   The MSP430G2253 Launchpad will be used as the reference microcontroller, the primary IDE used is Code Composer Studio (CCS). The MSP430G series family guide, as well as other useful information can be downloaded directly from Texas Instruments webpage here.

C Language Bitwise Operators

If you are familiar with Bitwise operators, skip this section and and start with the MSP430 GPIO ports section further down this page.

Some basic C language bitwise operators will be looked at first, then how these apply to GPIO ports will be demonstrated.  The C language has 8 types of operators and bitwise operators are 1 of these.  The bitwise operators are fairly easy to understand and if you have ever looked at logic gates and truth tables, then some of these will be immediately recognisable. For the following examples two variables will be used a and b, they will also be assigned values; Decimal: = 48 and b = 24, Binary: a = 0011 0000 and b = 0001 1000.

Bitwise Operator &

a 0011 0000
b 0001 1000
a&b 0001 0000

The binary AND operator copies a bit or logic 1 to the result, only if a logic 1 exits in both operands.  So the result of a&b = 0001 0000 or 16 in decimal.

Bitwise Operator |

a 0011 0000
b 0001 1000
a|b 0011 1000

The binary OR operator copies a bit or logic 1 to the result, if it exists in either operand.  So the result of a|b = 0011 1000 or 56 in decimal.

Bitwise Operator ^

a 0011 0000
b 0001 1000
a^b 0010 1000

The binary XOR operator copies a bit or logic 1 to the result, if it exists in one operand as a logic 1, but not both.  So the result of a^b = 0010 1000 or 40 in decimal.

Bitwise Operator ~

a 0011 0000
~a 1100 1111

The binary NOT operator effectively flips the bits, so 1’s become 0s and vice versa.  So the result of ~a = 1100 1111 or -48 in decimal (signed variable) or 207 in decimal (unsigned variable).

Bitwise Operator <<

a 0011 0000
a = a<<2 1100 0000

The binary LEFT SHIFT operator moves the operands bits left, by the number of bits specified, which is this case is 2.  So the result of aa<<2 = 1100 0000 or 192 in decimal.  This also effectively multiplies the value of a by a factor of 4.

Bitwise Operator >>

a 0011 0000
a = a>>2 0000 1100

The binary RIGHT SHIFT operator moves the operands bits right, by the number of bits specified, which is this case is 2.  So the result of a = a>>2 = 0000 1100 or 12 in decimal. This also effectively divides the value of a by a factor of 4.

  

MSP430 GPIO Ports

Looking at the 20 pin MSP430G2253 supplied with the Launchpad, it has two GPIO ports. Both ports have 8 GPIO pins numbered as follows, port 1 pins P1.0 to P1.7 and port 2 pins P2.0 to P2.7.  The image below is extracted from the MSP430G2253 datasheet.

MSP430 Programming Tutorial MSP430G2253 20 pin GPIO layout

All the individual GPIO pins can be configured to connect to internal peripherals, for example, providing a connection for the ADC to an external source, or providing the output from Timer module in the shape of a PWM signal.  The GPIO’s as the acronym tells also provide General Purpose Input and Output operations.  Not all of the GPIO pins can be configured to be used by all the internal peripherals, a detailed list of how the pins can configured can be found in the datasheet for that particular microcontroller.  The image below again shows an extract from the MSP430G2253 datasheet, illustrating the GPIO pins P1.0 and P1.1 and their individual associated peripheral functions.

MSP430 Programming Tutorial MSP430G2253 GPIO Functions

Defining how each of the eight GPIO pins are configured for each port, is achieved by individual registers.

PxIN Input Register

The PxIN register reflects the value of the signal being input into the GPIO pin, when configured as an I/O function.  So by reading this value you can determine if there is a logic 0 or a logic 1 on the input.  Bit = 0: The input is low, Bit = 1: The input is high.  A statement using this function for GPIO port 2 and pin P2.4, could look like this if ((P2IN & Bit4) == BIT4);.

PxOUT Output Register

The PxOUT register determines the value output to the GPIO pin, when the pin is configured as an I/O function.  The PxOUT register works in conjunction with the PxREN as follows:

Pullup/pulldown resistor disabled: Bit = 0: The output is low, Bit = 1: The output is high.

Pullup/pulldown resistor enabled: Bit = 0: The pin is pulled down, Bit = 1: The pin is pulled up.

A statement using this function for GPIO port 1 and pin P1.4, could look like this P1OUT &= ~BIT4.

PxREN Pullup/Pulldown Resistor Register

The PxREN register enables or disables the internal pullup/pulldown resistor, which corresponds to the individual I/O pin.  Bit = 0: Pullup/pulldown resistor disabled, Bit = 1: Pullup/pulldown resistor enabled.  A statement using this function for GPIO port 1 and pin P1.5, could look like this P1REN |= BIT5.

PxDIR Direction Register

The PxDIR register selects the direction of the I/O pin, whether it will be an input or an output.  This is regardless of the selected function of the pin.  Bit = 0: The port pin is switched to input direction, Bit = 1: The port pin is switched to output direction.    A statement using this function for GPIO port 1 and pin P1.3, could look like this P1DIR |= BIT3.

PxSEL and PxSEL2 Function Select Registers

The PxSEL and PxSEL2 registers allow the individual GPIO pins to be associated with the internal peripheral module functions, or simply left as standard I/O ports.  The image below was extracted from the MSP430 family guide.

MSP430 Programming Tutorial PxSEL and PXSEL2 multiplex functions

A statement using this function for GPIO port 2 and pin P2.1, could look like this P2SEL |= BIT1;.  When using these registers, it is important to consult the datasheet and pin schematics, for the specific device.

P1IFG, P2IFG Interrupt Flag Registers

Only GPIO ports 1 and 2 have interrupt functionality.  The P1IFG and P2IFG registers hold the interrupt flag for the corresponding I/O pin, the interrupt flag is set when the selected input signal edge occurs at the pin.  Bit = 0: No interrupt is pending, Bit = 1: An interrupt is pending.  A statement using this function for GPIO port 1 and pin P1.1, could look like this P1IFG &= ~BIT1;.

P1IES, P2IES Interrupt Edge Select Registers

The P1IES and P2IES registers allow the interrupt edge type to be selected for each I/O pin. Bit = 0: The PxIFGx flag is set with a low to high transition, Bit = 1: The PxIFGx flag is set with a high to low transition.  A statement using this function for GPIO port 1 and pin P1.1, could look like this P1IES &= ~BIT1;.

P1IE, P2IE Interrupt Enable Registers

The P1IE and P2IE register bit enables the associated PxIFG interrupt flag.  Bit = 0: The interrupt flag is disabled, Bit = 1: The interrupt flag is enabled.  A statement using this function for GPIO port 1 and pin P1.1, could look like this P1IE |= BIT1;.

Texas Instruments also recommends configuring unused pins as I/O function, output direction, and left unconnected to prevent a floating input and reduce power consumption.

So how to change the GPIO Registers to what you want, well part 2 of this tutorial will make the GPIO settings clear and hopefully easy.  Additionally an ADC register will be explained and demonstrated.

I take great care when writing all the tutorials and articles, ensuring all the code is fully tested to avoid issues for my readers.  All this takes time and a great deal of work, so please support the site by using the Adfly links etc.  If you have found this useful or have any problems implementing, please feel free to leave a comment and I will do my best to help.