Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
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 17 Oct 2015Another 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 18 Oct 20158 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?