Menu

all about electronic and microcontrollers

Sunday, October 17, 2010

Lesson nr.05-Timers and Interrupts

Hardware setup:
In this experiment we will work with interrupt which are enabled after the timer register TMR1 (TMR1H, TMR1L) overflow occurs.
As result combination of bits will be changing on PORT RB.0~RB.7.
As we know, button S1 and (pull-up) resistance with value of 4.7 K will provide the manual reset mode to the microcontroller.
ICSP connector provides the connection between the programmer (PicKit 2) and microcontroller (PIC16F628A).



Circuit Diagram:
For those who want to build it on their own breadboard or other platform, here is the electronic scheme built in Eagle Cad, free version:


Software:
Here is the C program written for MikroC PRO for PIC 2010 (version v4.15).
/*
'*******************************************************************************
'  Lesson nr.05:
'          Timers and Interrupts
'  Written by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          In this experiment we will work with interrupt which are enabled after
'          the timer register TMR1 (TMR1H, TMR1L) overflow occurs.
'          Combination of bits changing on PORT RB.0~RB.7.
'  Test configuration:
'    MCU:                        PIC16F628A
'    Test.Board:                 WB-106 Breadboard 2420 dots
'    SW:                         MikroC PRO for PIC 2010 (version v4.15)
'  Configuration Word
'    Oscillator:                 INTOSC:I/O on RA.6, I/O on RA.7
'    Watchdog Timer:             OFF
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
unsigned short cnt;                          // Define variable cnt;
void interrupt()
{
 if (PIR1.TMR2IF) {                          // If bit TMR2IF = 1,
                   cnt++ ;                   // Increment variable cnt by 1;
                   PIR1.TMR2IF = 0;          // Reset bit and
                   TMR2 = 0;                 // reset register TMR2;
                   }
}

void main() 
{                                            // Main;
 cnt = 0;                                    // Reset variable cnt;
 CMCON |=7;                                  // All I/O pins are configured as digital;
 TRISB = 0;                                  // All port B pins are configured as outputs;
 PORTB = 0xAA;                               // Logic state on port B pins ;
 T2CON = 0xFF;                               // Set timer T2;
 TMR2 = 0;                                   // Initial value of timer register TMR2;
 PIE1.TMR2IE = 1;                            // Enable interrupt;
 INTCON = 0xC0;                              // Set bits GIE and PEIE;

 while (1) {                                 // Endless loop;
            if (cnt > 10) {                  // Change PORTB after more than 10 interrupts;
                           PORTB = ~PORTB;   // Function Replace inverts the port B state;
                           cnt = 0;          // Reset variable cnt;
                           }                 // End if;
            }                                // End while;
}                                            // End.

1 comment:

  1. Salut Ducu!
    Am de facut un proiect:Led chaser cu PIC16F628A cu 8 leduri si 4 Switch-uri(push button):
    SW1:Prin apasare sa treaca dintr-un joc de lumini in altul;
    SW2:Viteza de executie a jocurilor(lent/normal/rapid);
    SW3:Lumina/umbra=in loc sa se plimbe un led aprins,sa se plimbe umbra pe ledurile aprinse,adik invers.
    SW4:RESET;
    Daca ai timp sa te ocupi de proiectul asta,anunta-ma te rog pe brv_90@yahoo.com
    Mersi fain!

    ReplyDelete

If you do not understand something, or if you make some aplication helped by this blog, let me know.