Reputation: 11
My issue arises with the variable total
. The debugger steps over like it's not even there, or if you get rid of the *
, it runs and runs and provides a weird number. The idea of the program is to add dimes (or 10) to the total
until it is greater than the goal which is 10000
.
I'm writing this in C with IAR Embedded Workbench and am using a MSP430G2553.
#include <msp430g2553.h>
#include <stdio.h>
volatile unsigned int i;
int dime=0;
int goalz =10000;
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P1DIR |= 0x00;
for(;;){
P1OUT =0x01;
while(1)
{
if((BIT4 & P1IN))
{
P1OUT |= 0x01;
dime++;
int *total = 0;
*total = &dime;
}
else
{
P1OUT |= 0x00;
}
}
}
}
Upvotes: 0
Views: 74
Reputation: 140256
int *total = 0;
*total = &dime;
that is wrong, because you're trying to store the address of dime
(not its value) in the location pointed by total
, i.e. zero => not what you want (someone suggests this is the location of a register, so even if it doesn't crash, it's not valid. Writing the INTENA register is not good!).
The debugger probably optimizes out this statement, writing directly in zero.
not completely sure of what you want to achieve, but you have to declare total
as a global variable and add dime
to it. No need to use pointers or variable addresses here.
Upvotes: 3
Reputation: 737
Probably, the debugger is steps over since the compiler is not even generating the code. You are creating the variable total (pointer or integer) in the stack (i.e., you add an item), you assign a value, and then you are out of scope so the variable is not there anymore.
I think that what you are trying to achieve is:
if (...) {
static int total = 0;
total = dime
}
but, then again, the variable total is completely useless, since it will always be the same value as dime. How should be total
and dime
different?
Upvotes: 1