/******* T1.c ****************** * * Use Fosc = 4 MHz for Fcpu = Fosc/4 = 1 MHz. * Sleep for 16 ms (nominal), using watchdog timeout for wakeup. * Toggle RC2 output every 16 milliseconds for measuring looptime with scope. * Blink LED on RD4 for 16 ms every four seconds. * Check pushbutton and turn on LED continuously while it is pressed. * * Current draw = 4 uA (with LED and LCD switched off) * ******* Program hierarchy ***** * * main * Initial * BlinkAlive * Pushbutton * ******************************* */ #include // Define PIC18LF4321 registers and bits /******************************* * Configuration selections ******************************* */ #pragma config OSC = INTIO1 // Use internal osc, RA6=Fosc/4, RA7=I/O #pragma config PWRT = ON // Enable power-up delay #pragma config LVP = OFF // Disable low-voltage programming #pragma config WDT = OFF // Disable watchdog timer initially #pragma config WDTPS = 4 // 16 millisecond WDT timeout period, nominal #pragma config MCLRE = ON // Enable master clear pin #pragma config PBADEN = DIG // PORTB<4:0> = digital #pragma config CCP2MX = RB3 // Connect CCP2 internally to RB3 pin #pragma config BOR = SOFT // Brown-out reset controlled by software #pragma config BORV = 3 // Brown-out voltage set for 2.1V, nominal #pragma config LPT1OSC = OFF // Deselect low-power Timer1 oscillator /******************************* * Global variables ******************************* */ unsigned int DELAY; // Counter for obtaining a delay unsigned char ALIVECNT; // Counter for blinking "Alive" LED /******************************* * Constant strings ******************************* */ /******************************* * Variable strings ******************************* */ /******************************* * Function prototypes ******************************* */ void Initial(void); void BlinkAlive(void); void Pushbutton(void); /******************************* * Macros ******************************* */ #define Delay(x) DELAY = x; while(--DELAY){ Nop(); Nop(); } /////// Main program ////////////////////////////////////////////////////////// /******************************* * main ******************************* */ void main() { Initial(); // Initialize everything while (1) { PORTCbits.RC2 = !PORTCbits.RC2; // Toggle pin, for measuring loop time BlinkAlive(); // Blink "Alive" LED Pushbutton(); // Turn on LED while pushbutton is pressed Sleep(); // Sleep, letting watchdog timer wake up chip Nop(); } } /******************************* * Initial * * This function performs all initializations of variables and registers. ******************************* */ void Initial() { OSCCON = 0b01100010; // Use Fosc = 4 MHz (Fcpu = 1 MHz) ADCON1 = 0b00001011; // RA0,RA1,RA2,RA3 pins analog; others digital TRISA = 0b00001111; // Set I/O for PORTA TRISB = 0b01000100; // Set I/O for PORTB TRISC = 0b10000000; // Set I/O for PORTC TRISD = 0b10000100; // Set I/O for PORTD TRISE = 0b00000010; // Set I/O for PORTE PORTA = 0; // Set initial state for all outputs low PORTB = 0; PORTC = 0; PORTD = 0b00100000; // except RD5 that drives LCD interrupt PORTE = 0; Delay(50000); // Pause for half a second RCONbits.SBOREN = 0; // Now disable brown-out reset ALIVECNT = 247; // Blink immediately WDTCONbits.SWDTEN = 1; // Enable watchdog timer } /******************************* * BlinkAlive * * This function briefly blinks the LED every four seconds. * With a looptime of about 16 ms, count 250 looptimes ******************************* */ void BlinkAlive() { PORTDbits.RD4 = 0; // Turn off LED if (++ALIVECNT == 250) // Increment counter and return if not 250 { ALIVECNT = 0; // Reset ALIVECNT PORTDbits.RD4 = 1; // Turn on LED for 16 ms every 4 secs } } /******************************* * Pushbutton * * This function overrides the role of the BlinkAlive function and turns on * the LED for the duration of a pushbutton press. ******************************* */ void Pushbutton() { PORTEbits.RE0 = 1; // Power up the pushbutton Nop(); // Delay one microsecond before checking it if (!PORTDbits.RD7) // If pressed { PORTDbits.RD4 = 1; // turn on LED } PORTEbits.RE0 = 0; // Power down the pushbutton }