PDA

View Full Version : TIMER1 intterupt of PIC 16f88 in C



mahmuttuncer
- 5th May 2015, 09:17
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

}
}

mackrackit
- 5th May 2015, 11:57
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.

richard
- 5th May 2015, 13:46
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