?? timer.c
字號:
/****************************************Copyright (c)**************************************************/
/**
;** 西南科技大學(xué)計算機(jī)科學(xué)學(xué)院
;**
;** http://www.cs.suswt.edu.cn
;**
;** 日期: 2007/10/10
;** 描述: 西南科技大學(xué)計算機(jī)學(xué)院CS-II型實驗板定時器驅(qū)動工作程序,包含定時器0,定時器1
;** 作者:
;**
;**--------------timer.c文件
;**------------------------------------------------------------------------------------------------------*/
/**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
#define _TIMER_C_
#ifdef _TIMER_C_
/************************************************
* Header File *
***********************************************/
#include"config.h"
#include"led.h"
#include"key.h"
#include"motor.h"
/************************************************
* Globale variable *
***********************************************/
uint32 div0 = 0;
uint32 div1 = 3;
uint32 overflow = 0;
extern uint8 motor_step_run_flag;
extern uint8 motor_step_speed;
extern uint8 motor_step_speed_flag;
extern uint8 key_up_flag;
extern uint8 motor_step_direction;
/**--------------------------------------------------------------------------**/
// 定時器0
#define TIMER0_ENABLE
#ifdef TIMER0_ENABLE
/*******************************************************************************
Function Name:void __irq IRQ_Timer0(void) *
Description : Timer0的中斷服務(wù)函數(shù),需要利用Timer0進(jìn)行中斷的程序都可以放在這個*
函數(shù)中 *
ads1.2 規(guī)定,在定義中斷服務(wù)函數(shù)時,必須加入關(guān)鍵字__irq保證函數(shù)返*
回時會切換處理器模式 *
需要注意的是:注意在退出中斷服務(wù)程序時,要清零相應(yīng)外設(shè)的中斷標(biāo)志*
以及VICVectAddr,為響應(yīng)下一次中斷做好準(zhǔn)備 *
*******************************************************************************/
void __irq IRQTimer0(void)
{
if(motor_step_speed_flag == FALSE)
{
switch(KEYScan())
{
case 0x00:motor_step_run_flag = TRUE;break;
case 0x08:motor_step_run_flag = FALSE;break;
case 0x01:motor_step_speed_flag = TRUE;break;
case 0x09:motor_step_speed_flag = FALSE;break;
case 0x02:motor_step_direction = CLOCKWISE;break;
case 0x0a:motor_step_direction = ANTICLOCKWISE;break;
default:break;
}
}
/* 上面添加需要定時器中斷的代碼*/
T0IR=0X01; //清除中斷
VICSoftIntClear=0x00000010;
VICVectAddr=0x00; //清除地址
}
/*******************************************************************************
Function Name: void Timer0_InterruptSet(void) *
Description :對中斷向量的設(shè)置,包括中斷類型,中斷優(yōu)先級,中斷服務(wù)地址 *
中斷使能。 *
*******************************************************************************/
void TIMER0InterruptSet(void)
{
VICIntSelect &= 0xffffffef; //第4、5位分別代表定時計數(shù)器0、1 設(shè)置中斷類型
VICVectCntl0 = 0x24; //為中斷源分配向量IRQ的優(yōu)先級 ,VICVectCntl n,n值越小優(yōu)先級越高,0<=n<=15
//0010 0100 中斷源序號為4 代表計數(shù)器0中斷
VICVectAddr0 = (int)IRQTimer0; //向量地址寄存器 為該中斷優(yōu)先級設(shè)置服務(wù)程序入口地址
VICIntEnable = 0x00000010; //中斷使能 允許相應(yīng)中斷源產(chǎn)生中斷
}
/*******************************************************************************
Function Name: void Timer0_Init(void) *
Description : 定時器計數(shù)器0初始化設(shè)置,包括預(yù)分頻值,匹配模式,啟動復(fù)位等。 * *
*******************************************************************************/
void TIMER0Init(void) //另外一種寫法 void Timer1_Init(uint32 Div,uint32 Ovf)
{
T0TC = 0;
T0PR = div0; //設(shè)置預(yù)分頻值 定時器計數(shù)時鐘頻率 Fpclk/(PR+1)
T0MCR = 0x03; //設(shè)置匹配模式 匹配后復(fù)位T0TC,并產(chǎn)生中斷標(biāo)志
T0MR0 = Fpclk/1000; //設(shè)置匹配值 可以理解為計數(shù)器益出值 設(shè)置1ms匹配值
T0TCR = 0x03; //啟動并復(fù)位
TIMER0InterruptSet();
}
/*******************************************************************************
Function Name: void Timer0_Start(void) *
Description : 定時器計數(shù)器0啟動 *
*******************************************************************************/
void TIMER0Start(void)
{
T0TCR = 0x01; //定時器使能,相當(dāng)與定時計數(shù)器的開關(guān)
}
#endif
/**--------------------------------------------------------------------------**/
// 定時器1
#define TIMER1_ENABLE
#ifdef TIMER1_ENABLE
/*******************************************************************************
Function Name:void __irq IRQ_Timer1(void) *
Description : Timer1的中斷服務(wù)函數(shù),需要利用Timer1進(jìn)行中斷的程序都可以放在這個*
函數(shù)中 *
ads1.2 規(guī)定,在定義中斷服務(wù)函數(shù)時,必須加入關(guān)鍵字__irq保證函數(shù)返*
回時會切換處理器模式 *
需要注意的是:注意在退出中斷服務(wù)程序時,要清零相應(yīng)外設(shè)的中斷標(biāo)志*
以及VICVectAddr,為響應(yīng)下一次中斷做好準(zhǔn)備 *
*******************************************************************************/
void __irq IRQTimer1(void)
{
if(motor_step_speed_flag==TRUE)
{
switch(KEYScan())
{
case 0x03:motor_step_speed = 0x00;break;
case 0x04:motor_step_speed = 0x01;break;
case 0x05:motor_step_speed = 0x02;break;
case 0x06:motor_step_speed = 0x03;break;
case 0x07:motor_step_speed = 0x04;break;
case 0x09:motor_step_speed_flag = FALSE;break;
case 0x0b:motor_step_speed = 0x05;break;
case 0x0c:motor_step_speed = 0x06;break;
case 0x0d:motor_step_speed = 0x07;break;
case 0x0e:motor_step_speed = 0x08;break;
case 0x0f:motor_step_speed = 0x09;break;
default:break;
}
}
LEDScan();
LEDPrint(motor_step_speed);
if(motor_step_run_flag == TRUE)
{
MOTORStepRun(motor_step_speed,motor_step_direction);
}
/* 上面添加需要定時器中斷的代碼*/
T1IR=0X01; //清除中斷
VICSoftIntClear=0x00000010;
VICVectAddr=0x00; //清除地址
}
/*******************************************************************************
Function Name: void Timer1_InterruptSet(void) *
Description :對中斷向量的設(shè)置,包括中斷類型,中斷優(yōu)先級,中斷服務(wù)地址中斷使能 *
*******************************************************************************/
void TIMER1InterruptSet(void)
{
VICIntSelect &= 0xffffffdf; //第4、5位分別代表定時計數(shù)器0、1 設(shè)置中斷類型
VICVectCntl1 = 0x25; //為中斷源分配向量IRQ的優(yōu)先級 ,VICVectCntl n,n值越小優(yōu)先級越高,0<=n<=15
//0010 0100 中斷源序號為4 代表計數(shù)器0中斷
VICVectAddr1 = (int)IRQTimer1; //向量地址寄存器 為該中斷優(yōu)先級設(shè)置服務(wù)程序入口地址
VICIntEnable = 0x00000020; //中斷使能 允許相應(yīng)中斷源產(chǎn)生中斷
}
/*******************************************************************************
Function Name: void Timer1_Init(void) *
Description : 定時器計數(shù)器1初始化設(shè)置,包括預(yù)分頻值,匹配模式,啟動復(fù)位等。 * *
*******************************************************************************/
void TIMER1Init(void) //另外一種寫法 void Timer1_Init(uint32 Div,uint32 Ovf)
{
T1TC = 0;
T1PR = div1; //設(shè)置預(yù)分頻值 定時器計數(shù)時鐘頻率 Fpclk/(PR+1)
T1MCR = 0x03; //設(shè)置匹配模式 匹配后復(fù)位T0TC,并產(chǎn)生中斷標(biāo)志
T1MR0 = Fpclk/1000; //設(shè)置匹配值 可以理解為計數(shù)器益出值 設(shè)置1ms匹配值
T1TCR = 0x03; //啟動并復(fù)位
TIMER1InterruptSet();
}
/*******************************************************************************
Function Name: void Timer0_Start(void) *
Description : 定時器計數(shù)器0啟動 *
*******************************************************************************/
void TIMER1Start(void)
{
T1TCR = 0x01; //定時器使能,相當(dāng)與定時計數(shù)器的開關(guān)
}
#endif
#endif
/*****************************************************************************
* End of Entire File *
****************************************************************************/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -