You are on page 1of 24

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

PIC Timer
What is watchdog timer?

A timer that monitors how long it takes the MCU to complete a scan. Watchdog timers output an error message if the MCU scan takes too long.

Example: Delay of 1 sec using Timer1 The following simple program creates a delay of 1 sec using TIMER1: #include<pic.h> int Count = 0; void main (void) { T1CON = 0b00000001; while (1) { while (! TMR1IF); TMR1IF = 0; Count ++; if (Count == 15) { Count = 0; } }
}

Prescaler and Postscaler

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

TIMER IN PIC
Timers are common features of most microcontroller. In simplified terms a timer is just a register whose value keeps increasing (or decreasing) by a constant rate without the help of the CPU. The CPU can read or write this register any time. It reads it find out how much time has elapsed. The Timer register can have the following bit length

8 bit timers - These can count between between 0-255 16 bit timers - These can count between 0-65536 32 bit timers - These can count between 0-4294967296

A timer has a clock source, for example of the clock source of 10KHz is input to a timer then one increment will take 100uS (micro second). This clock source can be obtained from the CPU clock. The CPU clock of popular MCU ranges from 1 MHz to 20MHz or can be more than 20MHz, this can be sometimes too fast. To help us to solve this problem, their is a thing called prescaler in the MCU. The function of prescaler is to divide the CPU clock to obtain a smaller frequency. Some typicle prescaler division factors: 1. 256 2. 128 3. 64 4. 32 5. 16 6. 8 7. 4 8. 2 9. 1 (Prescaler by-passed) Timers are also called Counters this is because they can be used to count external events. Example 1: Using timer of PIC 18F4520 TIMER0 control rsegister. The details of each bit is given below. Name T0CON Initial Value BIT TMR0ON 1 7 T08BIT T0CS T0SE PSA PS2 PS1 PS0 1 6 1 5 1 4 1 3 1 2 1 1 1 0

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

BIT7 - TMR0ON: Timer0 On, set this to 1 to start the timer. BIT6 - T08BIT: =1 for 8 bit mode and =0 for 16 bit mode. BIT5 - T0CS: Timer0 clock source. =1 for T0CLK pin input i.e. counter mode. Set to 0 for internal instruction clock. BIT4 - T0SE: Used in counter mode only. Please see datasheet for details. BIT3 - PSA: Prescaler Assignment. Set this to 0 to assign prescaler or 1 to by pass it. BIT2 to BIT0 - PS2 to PS0: Prescaler Division factor select PS2 PS1 PS0 0 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 Divide by 2 Divide by 4 Divide by 8 Divide by 16 Divide by 32 Divide by 64 Divide by 128 Divide by 256

// Using FOSC = 20MHZ, Timer 8 bit unsigned char counter=0;//Overflow counter void main() { //Setup Timer0 T0PS0=1; //Prescaler is divide by 256 T0PS1=1; T0PS2=1; PSA=0; //Timer Clock Source is from Prescaler T0CS=0; //Prescaler gets clock from FCPU (5MHz) T08BIT=1; //8 BIT MODE TMR0IE=1; //Enable TIMER0 Interrupt PEIE=1; //Enable Peripheral Interrupt GIE=1; //Enable INTs globally TMR0ON=1; //Now start the timer //Set RB1 as output because we have LED on it TRISB&=0B11111101; 3

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

while(1); }

//Infinited loop

//Main Interrupt Service Routine (ISR) void interrupt ISR() { //Check if it is TMR0 Overflow ISR if(TMR0IE && TMR0IF) { //TMR0 Overflow ISR counter++; //Increment Over Flow Counter if(counter==76) //How to get the 76 value? { if(RB1==0) RB1=1; else RB1=0; counter=0; } //Clear Flag TMR0IF=0; } } How to get the value 76 of counting value? Prescaler = FCPU/256 (Note: FCPU= Fosc/4) As our FCPU=20MHz/4 (We are running from 20MHz XTAL) =5MHz Time Period = 0.2uS Prescaller Period = 0.2 x 256 = 51.2uS Overflow Period = 51.2 x 256 = 13107.2 uS = 0.0131072 sec So we need 1/0.0131072 Over Flows to count for 1 sec = 76.2939 Overflows ANOTHER CALCULATION METHOD OF TIMER VALUE Example: For controlling RC Servo motor using PIC 16F690 The principal we use here is to set the TMR0 register to overflow every 0.1 ms and set our own counter variable (pulse_max) to count up to maximum 200; this will give us the constant 20 ms period which is the same as 50 Hz frequency required by most servo motor in the market at present. //Reset Counter

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

The TIMER0 period could be calculated using this formula bellow: TIMER0 period = [(TMR0 + 1)] x 4 x Tosc x (TIMER0 prescaler value) By selecting the TIMER0 prescaler of 2; PS2=0, PS1=0 and PS0=0 bits in OPTION_REG register and Initial the TMR0 register value to 156 (99 more counts to reach its maximum value of 255) with the system frequency clock of 8 Mhz. the PIC microcontroller TIMER0 overflow period can be calculated as follow: TIMER0 period = [((255 - 156) + 1)] x 4 x 1/8000000 x 2 = 0.0001s = 0.1 ms Sample Code /* Init TIMER0: Period: Fosc/4 x Prescaler x TMR0 0.0005 ms x 2 * 100 = 0.1 ms */ OPTION = 0b00000000; // 1:2 Prescaller TMR0 = 156; // Interupt every 0.1 ms T0IE = 1; // Enable interrupt on TMR0 overflow GIE = 1; // Global interrupt enable
..

// ISR Routine if(T0IF) { // TIMER0 Interrupt Flag pulse_max++; // Pulse Max Increment pulse_top++; // Pulse Top Increment /* MAX_VALUE=200 turn off the pulse */ if (pulse_max >= MAX_VALUE) { pulse_max=0; pulse_top=0; RC2=0; // Turn Off RC2 } if (pulse_top == top_value) { RC2=1; // Turn On RC2 } TMR0 = 156; // Initial Value for 0.1ms Interrupt T0IF = 0; // Clear TIMER0 interrupt flag } 5

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

How to calculate the value of 156 or the value 99 of the TMR0? We have: TIMER0 period = [(TMR0 + 1)] x 4 x Tosc x (TIMER0 prescaler value) TMR0 = {TIMER0 period/(4 x Tosc x TIMER0 prescaler value)} 1 TMR0 = {0.1ms / (4 x (1/8MHz)) x 2)} 1 = 99 TMR0 = {0.0001s / (4 x (1/8000000) x 2)} - 1= 99 (Count value) The value input to the TMR0 register = Max timer value Count = 255 99 = 156 THE DECLARATION AT THE BEGINNING OF A PIC PROGRAM This part can also call the Device Configuation Bits

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

CCSC REVIEW FOR TIMER USE


Timer0

These options lets the user configure and use timer0. It is available on all devices and is always enabled. The clock/counter is 8-bit on pic16s and 8 or 16 bit on pic18s. It counts up and also provides interrupt on overflow. The options available differ and are listed in the device header file. Relevant Functions: setup_timer_0(mode) set_timer0(value) or set_rtcc(value) Sets the source, prescale etc for timer0 Initializes the timer0 clock/counter. Value may be a 8 bit or 16 bit depending on the device. value=get_timer0 Relevant Preprocessor: None Relevant Interrupts : INT_TIMER0 or INT_RTCC Relevant Include Files: None, all functions built-in Relevant getenv() parameters: TIMER0 Example Code: 7 Returns 1 if the device has timer0 Interrupt fires when timer0 overflows Returns the value of the timer0 clock/counter

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

For PIC18F452 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_2|RTCC_8_BIT);// sets the internal clock as source //and prescale 2. At 20Mhz timer0 //will increment every 0.4us in this //setup and overflows every //102.4us set_timer0(0); time=get_timer0(); //this sets timer0 register to 0 //this will read the timer0 register value Timer1 These options lets the user configure and use timer1. The clock/counter is 16bit on pic16s and pic18s. It counts up and also provides interrupt on overflow. The options available differ and are listed in the device header file. Relevant Functions: setup_timer_1(mode) set_timer1(value) value=get_timer1 Relevant Preprocessor: None Relevant Interrupts: INT_TIMER1 Relevant Include Files: None, all functions built-in Relevant getenv() parameters: TIMER1 Example Code: For PIC18F452 setup_timer_1(T1_DISABLED); or setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //sets the internal clock as source 8 //disables timer1 Returns 1 if the device has timer1 Interrupt fires when timer1 overflows Disables or sets the source and prescale for timer1 Initializes the timer1 clock/counter Returns the value of the timer1 clock/counter

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

