Hi all,
I suspect that I am having an issue with LPM 4.5. I am programming a game and after flashing the MSP 430, nothing happens - none of the LEDs light up. When I have the device connected physically and I manually go down the code line by line in CCS, things work as expected. Could I be missing a setting or tuning?
Code is below-
#include <msp430.h>
//most up to date program 8/27/23
void on (int LED);//turns on LED by its number
void off (int LED);// turns off LED by its number
int BC (void);// (Button Check)
void cycle (void);// main loop of the game
void who (int check,int x);//this lets players know who lost
void initGpio(void);
void showLevel (int level,char inc); //shows the player level in binary
//global variable(s)
int x;
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop WDT
// Configure GPIO
initGpio();
// Determine whether we are coming out of an LPMx.5 or a regular RESET.
if (SYSRSTIV == SYSRSTIV_LPM5WU) // MSP430 just woke up from LPMx.5
{
if((P2IFG & BIT6) == BIT6) //P2.2
{
x=6;
}
else if((P2IFG & BIT7) == BIT7) //P2.3
{
x=5;
}
else {
x=7;
}
//****OUTPUTS & INPUTS****
//P1
P1SELC= 0X00;
P1DIR = 0xFF; //ALL P1 IS SET TO OUTPUTS
P1OUT = 0x00; // ALL LEDs OFF
//P2
P2IFG = 0;
P2SELC = 0X00;
P2DIR |= BIT0+BIT1; //P2.0 = LED9 P2.1 = LED10
P2DIR &= ~BIT6+BIT7; // P2.6= B1 P2.7 = B2
P2OUT = 0x00; // ALL LEDs OFF and pull downs on pins 6&7
P2REN |= BIT6+BIT7; // enable pulldown/pullup
PM5CTL0 &= ~LOCKLPM5;
__delay_cycles(250000); // quick fix for the instant fail
cycle();
//
}
// Go here if game is over or powered from cold start.
// It configures the device and puts the device into LPM4.5
P2DIR &= ~BIT6+BIT7; // Configure as input direction pin
P2OUT &= ~BIT6+BIT7; // Configure as pulled-down
P2REN |= BIT6+BIT7; // resistor register enable
P2IES &= ~BIT6+BIT7; // low/hi edge
P2IFG = 0x00; // Clear all P2 interrupt flags
P2IE |= BIT6+BIT7; // interrupt enabled
PM5CTL0 &= ~LOCKLPM5;
// Explicitly clear RTC control registers to disable it
// just in case if the RTC was previously enabled
RTCCTL = 0;
PMMCTL0_H = PMMPW_H; // Open PMM Registers for write
PMMCTL0_L &= ~(SVSHE); // Disable high-side SVS
PMMCTL0_L |= PMMREGOFF; // and set PMMREGOFF
PMMCTL0_H = 0; // Lock PMM Registers
// Enter LPM4 Note that this operation does not return. The LPM4.5
// will exit through a RESET event, resulting in a re-start
// of the code.
__bis_SR_register(LPM4_bits | GIE);
}
//**************************FUNCTIONS*******************************
void on (int LED)
{
switch (LED)
{
case 1:
P1OUT|= BIT0;
break;
case 2:
P1OUT|= BIT1;
break;
case 3:
P1OUT|= BIT2;
break;
case 4:
P1OUT|= BIT3;
break;
case 5:
P1OUT|= BIT4;
break;
case 6:
P1OUT|= BIT5;
break;
case 7:
P1OUT|= BIT6;
break;
case 8:
P1OUT|= BIT7;
break;
case 9:
P2OUT|= BIT0;
break;
case 10:
P2OUT|= BIT1;
break;
default:
break;
}
}
void off (int LED)
{
switch (LED)
{
case 1:
P1OUT&= ~BIT0;
break;
case 2:
P1OUT&= ~BIT1;
break;
case 3:
P1OUT&= ~BIT2;
break;
case 4:
P1OUT&= ~BIT3;
break;
case 5:
P1OUT&= ~BIT4;
break;
case 6:
P1OUT&= ~BIT5;
break;
case 7:
P1OUT&= ~BIT6;
break;
case 8:
P1OUT&= ~BIT7;
break;
case 9:
P2OUT&= ~BIT0;
break;
case 10:
P2OUT&= ~BIT1;
break;
default:
break;
}
}
//button check function
int BC (void)
{
int IN=0;
int input=0;
input= P2IN & BIT6+BIT7; //make sure there is no noise P2IN&0b11000000 P2IN&BIT7+BIT6
IN = input;
switch (input)
{
case 0X00:
IN=0; //no player hit the buttons
break;
case BIT6:
IN=1;//player 1 hit the button
break;
case BIT7:
IN=2;//player 2 hit the button
break;
case BIT6+BIT7:
IN=3;//both players hit the button
break;
default:
IN=0;//any other weird scenario
break;
}
return IN;
}
void who (int check,int x){
unsigned char z = 255;
int n;
if (check==1||x==1){
while(z>0){
for(n=5;n>0;n--){
on(n);
off(n);
}
z--;
}
}
if (check==2||x==10){
z = 255;
while(z>0){
for(n=10;n>5;n--){
on(n);
off(n);
}
z--;
}
}
}
void showLevel (int level,char inc){
P1OUT=level/inc;
if (P1OUT!= 0){ //fix human perception issue
__delay_cycles (1500000);
}
P1OUT=0x00;
}
void cycle (void)
{
int check=0; // x is the counter and also the LED number to be turned on or off||check is Button Check output
int BIG;
int level=0;
const char inc=10; // increment per push of button(max 255)
const int startspd=1000;
if (x==5){ //LEDs go one way or the other depending on which button was pressed to wake up
goto P2;
}
do{
for(;x<10;x++){
BIG = startspd-level;
do{
on(x);
check=BC();
if(x==1&&check==1)
{
level=level+inc;
__delay_cycles(300000);
off(x);
break;
}
else if ((check>0)||(BIG==2&&x==1)){
off(x);
goto LOSE;
}
off(x);
check=BC();
if(x==1&&check==1)
{
level=level+inc;
on(x);
__delay_cycles(300000);
off(x);
break;
}
else if ((check>0)||(BIG==2&&x==1)){
off(x);
goto LOSE;
}
BIG--;
}while (BIG>1);
}
P2:
for(;x>1;x--){
BIG = startspd-level;
do{
on(x);
check=BC();
if(x==10&&check==2)
{
level=level+inc;
__delay_cycles(300000);
off(x);
break;
}
else if ((check>0)||(BIG==2&&x==10)){
off(x);
goto LOSE;
}
off(x);
check=BC();
if(x==10&&check==2)
{
level=level+inc;
on(x);
__delay_cycles(300000);
off(x);
break;
}
else if ((check>0)||(BIG==2&&x==1)){
off(x);
goto LOSE;
}
BIG--;
}while (BIG>1);
}
}while(1);
LOSE:
__delay_cycles(250000);
on(x);
__delay_cycles(250000);
off(x);
__delay_cycles(250000);
//tell me who lost
who(check,x);
__delay_cycles(200000);
who(check,x);
showLevel(level,inc);
level=0; //reset levels
}
void initGpio()
{
P1SELC= 0x00; P2SELC= 0x00;
P1DIR = 0xFF; P2DIR = 0xFF;
P1REN = 0xFF; P2REN = 0xFF;
P1OUT = 0x00; P2OUT = 0x00;
// Disable the GPIO power-on default high-impedance mode
// to activate previously configured port settings
//PM5CTL0 &= ~LOCKLPM5;}
}