// PIC16F1615 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 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 = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON // Internal/External Switch Over (Internal External Switch Over mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit cannot be cleared once it is set by software)
#pragma config ZCD = OFF // Zero Cross Detect Disable Bit (ZCD disable. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
#pragma config PLLEN = ON // PLL Enable Bit (4x PLL is always enabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will 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 = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
// CONFIG3
#pragma config WDTCPS = WDTCPS1F// WDT Period Select (Software Control (WDTPS))
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config WDTCWS = WDTCWSSW// WDT Window Select (Software WDT window size control (WDTWS bits))
#pragma config WDTCCS = SWC // WDT Input Clock Selector (Software control, controlled by WDTCS bits)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#include <PIC16F1615.h>
volatile int t = 0;
void init(){
OSCCON =0xF0; //8MHz INTOSC x4= 32MHz (only needed for INTOSC)
TRISA = 0xFE; //A0 as output. 0xFE for r1 and higher, 0xFD for r0 boards
LATA = 0x01; //change to 0x01 for boards r1 and higher, 0x02 for r0 boards
T2CLKCON = 0x00; //clock source = Fosc/4
T2HLT = 0x00; //rising edge triggered, free running with software gate
T2CON = 0x60; //T2 off, /64 prescaler, 1:1 postcaler (125KHz)
T2PR = 125; //125KHz/125=1KHz or 1ms delay
TMR2=0; //clear TMR2 counter
TMR2IF = 0; //clear TMR2 interrupt flag
INTCON = 0xC0; //global interrupts enabled, peripheral interrupts enabled
PIE1 =0x02; //T2 interrupt enabled
}
void delay_millis(int time){
t=time;
TMR2=0;
T2ON=1;
while(t>0);
T2ON=0;
}
void main(void) {
init();
while(1){
delay_millis(1000);
LATA ^= 0x01; //change to 0x01 for boards r1 and higher, 0x02 for r0 boards
}
return;
}
void interrupt ISR(){
if(TMR2IF && TMR2IE){
TMR2IF = 0;
t--;
}
}