Last active
November 16, 2019 20:01
-
-
Save ElektroNeo/815f9bb2a9fda8625bde5566257dd4b3 to your computer and use it in GitHub Desktop.
GPIO operations (3) for PIC16F1508 microcontroller.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PIC16F1508 Configuration Bit Settings | |
// 'C' source line config statements | |
// CONFIG1 | |
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin) | |
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) | |
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) | |
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) | |
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) | |
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled) | |
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) | |
#pragma config IESO = OFF // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled) | |
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) | |
// CONFIG2 | |
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) | |
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset) | |
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) | |
#pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled) | |
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) | |
// #pragma config statements should precede project file includes. | |
// Use project enums instead of #define for ON and OFF. | |
#define _XTAL_FREQ 16000000 | |
#include <xc.h> | |
void main(void) { | |
// Internal oscillator. | |
OSCCONbits.SCS = 0b10; | |
// 16 MHz internal crystal. | |
OSCCONbits.IRCF = 0b1111; | |
// For digital i/o operations | |
ANSELA = 0; | |
ANSELB = 0; | |
// PORTA 0 and 1 bits are inputs. | |
TRISAbits.TRISA0 = 1; | |
TRISAbits.TRISA1 = 1; | |
// PORTB as Output | |
TRISB = 0; | |
PORTB = 0; | |
while(1) { | |
short btn1 = PORTAbits.RA0; | |
short btn2 = PORTAbits.RA1; | |
if(btn1 == 0 && btn2 == 0) { | |
PORTB = 0b00010000; // LED1 is on. | |
} else if(btn1 == 1 && btn2 == 0) { | |
PORTB = 0b00100000; // LED2 is on. | |
} else if(btn1 == 0 && btn2 == 1) { | |
PORTB = 0b01000000; // LED3 is on. | |
} else if(btn1 == 1 && btn2 == 1) { | |
PORTB = 0b10000000; // LED4 is on. | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PIC16F1508 Configuration Bit Settings | |
// 'C' source line config statements | |
// CONFIG1 | |
#pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator: I/O function on CLKIN pin) | |
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) | |
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) | |
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) | |
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) | |
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled) | |
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) | |
#pragma config IESO = OFF // Internal/External Switchover Mode (Internal/External Switchover Mode is disabled) | |
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled) | |
// CONFIG2 | |
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) | |
#pragma config STVREN = OFF // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset) | |
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) | |
#pragma config LPBOR = OFF // Low-Power Brown Out Reset (Low-Power BOR is disabled) | |
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming) | |
// #pragma config statements should precede project file includes. | |
// Use project enums instead of #define for ON and OFF. | |
#define _XTAL_FREQ 16000000 | |
#include <xc.h> | |
void main(void) { | |
// Internal oscillator. | |
OSCCONbits.SCS = 0b10; | |
// 16 MHz internal crystal. | |
OSCCONbits.IRCF = 0b1111; | |
// For digital i/o operations | |
ANSELA = 0; | |
ANSELB = 0; | |
// PORTA 0 and 1 bits are inputs. | |
TRISAbits.TRISA0 = 1; | |
TRISAbits.TRISA1 = 1; | |
// PORTB as Output | |
TRISB = 0; | |
PORTB = 0; | |
while(1) { | |
short btn1 = PORTAbits.RA0; | |
short btn2 = PORTAbits.RA1; | |
if(btn1 == 0 && btn2 == 0) { | |
PORTB = 0b00010000; // LED1 is on. | |
} else if(btn1 == 1 && btn2 == 0) { | |
PORTB = 0b00100000; // LED2 is on. | |
} else if(btn1 == 0 && btn2 == 1) { | |
PORTB = 0b01000000; // LED3 is on. | |
} else if(btn1 == 1 && btn2 == 1) { | |
PORTB = 0b10000000; // LED4 is on. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment