// mod10a.c RGD 1/4/03 // This is a simple test program for module 10 (ME499) #include <16f84a.H> // This tells the compiler that we will use a 4 MHz crystal #fuses HS, WDT, NOPROTECT // This sets up the internal device fuses: HS crystal, WDT ON, Code Protect OFF #use delay (clock=4000000) #byte PORTA = 5 #byte PORTB = 6 unsigned int x; /////////////////////////////////////// // SUBROUTINES void init_ports(void) { SET_TRIS_A(0b11111111); // PORTA = all outputs SET_TRIS_B(0b00000000); // PORTB = all inputs // set Watch Dog Timer prescaler to 1152 ms: SETUP_COUNTERS(RTCC_INTERNAL, WDT_1152MS); // Default output values: PORTB = 0b00000000; // turn off all LEDs // Default variable values x = 0b00000000; } ////////////////////////////////////// // PIC16F84a microcontroller goes here at RESET void main() { init_ports(); // Initialize ports restart_wdt(); // Reset the WDT CYCLE: // Run continuously x = input_a(); // read (input) all of the bits from port A into the variable x //NOTE: Port A only has five I/O pins, the remaining 3 bits are always = 0 if(x == 0b00000000) OUTPUT_B(0b00000000); // No buttons are pressed, so turn off all LEDs // note also the use of == as opposed to a single = // two == signs means "are the two values equal?" // a single = sign means "set the left variable to equal the value on the right" if(x == 0b00000001) OUTPUT_B(0b00000001); // Only SW1 was pressed, so turn ON the LED connected to RB0 if(x == 0b00000010) OUTPUT_B(0b00000010); // Only SW2 was pressed, so turn on the LED connected to RB1 while(x == 0b00000011) { // if both SW1 and SW2 are being pressed... OUTPUT_B(0b11111111); // turn ON all LEDs delay_ms(250); // wait for 1/4 of a second OUTPUT_B(0b00000000); // turn OFF all LEDs delay_ms(250); // wait for 1/4 of a second // NOTE: the above 4 lines will cause all of the LEDs to flash // for as long as both SW1 and SW2 remain pressed restart_wdt(); x = input_a(); // check to be sure both SW1 and SW2 are still pressed } // this is the end of the WHILE loop goto CYCLE; // go back and infinitely loop to the label CYCLE } // main…this is the end of the program