PIC16F684 Flashing an LED with delay_ms, TMR0, Interrupt, & WDT
The LED flashes approximately 150ms every 5 seconds. RC1 (pin 9), triggers the LED. Interestingly, the PIC consumes 28uA at idle, no matter which method is used. Compiles with Microchip’s XC8 (v1.38)
- Delay function, 28 bytes
- Timer 0 delay, 31 bytes
- Timer 0 Interrupt, 60 bytes
- WDT Sleep, 27 bytes
/* * File: Delay_Blinky.c * January 21, 2017, 1:14 AM */ #include// CONFIG #pragma config FOSC = INTOSCIO #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config MCLRE = OFF #pragma config CP = OFF #pragma config CPD = OFF #pragma config BOREN = OFF #pragma config IESO = OFF #pragma config FCMEN = OFF #define _XTAL_FREQ 31000 void main(void) { //Set clock to 31KHz OSCCONbits.IRCF2 = 0; OSCCONbits.IRCF1 = 0; OSCCONbits.IRCF0 = 0; TRISCbits.TRISC1 = 0; ANSEL = 0; while (1) { RC1 = 0; __delay_ms(5000); RC1 = 1; __delay_ms(150); } }
/* * File: TMR0_Blinky.c * January 21, 2017, 1:38 AM */ #include// CONFIG #pragma config FOSC = INTOSCIO #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config MCLRE = OFF #pragma config CP = OFF #pragma config CPD = OFF #pragma config BOREN = OFF #pragma config IESO = OFF #pragma config FCMEN = OFF #define _XTAL_FREQ 31000 void main(void) { //Set clock to 31KHz OSCCONbits.IRCF2 = 0; OSCCONbits.IRCF1 = 0; OSCCONbits.IRCF0 = 0; TRISCbits.TRISC1 = 0; ANSEL = 0; //Reset Timer 0 TMR0 = 0; //Set clock edge start and stop T0CS = 0; T0SE = 0; //Set prescaler to TMR0 PSA = 0; //Set prescaler bits PS2 = 1; PS1 = 1; PS0 = 0; while (1) { RC1 = 0; while(!T0IF); RC1 = 1; __delay_ms(150); T0IF = 0; } }
/* * File: Interrupt_Blinky.c * January 21, 2017, 1:56 AM */ #include// CONFIG #pragma config FOSC = INTOSCIO #pragma config WDTE = OFF #pragma config PWRTE = OFF #pragma config MCLRE = OFF #pragma config CP = OFF #pragma config CPD = OFF #pragma config BOREN = OFF #pragma config IESO = OFF #pragma config FCMEN = OFF #define _XTAL_FREQ 31000 void main(void) { //Set clock to 31KHz OSCCONbits.IRCF2 = 0; OSCCONbits.IRCF1 = 0; OSCCONbits.IRCF0 = 0; TRISCbits.TRISC1 = 0; ANSEL = 0; //Reset Timer 0 TMR0 = 0; //Set clock edge start and stop T0CS = 0; T0SE = 0; //Set prescaler to TMR0 PSA = 0; //Set prescaler bits PS2 = 1; PS1 = 1; PS0 = 0; T0IE = 1; //Enable interrupt timer INTCONbits.INTE = 1; //Enable External Interrupt INTCONbits.GIE = 1; //Enable Global Interrupt CLRWDT(); while (1); } void interrupt blinky(void) { if (INTCONbits.T0IF && INTCONbits.T0IE) { TMR0 -= 254; RC1 = 1; __delay_ms(150); RC1 = 0; INTCONbits.T0IF = 0; //Reset Timer } }
/* * File: Sleep_Blinky.c * January 21, 2017, 2:21 AM */ #include// CONFIG #pragma config FOSC = INTOSCIO #pragma config WDTE = ON #pragma config PWRTE = OFF #pragma config MCLRE = OFF #pragma config CP = OFF #pragma config CPD = OFF #pragma config BOREN = OFF #pragma config IESO = OFF #pragma config FCMEN = OFF #define _XTAL_FREQ 31000 void main(void) { //Set clock to 31KHz OSCCONbits.IRCF2 = 0; OSCCONbits.IRCF1 = 0; OSCCONbits.IRCF0 = 0; TRISCbits.TRISC1 = 0; ANSEL = 0; //Set prescaler to WDT PSA = 1; //Set prescaler bits PS2 = 1; PS1 = 1; PS0 = 1; while(1) { RC1 = 1; __delay_ms(150); RC1 = 0; CLRWDT(); SLEEP(); CLRWDT(); SLEEP(); } }
Leave a Reply