Compiling errors when using interrupts

Home Forums MPLAB XC8 Project Discussions Compiling errors when using interrupts

Viewing 7 reply threads
  • Author
    Posts
    • #7325
      AvatarJakkie
      Participant

      Good day guys,

      I hope someone would be able to assist me with the following problem. I used some of the code on this site and wrote a program that prints messages in a loop on the PIC Demo 2 Plus board LCD. I am using a PIC16F877 and the compiler is XC8 ver 2.20.

      The code compiles without any issues and the messages are displayed when running the project. I updated the code to display a different message when a button (RB0) is pressed. I want to do this by using the interrupt function on of the PIC. This is where the problem is. If I do something as simple as switching on a LED (RB1), that’s not problem. The interrupt vector is invoked and LED lights up when RB0 is pressed. However, the moment I uncomment the section to display the message, I get compiler errors and I don’t know why. Can someone please guide me in resolving this issue? The code section causing the error is on line 27-29 in the main.c file.

      The  “Compile_With_Errors.txt” and “Compile_Without_Errors.txt” files are the output when I compile the code.

      Attachments:
      You must be logged in to view attached files.
    • #7328
      AvatarJakkie
      Participant

      Apologies, I see the main.c and lcd.h files did not upload. I am posting the contents here:

      Contents of file main.c:

      Contents of file lcd.h:

       

      • This reply was modified 1 month, 1 week ago by AvatarJakkie.
      • This reply was modified 1 month, 1 week ago by Bitahwa BinduBitahwa Bindu.
      • This reply was modified 1 month, 1 week ago by Bitahwa BinduBitahwa Bindu.
    • #7330
      AvatarJakkie
      Participant

      I managed to fix the previous error by adding the function declaration in the lcd.h header file. I now get a new error:

       

      main.c:118:: error: (1387) inline delay argument must be constant
      main.c:42:: error: (1387) inline delay argument must be constant
      main.c:108:: error: (1387) inline delay argument must be constant

       

      Even if I pass a constant value like:

      __delay_ms(5);

      I still get the same error. Attached is the full output.txt file.

      Attachments:
      You must be logged in to view attached files.
    • #7332
      Bitahwa BinduBitahwa Bindu
      Keymaster

      Hi Jakkie,

      It’s easier to assist quickly if you upload your MPLAB project instead of individual files. One can just download your full project and compile it on our side to replicate the errors you are getting, but with individual files, we are forced to recreate your project which can take some time.

      About the first error, I see you are using  XC8 v2.20, is it set in C99 or C90 mode? Because I see this warning in the compilation log: advisory: (2049) C99 compliant libraries are currently not available for baseline or mid-range devices, or for enhanced mid-range devices using a reentrant stack. Can you try to set the compiler to C90 standard mode, it might resolve the error, it explained in this article, change the C standard and C library to C90: https://microchipdeveloper.com/xc8:moving-to-xc8-2-0

      About the second error, you didn’t specify what you added so it’s difficult to advice.

      If you still experience problems, just upload the full project in a zip format and we will check it for you, not individual files. The easiest way to do that, is to package your project, it will generate the zip file for you, Read this to learn how to package your project:https://microchipdeveloper.com/mplabx:projects-package

       

    • #7334
      AvatarJakkie
      Participant

      Hi Bitahwa,

      Thank you for the quick response. The mode was set to 99, so I changed it now to 90 and the warnings for that part of the problem is now gone.

      The second error is related to this error:

      main.c:118:: error: (1387) inline delay argument must be constant

      This happens when I execute (uncommend) the following statements from within the interrupt section (lines 27-29 in main.c file):

      DisplayClr(); // Clear LCD Lines
      LCDPutStr(” Button Pressed “); // Print string to LCD

      I have uploaded the packages as requested.

      Thank you for the assistance.

      J

      Attachments:
      You must be logged in to view attached files.
    • #7336
      Bitahwa BinduBitahwa Bindu
      Keymaster

      It’s not advisable to have a delay inside an interrupt service routine, the principle of an interrupt is to service the routine in a short time as possible and continue with the main code.

      The error you are getting tells you that the delay argument must be a constant, not a variable because it is expanded to generate the required delay at compile-time (i.e. before any variables have known values) and your delays functions are generated outside of the interrupt service routine ( your Delay_Seconds and the LCD delays, that’s why it’s complaining)

      In your situation the best option is to poll in the interrupt flag in the while (1) loop if there is an interrupt, then execute your code. If there is interrupt, set this flag to true, then check if it’s true in your code.

      Something like this:

      If you have have blocking functions in your while(1) loop, you might want to poll it inside these functions as well for immediate attention.

    • #7337
      AvatarJakkie
      Participant

      Hi Bitahwa,

      Yes I actually had the original code like that where I just set the flag and then check for it in the main – while(1) loop. This works well for the purpose of the experiment and just to play around to learn. However, if the main function had hundreds or even thousands of lines of code for actual real life applications, that might cause a problem if all that code has to be execute first before a check is done. Even if I check every now and then in between, there will still be code that has to run before the interrupt action is done. So the action to the interrupt might not be as immediate as anticipated which makes the idea of an interrupt meaningless.

      I’m going to make some changes to the delay sections/functions and see if I can actually find a way to run the message from the interrupt routine. If I can sort this, I will return to this post and share. There might be someone else struggling with the same problem.

      Thanks for the help, I appreciate.

    • #7338

      The other option is to generate delays with timer interrupts. Because either way it’s not a good idea to have blocking delays inside an interrupt service routine. You can have more than 1 interrupt in your code.

Viewing 7 reply threads
  • You must be logged in to reply to this topic.
Select your currency
ZARSouth African rand
EUR Euro