📄 bsp.c
字号:
/*
*********************************************************************************************************
* Microchip dsPIC33FJ
* Board Support Package
*
* Micrium
* (c) Copyright 2005, Micrium, Weston, FL
* All Rights Reserved
*
*
* File : BSP.C
* By : Eric Shufro
*********************************************************************************************************
*/
#include "includes.h"
/*
*********************************************************************************************************
* MPLAB CONFIGURATION MACROS
*********************************************************************************************************
*/
/* Disable the watchdog timer */
_FOSCSEL(FNOSC_PRI);
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_XT);
_FWDT(FWDTEN_OFF);
/*
*********************************************************************************************************
* VARIABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* PROTOTYPES
*********************************************************************************************************
*/
static void BSP_PLL_Init(void);
static void Tmr_TickInit(void);
/*
*********************************************************************************************************
* BSP INITIALIZATION
*
* Description : This function should be called by your application code before you make use of any of the
* functions found in this module.
*
* Arguments : none
*********************************************************************************************************
*/
void BSP_Init (void)
{
BSP_PLL_Init(); /* Initialize the PLL */
LED_Init(); /* Initialize the I/Os for the LED controls */
Tmr_TickInit();
Init_ADC(); /* Initialize the uC/OS-II tick interrupt */
}
/*
*********************************************************************************************************
* BSP_PLL_Init()
*
* Description : This function configures and enables the PLL with the external oscillator
* selected as the input clock to the PLL.
*
* Notes : 1) The PLL output frequency is calculated by FIN * (M / (N1 * N2)).
* 2) FIN is the PLL input clock frequency, defined in bsp.h as
* CPU_PRIMARY_OSC_FR. This is the same as the external primary
* oscillator on the Explorer 16 Evaluation Board.
* 3) M is the desired PLL multiplier
* 4) N1 is the divider for FIN before FIN enters the PLL block (Pre-Divider)
* 5) N2 is the PLL output divider (Post-Divider)
*
* Summary : The PLL is configured as (8MHZ) * (40 / (2 * 2)) = 80MHZ
* The processor clock is (1/2) of the PLL output.
* Performance = 40 MIPS.
*********************************************************************************************************
*/
void BSP_PLL_Init (void)
{
PLLFBD = 38; /* Set the Multiplier (M) to 40 (2 added automatically) */
CLKDIV = 0; /* Clear the PLL Pre Divider bits, N1 = N2 = 2 */
}
/*
*********************************************************************************************************
* BSP_CPU_ClkFrq()
* Description : This function determines the CPU clock frequency (Fcy)
* Returns : The CPU frequency in (HZ)
*********************************************************************************************************
*/
INT32U BSP_CPU_ClkFrq (void)
{
INT8U Clk_Selected;
INT8U PLL_n1;
INT8U PLL_n2;
INT16U PLL_m;
INT16U FRC_Div;
INT32U CPU_Clk_Frq;
PLL_m = (PLLFBD & PLLDIV_MASK) + 2; /* Get the Multiplier value */
PLL_n1 = (CLKDIV & PLLPRE_MASK) + 2; /* Computer the Pre Divider value */
PLL_n2 = ((CLKDIV & PLLPOST_MASK) >> 6); /* Get the Post Divider register value */
PLL_n2 = ((PLL_n2 * 2) + 2); /* Compute the Post Divider value */
FRC_Div = ((CLKDIV & FRCDIV_MASK) >> 8); /* Get the FRC Oscillator Divider register value */
FRC_Div = ((1 << FRC_Div) * 2); /* Compute the FRC Divider value */
Clk_Selected = (OSCCON & COSC_MASK) >> 12; /* Determine which clock source is currently selected */
switch (Clk_Selected) {
case 0: /* Fast Oscillator (FRC) Selected */
case 7:
CPU_Clk_Frq = CPU_FRC_OSC_FRQ; /* Return the frequency of the internal fast oscillator */
break;
case 1: /* Fast Oscillator (FRC) with PLL Selected */
CPU_Clk_Frq = ((CPU_FRC_OSC_FRQ * PLL_m) / /* Compute the PLL output frequency using the FRC as FIN */
(FRC_Div * PLL_n1 * PLL_n2));
break;
case 2: /* Primary External Oscillator Selected */
CPU_Clk_Frq = CPU_PRIMARY_OSC_FRQ; /* Return the frequency of the primary external oscillator */
break;
case 3: /* Primary External Oscillator with PLL Selected */
CPU_Clk_Frq = ((CPU_PRIMARY_OSC_FRQ * PLL_m) / /* Compute the PLL output frq using the PRI EXT OSC as FIN */
(PLL_n1 * PLL_n2));
break;
case 4: /* Secondary External Low Power Oscillator (SOSC) Selected */
case 5: /* Internal Low Power RC Oscillator Selected */
CPU_Clk_Frq = CPU_SECONDARY_OSC_FRQ; /* Return the frq of the external secondary Low Power OSC */
break;
case 6:
CPU_Clk_Frq = 0; /* Return 0 for the Reserved clock setting */
break;
default:
CPU_Clk_Frq = 0; /* Return 0 if the clock source cannot be determined */
break;
}
CPU_Clk_Frq /= 2; /* Divide the final frq by 2, get the actual CPU Frq (Fcy) */
return (CPU_Clk_Frq); /* Return the operating frequency */
}
/*
*********************************************************************************************************
* DISABLE ALL INTERRUPTS
*
* Description : This function disables all interrupts from the interrupt controller.
*
* Arguments : none
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LED I/O INITIALIZATION
*
* Description : This function initializes the I/O Pins used by the onboard LEDs
*
* Arguments : none
*
* Notes : 1) Jumper JP2 on the Explorer 16 board must be connected to enable the onboard LEDs
* 2) JTAG must be DISABLED in order to utilize all of PORTA I/O Lines for LEDs
*********************************************************************************************************
*/
void LED_Init (void)
{
TRISB = 0; /* Set all PORTA pins to output */
LATB = 0xff; /* Shut off all LEDs */
}
/*
*********************************************************************************************************
* LED ON
*
* Description : This function is used to control any or all the LEDs on the board.
*
* Arguments : led is the number of the LED to control
* 0 indicates that you want ALL the LEDs to be ON
* 1 turns ON LED1
* 2 turns ON LED2
* ...
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -