Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // For MPLAB X with the XC8 Compiler
- // by Ido Gendel (info AT idogendel.com)
- // Connections : Red LED to GP4, Green LED to GP5
- // Three momentary buttons from GND to GP0-GP2
- #define _XTAL_FREQ 4000000
- #include <xc.h>
- #pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT , GP5/OSC1/CLKIN)
- #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
- #pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled)
- #pragma config MCLRE = OFF // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
- #pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled)
- #pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
- #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
- #define redLedIO 4
- #define greenLedIO 5
- char buttonInput() {
- // Reversed logic reversed...
- return 7 - (GPIO & 7);
- }
- int main() {
- const char secretCode[] = {1, 2, 4 ,2 ,1};
- char codeIndex;
- ANSEL = 0; // ANalog SELect; all digital
- CMCON = 7; // CoMparator CONtrol; comparator off
- ADCON0 = 0; // A/D CONtrol; A/D conversion off
- TRISIO = 7; // GP0-2 inputs
- OPTION_REG &= 127; // GPPU clear - pullups enabled
- WPU = 7; // Internal pull-up on GP0-2
- while (1) {
- GPIO = 1 << redLedIO; // Red LED pin High
- codeIndex = 0;
- while (codeIndex < sizeof(secretCode)) {
- // Wait for input on any button, then debounce
- while (!buttonInput()) ;
- __delay_ms(10);
- // Check input
- if (buttonInput() == secretCode[codeIndex]) codeIndex++;
- else codeIndex = (buttonInput() == secretCode[0]) ? 1 : 0;
- // Wait for button release, then debounce
- while (buttonInput()) ;
- __delay_ms(10);
- } // while
- // Correct combination sequence
- GPIO = 1 << greenLedIO; // Green LED pin High
- __delay_ms(1000);
- } // while
- return 0;
- } // main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement