Professional Documents
Culture Documents
void main()
{
OPTION_REG = 0x0E; // assign prescaler to the WDT, with 1:64 rate
asm CLRWDT; // asm line, clear WatchDog Timer
PORTB = 0x0F; // Initialize PORTB
TRISB = 0; // Configure PORTB as output
Delay_ms(300); // Wait 0.3 seconds
PORTB = 0xF0; // Change PORTB value
while (1); // endless loop, WatchDog_Timer will reset PIC
}
PORTB = var;
do {
if (cnt == 400) {
var = ~var;
PORTB = var; // Toggle PORTB LEDs
cnt = 0; // Reset cnt
}
} while(1);
}
/* This code demonstrates how to use TMR1 for interrupts. Program toggles
LEDs on PORTB. */
var = ~var;
PORTB = var; // then toggle PORTB leds and
cnt = 0; // reset cnt
}
} while (1);
}
/* This code demonstrates using interrupts and TMR2. Program toggles LEDs on
PORTB and PORTD each second. */
void interrupt() {
cnt++ ; // increment counter
PIR1.TMR2IF = 0; // clears TMR2IF
TMR2 = 0;
}
void main() {
cnt = 0; // initialize cnt
PORTB = 0xFF; // Initialize PORTB
TRISB = 0; // PORTB is output
PORTD = 0x00; // Initialize PORTD
TRISD = 0; // PORTD is output
T2CON = 0xFF; // Timer2 settings