TIMER1 intterupt of PIC 16f88 in C


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2015
    Posts
    1

    Default TIMER1 intterupt of PIC 16f88 in C

    Hi;
    I'm trying to work ultrasonic sensor (HCSR-04) with PIC16f88. Since other interrutps (RB0 and INT0) were used I'm trying to operate HCSR04 with Timer1.When I connected trigger and echo to oscillator everyting seems OK. I can see trigger and corresponding echo related with distance. I think interrupt never occurs. When I read data sheet of microcontroller I understand as RB6 is related with TMR1 interrupt. Here is my code.


    #include <xc.h>

    #define _XTAL_FREQ 8000000 // crystal freq defined as 8 Mhz
    int b;

    void main()

    {

    OSCCON = 0b01111000;// 8 Mhz selected Internal Oscillator Freq

    INTCON = 0b11000000; // GIE and PEIE is enabled

    TRISB = 0b01000000;// RB6 selected as input(echo connected here)

    T1CON = 0b01001000; // // System clock is derived from Timer1 oscillator with
    // 1:1 prescale value and oscillator is enabled

    PIE1bits.TMR1IE=1; // tmr1 overflow is enabled


    ei(); // interrupt is enabled


    while(1)

    {

    TMR1H = 0; //Setting Initial Value of Timer
    TMR1L = 0; //Setting Initial Value of Timer
    b = 0; // TM1 value is taken in b (look at interrupt at below)

    RB5=1;
    __delay_us(10); //trigger is sent
    RB5=0;
    __delay_ms(100); //Waiting for ECHO








    if(b>2 && b<=20) // If measured distance is between 2-20 cm led
    // at RB1 blinks
    {
    RB1=1;
    __delay_ms(500);
    RB1=0;
    }

    if(b>20 ) // If measured distance is higher than 20 cm led // at RB2 blinks
    {
    RB2=1;
    __delay_ms(500);
    RB2=0;
    }

    } // while closed

    }// main closed

    void interrupt echo()
    {

    if(PIR1bits.TMR1IF == 1) // Code enters if when flag condition occured

    {
    //PIE1bits.TMR1IE=0; // I also tried with that but it didnít work

    T1CONbits.TMR1ON = 1; //Start Timer

    if(RB6 == 0) //If ECHO is LOW
    {
    T1CONbits.TMR1ON = 0; //Stop Timer
    b = (TMR1L | (TMR1H<<8))/58; //Calculate Distance
    }



    PIR1bits.TMR1IF=0; //Clear PORTB On*Change Interrupt flag
    //PIE1bits.TMR1IE=1;// Enable is opened if it is closed at top

    }
    }



  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: TIMER1 intterupt of PIC 16f88

    There are a few here that use "C", but the most of us use Pic Basic. I suggest you try over at the MicroChip forums.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,368


    Did you find this post helpful? Yes | No

    Default Re: TIMER1 intterupt of PIC 16f88 in C

    timer1 is not turned on until the interrupt fires therefore it can't trigger an interrupt. the whole concept is flawed
    why no just use portb int on change (its available on a pic16f88 ) portb.rb4:7

Similar Threads

  1. help hpwm in pic 16F88
    By selim in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 25th March 2010, 18:51
  2. DT inst. interrupt & Timer0 with pic 16F88
    By aratti in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 17th December 2009, 22:23
  3. timer1 help
    By robertpeach in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th September 2009, 10:05
  4. 16F88 code locking pic ?
    By ruijc in forum General
    Replies: 11
    Last Post: - 3rd December 2007, 00:41
  5. Stepper motor control using PIC 16F88
    By tonykeys in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 3rd February 2006, 21:09

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts