TIMER1 intterupt of PIC 16f88

(instructions, reset, WDT, specifications...) PIC12F6xx, PIC16Fxxx, PIC16F6x, PIC16F7x

TIMER1 intterupt of PIC 16f88

Postby mahmuttuncer » Tue May 05, 2015 8:48 am

Hi;
I'm trying to work ultrasonic sensor (HCSR-04) with PIC16f88 since other interrutps (RB0 and INT0) were used I'm trying to opertare 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.


Code: Select all
#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;                       // TMR1 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
 
  }
}
mahmuttuncer
 
Posts: 1
Joined: Tue May 05, 2015 8:39 am

Re: TIMER1 intterupt of PIC 16f88

Postby ric » Thu May 07, 2015 11:47 pm

Note, I have moved your post from the "14-Bit Core (enhanced)" forum, to the "14-Bit Core" forum, because the PIC16F88 is not part of the enhanced family.
I have also edited your post to put "code" tags around your code to make it more readable.

Now, some comments on your code.
Do NOT enable interrupts until you have finished initialising all your peripherals.
"INTCON = 0b11000000; " sets the GIE bit, which is exactly the same as what "ei();" does.

You will never get a Timer-1 overflow for two reasons:
[1] you are zeroing TMRH and TMRL inside your main loop. That should be BEFORE the "while(1)"
[2] you haven't turned the timer on. The only place you set the TMR1ON bit is inside the interrupt service, but you will never get there if the timer isn't running.

Also, do go to the effort of keeping your comments matching your code.
Leaving an incorrect comment just confuses anyone reading your code, and yourself too.
e.g.
Code: Select all
  PIR1bits.TMR1IF=0;                    //Clear PORTB On­Change Interrupt flag
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: TIMER1 intterupt of PIC 16f88

Postby ric » Wed May 13, 2015 1:41 am

It appears that mahmuttuncer hasn't bothered checking back since 30 minutes after asking this question. :(
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: TIMER1 intterupt of PIC 16f88

Postby jtemples » Thu May 14, 2015 6:31 am

Likewise with his post on the Microchip forum: http://www.microchip.com/forums/m861806.aspx
jtemples
Verified identity
 
Posts: 195
Joined: Sun May 25, 2014 2:23 am
Location: The 805
PIC experience: Professional 5+ years with MCHP products


Return to 14-Bit Core

Who is online

Users browsing this forum: No registered users and 2 guests

cron