/*******************************************************************************
* FileName: can.c
* Dependencies: Header (.h) files if applicable, see below
* Processor: PIC24HJ128GP506A
*
* ADDITIONAL NOTES:
*
*
* REVISION HISTORY:
* 2014/07/17 First release of source file
*
*
*******************************************************************************/
#include "can.h"
#include <p24HJ128GP506A.h>
#include "common.h"
#include "delay.h"
#include "led.h"
/******************************************************************************/
_FOSCSEL(FNOSC_FRC); // Select Internal FRC at POR
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF); // Enable Clock Switching and Configure
//_FOSCSEL(FNOSC_FRC);
//_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_XT); // Clock switching enabled and fail safe clock monitor is disabled
// // OSC2 pin function: OSC2 is clock output
// // Primary oscillator mode: XT crystal
_FWDT(FWDTEN_OFF); // Watchdog timer enabled/disabled by user software
_FICD(JTAGEN_OFF & ICS_PGD2); // JTAG disabled and use ICD channel PGEC2/PGED2
/******************************************************************************/
void InitSys( void )
{
RCONbits.SWDTEN = 0; // Disable watch dog timer
// Configure PLL prescaler, PLL postscaler, PLL divisor
PLLFBD = 41; // M = 43
CLKDIVbits.PLLPOST = 0; // N2 = 2
CLKDIVbits.PLLPRE = 0; // N1 = 2
__builtin_write_OSCCONH(0x01); // Initiate Clock Switch to Internal FRC with PLL (NOSC = 0b001)
__builtin_write_OSCCONL(0x01);
while( OSCCONbits.COSC != 0b001 ) Nop(); // Wait for Clock switch to occur
while( OSCCONbits.LOCK != 1 ) Nop(); // Wait for PLL to lock
// // Configure oscillator to operate the device at 40MHz
// // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
// // Fosc= 8M*40/(2*2)=80MHz for 8MHz input clock
// PLLFBD = 38; // M=40
// CLKDIVbits.PLLPOST = 0; // N1=2
// CLKDIVbits.PLLPRE = 0; // N2=2
// OSCTUN=0; // Tune FRC oscillator, if FRC is used
// // Clock switching to incorporate PLL
// __builtin_write_OSCCONH(0x01); // Initiate clock switch to FRC with PLL(NOSC=0b001)
// __builtin_write_OSCCONL(0x01); // Start clock switching
// while( OSCCONbits.COSC != 0b001 ) Nop(); // Wait for clock switch to occur
// while( OSCCONbits.LOCK != 1 ) Nop(); // Wait for PLL to lock
}
/******************************************************************************/
void InitTimer1( void )
{
T1CON = 0; // Timer1 reset
IFS0bits.T1IF = 0; // Reset timer1 interrupt flag
IPC0bits.T1IP = 6; // Timer1 interrupt priority level=4
IEC0bits.T1IE = 1; // Enable timer1 interrupt
TMR1= 0;
PR1 = 39629; // Timer1 period register=39629, resulting timing period 1ms@Fcy=39.6288MHz
// PR1 = 40000; // Timer1 period register=40000, resulting timing period 1ms@Fcy=40MHz
T1CONbits.TON = 1; // Enable timer1 and start the counter
}
/******************************************************************************/
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt( void )
{
static INT16U Count;
IFS0bits.T1IF = 0; // Reset timer1 interrupt flag
T1CONbits.TON = 0;
Count++;
if( Count == 500 )
{
Count = 0;
LEDDBG_BLINK(); // Toggle debug LED
}
TMR1 = 0; // Clear timer1 counter
T1CONbits.TON = 1;
}
/******************************************************************************/
/******************************************************************************/
INT16S main( void )
{
INT16U Tmp;
INT08U i;
Nop();
Nop();
InitSys(); // Initialize mcu primary modules: WDT/OSC/CLK and GPIOs
InitTimer1(); // Initialize timer1 for period interrupts
while(1)
{
Nop();
Nop();
}
}
/******************************************************************************/