Code Breakdown:
1. Preprocessor Directives:
#include <pic.h>: Includes the header filepic.h, which contains definitions and declarations specific to PIC microcontrollers.#define _XTAL_FREQ 4000000: Defines a preprocessor constant_XTAL_FREQwith 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 variablecountto 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 thecountvariable 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 thecountvariable.if (count == 250) { ... }: Checks if 250 4ms intervals have elapsed (1 second).RC0 = !RC0;: Toggles the RC0 output.count = 0;: Resets thecountvariable 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;
}
}
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
Post a Comment