• AVR Freaks

TMR1 interrupt of PIC16f88

Author
mahmuttuncer
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2015/05/05 00:31:47
  • Location: 0
  • Status: offline
2015/05/05 00:54:32 (permalink)
0

TMR1 interrupt of PIC16f88

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 everything seems OK. I can see trigger and corresponding echo (when distance changed echo responds). 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
 
 }
}



#1

1 Reply Related Threads

    DominusT
    Super Member
    • Total Posts : 1290
    • Reward points : 0
    • Joined: 2005/07/22 08:31:18
    • Status: online
    Re: TMR1 interrupt of PIC16f88 2015/05/06 13:19:43 (permalink)
    0 (1)
    I think you should turn the timer on the main function.
     
    void main(void)
    {
    ....

    ....
    T1CONbits.TMR1ON = 1; // Start Timer
    }

    #2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5