Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma config FOSC = INTOSCIO // IO function on GP4 & GP5
- #pragma config WDTE = OFF // Disable watchdog timer
- #pragma config PWRTE = OFF // Disable power-up timer
- #pragma config MCLRE = OFF // Disable reset on GP3
- #pragma config CP = OFF // Disable code protection
- #pragma config CPD = OFF // Disable data code protection
- #pragma config BOREN = ON // Enable brown out detection
- #pragma config IESO = OFF // Disable oscillator switching
- #pragma config FCMEN = ON // Enable fail-safe clock monitor
- #define _XTAL_FREQ 4000000 // Define 4Mhz frequency for compiler
- #include <xc.h>
- #include <stdbool.h>
- void init(void) {
- OPTION_REGbits.nGPPU = 0; // Enable internal IO pull-ups
- OPTION_REGbits.T0CS = 0; // Internal instruction cycle clock
- OPTION_REGbits.PSA = 0; // Assign prescaler to internal Timer0 module
- OPTION_REGbits.PS = 0b000; // 1:1 prescaler rate
- OSCCONbits.IRCF = 0b110; // 4MHz internal oscillator
- OSCCONbits.OSTS = 0; // Device is running from internal oscillator
- OSCCONbits.HTS = 1; // HFINTOSC is stable 8MHz to 125kHz
- OSCCONbits.LTS = 0; // LFINTOSC is not stable 31 kHz or below
- OSCCONbits.SCS = 1; // Internal oscillator for system clock
- ANSELbits.ADCS = 0b000; // FOSC/2 AD conversion clock
- ANSELbits.ANS = 0b0000; // Set all AN pins to digital mode
- CMCON0bits.COUT = 0b00; // Set comparator output bits
- CMCON0bits.CINV = 0; // Do not invert output
- CMCON0bits.CIS = 0; // CIS has no effect so we'll just make it zero
- CMCON0bits.CM = 0b000; // Disable comparator output
- CMCON1bits.T1GSS = 0; // Timer1 gate source is comparator output
- CMCON1bits.CMSYNC = 1; // Comparator output synced with falling edge of Timer1 clock
- WDTCONbits.WDTPS = 0; // Watchdog timer period 1:32
- WDTCONbits.SWDTEN = 0; // Disable watchdog timer
- return;
- }
- int main(void) {
- init(); // Initialize hardware
- TRISIO = 0b00100000; // Only GP5 as input
- GPIO = 0; // All IO low
- while(true) {
- if(GP5 == 0) { // If GP5 is brough low (connected to ground)
- GP0 = 1; // Bring GP0 high and light the LED
- }
- else { // If GP5 is brought high
- GP0 = 0; // Bring GP0 low to turn off the LED
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement