• AVR Freaks

Hot!How to trigger an interupt from PIC16F57 timer

Author
Leolabs
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2019/11/08 06:58:15
  • Location: 0
  • Status: offline
2019/11/11 23:36:02 (permalink)
0

How to trigger an interupt from PIC16F57 timer

Hi,I know what the PIC16F57 itself come with Timer0,but not interrupt.Those timer examples found on internet come with using build-in interrupt inside the mcu.
 
What I'm trying to achieve:
  1. Monitor if a switch was pressed(input stay HIGH)
  2. LED will turn ON after 3s
  3. LED will remain ON if switch is still being pressed/latched
  4. if switch input is LOW logic level,then LED will off
The default __delay_ms(3000) method is not practical since switch off the LED also need to wait for 3s if the 'switch on' routine is not completed.
 
I'm using MPLAB XIDE in C language and no way to change the mcu to other model as the PCB is already fixed for PIC16F57.
 
Any method to work around?Thanks!
#1

9 Replies Related Threads

    GeorgeAD
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2019/11/13 10:59:04
    • Location: 0
    • Status: offline
    Re: How to trigger an interupt from PIC16F57 timer 2019/11/13 12:17:47 (permalink)
    0
    Should you set up lets say a Timer (Timer0 for example), to adjust the frequency to either the internal crystal or to an external one, then using the OPTION_REG, initialize your Timer0 and then set up an interrupt handling function (what happens when overflow (interrupt) occurs, example below:


    /** TIMER0 setup **/
    OPTION_REG = 1;
    OPTION_REGbits.PS2 = 0;
    OPTION_REGbits.PSA = 0;
    OPTION_REGbits.TMR0CS = 0;
    INTCONbits.TMR0IE = 1; //Enable timer interrupt bit in PIE1 register
    GIE=1; //Enable Global Interrupt
    PEIE=1;
     
    void timer_isr()
    {
    if(TMR0IF==1) // Timer flag has been triggered due to timer overflow  <- 
    {
    TMR0 = 100; //Load the timer Value
    TMR0IF=0; // Clear timer interrupt flag
    count++;
    }

    if (count == 250)
    {
    PORTAbits.RA0 = 1; // light up a LED during interrupt
    count=0;

    }
    }
     
    Mind that I just started with PIC controllers today and there is a good chance I am talking horse crap.
    #2
    Aussie Susan
    Super Member
    • Total Posts : 3638
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How to trigger an interupt from PIC16F57 timer 2019/11/13 18:04:41 (permalink)
    +1 (1)
    I must admit I am not really clear on what you are trying to do but this may give you an idea.
    Assuming that you have the timer set up to not roll over the 8-bit value from 0xff to 0x00  within your 3 second limit, you could:
    - wait for the switch to be pressed (don't forget to debounce the pin)
    - start the timer from 0
    - enter a loop that checks if the timer value exceeds whatever represents 3 seconds
    - within the loop check to see if the switch is still pressed and break from the loop if it is not
    - outside the loop turn off the LED
    - if necessary wait for the switch to be released before starting all over again
    Susan
    #3
    AMPS
    Super Member
    • Total Posts : 468
    • Reward points : 0
    • Status: offline
    Re: How to trigger an interupt from PIC16F57 timer 2019/11/14 01:28:45 (permalink)
    0
    Its not clear what trying to achieve.
    What compiler u r using.can you elobrate how does it work

    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #4
    1and0
    Access is Denied
    • Total Posts : 10005
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: How to trigger an interupt from PIC16F57 timer 2019/11/14 01:57:48 (permalink)
    +1 (1)
    Leolabs
    What I'm trying to achieve:
    1. Monitor if a switch was pressed(input stay HIGH)
    2. LED will turn ON after 3s
    3. LED will remain ON if switch is still being pressed/latched
    4. if switch input is LOW logic level,then LED will off

    Here's the pseudo code:
     
    1. Check if switch is pressed; if not, go to Step 1.
    2. Switch is pressed, reset counter = 0.
    3. Delay 1 ms and increment counter.
    4. If counter = 3000, decrement counter and turn on LED.
    5. Check if switch is still pressed; if yes, go to Step 3.
    6. Switch is not pressed, turn off LED.
    7. Go to Step 1.
    #5
    Aussie Susan
    Super Member
    • Total Posts : 3638
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: How to trigger an interupt from PIC16F57 timer 2019/11/14 18:13:11 (permalink)
    0
    According to that pseudo code, you only check the switch every 3 seconds and so could be on for up to 3 seconds after the switch is released; if you use mine then the switch is checked each time through the counter loop and so will turn off immediately the switch is released.
    Regardless....
    What is stopping you from coding your pseudo code?
    Susan
    #6
    ric
    Super Member
    • Total Posts : 24638
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: How to trigger an interupt from PIC16F57 timer 2019/11/14 18:24:29 (permalink)
    0
    Which post are you commenting on Susan?
    The "pseudo code" was posted by 1and0 in post#5. That code is checking the switch every one millisecond.
     

    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!
    #7
    AMPS
    Super Member
    • Total Posts : 468
    • Reward points : 0
    • Status: offline
    Re: How to trigger an interupt from PIC16F57 timer 2019/11/14 20:10:00 (permalink)
    0
    i have checked datasheet TIMR0 how it work here. There no information on Interrupt and interrupt handler.
    like other controller GIE/PEIE/ TMR0ON/TMR0OFF no information.

    #include <xc.h>
    #pragma config OSC = HS // Oscillator selection bits (HS oscillator)
    #pragma config WDT = ON // Watchdog timer enable bit (WDT enabled)
    #pragma config CP = OFF // Code protection bit (Code protection off)
    #define _XTAL_FREQ 12000000
    #define LED_G RC4
    #define Switch RC0
    unsigned char SW_Pressed_Flag=0;
    void ISR() {
    if(Switch==1) {
    SW_Pressed_Flag=1;
    } else if((Switch==0)&&(SW_Pressed_Flag==0)) {
    LED_G=0;
    }
    if(SW_Pressed_Flag==1) {
    LED_G=1;
    __delay_ms(3);
    LED_G=1;
    SW_Pressed_Flag=0;
    }
     
    }

    void main(void) {
    PORTA = 0b00000000;
    TRISA = 0b00000000;
    TRISB =0b00000000;
    PORTB = 0b00000000;
    TRISC =0b00000001;
    PORTC = 0b00000000;
    OPTION=0X03;
    TMR0=250;
    while(1) {
    ISR();
    }
    return;
    }


    Amps
    *.*.*.*.*.*.*.*.*.*.*.*.*
    #8
    ric
    Super Member
    • Total Posts : 24638
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: How to trigger an interupt from PIC16F57 timer 2019/11/14 20:44:55 (permalink)
    0
    ajitnayak87
    i have checked datasheet TIMR0 how it work here. There no information on Interrupt and interrupt handler.
    ...

    The OP stated in the very first post that this PIC does not have interrupts.
    That is the whole point of this topic, he is asking how to get a similar effect without them.
     

    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!
    #9
    1and0
    Access is Denied
    • Total Posts : 10005
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: How to trigger an interupt from PIC16F57 timer 2019/11/15 09:30:58 (permalink)
    0
    Aussie Susan
    According to that pseudo code, you only check the switch every 3 seconds and so could be on for up to 3 seconds after the switch is released; if you use mine then the switch is checked each time through the counter loop and so will turn off immediately the switch is released.

    The switch is checked every one millisecond, as Ric said. ;)  
     
    For my previous post, actually there's no need to decrement the counter. Here's another one with faster release:
     
    1. Check if switch is pressed; if not, go to Step 1.
    2. Switch is pressed, reset counter = 0.
    3. Delay 1 ms and increment counter.
    4. If counter = 3000, turn on LED and skip to Step 6.
    5. Check if switch is still pressed; if yes, go to Step 3.
    6. Wait here for switch release.
    7. Turn off LED.
    8. Go to Step 1
    #10
    Jump to:
    © 2019 APG vNext Commercial Version 4.5