Pages

How To Program PIC Microcontroller Interrupt in C

We were using polling technique before in which we were waiting for some event to happen, so in that way we were consuming a lot of CPU time by keeping CPU. But in Interrupt method we can do other work and when interrupt happens we may serve that interrupt according to it’s need or importance. 

PIC Microcontroller has following types of Interrupt-

  1. Timer Interrupt
  2. Serial Interrupt
  3. External Interrupt
  4. Port B Interrupt
In all these Interrupts you 1st need to check if an interrupt occurred, then you need to check which type of Interrupt occurred and you need to write Interrupt Subroutine
The following steps are same in this type of Interrupt–
  1. Assign Interrupt to that location where it has Highest Priority(0×08) and when interrupt occurs Program Counter should jump to that location(0×08)
    #pragma code in=0×08
    in()
    {
    _asm
    GOTO chk
    _endasm
    }
    #pragma code
  2. Check which type of Interrupt Occurred and Jump to that Interrupt Subroutine
    #pragma interrupt chk
    void chk()
    {
    if(INTCONbits.TMR0IF==1)
    T0();
    if(PIR1bits.TMR1IF==1)
    T1();
    }
    You can download Interrupt files from here…. ENJOY……

PIC Timer Programming In C

So far we are able to provide delay by using for loop, Now in this tutorial we are going to learn how to program PIC Timer in C language. PIC Microcontroller(18f4550) has 4 Timers – Timer 0,Timer 1,Timer 2,Timer 3
Many of PIC 18 timers are 16 bit wide. PIC 18 has 8 bit Architecture so 16 bit timer is accessed as two separate registers of low byte(TMRxL) and high byte(TMRxH) where x=0,1,2,3. First of all we are going to talk about Timer0. Timer0 can be used as 8-bit or 16-bit timer. 

  1. T0CON register is used to control Timer0. You don’t need to learn any of registers. These are in given in the Datasheet of PIC Microcontroller.t0con
  2. PIC Microcontroller first divide the crystal frequency by 4 before further use for timers. Let’s say I am using 10MHZ crystal then PIC Microcontroller divide this frequency by 4 i.e. 10/4 = 2.5MHZ. So my one machine cycle is 1/2.5MHZ = .4 Micro sec
  3. Steps to Program Timer0
    1. Load T0CON register with selected prescaler As I told  you PIC Microcontroller divides crystal frequency by 4 then prescale further divide this frequency more. let’s say I select prescale as 001 so I divide crystal frequency by 4 more 2.5/4 = 0.625
    2. Set values for TMR0L and TMR0H according to your delay
    3. Timer on
    4. Keep monitoring TMR0IF bit. TMR0IF bit becomes one when Timer0 reaches value FFFFH
    5. When TMR0IF bit becomes 1 Stop Timer
    6. Clear TMR0IF
  4. In the following Program Led is blinking and you have two different delays which can be choose with a switch 

PIC(18F) Microcontroller Programming in C (I/O Interfacing)

This is 2nd tutorial on I/O programming of PIC Microcontroller. In this tutorial we are going to learn how to make delay program in PIC Microcontroller using integer values. The following program sends values from zero to 100 on Port B with some delay.

  1. In this program you can change delay interval by changing the value 1000 in delay function
    #include  Header File attached to Project
    void delay();                     Initialize delay function
    void main()
    {
    unsigned int i;                  Initialize i as integer variableTRISB=0;                              Make Port B as Output
    for(i=0;i<=100;i++)
    {
    PORTA=i;                             Send value of i at Port B
    delay();                                Call delay function
    }
    }

    void delay()
    {
    unsigned int j;                    Make j integer variable
    for(j=0;j<1000;j++);           Delay
    }
  2. If I need different delay intervals I can make a variable delay function as follow:
    void delay(unsigned int value)
    {
    unsigned int j;
     Make j integer variable
    for(j=0;j<value;j++); Delay
    }
    so I can call this function by writing void delay(1000);
  3. One thing you need to know about Microcontrollers is that Microcontrollers only understand Hex data (binary values). Hex data can be write as 0XAA(where 0X means it’s hex data). So if I want to send Hex Data on Port i  can do it as follow:
    void main()
    {
    unsigned int i; 
    Initialize i as integer variable
    unsigned char z[] = “0X55,0XAA,0X00”; Hexadecimal data
    TRISB=0; Make Port B as Output
    for(i=0;i<=2;i++)
    {
    PORTA=z[i];
     Send value of i at Port B
    delay(); Call delay function
    }
    }
  4. Keep trying these type of Program. For practice, try to solve the following problem:
    Write a Program which display 0 to 9 counting on 7 segment display.

0 comments:

Post a Comment