#pragma config BWRP = OFF // Boot Segment Write Protect (Disabled) #pragma config BSS = OFF // Boot segment Protect (No boot program flash segment) // FGS #pragma config GWRP = OFF // General Segment Write Protect (General segment may be written) #pragma config GSS0 = OFF // General Segment Code Protect (No Protection) // FOSCSEL #pragma config FNOSC = FRCPLL // Oscillator Select (Fast RC Oscillator with Postscaler and PLL Module (FRCDIV+PLL)) #pragma config SOSCSRC = ANA // SOSC Source Type (Analog Mode for use with crystal) #pragma config LPRCSEL = HP // LPRC Oscillator Power and Accuracy (High Power, High Accuracy Mode) #pragma config IESO = ON // Internal External Switch Over bit (Internal External Switchover mode enabled (Two-speed Start-up enabled)) // FOSC #pragma config POSCMOD = NONE // Primary Oscillator Configuration bits (Primary oscillator disabled) #pragma config OSCIOFNC = ON // CLKO Enable Configuration bit (CLKO output signal is active on the OSCO pin) #pragma config POSCFREQ = HS // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock input frequency greater than 8MHz) #pragma config SOSCSEL = SOSCHP // SOSC Power Selection Configuration bits (Secondary Oscillator configured for high-power operation) #pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Both Clock Switching and Fail-safe Clock Monitor are disabled) // FWDT #pragma config WDTPS = PS32768 // Watchdog Timer Postscale Select bits (1:32768) #pragma config FWPSA = PR128 // WDT Prescaler bit (WDT prescaler ratio of 1:128) #pragma config FWDTEN = OFF // Watchdog Timer Enable bits (WDT disabled in hardware; SWDTEN bit disabled) #pragma config WINDIS = OFF // Windowed Watchdog Timer Disable bit (Standard WDT selected(windowed WDT disabled)) // FPOR #pragma config BOREN = BOR3 // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware, SBOREN bit disabled) #pragma config LVRCFG = OFF // Low Voltage Regulator Configuration bit (Low Voltage regulator is not available) #pragma config PWRTEN = ON // Power-up Timer Enable bit (PWRT enabled) #pragma config I2C1SEL = PRI // Alternate I2C1 Pin Mapping bit (Use Default SCL1/SDA1 Pins For I2C1) #pragma config BORV = V18 // Brown-out Reset Voltage bits (Brown-out Reset set to lowest voltage (1.8V)) #pragma config MCLRE = ON // MCLR Pin Enable bit (RA5 input pin disabled,MCLR pin enabled) // FICD #pragma config ICS = PGx1 // ICD Pin Placement Select bits (EMUC/EMUD share PGC1/PGD1) // FDS #pragma config DSWDTPS = DSWDTPSF // Deep Sleep Watchdog Timer Postscale Select bits (1:2,147,483,648 (25.7 Days)) #pragma config DSWDTOSC = LPRC // DSWDT Reference Clock Select bit (DSWDT uses Low Power RC Oscillator (LPRC)) #pragma config DSBOREN = ON // Deep Sleep Zero-Power BOR Enable bit (Deep Sleep BOR enabled in Deep Sleep) #pragma config DSWDTEN = ON // Deep Sleep Watchdog Timer Enable bit (DSWDT enabled) #include "xc.h" //---TIMER 1--- (increments time_ms every millisecond) volatile unsigned long time_ms = 0; void __attribute__((__interrupt__, __auto_psv__, __shadow__))_T1Interrupt(void){ IFS0bits.T1IF = 0; //clear Interrupt Flag time_ms++; } void timer1Init(void){ T1CON = 0x0000; //Everything off, 1:1 from Internal Oscillator TMR1 = 0x0000; PR1 = 16024; //Interrupt Occurs every millisecond (should be 16000 nominal) IPC0bits.T1IP = 0x02; //Set Interrupt Priority to 1 IFS0bits.T1IF = 0; //clear Interrupt Flag IEC0bits.T1IE = 1; //Enable Interrupt for Timer 1 T1CONbits.TON = 1; //Start Timer 1 } //---UART--- void uartInit(){ U1BRG = 0x0000; // 1 Mbps U1MODE = 0x0000; // 8 bit, no parity, 1 stop bit U1MODEbits.UARTEN = 1; //Enable UART U1STAbits.UTXEN = 1; } void printString(char *str){ unsigned int i = 0; while(str[i] != 0){ while(U1STAbits.UTXBF); //wait if transmit buffer is full U1TXREG = str[i++]; } while(U1STAbits.UTXBF); U1TXREG = 0x0A; //new line } int main(void){ //---INIT--- CLKDIV = 0x0000; //no prescaler for anything //timer 1 timer1Init(); //start Timer 1 unsigned long time_last = 0; //uart uartInit(); //Enable UART U1STAbits.OERR = 0; //clear input buffer while(U1STAbits.URXDA){volatile int trash = U1RXREG;} //io TRISB &= ~(1<<5); //RB5 as output (Pin 14) //---MAIN LOOP--- while(1){ if(time_ms - time_last >= 100){ //Execute every 100 ms time_last = time_ms; LATB ^= (1<<5); //Heartbeat while(U1STAbits.URXDA){ //Check if Data is available in RXREG printString("Data available"); while(U1STAbits.UTXBF); //Wait if trasmit buffer is full U1TXREG = U1RXREG; //Output recieved data } } } return 1; }