• AVR Freaks

AnsweredHot!Run time error with interrupts: Code stops suddenly upon button press

Author
kino
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2020/07/08 18:35:00
  • Location: 0
  • Status: offline
2021/02/13 14:22:59 (permalink)
0

Run time error with interrupts: Code stops suddenly upon button press

Good day guys,
Im working on a stop watch code on the UBW32 (pic32mx795 breakout board). I had gotten the code to work successfully with a little guidance from some of the members on here.
The stop watch code works fine except for one thing, the last message thats suppose to say "XX.XXs elapsed" wont print the elapsed.
 
The problem real problem currently facing
 
The book asked me to use polling to print current running time so that the user has some visual idea of the current time (instead of it happening in the background). I got the current time to run but the moment I press the button to stop the timer, instead of going to the last part of the code (where the LED changes and the message is returned) it just stops in the terminal. 
 
The code is attached below, any and all help will be greatly appreciated.
 
 
#1
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/13 15:50:12 (permalink)
5 (2)
Does it help if you change
int state;

to
volatile int state;

 
You should always make a variable "volatile" if you change it inside an ISR, and read the value outside the ISR.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#2
kino
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2020/07/08 18:35:00
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/13 17:44:14 (permalink)
0
"You should always make a variable "volatile" if you change it inside an ISR, and read the value outside the ISR."
Yea you're right the book did mention this, advising that we should include it so that the compiler does not skip over it and always generate assembly codes for it (or something like that).
 
I made it volatile but it still pauses when I press the stop button.
 
dont know if I mentioned this before but without the code that does the polling,i.e. if I had left the while loop to "do nothing", the program/board would respond to the stop button printing out the message.
#3
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/13 17:53:04 (permalink)
5 (1)
I just noticed, you don't have a "superloop".
i.e. you are allowing your main() function to exit. NEVER do that!
There should be a "do forever" loop around all the code after your initialisation.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#4
kino
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2020/07/08 18:35:00
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/13 18:38:53 (permalink)
0
"you are allowing your main() function to exit. NEVER do that!"
 
Ah I see, newbie mistake. I'll never repeat it in the future. On that note tho, not sure if its present in the code I uploaded but I did create an infinite loop at the end of the code that would keep led 1 off and 2 on indicating that the stop button was pressed.
 
When I wrapped the whole thing in the superloop it still does the same thing.
 
#5
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/13 19:04:29 (permalink)
0
Please post what your code looks like now.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#6
kino
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2020/07/08 18:35:00
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/13 19:24:24 (permalink)
0
This is the code I currently have
#7
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/13 19:55:44 (permalink)
0
When you exit the wait loop, the next line is
        if (state == 4){


If everything worked as expected, "state" must be 4 at that time.
Try just printing the value of the state variable instead to see what is happening. Presumably it is not "2" if the elapsed time stops printing.
Note: your "Exit_time" variable should also be volatile.
 
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#8
kino
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2020/07/08 18:35:00
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/13 21:09:18 (permalink)
0
Very strange behavior.
When I comment out the "if(state = 4) {" part, the code runs but nothing displays after I press the button; it just pauses like before. 
 
When I comment out this small block of code:
 
disp_time = _CP0_GET_COUNT();                                        
elapsed = disp_time *0.000000025;                                  
sprintf(msg,"%4.2Lfs", elapsed);                                   
NU32_WriteUART3(msg);                                              
NU32_WriteUART3("\n\r");                                          
delay();
 
The code after the while loop runs and it shows that the board is in state 4 as expected, therefore printing the "done" message.
I'm not sure what in this piece of code is wrong. I can rule out the NU32 write functions cause they are present at more than one place throughout the code. that leaves the delay function call and the other 3 lines.
#9
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/13 21:31:32 (permalink)
0
I wonder if _CP0_GET_COUNT(); is reentrant.
You are calling it from both the ISR and the main code.
Maybe disable interrupts aroud that call.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#10
andersm
Super Member
  • Total Posts : 2901
  • Reward points : 0
  • Joined: 2012/10/07 14:57:44
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/14 03:26:04 (permalink)
5 (1)
ricI wonder if _CP0_GET_COUNT(); is reentrant.

Yes, because it ultimately resolves to a single mfc0 instruction.
 
OP: Since you know that commenting out that block lets the code run, uncomment the code one line at a time until it starts hanging again (my money is on the sprintf() call).
post edited by andersm - 2021/02/14 03:27:59
#11
kino
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2020/07/08 18:35:00
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/14 12:03:49 (permalink)
0
@andersm
 
That was a nice suggestion to find the code that was giving the issue. I did, and it turns out that the delay is the issue. Not sure why this is the case tho. Without the delay, as you can imagine the times print to the terminal very fast but when the button is pressed it goes to the done state. (Although sometimes it gets buggy, so it does not always arrive at the "done" state). Why would such a simple delay function stop the code form progressing?
 
@ric
 
"I wonder if _CP0_GET_COUNT(); is reentrant.
You are calling it from both the ISR and the main code.
Maybe disable interrupts aroud that call."
 
I tried disabling the interrupts round the call for setting the "disp_time" variable, however the code still behaved the same. 
 
#12
dan1138
Super Member
  • Total Posts : 4324
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/14 17:33:14 (permalink) ☼ Best Answerby kino 2021/02/27 13:31:40
5 (1)
@kino,
 
I suspect that you have calls to many blocking functions in your process loop.
 
After rewriting your application to not use blocking functions it seems to work.
 
Try the attached project to see of it works in your hardware.
 
And another thing the UBW32 user switch is connected to RE6 but the external interrupt 2 is connected to RE9.
 
When RE6 and RE9 are not connected together my project will not work.
#13
cirilo.b
Senior Member
  • Total Posts : 73
  • Reward points : 0
  • Joined: 2020/09/08 18:40:42
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/16 13:41:54 (permalink)
4 (1)
Why copy 'Exit time'? The ISR isn't going to modify it in 'state 4'. You'll also want a delay after the 'if(state == 3)' otherwise the other end of the loop might prime the trigger a bit quick. Check to make sure you have at least 60ms of delays between changes in state; that should be enough to get rid of the problems with 'switch bounce'.
 
It may be that you're not clearing the interrupt correctly.  For every interrupt you need to check the conditions for clearing it, and sometimes it's really not obvious from the docs. In the case of the Change Notification, according to DS60001120: "Read the corresponding PORTX registers to clear the CN interrupt".  So read the port register before clearing the IF. Unfortunately my copy of DS60001120 seems to be for the PIC32MZ series which handles pin interrupts slightly differently; the MX series for example doesn't have the CNNEx registers (not to be confused with CNENx). So whether or not the IF flag is being cleared in your current code is anyone's guess.
#14
kino
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2020/07/08 18:35:00
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/17 11:07:05 (permalink)
0
@dan
Sorry about the late response, was a bit caught up for the past few days.
 
Thank you for posting your code, I tried uploading to the board as is:
 
It uploaded successfully but only garbage values were being printed to the terminal. I figured this was because of the way in which you communicated with the UART3 peripheral. I'm not sure why this board is not responding to it. Dont know what I'm missing but embedded is not clicking to me as I'd like it to.
 
I had modified your main code so that it worked with the NU32 library. I got the characters on the terminal recognizable but the code stop running after I press the button to begin the timer. I will keep trying until something works.
I think the problem is in the sprintf function call seeing as how mplab keeps posting warnings about them.
 
Again, thank you for taking the time to write an entire project code. I loved the way your logic flowed, made the code easy to understand and edit.  
#15
kino
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2020/07/08 18:35:00
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/17 11:21:39 (permalink)
0
@cirilo
 
I never knew that there were conditions to clear interrupt flags. The book did mention that each interrupt is configured differently based on the peripheral but I thought that once you set the IF bit to zero, the flag would be cleared. Thank you for pointing that out to me. I'll make sure that my interrupt is clearing properly, sounds like that could be posing the issue im facing.
 
#16
dan1138
Super Member
  • Total Posts : 4324
  • Reward points : 0
  • Joined: 2007/02/21 23:04:16
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/17 17:49:36 (permalink)
5 (1)
@kino,

Your book is correct that the method to clear the interrupt request flags does depend on how the hardware on chip is implemented.

Of all of the interrupt sources available in the PIC32MX795F512L the external interrupts are the most straightforward.

I suspect what is causing your issues may not be so obvious.

The code you posted enables hardware flow control using RTS/CTS on UART3.

I observed behavior like you have described until I reconfigured the UART to not use hardware flow control.

I also see you have configured the UART baud rate to 230400 baud. This is a problem because this rate cannot be setup accurately with a 8MHz system oscillator. Even using the PLL to set the system clock to 80MHz it is still cannot be reached accurately enough for stable asynchronous serial operations.

On top of this I have destroyed my UBW32, but don't fret for me as I have a DM240001-3 Explorer 16/32 with a PIC32MX795F512L PIM.

I have attached the project I made after porting my code from the UBW32 to the DM240001-3 and adjusting for the differences in how the hardware is connected.
 
I changed the UART baud rate to 250000 bits per second. On my Windows system I am using TeraTerm v4.93 and it seems to work with the USB to serial bridge chip on the DM240001-3 just fine at 250K baud.
 
Also I added support for the 16x2 ASCII LCD module on the DM240001-3.
 
My code will run even when this module is not present.
#17
kino
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2020/07/08 18:35:00
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/17 19:45:59 (permalink)
0
@dan
 
"I observed behavior like you have described until I reconfigured the UART to not use hardware flow control."
 
This sounds counter intuitive, you would think that hardware flow control would make the transmission more reliable. I'm a beginner at this so I dont really know too much about anything really.
 
Sorry about your UBW32 (RIP), but seeing as you had one, you most likely knew that it did not come with a USB-UART converter like the NU32. I had bought the ft232h board by adafruit, a breakout board of an ftdi chip that I used for the serial communication. I'm trying to follow the book to the T, which used the NU32 board so writing the codes and using their libraries has not been the easiest. Noticed you had configured your code to use RTS and CTS, will try to turn that off and see the response.
 
"I also see you have configured the UART baud rate to 230400 baud. This is a problem because this rate cannot be setup accurately with a 8MHz system oscillator. Even using the PLL to set the system clock to 80MHz it is still cannot be reached accurately enough for stable asynchronous serial operations."
 
Thank you for pointing this out. The NU32 library wasnt written by me, the makers of the book wrote it. I know the baud rate is the transmission speed but I dont how this ties in with the fidelity of the information passed. When I changed the baud rate in the pass, the code ran just fine. I dont know why they made it 230400...
 
 
I started on the Pic32 usb starter kit, which had one of those 795mx PIM. Needed the expansion board or an explorer like you have to access the pins. so i ended up buying the UBW32 cause it was the cheaper alternative. Should have just saved up a bit more tbh; couldn't even get the LCD demo to work that the book used to demonstrate libraries.
 
Thanks again dan
#18
ric
Super Member
  • Total Posts : 30244
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/17 20:05:45 (permalink)
4 (1)
kino
 
Thank you for pointing this out. The NU32 library wasnt written by me, the makers of the book wrote it. I know the baud rate is the transmission speed but I dont how this ties in with the fidelity of the information passed. When I changed the baud rate in the pass, the code ran just fine. I dont know why they made it 230400...

The baud rate is generated by doing integer division of the main clock/4 or /16.
230400 is a standard rate, but very hard to achieve by ineger division of common clock frequencies.
That means the real baud rate you are using won't actually be 230400, but will be off by some error factor.
If the rate you are really using is more than 2-3% off the correct frequency, you will get transmission errors.
250000 is a much easier speed to generate.
 

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#19
kino
Starting Member
  • Total Posts : 35
  • Reward points : 0
  • Joined: 2020/07/08 18:35:00
  • Location: 0
  • Status: offline
Re: Run time error with interrupts: Code stops suddenly upon button press 2021/02/27 13:37:46 (permalink)
0
Took a break to catch up with school.
 
Marked dan's response as the answer. The problem with my code is definitely the delay() function. Got it to work with little to no errors once that function was removed. I will attempt to make it perfect in the future once my understanding of the pic32 increases. 
 
Thanks for all the help received it was very much appreciated.
 
P.s @ric
 
Thanks for your explanation of the baud rate on the PIC, that gave me insights that would take days of reading to get.
#20
Jump to:
© 2021 APG vNext Commercial Version 4.5