1. Preprocessor Directives:
#include <pic.h>: Includes the header filepic.h, which contains definitions and declarations specific to PIC microcontrollers.
2. 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 = 1; PS0 = 1;: Sets the prescaler to 256, providing a timer tick every 50 milliseconds.TMR0 = 217;: Initializes TMR0 with the value 217 to generate a 10ms delay.RC2 = 1;: Sets the output on RC2.
-
Main Loop:
while (1) { ... }: The main loop continues indefinitely.- Interrupt Handling:
if (T0IF) { ... }: Checks if the TMR0 interrupt flag is set.TMR0 = 217;: Reloads TMR0 with the value 217 to start another 10ms delay.RC2 = !RC2;: Toggles the RC2 output.T0IF = 0;: Clears the TMR0 interrupt flag.
Explanation:
The code implements a simple blinking LED circuit that toggles an output pin (RC2) every 10 milliseconds. The main components of the code are:
- Timer Initialization: TMR0 is configured as an internal timer with a prescaler of 256, providing a timer tick every 50 milliseconds. The initial value of TMR0 is set to 217 to generate a 10ms delay.
- Interrupt Handling: The TMR0 interrupt is used to toggle the output pin and reload the timer for the next 10ms delay.
#include <pic.h>
void main()
{
TRISC=0; // PORTC output
PORTC=0; // PORTC clear
T0CS=0; // Internal clock select (Fosc/4)
PSA=0; // Prescaler assigned to TMR0
PS2=1;
PS1=1;
PS0=1; // 1:256 Prescaler ratio
TMR0=217; // (256-39=6) for 39 increments--->39x256=10000uS
RC2=1; // RC0 LED ON
while(1)
{
if(T0IF==1){
TMR0=217;
RC2=!RC2;
T0IF=0; //clear overflow flag bit
}
}
}
void main()
{
TRISC=0; // PORTC output
PORTC=0; // PORTC clear
T0CS=0; // Internal clock select (Fosc/4)
PSA=0; // Prescaler assigned to TMR0
PS2=1;
PS1=1;
PS0=1; // 1:256 Prescaler ratio
TMR0=217; // (256-39=6) for 39 increments--->39x256=10000uS
RC2=1; // RC0 LED ON
while(1)
{
if(T0IF==1){
TMR0=217;
RC2=!RC2;
T0IF=0; //clear overflow flag bit
}
}
}

Comments
Post a Comment