Advertisement
TheHiddenHour

PIC12F683 XC8 Digital Input Example

Feb 21st, 2020
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #pragma config FOSC = INTOSCIO // IO function on GP4 & GP5
  2. #pragma config WDTE = OFF // Disable watchdog timer
  3. #pragma config PWRTE = OFF // Disable power-up timer
  4. #pragma config MCLRE = OFF // Disable reset on GP3
  5. #pragma config CP = OFF // Disable code protection
  6. #pragma config CPD = OFF // Disable data code protection
  7. #pragma config BOREN = ON // Enable brown out detection
  8. #pragma config IESO = OFF // Disable oscillator switching
  9. #pragma config FCMEN = ON // Enable fail-safe clock monitor
  10.  
  11. #define _XTAL_FREQ 4000000 // Define 4Mhz frequency for compiler
  12.  
  13. #include <xc.h>
  14. #include <stdbool.h>
  15.  
  16. void init(void) {
  17.     OPTION_REGbits.nGPPU = 0; // Enable internal IO pull-ups
  18.     OPTION_REGbits.T0CS = 0; // Internal instruction cycle clock
  19.     OPTION_REGbits.PSA = 0; // Assign prescaler to internal Timer0 module
  20.     OPTION_REGbits.PS = 0b000; // 1:1 prescaler rate
  21.     OSCCONbits.IRCF = 0b110; // 4MHz internal oscillator
  22.     OSCCONbits.OSTS = 0; // Device is running from internal oscillator
  23.     OSCCONbits.HTS = 1; // HFINTOSC is stable 8MHz to 125kHz
  24.     OSCCONbits.LTS = 0; // LFINTOSC is not stable 31 kHz or below
  25.     OSCCONbits.SCS = 1; // Internal oscillator for system clock
  26.     ANSELbits.ADCS = 0b000; // FOSC/2 AD conversion clock
  27.     ANSELbits.ANS = 0b0000; // Set all AN pins to digital mode
  28.     CMCON0bits.COUT = 0b00; // Set comparator output bits
  29.     CMCON0bits.CINV = 0; // Do not invert output
  30.     CMCON0bits.CIS = 0; // CIS has no effect so we'll just make it zero
  31.     CMCON0bits.CM = 0b000; // Disable comparator output
  32.     CMCON1bits.T1GSS = 0; // Timer1 gate source is comparator output
  33.     CMCON1bits.CMSYNC = 1; // Comparator output synced with falling edge of Timer1 clock
  34.     WDTCONbits.WDTPS = 0; // Watchdog timer period 1:32
  35.     WDTCONbits.SWDTEN = 0; // Disable watchdog timer
  36.    
  37.     return;
  38. }
  39.  
  40. int main(void) {
  41.     init(); // Initialize hardware
  42.    
  43.     TRISIO = 0b00100000; // Only GP5 as input
  44.     GPIO = 0; // All IO low
  45.    
  46.     while(true) {
  47.         if(GP5 == 0) { // If GP5 is brough low (connected to ground)
  48.             GP0 = 1; // Bring GP0 high and light the LED
  49.         }
  50.         else { // If GP5 is brought high
  51.             GP0 = 0; // Bring GP0 low to turn off the LED
  52.         }
  53.     }
  54.    
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement