|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
void SYSTEMCLOCK_Init(void)
{
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency and enable
// missing clock detector
CLKMUL = 0x00; // Select internal oscillator as
// input to clock multiplier
CLKMUL |= 0x80; // Enable clock multiplier
Delay(); // Delay for clock multiplier to begin
CLKMUL |= 0xC0; // Initialize the clock multiplier
Delay(); // Delay for clock multiplier to begin
while(!(CLKMUL & 0x20)); // Wait for multiplier to lock
CLKSEL = 0x03; // Select system clock 48MHZ
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
// Return Value : None
// Parameters : None
// Configure the Crossbar and GPIO ports
// P0.0 TX1 (UART 1)
// P0.1 RX1 (UART 1)
// P0.2
// P0.3
// P0.4 TX0 (UART 0)
// P0.5 RX0 (UART 0)
// P2.0
// P2.1
// P2.2 LED
//------------------------------------------------------------------
void PORT_Init(void)
{
P2MDOUT |= 0x04; // set LED to push-pull
// P0SKIP |= 0x30; //交叉开关跳过的将作为模拟输入(ADC 或比较
//器)或特殊功能(VREF 输入、外部振荡器电路、CNVSTR 输入)
XBR0 = 0x01; //(UART TX0, RX0 连到端口引脚P0.4 和P0.5)交叉开关使能串口,I2C接口不使能
XBR2 = 0x01; // route UART 1 to crossbar
XBR1 = 0x40; // 使能交叉开关,使能弱上拉。IIC接口只需要交叉开关使能即可
//不需要跳过这些接口,默认按顺序排队
P0MDIN = 0xFF; // 全部P0口不配置为模拟输入,即配置为数字输入
P0MDOUT |= 0x11; //使能 UTX0,UTX1为推挽输出
P0 = 0xFF;
P1SKIP |= 0x28; //交叉开关跳过P1.3(模拟信号采样),P1.5(作为VREF参考电压输入脚)
P1MDIN &= 0xD7; //P1.5,该管脚作为VREF参考电压输入脚,P1.5为模拟输入
P1MDOUT = 0xFF; //推挽输出,必须为推挽输出,因为需要输出高电平,而开漏只能输出低电平,
//这组IO存在控制通道选择的功能,会输出高电平
P1 |= 0x03; //P1口输出0x03,
P2MDIN = 0xFF; //全部P2口不配置为模拟输入,即配置为数字输入
P2MDOUT = 0xff;
P2 = 0Xff;
P3MDOUT = 0xff;
P3 = 0Xff;
P4MDOUT = 0xff;
P4 = 0Xff;
}
//--------------------------------------------------------
//delay()
//--------------------------------------------------------
void Delay(void)
{
int x;
for(x = 0 ;x < 500 ; x)
x++;
}
//-----------------------------------------------------------------------------
// Timer0_Init
//-----------------------------------------------------------------------------
// Return Value : None
// Parameters : None
//
// This function configures the Timer0 as a 16-bit timer, interrupt enabled.
// Using the internal osc. at 12MHz with a prescaler of 1:8 and reloading the
// T0 registers with TIMER0_RELOAD_HIGH/LOW it will interrupt and then toggle
// the LED.
//
// Note: The Timer0 uses a 1:48 prescaler. If this setting changes, the
// TIMER_PRESCALER constant must also be changed.
//-----------------------------------------------------------------------------
void Timer0_Init(void)
{
TH0 = TIMER0_RELOAD_HIGH; // Init Timer0 High register
TL0 = TIMER0_RELOAD_LOW ; // Init Timer0 Low register
TMOD |= 0x01; // Timer0 in 16-bit mode
CKCON |= 0x02; // Timer0 uses a 1:48 prescaler
ET0 = 1; // Timer0 interrupt enabled
TCON &= ~0x10; // Timer0 OFF
}
//---------------------------------------------------------
// Interrupt Service Routines
//---------------------------------------------------------
// Here we process the Timer0 interrupt and toggle the LED
//---------------------------------------------------------
//-------TIMER0 interrupt
//---------------------------------------------------------
void TIMER0_interrupt() interrupt 1
{
// LED = ~LED;
TH0 = TIMER0_RELOAD_HIGH; // Reinit Timer0 High register
TL0 = TIMER0_RELOAD_LOW; // Reinit Timer0 Low register
Accumulate_time ++; //计时累加
}
//void Timer3_Init(void) //用于寻线器,定时器3采用双8位定时器功能,
//{ //高字节中断,低字节中断
// TMR3CN = 0x00; //16bit Timer3
// CKCON |= 0xc0; //Timer3 USE system clock.
//
// TMR3RL = 0xffff - 480; // Timer3 configured to overflow after ,10ms,16位模式
// TMR3 = TMR3RL;
// EIP1 |= 0x80;
// EIE1 |= 0x80; // Timer3 interrupt enable
// TMR3CN |= 0x04; // Start Timer3
//}
Timer3_Init (SYSCLK/TIMER3L_RATE/12 , SYSCLK/TIMER3H_RATE/12); // Init Timer 3
{ //高字节中断,低字节中断
TMR3CN = 0x00; //two 8 bit Timer3
TMR3CN |= 0x28;
// CKCON |= 0xc0;
// CKCON |= 0x10;
CKCON &= 0x3f; //Timer3L , Timer3H uses SYSCLK/12 as its timebase
TMR3RLL = 0xff-countsL; // Init reload values
TMR3RLH = 0xff-countsH;
TMR3L = TMR3RLL; // Init Timer3 with reload value
TMR3H = TMR3RLH;
// EIP1 |= 0x80;
EIE1 |= 0x80; // Timer3 interrupt enable
TMR3CN |= 0x04; // Start Timer3
} |
|