Header Ads

Timer1 Interrupt of PIC16F877A Microcontroller

Welcome to MINA TECHNOLOGY.

In this tutorial we will learn how to how to occur Timer1 Interrupt of PIC16F877A Microcontroller. I previous tutorials we seen how to to occur Timer0 Interrupt. Here I am use Mikro C compiler for code editing. You can watch the video or read the written tutorial below.




Interrupt
Interrupt is a signal Microcontroller emitted by hardware or software indicating and event that needs immediate attention .



Timer1 Module

The Timer1 module is a 16-bit timer/counter consisting of two 8-bit registers (TMR1H and TMR1L), which are readable and writable. The TMR1 Register pair(TMR1H:TMR1L) increments from 0000h to FFFFh control bit TMR1ON (T1CON<0>). and rolls over to 0000h. The TMR1 Interrupt, if enabled, is generated on overflow, which is latched in interrupt flag bit TMR1IF (PIR1<0>). This interrupt can be enabled/disabled by setting/clearing TMR1 interrupt control register. enable bit TMR1IE (PIE1<0>).

Timer1 can operate in one of two modes:

  1. As a timer
  2. As a counter 

Timer1 Interrupt control Registers


T1CON: TIMRER1 CONTROL REGISTER

bit 7-6             Unimplemented: Read as '0' 

bit 5-4            T1CKPS1:T1CKPS0: Timer1 Input Clock Prescale Select bits 
                      11 = 1:8 Prescale value 
                      10 = 1:4 Prescale value 
                      01 = 1:2 Prescale value 
                      00 = 1:1 Prescale value 

bit 3              T1OSCEN: Timer1 Oscillator Enable Control bit 
                     1= Oscillator is enabled 
                     0 = Oscillator is shut-off (the oscillator inverter is turned off to eliminate power drain) 

bit 2               T1SYNC: Timer1 External Clock Input Synchronization Control bit 
                       When TMR1CS = 1:
                      1 = Do not synchronize external clock input 
                      0 = Synchronize external clock input 

                      When TMR1CS = 0:
                     This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0. 

bit 1               TMR1CS: Timer1 Clock Source Select bit 
                      1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge) 
                      0 = Internal clock (FOSC/4)  
  
bit 0               TMR1ON: Timer1 On bit 
                      1 = Enables Timer1 
                      0 =Stops Timer1



PIR1 REGISTER
The PIR1 Register contain individual control bit for the peripheral interrupts.


bit 7            PSPIF(1): Parallel Slave Port Read/Write Interrupt Flag bit 
                   1 = A read or a write operation has taken place (must be cleared in software) 
                   0 = No read or write has occurred 

bit 6           ADIF: A/D Converter Interrupt Flag bit 
                  1 = An A/D conversion completed 
                  0 = The A/D conversion is not complete 

bit 5           RCIF: USART Receive Interrupt Flag bit 
                  1 = The USART receive buffer is full 
                  0 = The USART receive buffer is empty 

bit 4          TXIF: USART Transmit Interrupt Flag bit 
                 1 = The USART transmit buffer is empty 
                  0 = The USART transmit buffer is full 

bit 3          SSPIF: Synchronous Serial Port (SSP) Interrupt Flag 
                  1 = The SSP interrupt condition has occurred, and must be cleared in software before                                  returning from the Interrupt Service Routine. The conditions that will set this bit are: 
                        • SPI 
                               - A transmission/reception has taken place. 
                        •  I2C Slave 
                               - A transmission/reception has taken place. 
                        • I2C Master 
                              - A transmission/reception has taken place. 
                              - The initiated START condition was completed by the SSP module. 
                              - The initiated STOP condition was completed by the SSP module.
                              - The initiated Restart condition was completed by the SSP module. 
                              - The initiated Acknowledge condition was completed by the SSP module.
                              - A START condition occurred while the SSP module was idle (Multi-Master                                           system). 
                              - A STOP condition occurred while the SSP module was idle (Multi-Master                                             system). 
                  0 = No SSP interrupt condition has occurred. 

bit 2            CCP1IF: CCP1 Interrupt Flag bit 
                   Capture mode: 
                   1 = A TMR1 register capture occurred (must be cleared in software) 
                   0 = No TMR1 register capture occurred 

                   Compare mode: 
                   1 = A TMR1 register compare match occurred (must be cleared in software) 
                   0 = No TMR1 register compare match occurred PWM mode: Unused in this mode 

bit 1            TMR2IF: TMR2 to PR2 Match Interrupt Flag bit 
                   1 = TMR2 to PR2 match occurred (must be cleared in software) 
                   0 = No TMR2 to PR2 match occurred 

bit 0            TMR1IF: TMR1 Overflow Interrupt Flag bit 
                   1 = TMR1 register overflowed (must be cleared in software) 
                    0 = TMR1 register did not overflow 

Note 1: PSPIF is reserved on PIC16F873/876 devices; always maintain this bit clear.




Circuit diagram



Timer1 interrupt time is 250 ms. Im use here Timer Calculator for actual interrupt time.

Timer Calculator

Mikro C Code:

//Timer1
//Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 250 ms


void main(){
  TRISB = 0X00;
  PORTB = 0X00;
  T1CON         = 0x31;
  TMR1IF_bit         = 0;
  TMR1H         = 0x0B;
  TMR1L         = 0xDC;
  TMR1IE_bit         = 1;
  INTCON         = 0xC0;
  while(1){
           PORTB.F1 = 1;
           delay_ms(50);
           PORTB.F1 = 0;
           delay_ms(50);
           }
}

void Interrupt(){
  if (TMR1IF_bit){

    TMR1H         = 0x0B;
    TMR1L         = 0xDC;
    PORTB.F0 = 1;
    delay_ms(50);
    PORTB.F0 = 0;
    Delay_ms(50);
    TMR1IF_bit = 0;
    
  }
}




Click the download button for program file:
download







No comments

Theme images by 5ugarless. Powered by Blogger.