• AVR Freaks

Hot!LVD interrupt with PIC18F452 in XC8 compiller

Author
Flyingman1975
Starting Member
  • Total Posts : 79
  • Reward points : 0
  • Joined: 2015/05/27 13:47:46
  • Location: Prague, Czech Republic
  • Status: offline
2019/04/07 00:52:31 (permalink)
0

LVD interrupt with PIC18F452 in XC8 compiller

Hi friends,
I would like to ask you for help with interrupts. I am a confused with interrupts and priorities and how to define interrupts by right way :(.
 
I made simply water-flow metter with PIC18F452. I call interrupt code by Timer0 every 2 secconds. During this periode is counting intput pulses by counter Timer1. Results are printed on LCD. This is working. But I would resolve how to save last value when the flowmetter is switched off. I think that saving values to EEPROM memory every 2 second is not healthy for EEPROM memory life. So my idea was use LVD module to save values after switching off during short time od discharge capacitors after switching off. But i am not sure if this is the best way how to do it :).
 
So I tried setup LVD. My power voltage is 3.3V, LVD voltage is set on 2.8-2.97V. I tried to add LVD interrupt code and changed Timer0 interrupt to low priority and LVD to high priority. But after adding LVD setup my code stopped working.
 
I know that my interrupt code is confused. Please, could you help me with the right interrupt setup ?
 
Thank you very much !!
 
MCC is not available for this MPC.
MPLAB X IDE v4.01
XC8 v1.45
 
post edited by Flyingman1975 - 2019/04/07 01:18:35
#1

11 Replies Related Threads

    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 02:38:11 (permalink)
    +1 (1)
    Can’t say why the LVD is not working, but with regard to your ADC interrupt:
    Move ALL to floating point calculations out of there.
    Read ONLY AD_Input value, and pass this to your mainline.
    Do all your floating point stuff in main.
    Diasable and enable interrupts as you read global AD_Input and as you write global sumLitr. But make sure these reads/writes are just copies from/to temporary work variables, and not actual calculations. If you surround actual calculations with GIE=0/GIE=1 you will have interrupts disabled for a long period.

    What is this for, you do not use the pointer:
    float sumLitr, *p_sumLitr = &sumLitr;

    Edit: Mark any global variable used both inside and outside an ISR volatile.
    Edit again: Define “stopped working”.
    post edited by pcbbc - 2019/04/07 02:42:50
    #2
    Flyingman1975
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2015/05/27 13:47:46
    • Location: Prague, Czech Republic
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 03:14:04 (permalink)
    0
    Hi, thank you for your answer.
    1. moving float calculations and disbling of interrupts at ADC is good idea.
    2. float sumLitr, *p_sumLitr = &sumLitr; - I had to define this as pointer because regarding this value to write memory as float -> void WRITE_FLOAT_EEPROM(unsigned char ee_addr, void *obj_p, char obj_size). But normaly i use it as usual variable.
    3. what is ISR ?
    4. not working.. nothing is on lcd display..
    just i discovered that when I remove condition "if (lcdPrint == 1)" so commands in while(1) working but without values.
    So the interrupt "void interrupt low_priority tc_int  (void)" does not executed now.
    #3
    qhb
    Superb Member
    • Total Posts : 9985
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 03:19:19 (permalink)
    +1 (1)

    2. float sumLitr, *p_sumLitr = &sumLitr; - I had to define this as pointer because regarding this value to write memory as float -> void WRITE_FLOAT_EEPROM(unsigned char ee_addr, void *obj_p, char obj_size). But normaly i use it as usual variable.

    But you never use the variable p_sumLitr.
    When you call
    WRITE_FLOAT_EEPROM(0, &sumLitr, 4);

    plainly you just pass the address of the original variable directly.
     
    Flyingman1975
    3. what is ISR ?

    Interrupt Service Routine
     
     
    #4
    qhb
    Superb Member
    • Total Posts : 9985
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 03:27:35 (permalink)
    +2 (2)
    n.b. interuptInit() should NOT set the GIE bit while it is still setting everything up.
    Setting that bit should be left until after you have done ALL your initialisation.
     
    #5
    Flyingman1975
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2015/05/27 13:47:46
    • Location: Prague, Czech Republic
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 04:12:21 (permalink)
    0
    Yes, i hade GIE=1 in initialization, thank you. But there is a problem why after LVD interrupt declaration stopped working Timer0 interrupt :((
    #6
    qhb
    Superb Member
    • Total Posts : 9985
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 04:33:49 (permalink)
    +2 (2)
    Ordinarily, I would strongly recommend against calling a blocking function like eeprom_write() from inside an interrupt service.
    I do recognise this is a special case, as you only do it when power is failing.
    Also, you MUST NOT MUST NOT MUST NOT set the GIE bit while inside an interrupt service.
    Now the eeprom write function, reads the current state of GIE (which should be clear if called from inside an interrupt service), and writes it back afterwards.
    If you're only calling this from the ISR, then get rid of the code reading and writing GIE to make absolutely sure it can't get set.
     
    Have you tried running this inside the debugger, and manually triggering the interrupt?
     
     
    #7
    Flyingman1975
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2015/05/27 13:47:46
    • Location: Prague, Czech Republic
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 05:54:17 (permalink)
    0
    I cant use debugger, I am afraid :(
     
    But just I discavered one thing. I deleted all settings od LVD. At the and stayed only empty:
     
    void interrupt high_priority lv_int (void) 
    {
    }

    Still not working, but when I delete this empty as well, it is ok. So problem will be somewhere in this declaration.
    #8
    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 06:12:29 (permalink)
    +3 (3)
    I may have missed it, but where are you setting IPEN in RCON....

    The interrupt priority feature is enabled by setting the IPEN bit (RCON<7>). When interrupt priority is enabled, there are two bits which enable interrupts glo- bally. Setting the GIEH bit (INTCON<7>) enables all interrupts that have the priority bit set. Setting the GIEL bit (INTCON<6>) enables all interrupts that have the priority bit cleared. When the interrupt flag, enable bit and appropriate global interrupt enable bit are set, the interrupt will vector immediately to address 000008h or 000018h, depending on the priority level. Individual interrupts can be disabled through their corresponding enable bits.
    #9
    Flyingman1975
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2015/05/27 13:47:46
    • Location: Prague, Czech Republic
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 07:02:14 (permalink)
    0
    You are great !! Zes, misteake was unset IPEN bit. I am still greenhorn :)
     
    Thank you very much, thank you all !!
    #10
    pcbbc
    Super Member
    • Total Posts : 926
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 07:07:22 (permalink)
    +1 (1)
    Flyingman1975
    You are great !! Zes, misteake was unset IPEN bit. I am still greenhorn :) Thank you very much, thank you all !!
    NP. To be honest I just read your program, and then read the datasheet. But you are very welcome.
    #11
    Flyingman1975
    Starting Member
    • Total Posts : 79
    • Reward points : 0
    • Joined: 2015/05/27 13:47:46
    • Location: Prague, Czech Republic
    • Status: offline
    Re: LVD interrupt with PIC18F452 in XC8 compiller 2019/04/07 08:31:16 (permalink)
    +1 (1)
    Yes, I studied datasheet, forums, etc.. But if you have little experience, you often dont know what to look for :) 
    #12
    Jump to:
    © 2019 APG vNext Commercial Version 4.5