Skip to main content

timer_OneS_int

Code Breakdown:

1. Preprocessor Directives:

  • #include <pic.h>: Includes the header file pic.h, which contains definitions and declarations specific to PIC microcontrollers.
  • #define _XTAL_FREQ 4000000: Defines a preprocessor constant _XTAL_FREQ with the value 4000000, representing the crystal frequency in Hertz. This value is used by the compiler to calculate delay times and other timing-related calculations.

2. Variable Declarations:

  • unsigned char count;: Declares an unsigned character variable count to keep track of the number of 4ms intervals that have elapsed.

3. main() Function:

  • Initialization:

    • TRISC = 0;: Configures PORTC as output pins.
    • PORTC = 0;: Clears the output pins on PORTC.
    • T0CS = 0;: Selects the internal clock source (Fosc/4) for TMR0.
    • PSA = 0;: Assigns the prescaler to TMR0.
    • PS2 = 1; PS1 = 0; PS0 = 0;: Sets the prescaler to 32, providing a timer tick every 125 microseconds.
    • count = 0;: Initializes the count variable to 0.
    • T0IF = 0;: Clears the TMR0 interrupt flag.
    • T0IE = 1;: Enables the TMR0 interrupt.
    • GIE = 1;: Enables global interrupts.
    • TMR0 = 131;: Initializes TMR0 with the value 131 to generate a 4ms delay.
    • RC0 = 1;: Sets the output on RC0.
  • Main Loop:

    • while (1) { ... }: The main loop continues indefinitely.
    • RC5 = 1; __delay_ms(150); RC5 = 0; __delay_ms(150);: Toggles RC5 with a 150ms delay between states.

4. Interrupt Service Routine (ISR):

  • void interrupt isr() { ... }: Defines the interrupt service routine.
    • if (T0IF == 1) { ... }: Checks if the TMR0 interrupt flag is set.
    • TMR0 = 131;: Reloads TMR0 with the value 131 to start another 4ms delay.
    • count++;: Increments the count variable.
    • if (count == 250) { ... }: Checks if 250 4ms intervals have elapsed (1 second).
      • RC0 = !RC0;: Toggles the RC0 output.
      • count = 0;: Resets the count variable to 0.
    • T0IF = 0;: Clears the TMR0 interrupt flag.

Explanation:

The code implements a simple blinking LED circuit that toggles an output pin (RC0) every second. The main components of the code are:

  • Timer Initialization: TMR0 is configured as an internal timer with a prescaler of 32, providing a timer tick every 125 microseconds. The initial value of TMR0 is set to 131 to generate a 4ms delay.
  • Interrupt Handling: The TMR0 interrupt is used to increment a counter and toggle the output pin after 250 4ms intervals (1 second).
  • Main Loop: The main loop includes a delay loop that toggles another output pin (RC5) every 300ms, independent of the timer-based LED toggling.


#include <pic.h>
unsigned char count;
#define _XTAL_FREQ 4000000


void main()
{
TRISC=0;    // PORTC output
PORTC=0;   

T0CS=0;        // Internal Mode (Fosc/4)
PSA=0;        // Prescaler assigned to TMR0
PS2=1;
PS1=0;
PS0=0;        // Prescaler ratio 1:32
count=0;

T0IF=0;        // Clear TMR0 Overflow int. bit
T0IE=1;        // TMR0 overflow int. enable
GIE=1;        // Global int. enable

TMR0=131;    // (256-125=131) for 4mS time
RC0=1;        // output on
while(1)   
{
RC5=1;
__delay_ms(150);
RC5=0;
__delay_ms(150);

}
}

void interrupt isr()    // interrupt function
{
    if(T0IF==1){
    TMR0=131;   
    count++;       
        if(count==250){        // 0.5Sec?
        RC0=!RC0; // RC0 toggle
        count=0;
        }       
    T0IF=0;
    }

}




Comments

Popular posts from this blog

ANALOGUE to DIGITAL (A/D) Converter in PIC16F87X Series

ANALOGUE to DIGITAL (A/D) Converter What is a Sample & Sample rate? A "sample" is a single measurement of amplitude. Sample rate is simply the number of samples (or measurements of amplitude) taken per second. Sampling Intervals Quantization The samples are assigned a binary number approximating their sampled value. Quantizing divides up the sampled voltage range into 2n-1 quantizing intervals, where “n” is the number of bits per sample (the sampling resolution). For example, an 8-bit system can identify 28 (256) discrete sampled signal values (255 quantizing intervals). The amplitude of such a signal can occupy the entire quantizing range. A/D Reference Voltages Successive Approximation A/D Example 1 Example 2  Example 3 A/D Converter Modules in PIC16F87X series 28-pin devices has 5 modules. (AN0-AN4)         PIC16F873        PIC16F876 4...

Other PicBasic commands 01

W e shall n o w brief l y look at the remaining PicBasic commands in alphabetical order w hich are useful during the pr o g ram d e v elopment. More details about these commands can be obtained from the PicBasic manual. EEP R OM EEP R OM Location, (constant, constant,….., constant) Thi s comman d store s constant s i n consecut i v e b yte s i n on-chi p EEP R O M memo r y . Th e command on l y w ork s wit h th e PI C microcontroller s tha t h a v e EEP R OM , suc h a s th e PIC16F84 , PIC16F877, etc . Locatio n i s optional , an d i f omitte d th e f irs t EEP R O M locatio n i s assumed . Constant s ca n be numeri c constant s o r strin g constants . String s ar e store d a s consecut i v e b yte s o f ASCI I v alues . An e xampl e i s g i v e n bel o w . EEP R OM 3, (5, 2, 8)        ‘ Store 5 in location 3, ‘ 2 in location 4, and 8 in ‘ location 5 END END St...

Voltmeter

This code to read an analog temperature sensor, convert the reading to a digital value using the ADC, and display the temperature on a 4-digit 7-segment display. The display is updated periodically based on a timer interrupt. 1.Initialization: Setting up ports, configuring ADC, and Timer0. 2.Infinite Loop: Continuously reads analog input and updates the display. 3.ADC Conversion: Reads analog voltage from a sensor. 4.Voltage Calculation: Converts ADC value to voltage. 5.Digit Extraction: Extracts individual digits from the temperature/voltage value. 6.Display Function: Displays the digits on a 4-digit 7-segment display with slight delays. --------------------------------------------- #include <pic.h> // Include the header file for PIC microcontroller family. unsigned char PORTB_value[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; // Array containing values for 7-segment display. unsigned int a2d_value,temp; // Variables for analog to digital conversion and temporary sto...