Jump to content

Need help understanding this code

Recommended Posts

The code in main.txt sets up interrupts that are triggered when pins change from high to low (falling edge). A counter J is increased by some of these interrupts.


That being said, the code has quite a few issues. If this is not your code, I would look for a better example as this one will cause a lot of grief to get working. If it is your code, I suggest to first think about the structure of the program before reattempting the implementation.


A few issues:

- the variable j used by the interrupt routine to keep track of the pulses should be declared as volatile.

- the setup of interrupts that configure interrupts is very convoluted and without further documentation, it will be hard to figure out how they interact and how the program is supposed to work

- there are several interrupt routines that include printing out of date, this is bad practice and almost certainly will create issues.

Link to post
Share on other sites



I am new to programming micro controllers. I only started a couple of days ago. I'm kind of uncomfortable using that energia library because it feels like...cheating. The project i'm working on is a heartbeat sensor with a physical circuit and micro controller to count the beats. I want to make this project from scratch as a learning experience, and using a library that does all of the work for me defeats that purpose I think. 


Also, that is not my code. I found it on an instructable.


So far, I've just learned how to blink LED's in different patterns at different speeds using interrupts and timers. What should I focus on learning to be able to make the heart rate monitor? Is there anything in particular I should look into?

Link to post
Share on other sites

At least in the MSP430 line, each mcu family has a bunch of examples that go with it.  If you're looking to go lower level than Energia (I prefer code composer for MSP430 as it's a pretty simple chip) - there are some great examples there.  There should be some timer examples - which allow you to count the number of ticks between events.  They also come with comments at the top of the file that explain what it does.  


http://www.ti.com/product/MSP430G2553/toolssoftware#softTools - download the "Code Examples".

Link to post
Share on other sites

@@ak96 Nothing wrong with preferring bare metal, my preference as well :)


Take a look at the innards of the counter library, even if you don't plan to use Energia. It basically sets up a timer to count up when a pin changes from 0 to 1. Using the timer, you don't need an interrupt for the counting itself.


The source code of the library may look a bit cryptic, because it tries to accommodate many different MSP430s. The gist is:


1. Set GPIO pin as input and select it's function (PxSEL) as timer input. The timer input (TxxCLK) is only available on a few pins, consult your MCUs datasheet.

2. Configure timer for external clock and continuous mode

3. Functions to start, stop and reset the timer as well as reading out the current timer value.


Run the timer for a known time, and the number of pulses will allow to determine the rate.


That being said, not sure if that's the best approach for a heart rate monitor. Measuring the time between pulses might be more sensible:


1. Start the timer, driven by one of the MSP's internal clocks. Use a divider to avoid overflowing the 16bit timer for the expected heart rate.

2. Wait for a pulse on a pin. A busy loop reading the pin is probably good enough for a beginner project.

3. When a pulse arrives, read the timer value.

4. Calculate the rate using clock frequency, timer divider and measured timer value.


I don't know what output signal heart rate monitors provide. If it's an analog wave form, you may need some circuitry to convert it to something that is usable for digital inputs and/or use the built-in comparator or ADC peripherals. 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...