Tool/software: Code Composer Studio
Hi all, I'm in need of a little assistance with a coding project I have going on. I have attached what I have so far along with the requirements needed.
As of this moment, only the "countup" function is working through the S2 Button on the launchpad. The connected third switch to P1.4 and P2.1 is not counting backwards through the LEDs as it should.
All help is greatly appreciated!!
#include <msp430.h>
#define OUTPUTMASK (BIT6 + BIT0 + BIT4)
// Global variables to hold current state and # of pushes
char currentState;
char pushes;
char direction;
// Initialization function
void init(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR |= OUTPUTMASK; // Set P1.0, P1.6, and P1.4 to output direction
P1REN |= BIT3; // P1.3 enable internal resistor
P1OUT |= BIT3 + BIT4; // Set bits 3 and 4 of P1
P2DIR &= ~(BIT1); // Makes this an input
P2REN |= BIT1; // P2.2 enable internal resistor
P2OUT &= ~(BIT1); // Make internal resistor pulldown
currentState = 0; // Initialize global variable
pushes = 0; // Initialize global variable
P1OUT &= ~(OUTPUTMASK); // Clear output LEDs
}
// Read input function
char readInput(void)
{
char local = 0;
if (!(BIT3 & P1IN)) // Check for button push
{
pushes += 1; // increment pushes variable
local = 1;
direction = 1;
_delay_cycles(10000); // Wait for bouncing to end
}
else if (BIT1 & P2IN) // Check for button push
{
pushes -= 1; // decrement pushes variable
local = 1;
direction = 0;
_delay_cycles(10000); // Wait for bouncing to end
}
else{
local = 0;
_delay_cycles(10000); // Wait for bouncing to end
}
return local;
}
// Count up function
void countUp(void)
{
switch(currentState){
case 0: {
currentState = 1;
P1OUT |= BIT6;
P1OUT &= ~(BIT0);
break;
}
case 1: {
currentState = 2;
P1OUT &= ~(BIT6);
P1OUT |= BIT0;
break;
}
case 2: {
currentState = 3;
P1OUT |= BIT6;
P1OUT |= BIT0;
break;
}
case 3: {
currentState = 0;
P1OUT &= ~(BIT6);
P1OUT &= ~(BIT0);
break;
}
}
}
// Count down function
void countDown(void)
{
switch (currentState)
{
case 3:
currentState = 2;
P1OUT |= BIT6;
P1OUT &= ~(BIT0);
break;
case 2:
currentState = 1;
P1OUT &= ~(BIT6);
P1OUT |= BIT0;
break;
case 1:
currentState = 0;
P1OUT |= BIT6;
P1OUT |= BIT0;
break;
case 0:
currentState = 3;
P1OUT &= ~(BIT6);
P1OUT &= ~(BIT0);
break;
}
}
// 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(direction){
countUp();
}
else{
countDown();
}
}
previousState = enable;
}
}
