MSP430 Polling Vs Interrupt

In this example other than the usual polling method we uses an interrupt to get the switch reading…
In MSP430 port1 interrupt service routine is written like this

#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{

}

I used code composer studio to get the output of interrupt code…The mspgcc interrupt service routine is like this but i failed to get the output using mspgcc

//port1 interrupt service routine
void Port_1 (void) __attribute__((interrupt(PORT1_VECTOR)));
void Port_1 (void)
{

}

This example also reads switch and toggles light…This is the full code of the interrupt program..

/*interrupt.c
ganeshredcobra@gmail.com
GPL
*/
#include <msp430g2553.h>
#define LED1 BIT0
#define LED2 BIT6
#define BUTTON BIT3
volatile unsigned int i;//to prevent optimization
void main(void)
{
WDTCTL=WDTPW+WDTHOLD;
P1DIR |= (LED1+LED2);//
P1OUT &= ~(LED1+LED2);
P1IE |= BUTTON;
P1IFG &= ~BUTTON;

//__enable_interrupt();//enable all interrupts
_BIS_SR(LPM4_bits+GIE);
for(;;)
{}
}

//port1 interrupt service routine
#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
P1OUT ^= (LED1+LED2);
P1IFG &= ~BUTTON;
P1IES ^= BUTTON;
}

MSP430 GPIO Programming Reading an external switch

To read an external switch using MSP430 an external switch is connected to P1.4…We uses polling method here to read the switch

/*button_press.c
ganeshredcobra@gmail.com
GPL
An external button connected to P1.4 is used in this example...
connect P1.4 to vcc through a resistor...
*/
#include 

volatile unsigned int i;//to prevent optimization
void main(void)
{
	WDTCTL=WDTPW+WDTHOLD;
 	P1DIR|=0X01;//set all bits in P1 to input except BIT0
	for(;;)
	{
	  if((P1IN & BIT4)==0)//while pushing S1
		P1OUT=0X00;//LED ON
	  else
		P1OUT=0x01;//LED OFF		 
	}
}

When we pushes the button LED will be ON and when we releases it will be OFF…
Happy Hacking 🙂

STM32F4Discovery GPIO Programming using ChibiOS RTOS

To use the gpio pins…

cd ChibiOS_2.4.1/demos/ARMCM4-STM32F407-DISCOVERY/

Similar to the earlier method edit the main.c file and insert this code

#include "ch.h"
#include "hal.h"
int main(void) {
halInit();
chSysInit();
palSetPadMode(GPIOD, 4, PAL_MODE_OUTPUT_PUSHPULL); /* PD4 */
while (TRUE) {
palSetPad(GPIOD, 4);
chThdSleepMilliseconds(500);
palClearPad(GPIOD, 4);
chThdSleepMilliseconds(500);
}
}

The palSetPadMode configures PD4 or GPIOD’s fourth pin as output..now connect an led to the PD4 and see output

Now repeat the older steps to generate binary

cd ChibiOS_2.4.1/demos/ARMCM4-STM32F407-DISCOVERY/

make

st-flash write build/ch.bin 0x08000000

Happy Hacking 🙂