//and prescale as 8. At 20Mhz timer1 will increment //every 1.6us in this setup and overflows every //104.896ms set_timer1(0); time=get_timer1(); //this sets timer1 register to 0 //this will read the timer1 register value Timer2 These options lets the user configure and use timer2. The clock/counter is 8bit on pic16s and pic18s. It counts up and also provides interrupt on overflow. The options available differ and are listed in the device header file. //At 20Mhz timer2 will increment every .8us in this Relevant Functions: setup_timer_2 (mode,period,postscale) set_timer2(value) value=get_timer2 Relevant Preprocessor: None Relevant Interrupts: INT_TIMER2 Relevant Include Files: None, all functions built-in Relevant getenv() parameters: TIMER2 Example Code: For PIC18F452 setup_timer_2(T2_DISABLED); or setup_timer_2(T2_DIV_BY_4,0xc0,2); //sets the prescale as 4, period as 0xc0 and postscales as 2. //setup overflows every 154.4us and interrupt every 308.2us 9 //disables timer2 Returns 1 if the device has timer2 Interrupt fires when timer2 overflows Disables or sets the prescale, period and a postscale for timer2 Initializes the timer2 clock/counter Returns the value of the timer2 clock/counter

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

set_timer2(0); time=get_timer2();

//this sets timer2 register to 0 //this will read the timer1 register value

Example:
#include <16f877A.h> #include <def_877A.h> #fuses HS,NOWDT,NOPROTECT,NOLVP #use delay(clock=20000000) #define #define #define #define DIR_LEFT RC0 EN_LEFT RC1 DIR_RIGHT RC3 EN_RIGHT RC2 EN_RIGHT=0; } void forward() { motor_left_forward(); motor_right_forward(); } // Backward void reverse() { motor_left_reverse(); motor_right_reverse(); } void stop() { motor_left_stop(); motor_right_stop(); } void turn_left() { motor_left_forward(); motor_right_reverse(); // or can be motor_right_stop(); } void turn_right() { motor_left_reverse(); // hoc motor_left_stop(); motor_right_forward(); } // Main program void main () { TRISC=0x00; // PORTC control motor TRISD=0x00; // PORTD input sensor signals PORTC=0x00; // Initial value of PORTC while(1) { switch (SENSOR) { case 0b00011000: forward(); break; case 0b00001100: turn_left(); break; case 0b00000110: turn_left(); break; case 0b00000011: turn_left(); break; case 0b00000001: turn_left(); break; case 0b00110000: turn_right(); break;

#define SENSOR PORTD // Declare sub programs void motor_left_forward(); void motor_left_reverse(); void motor_left_stop(); void motor_right_forward(); void motor_right_reverse(); void motor_right_stop(); void forward(); void reverse(); void stop(); void turn_left(); void turn_right(); // Sub program void motor_left_forward() { DIR_LEFT=1; // CW EN_LEFT=1; // Enable } void motor_left_reverse() { DIR_LEFT=0; // CCW EN_LEFT=1; // Enable } void motor_left_stop() { EN_LEFT=0; // Stop } void motor_right_forward() { DIR_RIGHT=1;// CW EN_RIGHT=1; } void motor_right_reverse() { DIR_RIGHT=0; EN_RIGHT=1; } void motor_right_stop() {

10

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

case 0b01100000: turn_right(); break; case 0b11000000: turn_right(); break;

case 0b10000000: turn_right(); break; }} }

