Chap 3 4 interrupt-tmro
-
Upload
jay-leong -
Category
Engineering
-
view
165 -
download
0
description
Transcript of Chap 3 4 interrupt-tmro
Continue..
Special Function Registers
Info
TRISC
00100000
PORTC
00100000
void main(){
TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output
PORTC = 0b00000000; //Clear all bits of PORTC
while(1) //infinite loop
{
if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed)
{
PORTC.F2 = 1; //LED1 ON
}
else // got 1 or 5V (button1 not pressed)
{
PORTC.F2 = 0; //LED1 OFF
}
}
}
•Check if input at bit 5 get 0 or 1, in this case button is not press. So bit 5 got 1.
Digital 1 = 5Volt
Continue..
Special Function Registers
Info
TRISC
00100000
PORTC
00000000
void main(){
TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output
PORTC = 0b00000000; //Clear all bits of PORTC
while(1) //infinite loop
{
if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed)
{
PORTC.F2 = 1; //LED1 ON
}
else // got 1 or 5V (button1 not pressed)
{
PORTC.F2 = 0; //LED1 OFF
}
}
}
•Program goes to loop and start again to check the button•This time button is pressed and bit 5 got 0 (TRUE signal)
Digital 0 = 0Volt
Continue..
Special Function Registers
Info
TRISC
00100000
PORTC
00000100
void main(){
TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output
PORTC = 0b00000000; //Clear all bits of PORTC
while(1) //infinite loop
{
if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed)
{
PORTC.F2 = 1; //LED1 ON
}
else // got 1 or 5V (button1 not pressed)
{
PORTC.F2 = 0; //LED1 OFF
}
}
}
•PIC send 5Volt to PORTC bit 5•D1 light up.
Case 8 : 2 LEDs and 2 Buttons
Info
•In this example, there are two buttons that use different circuits. B1 and B2 respectively give the values of 0 and 1 when pressed.
“Animated Gif”
#define sevensegment PORTBvoid main(){ TRISB = 0b00000000; //PORTB as output PORTB = 0b00000000; //Clear PORTB while(1) //infinite loop { //Using dec2bcd.Convert 8 bit decimal to binary code decimal //example: dec2bcd(13) = 00010011, dec2bcd(88) = 10001000 sevensegment = dec2bcd(1); //binary code decimal of no. 1 (00000001) Delay_ms(1000); sevensegment = dec2bcd(2); //binary code decimal of no. 2 (00000010) Delay_ms(1000); sevensegment = dec2bcd(3); //binary code decimal of no. 3 (00000011) Delay_ms(1000); }}
Case 10 : Seven Segment with decoder
“Animated Gif”
Various styles of code programming. Each of them produce the same output.
=
Continue..
#define sevensegment PORTBvoid main(){ TRISB = 0b00000000; //PORTB as output PORTB = 0b00000000; //Clear PORTB while(1) //infinite loop { //Using dec2bcd.Convert 8 bit decimal to binary code decimal //example: dec2bcd(13) = 00010011, dec2bcd(88) = 10001000 sevensegment = dec2bcd(1); //binary code decimal of no. 1 (00000001) Delay_ms(1000); sevensegment = dec2bcd(2); //binary code decimal of no. 2 (00000010) Delay_ms(1000); sevensegment = dec2bcd(3); //binary code decimal of no. 3 (00000011) Delay_ms(1000); }}
#define sevensegment PORTBint i;void main(){ TRISB = 0b00000000; //PORTB as output PORTB = 0b00000000; //Clear PORTB while(1) //infinite loop { for (i=1;i<=3;i++) //using “for loop” { sevensegment = dec2bcd(i); Delay_ms(1000); } }}
#define one 0b00000110#define two 0b01011011#define three 0b01001111#define sevensegment PORTB
void main(){ TRISB = 0b00000000; //PORTB as output PORTB = 0b00000000; //Clear PORTB while(1) //infinite loop { sevensegment = one; //display pattern one Delay_ms(1000); sevensegment = two; //display pattern two Delay_ms(1000); sevensegment = three; //display pattern three Delay_ms(1000); }}
Case 11 : Seven Segment WITHOUT decoder
“Animated Gif”
Continue..
9
Interrupts
• Interrupts are a mechanism of a microcontroller which enables it to respond to some events at the moment they occur, regardless of what microcontroller is doing at the time.
• It provides connection between a microcontroller and environment which surrounds it.
• Each interrupt changes the program flow, interrupts it and after executing an interrupt subprogram (interrupt routine) it return to the point in the main program where control was first transferred.
10
Interrupts – Responding To An Interrupt Request.
Finish Current Instruction
Interrupt line active?
Clear GIE
Stack PC
Goto 004h
Yes
Next Instruction
Interrupt service routine Clear interrupt flag
retfie
No
EventGIE set Interrupt Flag set
Interrupt System
• The first thing the microcontroller does when an interrupt request arrives is to execute the current instruction and then stops the regular program execution.
• As a result, the current program memory address is automatically pushed onto the stack and the default address (predefined by the manufacturer) is written to the program counter.
• The location from where the program proceeds with execution is called an interrupt vector. For the PIC16F877A microcontroller, this address is 0004h. As seen in figure below, the location containing the interrupt vector is passed over during regular program execution.
MikroC recognizes an interrupt routine to be executed as the void interrupt() function. The body of that function, i.e. interrupt routine, should be written by the user.
void interrupt() { // Interrupt routine i++ ; //i.e: Interrupt causes variable i to be incremented by 1}
void main() { TRISx = 0bxxxxxxxx; xxxxxxxxxxxxxxxxxx;}
Continue..
Simplified outline of registers PIC16F877A related to interrupts
Interrupt Registers
14
Interrupt Control
• Control register of an interrupt is called INTCON and can be accessed regardless of the bank selected. Its role is to allow or disallowed interrupts, and in case they are not allowed, it registers single interrupt requests through its own bits.
Continue..
Interrupt control register (INTCONT) bit functionsBit Label Name Function
0 RBIF Port B Change Interrupt Flag Set when any one of RB4 to RB7 changes state
1 INTF RB0 pin Interrupt Flag Wet when RB0 detects interrupt input (i.e Button input)
2 TMR0IF Timer Overflow Interrupt Flag Set when Timer TMR0 rolls over from FF to 00 (255 to 0)
3 RBIE Port B Change Interrupt Enable Set to enable Port B change interrupt
4 INTE RB0 pin Interrupt Enable Set to enable RB0 interrupt
5 TMR0IE Timer Overflow Interrupt Enable Set to enable Timer Overflow interrupt
6 PEIE Data EEPROM Write Interrupt EnableSet to enable interrupt on completion of write operation to non-volatile
data memory
7 GIE Global Interrupt Enable Enable all interrupts which have been selected
• Bit 7 GIE (Global Interrupt Enable bit) Bit which enables or disables all interrupts.1 = all interrupts are enabled0 = all interrupts are disabled
• Bit 6 PEIE (EEPROM Write Complete Interrupt Enable bit) Bit which enables an interrupt at the end of a writing routine to EEPROM1 = interrupt enabled0 = interrupt disabledIf PEIE and PEIF (which is in EECON1 register) are set simultaneously , an interrupt will occur.
• bit 5 TMR0IE (TMR0 Overflow Interrupt Enable bit) Bit which enables interrupts during counter TMR0 overflow.1 = interrupt enabled0 = interrupt disabledIf TMR0IE and TMR0IF are set simultaneously, interrupt will occur.
• bit 4 INTE (INT External Interrupt Enable bit) Bit which enables external interrupt from pin RB0/INT.1 = external interrupt enabled0 = external interrupt disabledIf INTE and INTF are set simultaneously, an interrupt will occur.
Continue..
Continue..
• bit 3 RBIE (RB port change Interrupt Enable bit) Enables interrupts to occur at the change of status of pins 4, 5, 6, and 7 of port B. 1 = enables interrupts at the change of status0 =interrupts disabled at the change of statusIf RBIE and RBIF are simultaneously set, an interrupt will occur.
• bit 2 TMR0IF (TMR0 Overflow Interrupt Flag bit) Overflow of counter TMR0.1 = counter changed its status from FFh to 00h0 = overflow did not occurBit must be cleared in program in order for an interrupt to be detected.
• bit 1 INTF (INT External Interrupt Flag bit) External interrupt occurred.1 = interrupt occurred0 = interrupt did not occurIf a rising or falling edge was detected on pin RB0/INT, (which is defined with bit INTEDG in OPTION register), bit INTF is set.
• bit 0 RBIF (RB Port Change Interrupt Flag bit) Bit which informs about changes on pins 4, 5, 6 and 7 of port B.1 = at least one pin has changed its status0 = no change occurred on any of the pinsBit has to be cleared in an interrupt subroutine to be able to detect further interrupts.
Case 16: Interrupt INT
Special Function Registers
Info
PORTBxxxxxxxx
TRISBxxxxxxxx
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Base on the circuit, the LED at PORTC.F6 is always blinking every 1000ms.•Then, when interrupt is occurred (button interrupt is pressed), the LED2 at PORTC.F7 will turn ON for 5 second. •After 5 second the LED2 is turned OFF and LED1 will back to its normal operation.
PORTCxxxxxxxx
TRISCxxxxxxxx
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
x x x x x x x x
Continue..
Special Function Registers
Info
PORTBxxxxxxxx
TRISBxxxxxxxx
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Execute the code•Define the label according to its registers
PORTCxxxxxxxx
TRISCxxxxxxxx
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
x x x x x x x x
Continue..
Special Function Registers
Info
PORTBxxxxxxxx
TRISBxxxxxxxx
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Execute main function
PORTCxxxxxxxx
TRISCxxxxxxxx
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
x x x x x x x x
Continue..
Special Function Registers
Info
PORTBxxxxxxxx
TRISBxxxxxxxx
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Enable Global Interrupt
PORTCxxxxxxxx
TRISCxxxxxxxx
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x x x x x x
Continue..
Special Function Registers
Info
PORTBxxxxxxxx
TRISBxxxxxxxx
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Enable Interrupt(INT) at RB0/INT
PORTCxxxxxxxx
TRISCxxxxxxxx
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Special Function Registers
Info
PORTBxxxxxxxx
TRISBxxxxxxxx
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Set PORTC as output
PORTCxxxxxxxx
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Special Function Registers
Info
PORTBxxxxxxxx
TRISBxxxxxxxx
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Clear PORTC
PORTC00000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Special Function Registers
Info
PORTBxxxxxxxx
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Set PORTB bit 0 as input, the rest as output.
PORTC00000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Clear PORTB
PORTC00000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Toggle bit 6 at PORTC. Previous value is 0, after toggle the value is 1•LED1 will emit light.•Use the ~ symbol to toggle function
PORTC01000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Wait for 1 second
PORTC01000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Toggle from 1 to 0
PORTC00000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Wait for 1 second
PORTC00000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Toggle from 0 to 1
PORTC01000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Wait for 1 second
PORTC01000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x x x
Continue..
Info
•LED1 keep blinking until button interrupt is pressed.
“Animated Gif”
Continue..
Special Function Registers
Info
PORTB00000001
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•When the button is pressed, register INTF is equal to 1.•Now CPU jump to interrupt subroutine to execute the next instructions.•LED1 is stop blinking
PORTC01000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x 1 x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•In the subroutine, LED2 is ON
PORTC11000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x 1 x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Wait for 5 second
PORTC11000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x 1 x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•LED2 is OFF
PORTC01000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x 1 x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•Clear bit INTF•If INTF not clear, system will always jump to subroutine (Always interrupt)•After clear, CPU will go back to its last position in main function. •This is what we call as return from subroutine.
PORTC01000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x 0 x
Continue..
Special Function Registers
Info
PORTB00000000
TRISB00000001
#define GIE INTCON.F7#define INTE INTCON.F4#define INTF INTCON.F1#define LED1 PORTC.F6#define LED2 PORTC.F7void interrupt (void) //interrupt subroutine{ LED2 = 1; //LED2 ON Delay_ms(5000); //delay for 5 sec LED2 = 0; //LED2 OFF INTF = 0; //clear INTE interrupt flag}void main (void){ GIE = 1; //enable global interrupt INTE = 1; //enable RB0/INT interrupt TRISC = 0b00000000; //PortC as output PORTC = 0b00000000; //Clear PORTC TRISB = 0b00000001; //PortB bit 0 as input PORTB = 0b00000000; //Clear PORTB while(1) //endless loop { LED1 = ~LED1; //toggle LED1 Delay_ms(1000); //Wait 1 sec }}
•So, LED1 back to its original condition (Always Blink)
PORTC01000000
TRISC00000000
INTCONGIE PEIE TMR
0IEINTE RBIE TMR
0IFINTF RBIF
1 x x 1 x x 0 x
Continue..
Info“Animated Gif”
•Base on the circuit, the LED at PORTC.F6 is always blinking every 1000ms.•Then, when interrupt is occurred (button interrupt is pressed), the LED2 at PORTC.F7 will turn ON for 5 second. •After 5 second the LED2 is turned OFF and LED1 will back to its normal operation.
41
Free-Run Timer TMR0
Independent Timer TMR0
The timer TMR0 has a wide range of applications in practice. Only few programs do not use it in some way.
The timer TMR0 module is an 8 bit timer/counter with the following features:‐• 8 bit timer/counter register‐• 8 bit prescaler (shared with Watchdog timer)‐• Programmable internal or external clock source• INTERRUPT ON OVERFLOW (TMR0 is counting from 0 to 255, when 255+1, Overflow happen because the TMR0 register only can hold 8 bit value. So TMR0IF (overflow flag) is equal to 1. At this moment an interrupt is occurred. TMR0 will count again start from 0 until it overflow again and again)• Programmable external clock edge selection
Prescaler register is used to control the speed of TMR0 overflow. We can control how quickly and slowly overflow can occur.
Registers related to TMR0
Prescaler ratio of 1:2 is the fastest and
1:256 is the slowest for TMR0 to overflow
Registers related to TMR0
TMR0 Overflow time (Calculation)
By loading a value into the TMR0 register we can control the count until an overflow occurs. The formula that follows can be used to calculate the time it will take for the timer to overflow (or to generate an interrupt) given the oscillator period, the value loaded into the timer, and the prescaler value:
Overflow time = 4 x TOSC x Prescaler x (256 – TMR0)Where:•Overflow time is in us•TOSC is the oscillator period in us ( i.e: 1 / 4Mhz = 0.25us, 4Mhz is PIC clock)•Prescaler is the prescaler value•TMR0 is the starting count value loaded into TMR0 register
Calculation Continue..
For example, assume that we are using a 131072Hz crystal, and the prescaler is chosen as 1:128 by setting bits PS2:PS0 to 010. Also assume that the value loaded into the timer register TMR0 is decimal 0 (counting from 0). The overflow time is then given by: 131072HZ clock has a period; T = 1/f = 7.6294ususing the above formula Overflow time = 4 x 7.6294 x 128 x (256 – 0) = 1000000us = 1 secThus, the timer will overflow after 1sec, and a timer interrupt will be generated if the timer interrupt(TMR0IE) and global interrupts(GIE) are enabled.What we normally want is to know what value to load into the TMR0 register for a required overflow time. This can be calculated by modifying Equation above as follows: TMR0 = 256 – (Overflow time) / (4 x TOSC x Prescaler)
Overflow time = 4 x TOSC x Prescaler x (256 – TMR0)
Case 17: Interrupt by TMR0
Info
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
•LED1 blinking for 1 second by hardware timer (TMR0).•Clock 131072Hz•Prescaler 1:128•TMRO start count from 0•All the above information cause the microcontroller to do interrupt every 1 second.•Inside the while loop, no such command in it (Nothing to do).•No software delay (i.e: Delay_ms(xx)) used in the programming code.
Case 17: Interrupt by TMR0
Info
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Calculation: Given below•Clock 131072Hz•T = 1/131072 = 7.6294us•Prescaler 1:128•TMRO start count from 0Overflow Time = 4 x 7.6294 x 128 x 256 = 1000000us = 1sec
From the calculation, every 1 second PIC is overflow and jump to interrupt subroutine if GIE = 1 and TMR0IE = 1
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Execute the code•Define the label according to its registers
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
x x x x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x x x x x x x
TMR0x
Register
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Enable Global Interrupt
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x x x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x x x x x x x
TMR0x
Register
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Enable TMR0 interrupt
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x x x x x x x
TMR0x
Register
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Set prescale to 1:128, to slow down the overflow time.
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x x x x 1 1 0
TMR0x
Register
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•User internal clock.•Use 131072Hz as a clock to TMR0
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x x 1 1 0
TMR0x
Register
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Set prescaler to use for TMR0 not for Watchdog.
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0x
Register
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Set prescaler to use for TMR0 not for Watchdog.
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR00
Register
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Setup PORTC as output (LED1)•At this time TMR0 already counting..
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR01
Register
TMRO is counting
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•System do nothing..•Endless loop in looping while
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR02
Register
TMRO is counting
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•System do nothing..•Endless loop in looping while
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR03
Register
TMRO is counting
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•System do nothing..•Endless loop in looping while
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR04
Register
TMRO is counting
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•System do nothing..•Endless loop in looping while
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0Register
TMRO is counting120
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•System do nothing..•Endless loop in looping while
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0Register
TMRO is counting253
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•System do nothing..•Endless loop in looping while
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0Register
TMRO is counting254
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•System do nothing..•Endless loop in looping while
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x x x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0Register
TMRO is counting255
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•System overflow. (Time taken 1 sec)•TMR0 is 8 bit. Only can hold maximum value to 255 (FF or 11111111)•TMR0IF (TMR0 Flag) equal to 1•Interrupt occurred. Jump to subroutine
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x 1 x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0Register
TMRO is counting0
Time taken 1
sec
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Toggle the LED1
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x 1 x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0Register
TMRO is counting1
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Clear TMR0 flag, TMR0IF = 0
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x 0 x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0Register
TMRO is counting2
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Set TMR0 to 0•TMR0 start counting from 0
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x 0 x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0Register
TMRO is counting0
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•Return from interrupt subroutine•CPU back to its previous location (main function)
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x 0 x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0Register
TMRO is counting1
#define GIE INTCON.F7#define TMR0IE INTCON.F5#define TMR0IF INTCON.F2#define PS0 OPTION_REG.F0#define PS1 OPTION_REG.F1#define PS2 OPTION_REG.F2#define PSA OPTION_REG.F3#define T0CS OPTION_REG.F5#define LED1 PORTC.F0void interrupt (void) //interrupt subroutine{ LED1 = ~LED1; //Toggle LED1 TMR0IF = 0; //clear TMR0 interrupt flag TMR0 =0; //TMR0 value start to count from 0}void main (void){ GIE = 1; //enable global interrupt TMR0IE = 1; //enable interrupt type TMR0 PS0 = 0; // use prescaler 1:128 PS1 = 1; PS2 = 1; T0CS = 0; //Use internal clock (Fosc / 4) PSA = 0; //Prescaler is assigned to TMR0 TMR0 = 0; //start value of TMR0 to count TRISC = 0b00000000; //portc as output PORTC = 0b00000000; //clear portc while(1) { //Do nothing.. }}//end of main func
Continue..
Special Function Registers
Info•TMR0 is counting again until overflow•Every time it overflow interrupt is occurred and it will toggle the LED1 every 1 second.•So you can see the LED1 is toggle every 1 second
INTCONGIE PEIE TMR0IE INTE RBI
ETMR0IF INTF RBIF
1 x 1 x x 0 x x
OPTION_REGRBPU INTEDG TOCS TOSE PSA PS2 PS1 PS0
x x 0 x 0 1 1 0
TMR0Register
TMRO is counting2
Continue
Info
•LED1 blinking for 1 second by hardware timer (TMR0).•Clock 131072Hz•Prescaler 1:128•TMRO start count from 0•All the above information cause the microcontroller to do interrupt every 1 second.•Inside the while loop, no such command in it (Nothing to do).•No software delay (i.e: Delay_ms(xx)) used in the programming code.
“Animated Gif”