• AVR Freaks

Timer 0 Interrup on PIC18F87J11

Author
maximus383
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2020/01/12 13:25:27
  • Location: 0
  • Status: offline
2020/01/16 00:01:33 (permalink)
0

Timer 0 Interrup on PIC18F87J11

I made a program which activate timer 0 interrupt using a pre-defined variabiles. Those variabiles (p1, p2) are defined in the function "conf()". And I trying to activate the interruption by using the RB0 button.
 

#include <xc.h>
#define _XTAL_FREQ 4000000
#define p1 0b10001000
#define p2 0b10101010

unsigned char counter=0;
void interrupt f1()
{
 if(TMR0IE && TMR0IF)
  {
   counter++;
  }
if(counter==30)
{
 LATDbits.LATD0=1;
 LATDbits.LATD1=1;
 LATDbits.LATD2=1;
 LATDbits.LATD3=1;
 LATDbits.LATD4=1;
 counter=0;
}
 INTCONbits.TMR0IF=0;
}


void conf()
 {
  T0CON = T0CON|p1;
  INTCON = INTCON|p2;
 }

int main(){
 WDTCONbits.ADSHR=1;
 MEMCONbits.EBDIS=1;
 TRISD=0x0;
 LATB=0x0;
 TRISB=0x01;
 LATA=0x0;
 TRISA=0x20;
 PCFG3=1;
 PCFG2=1;
 PCFG1=1;
 PCFG0=1;
while(1){
 if(RB0==0)
 {
  conf();
 }
 else{LATD=0x0;}

}
}

It's ok like that or I need to change something? 
#1

1 Reply Related Threads

    ric
    Super Member
    • Total Posts : 25493
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Timer 0 Interrup on PIC18F87J11 2020/01/17 14:54:23 (permalink)
    0
    Some comments in your code would be very helpful.
    Otherwise anyone looking at this
    #define p1 0b10001000
    #define p2 0b10101010
    Has to go and refer to the datasheet to figure out what the hell you are trying to do.
     
    p2 is written to INTCON, so you are setting:
    GIE, TMR0IE, RBIE, and INT0IF
    Never, ever enable an interrupt that your ISR does not handle. That will make your code crash, continually running only the ISR whenver that interrupt request flag gets set.
     
    Please explain what you expect this code to do.
    It appears to me, when the RB0 pin is held low, it will very rapidly write to T0CON and INTCON over and over.
    (I have no idea why you are setting RBIE or INT0IF.)
    When RB0 is high, it rapidly clears all of the LATD port over and over.
     
    So, if you pull RB0 low, and hold it low, after 30 timer rollovers, it will raise the RD0-RD3 pins, so long as RB0 is still low.
    (If RB0 is high, they would be set very very briefly, but immediately get cleared by your non interrupt code)
     
    Also note:
    void interrupt f1()
    should be
    void interrupt f1(void)


    and
    int main(){
    should be
    void main(void){


     
     

    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
    Jump to:
    © 2020 APG vNext Commercial Version 4.5