Jump to content

Code Template

Recommended Posts

Here's a little code template I have created.

Not sure what I am going to use it for, but someone might find it helpful.

Handling of the interrupts is done in main rather than in interrupt routine itself.


#include "msp430g2231.h"

#define EV_TIMER BIT0
#define EV_PORT1 BIT1
#define EV_WDT BIT2
#define EV_USI BIT3

unsigned char events = 0;

void main(void)
WDTCTL = WDTPW + WDTHOLD;                 // stop WDT

// configure all here

__bis_SR_register(LPM0_bits + GIE); // switch to LPM0 with interrupts

while(1) {
	while(events) {
		if(events & EV_TIMER) {
			events &= EV_TIMER;
			// do your thing

			//clear TAIFG, etc.
			//enable interrupts, main, CCR0, CCR1, etc.
		} else  if(events & EV_PORT1) {
			events &= ~EV_PORT1;
			// do your thing

			//P1IFG &= ~BITn;	// clear IFG
			//P1IE |= BITn;		// enable interrupt
		} else  if(events & EV_WDT) {
			events &= ~EV_WDT;
			// do your thing

			IFG1 &= ~WDTIFG;
			//IE1 |= WDTIE;		// enable WDT interrupt
		} else  if(events & EV_USI) {
			events &= ~EV_USI;
			// do your thing

			//USICTL1 |= USIIE;	// enable interrupts
	__bis_SR_register(LPM0_bits); // switch to LPM0

// WDT interrupt service routine
#pragma vector=WDT_VECTOR
__interrupt void Watchdog_Timer (void)
//IE1 &= ~WDTIE;		// disable WDT interrupt
events |= EV_WDT;
__bic_SR_register_on_exit(LPM0_bits); // exit LPM0

// Timer A interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
//disable interrupts, main, CCR0, CCR1, etc.
events |= EV_TIMER;
__bic_SR_register_on_exit(LPM0_bits); // exit LPM0

// Port 1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
//P1IE &= ~BITn;			// disable interrupt
events |= EV_PORT1;
__bic_SR_register_on_exit(LPM0_bits); // exit LPM0

// USI interrupt service routine
#pragma vector = USI_VECTOR
__interrupt void USI_TXRX (void)
//USICTL1 &= ~USIIE;	// disable interrupts
events |= EV_USI;
__bic_SR_register_on_exit(LPM0_bits); // exit LPM0

// TODO Add ADC routine

Link to post
Share on other sites

Hey, that's interesting. I've been reading the uC/OS-III manual recently. This reminds me of how it deals with interrupts. It would be neat to implement priorities and communication between processes too. I guess you might now have much RAM or flash left over for your actual application if you did all that though.

Link to post
Share on other sites
I've been reading the uC/OS-III manual recently.

How is that book? I haven't cracked mine yet; haven't even looked at the terms of the "$1000" license we won. ;)


Eh. Parts of it make me say, "that's really clever," or it shines some light on some other related topic about OS'es I was wondering about. The rest of the time it's like reading a technical document. Sometimes, the authors of books like this try to use some humor or analogies to make it an easier read. This guy didn't.


There were several times I wanted to skip using the OS altogether. There's a fair amount of extra coding involved with using it. Now that I've read a little more, I think it's worth it.


I've got some planning done on paper, and some parts arrived today, but I haven't actually written any code yet.

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...