below is a code for the MSP430G2 micro-controller. I am having trouble adding to this assignment so far to include using the Interrupt Service Routine to my code. This code is a two bit counter which counts up using the S2 button on the launchpad and then counts down using an externally attached switch connected to pins 1.7 and 2.4, which can be seen in the code. If anyone can help explain how to add the ISR to this code, I would highly appreciate the help!
#include <msp430.h>
#define LEDS (BIT6 + BIT0)
// Global variables to hold current state and # of pushes
char pushes;
// Initialization function
void init(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= LEDS; // Set P1.0, P1.6 to output direction
P1DIR &= ~ BIT3; // Turn P1.3 into input
P1REN |= BIT3; // Enable internal resistor
P1OUT |= BIT3; // Enable as pullup resistor
P1DIR |= BIT7; // Set BIT 7 to output
P1OUT |= BIT7; // Set BIT 7 as VCC
P2DIR &= ~ (BIT4); // Makes this an input
P2REN |= (BIT4); // Turns on internal resistor
P2OUT &= ~(BIT4); // Make internal resistor pulldown
pushes = 0; // Initialize global variable
P1OUT &= ~(LEDS); // Clear output LEDs
}
// Read input function
char readInput(void)
{
char local = 0;
if (!(BIT3 & P1IN)) // Check for button push
{
local = 1;
_delay_cycles(10000); // Wait for bouncing to end
}
if ((P2IN & BIT4)) // Check for button push
{
local = 2;
_delay_cycles(10000); // Wait for bouncing to end
}
return local;
}
// Count up function
void countUp(void)
{
pushes += 1; // increment pushes variable
if (pushes & BIT0){
P1OUT |= BIT6;
}
else{
P1OUT &= ~BIT6;
}
if (pushes & BIT1){
P1OUT |= BIT0;
}
else{
P1OUT &= ~BIT0;
}
}
// Count down function
void countDown(void)
{
pushes -= 1; // decrement pushes variable
if (pushes & BIT0){
P1OUT |= BIT6;
}
else{
P1OUT &= ~BIT6;
}
if (pushes & BIT1){
P1OUT |= BIT0;
}
else{
P1OUT &= ~BIT0;
}
}
// Main function
int main(void)
{
char enable = 0; // Holds status of the S2 button
char previousState = 0; // Holds previous state of S2 button
init(); // One time initialization function
while (1)
{
enable = readInput(); // Check the buttons
if(enable && !previousState){
// If button is pressed, allow counter to increment/decrement once
if(readInput()==1){
countUp();
}
if(readInput()==2){
countDown();
}
}
previousState = enable;
}
}