8 years, 5 months ago.

problem with code (nucleo103)

hi to everyone i am new to embed, i started before few days with the STM32 Nucleo, the proccesor is the F103RB and i have problem with this code (it doestn't work correctly). Can anyone help me? thanks the code:

led_project

 
BusOut myleds1(D2, D3, D4, D5);
BusOut myleds2(D6, D7, D8, D9);
char x=1;
InterruptIn button (USER_BUTTON);
int prognum;

void prog() {
    prognum = prognum + 1;
}

void prog1() {
        for(int i=0; i<3; i++) {
            x = x << 1;
            myleds1 = x;
            wait(0.08);
        }
        for(int i=0; i<3; i++) {
            x = x << 1;
            myleds2 = x;
            wait(0.08);
        }
        for(int i=0; i<3; i++) {
            x = x >> 1;
            myleds2 = x;
            wait(0.08);
        }
        for(int i=0; i<3; i++) {
            x = x >> 1;
            myleds1 = x;
            wait(0.08);
        }
    }
    
void prog2 () {
        
        for(int i=0; i<32; i++) {
            myleds1 = i;
            wait(0.08);
            myleds2 = i;
            wait(0.08);
        }
        }
        
void prog3() {
    for(int i=0; i<3; i++) {
            x = x << 1;
            myleds1 = x;
            wait(0.08);
        }
    for(int i=0; i<3; i++) {
            x = x << 1;
            myleds2 = x;
            wait(0.08);
        }
    }
    
void prog4() {
    for(int i=0; i<3; i++) {
            x = x >> 1;
            myleds1 = x;
            wait(0.08);
        }
    for(int i=0; i<3; i++) {
            x = x >> 1;
            myleds2 = x;
            wait(0.08);
        }
    }
 
int main() {
        button.rise(&prog); 
        prognum=1;
        while(1) {
            if(prognum == 1) {
                prog1();
            }else if(prognum == 2) {
                prog2();
            }else if (prognum == 3){
                prog3();
            }else if (prognum ==4) {
                prog4();
                prognum=1;
            }
        
        
    }
}

2 Answers

8 years, 5 months ago.

One problem you often see with global variables that are modified in an interrupt handler is that the compiler does not see what is going on and optimises the code in such a way that the variable does not seem to change at all. The fix is to declare the variable as 'volatile'.

volatile int prognum;

A second problem may be that the user button has some signal bounce and you get multiple interrupts on a single keypress. You could increase the wait(0.08) in the progx or even wait until the key has been released again.

Thank you for the answer, i changed the variable but may be the button has a problem, it's onboard and i cant do anything. I' ll try to add a external button with a capacitor to remove the boince

posted by Ioannis Karaxalios 17 Oct 2015

Another problem is a real possibility of passing a variable prognum to a value greater than four (eg fast pulses on the button). Then the main loop of the program there will be nothing doing and did not return variable to the value of one.
I propose a change:

void prog () {
     prognum = prognum + 1;
     if (prognum> 4) {prognum = 1};
}

another possibility of trouble, it uninitialized variable x in functions implementing a shift. Relying on the value which is after the previous function is unsafe when programs may be in a different order (due to the possibility of rapid changes in prognum than performing time one progx)

posted by Nothing Special 18 Oct 2015

thank you i try to use a switch instead of button

posted by Ioannis Karaxalios 19 Oct 2015

Further to Wim's comment...

Don't you need the wait(0.08) within the Interupt service routine? i.e.

void prog() {
    prognum = prognum + 1;
    wait(0.08);
}

Else the debounce will effect any wait outside in other parts of the program.

posted by Martin Simpson 19 Oct 2015
8 years, 5 months ago.

Seems to be working here. I changed this:

BusOut myleds1(D2, D3, D4, D5);

to this:

BusOut myleds1(D2, D3, D4, D13);

just so I could see something working on my 103 board and it flashes as expected.

I am assuming you have a custom board connected? Or looking with a scope? What is not working?

thank you, i changed and this and at the beggining i have two programs, one and half. I have the half prog1 and the prog2. I making the test at a breadboard with led, when i finish the code maybe i create a board with transistor and drive led ribbons.

posted by Ioannis Karaxalios 17 Oct 2015