USING PWM IN PIC i vi iu khin tc ng c DC trong robot, phng php c s dng ph bin nht l iu bin rng xung (Pulse Width Modulation) hay c gi tt l iu xung, bm xung hoc PWM. Nguyn l ca phng php ny l bt tt nhanh ngun in cp vo ng c to ra mt tn hiu xung. Khi vic bt tt tn s ln (thng s dng t 1kHz n 20kHz), ng c s chy vi 1 tc n nh nh moment quay. Thi gian cp ngun cho ng c l T-on, thi gian tt ngun ng c l T-off. Vic thay i thi gian T-on v T-off lm thay i in p hiu dng cp cho ng c. i vi ng c DC, tc ng c tng i t l thun vi in p cp cho ng c. V vy, bng cch thay i rng ca xung, ta thay i c tc ca ng c DC. i lng biu din mi quan h gia T-on v T-off c gi l duty cycle

Example: PWM SIGNAL BY PIC 16F877 setup_timer_2 (mode, period, postscale) mode: T2_DIV_BY_1, T2_DIV_BY_4, T2_DIV_BY_16

period: 0-255 postscale: 1

o setup_ccp1(mode) v setup_ccp2(mode) mode: CCP_PWM: chn ch PWM. CCP_OFF: tt ch PWM. o set_pwm1_duty(value) v set_pwm2_duty(value) Nu value l gi tr kiu int 8bit:

Nu value l gi tr long int 16bit: 11

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

Example: Ta mun iu xung PWM vi tn s 10kHz vi tn s thch anh (fosc) s dng l 20MHz (value 8bit).

Vi mode = [1,4,16] v period = 0-255 ta c th chn o mode = 4; period = 124 o mode = 16; period = 32 cho vic iu xung c mn (chn c nhiu gi tr duty cycle) ta chn mode = 4 v period = 124. Nh vy, duty_cycle t 0% n 100% ta cho value t 0 n 125.

void left_motor_forward(int value) { MOTOR_LEFT_DIR=0; setup_timer_2(T2_DIV_BY_4,124,1); // Dieu xung 10kHz setup_ccp2(CCP_PWM); set_pwm2_duty(value); } void right_motor_forward(int value) { MOTOR_RIGHT_DIR=0; setup_timer_2(T2_DIV_BY_4,124,1); // 10kHz PWM setup_ccp1(CCP_PWM); set_pwm1_duty(value); } void left_motor_reverse(int value) { MOTOR_LEFT_DIR=1; setup_timer_2(T2_DIV_BY_4,124,1); // 10kHz PWM setup_ccp2(CCP_PWM); set_pwm2_duty(value); } void right_motor_reverse(int value) { MOTOR_RIGHT_DIR=1; setup_timer_2(T2_DIV_BY_4,124,1); // 10kHz PWM setup_ccp1(CCP_PWM);

set_pwm1_duty(value); } void left_motor_stop() { setup_ccp1(CCP_OFF); } void right_motor_stop() { setup_ccp1(CCP_OFF); }

12

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

INTERRUPTS TYPICLE TYPES


There are 2 methods for communicating between the microcontroller and the external system:

POLLING INTERRUPTS

POLLING _ In this method, microcontroller accesses at the exact time interval the external device, and gets the required information. _ The time periods is determined by user. In fact, you can say that, when using the method - polling, the processor must access the device itself and request the desired information that is needed to be processed. _ In this method, there is no independence for the external systems themselves. They depend on the microcontroller. The processor may only access the external device and get from it the information needed. _ The main drawback writing program that uses this method is a waste of time. The micro needs to wait and review whether a new information arrived. INTERRUPTS Interrupt is the signal sent to the micro to mark the event that requires immediate attention. Interrupt is asking" the processor to stop to perform the current program and to make time to execute a special code. In fact, the method of interrupt defines the option to transfer the information generated by internal or external systems inside the micro by them self! Once the system has finished the task imposed on it, the processor will be notified that it can access and receive the information and use it.

INTERRUPT SOURCES External hardware devices are sending interrupts to microcontroller in order to receive the treatment. The micro can send to itself an interrupt as a result of executing the code to report the failure in the process. In the multi-processor system, processors can send interrupts to each other as communication between them, for example for the division of work between them. There are two types of interrupts: software interrupts and hardware interrupts. 13

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

SOFTWARE INTERRUPTS Software interrupts come from a program that runs by the processor and request the processor to stop running the program, go to make a interrupt and then to return to continue to execute the program.

Example: Procedure - when there is a procedure call, the processor stops the execution of the program, jumps to the place in memory that reserved for a procedure executes the procedure and only then returns back to the program and continues to execute.

HARDWARE INTERRUPTS The hardware interrupts are sent to the microcontroller by external hardware devices. Some of the interrupts can be blocked = (masking) by Interrupt Enable bit (IE). When the interrupt is blocked the micro does not see the request for an interrupt, therefore wont be available to execute it. The blocked interrupt wont be executed till the block is removed. There are interrupts that can not be blocked. These are used to report on critical hardware issues, such as the drop of voltage. We want an immediate response from the microcontroller to these kind interrupts, without the ability to ignore them.

Example:

PIC16F877 INTERRUPTS 14

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

The microcontroller has 14 interrupt sources XXIF is an interrupt flag that shows the result that we are getting from an interrupt. XXIE is an interrupt enable bit that is used to enable or block the interrupt. The interrupts on the left side of the figure are low priority and all of them together can be blocked by enabling bit interrupt PEIE = 0. We can determine whether or not to allow the system to address the interrupts. This is done by using Global Interrupt Enable bit GIE.

Examples:

EEIF - EEPROM Write Operation Interrupt Flag bit This flag bit appears in the memory components such Data EEPROM and Flash Program Memory located inside the PIC. The interrupt flag EEIF is used to show that the writing process to the memory is completed. In order to write to the memory again, the EEIF must be cleared in the software. PSPIF Parallel Slave Port Read/Write Interrupt Flag bit This interrupt flag appears when we are utilizing PORTD. PORTD operates as an 8-bit wide Parallel Slave Port (PSP), or microprocessor port, when control bit PSPMODE (TRISE<4>) is set. Interrupt flag PSPIF is designed to inform that the operation of reading/writing from/to PORTD is ended. ADIF - A/D Converter Interrupt Flag bit This interrupt flag associated with A/D component (converting information from analog to digital). A/D Converter Interrupt Flag bit is set when the A/D conversion completed and the result has already saved in the register ADRESH: ADRESL. The flag is intended to flag processor, that the A/D converter is free and can start making a new conversion. RCIF - USART Receive Interrupt Flag bit This interrupt flag appears in the USART unit when it is used as a receiver. The flag RCIF marks an action of receiving the information. When the information that was sent, reached the receiver, its saved first at the shift RSR register. Immediately after that, the information is moved to RCREG register. After the information is moved to RCREG register, the USART Receive Interrupt Flag bit - RCIF is set (RCIF=1); This flag lets processor know that RCREG register is full and can not receive more information; the new information will overwrite the existing information that is stored inside the RCREG register.

15

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

The flag is cleared by the hardware automatically, as soon as the information stored inside the RCREG register is moved into the PIC microcontroller and the register is empty. This marks that processor can receive a new information. TXIF - USART Transmit Interrupt Flag bit This interrupt flag appears in the USART unit when it is used as a transmitter. The flag marks that the transmitter is ready to receive new information for transmission. The information/data we want to transmit is stored in the temporary register TXREG, where the information/data waits to be moved to the register TSR, from where it will be transmitted. When the register TXREG is empty,the USART Transmit Interrupt Flag bit is set (TXIF = 1). This flag lets processor know that a new/additional information/data can be uploaded to the TXREG register for the transmission. As long as TXREG register contains information/data the TXIF flag will be zero (TXIF=0), to mark that the USART transmit buffer is full. SSPIF - Synchronous Serial Port (SSP) Interrupt Flag bit This interrupt flag appears in the Master Synchronous Serial Port (MSSP) module, which is a serial interface, useful for communicating with other peripheral or microcontroller devices. These peripheral devices may be serial EEPROMs, shift registers, display drivers, A/D converters, etc. CCP1IF - CCP1 Interrupt Flag bit This interrupt flag appears in the Capture / Compare Module. TMR2IF - TMR2 to PR2 Match Interrupt Flag bit This interrupt flag appears in the TIMER2 module. This module can be used as a counter or as frequency divider, depends on the values of TMR2 and PR2 registers. Register TMR2 contains the initial value for the counting, and register PR2 contains the final value for the counting. When the value of the register TMR2 is equal to the value of the register PR2, the flag TMR2IF is set to indicate the end of counting. TMR1IF - TMR1 Overflow Interrupt Flag bit This interrupt flag appears within timer module TIMER1. Timer1 can operate in one of two modes: as a timer as a counter 16

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

The operating mode is determined by the clock select bit, TMR1CS (T1CON<1>).The TMR1 register pair (TMR1H:TMR1L) increments from 0000h to FFFFh and rolls over to 0000h and starts the counting again. 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 enable bit, TMR1IE (PIE1<0>). CCP2IF - CCP2 Interrupt Flag bit This interrupt flag appears in the Capture / Compare Module BCLIF - Bus Collision Interrupt Flag bit This interrupt flag appears in MSSP. The Master Synchronous Serial Port (MSSP) module is a serial interface, useful for communicating with other peripheral or microcontroller devices. T0IF This interrupt flag appears in timer/counter module TIMER0. The TMR0 interrupt is generated when the TMR0 register overflows from FFh to 00h. This overflow sets bit TMR0IF (INTCON<2>). Bit TMR0IF must be cleared in software by the Timer0 module Interrupt Service Routine before re-enabling this interrupt. INTF - RB0/INT External Interrupt Flag bit This is universal external interrupt flag bit. The external interrupt can appear through the pin RB0 of PORTB, when PORTB set as an input. RBIF - RB Port Change Interrupt Flag bit This flag is set if at least one of the RB7:RB4 pins changed state. In order to enable this interrupt, pins RB7: RB4 must be defined as input. INTERRUPT FUNCTION This function is a special function. So far functions were called from the main program or other functions. The interrupt functions is called by the hardware itself and operates by following steps:
1.

After the interrupt function is loaded into the memory, the function waits for a moment for the interrupt to occur;

17

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

2.

When the interrupt has occurred, the operating system stops the execution of the main function and free itself to perform the interrupt function;

3.

After the execution of the interrupt function, the operating system continues to run the main function from the place it stopped before the interrupt has occurred.

Example:

Lets write an example program, where we will explore the way we need to define the external interrupt connected to PORTB pin RB0. The program will play a sound from a buzzer that is located on the PIC development board, every time there is an external interrupt that is generated through RB0 pin. To make our live trouble-free we will not connect any external device to the PORTB0. Instead, we will create an external interrupt using our program itself, by incrementing the value of PORTB by 1. As explained above, when there is a logic change in the pin RB0, from "0" to "1", the external interrupt flag will be set INTF = 1. In this section of code, we will increase the value of PORTB by 1 (PORTB + + ). When we are increasing the value by 1, the last bit (LSB) will vary each cycle of the program from "0" to "1" and vice versa. Thus, any change in the last bit (which is actually a change to the pin RB0) from "0" to "1" will cause the INTF flag to be set. The change in the value in the last bit of PortB is described in the table below: 00000000 00000001 00000010 00000011 00000100 00000101 00000110 00000111 a change from 0 to 1 a change from 0 to 1 a change from 0 to 1

a change from 0 to 1

18

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

#include <pic.h> int i=0; int j=0; //---Function Definition----interrupt beep(void) // Interrupt function definition { if (INTF) // If the external flag is 1, do ..... { INTF=0; // Reset the external interrupt flag for(i=0;i<300;i++) // A loop for creating a beep sound { RA4=0; // By setting LED RA4 high and low... RC2=1; // the will blink for(j=0;j<10;j++); // By setting the RC2 high and low ... RA4=1; //freguently, a sound is generated RC2=0; for(j=0;j<10;j++); } } } //--Main Funtion -void main(void) { TRISA=0; // Ser PORT A as an OUTPUT TRISB=0; // Set PORT B as an OUTPUT TRISC=0; // Set PORT C as an OUTPUT PORTC=0; // Set the value of PORT C to 0 INTF=0; // Reset the external interrupt flag INTE=1; // Enable external interrupts from RB0 GIE=1; // Global interrupt enable while(1) { PORTB++;// Increase the PORT B value by 1. for(i=0;i<30000;i++); // a simple delay } }

19

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

Example:

20

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

Example:

21

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

22

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

23

Generated by Foxit PDF Creator Foxit Software http://www.foxitsoftware.com For evaluation only.

24

You might also like