一、初始化
// 设置系统时钟和外设总线时钟 SYSCLK=80MHz PBCLK=80MHz
void SetupHardware(void) //时钟初始化
{
SYSTEMConfigPerformance( configCPU_CLOCK_HZ - 1 ); //SysCLK主频80M
INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR); //多向量中断模式
AD1PCFGSET = 0xFFFFFFFF; //数字模式,关闭模拟模式
mJTAGPortEnable(DEBUG_JTAGPORT_OFF); //关闭JTAG
INTDisableInterrupts(); //关闭中断
}
void __inline__ IOPortIni(void) //IO初始化
{
PORTSetPinsDigitalIn(IOPORT_B,BIT_14); // CAN_RX_VCU2
PORTSetPinsDigitalOut(IOPORT_B, BIT_8 // CAN_TX_VCU2
| BIT_12 // LED绿
| BIT_13); // LED红
PORTSetPinsDigitalIn( IOPORT_D, BIT_9 );// RTCC数据
PORTSetPinsDigitalOut(IOPORT_D, BIT_10 );// RTCC时钟
PORTSetPinsDigitalIn(IOPORT_F, BIT_0); // CAN_RX_LMU1
PORTSetPinsDigitalOut(IOPORT_F, BIT_1); // CAN_TX_LMU1
mJTAGPortEnable(DEBUG_JTAGPORT_OFF); // 关闭JTAG复用功能,等同于DDPCONbits.JTAGEN = 0 ;
}
//---------------------------------------------------------------------------------
// main
//---------------------------------------------------------------------------------
int main (void)
{
SetupHardware(); //时钟初始化
IOPortIni(); //IO初始化
EnableWDT(); //打开看门狗
while(1)
{
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 延时微秒,单位:5 微秒
// 实际的工作频率为时钟频率/4,执行指令采用流水线方式,指令周期=1/(80M/4)*5级流水线=0.25微秒
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void mDelay5uS(UINT16 us)
{
#if (GetSystemClock()==16000000) // 2*us*2*1.25=5us
us *= 2;
#elif (GetSystemClock()==80000000) // 10*us*2*0.25=5us
us *= 10;
#endif
do
{ us--;} //1指令周期
while (us); //1
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 延时毫秒时间, 单位:1 毫秒
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void mDelaymS(UINT16 ms)
{
do
{
mDelayuS(200); // 200 * 5us 为 1ms
}
while (--ms);
}
|