ok6410的定时器0 测试程序
1 #include "def.h" 2 #include "gpio.h" 3 #include "library.h" 4 #include "system.h" 5 #include "timer.h" 6 #include "intc.h" 7 #include "sfr6410.h" 8 9 /* 10 11 此函数的功能仅仅在于测试TIMER0的定时功能,以最简单的形式展示 “初始化定时器以及相关中断” 12 上述头文件在国嵌资料中有提供。 13 14 15 实现效果是:定时地在 LED1, LED2 , LED3之间轮转: 16 */ 17 #define GPIO ((volatile oGPIO_REGS*)GPIO_BASE) 18 19 extern void SYSC_GetClkInform(void); 20 void LedInit(void); 21 void LedPlay(u8 l_num); 22 void Delay(int time); 23 void TimerInit(u8 t_num, int ms); 24 25 26 int main(void) 27 { 28 //使能中断向量控制器: 29 SYSTEM_EnableVIC(); 30 //使能IRQ模式中断: 31 SYSTEM_EnableIRQ(); 32 //得到g_PCLK时钟: 33 SYSC_GetClkInform(); 34 //初始化中断: 35 INTC_Init(); 36 //定时器初始化: 37 TimerInit(0 , 500); 38 //Led初始化: 39 LedInit(); 40 41 while(1); 42 } 43 //定时器服务例程 44 void __irq Isr_Timer(void) 45 { 46 // 1. 清除对应的未决位: 47 u32 uConValue; 48 uConValue = Inp32(rTINT_CSTAT); 49 uConValue |= (0x1<<(5 + 1)); 50 Outp32(rTINT_CSTAT,uConValue); 51 // 2. 需要处理的东西/////////////////// 52 LedPlay(1); 53 Delay(100); 54 LedPlay(2); 55 Delay(100); 56 LedPlay(3); 57 58 // 3. 必须清除中断服务运行地址,为下次中断准备: 59 Outp32(rVIC0ADDR,0); 60 61 } 62 63 //定时器初始化; 64 void TimerInit(u8 t_num, int ms) 65 { 66 u32 uConValue; 67 // 1. 对中断进行配置前最好先关闭定时器: 68 Outp32(rTCON,0); 69 // 2. 对定时器配置定时器所采用的频率,最后得到2048的分频: 70 71 //第一步:配置precaler0为127分频: 72 uConValue = Inp32(rTCFG0); 73 uConValue &= ~0xff; 74 uConValue |= 0x7f; 75 Outp32(rTCFG0, uConValue); 76 //第二步:配置二级分频为16;共分频2048: 77 uConValue = Inp32(rTCFG1); 78 uConValue &= ~0xf; 79 uConValue |= 0x4; 80 Outp32(rTCFG1,uConValue); 81 // 3. 设置定时器所需要的计算值: 82 83 // 可以算得1微秒的 计数值 为g_PCLK/2048/1000 设为500毫秒计算值: 84 if(t_num == 1) 85 Outp32(rTCNTB1,((g_PCLK>>11)/1000) * ms); 86 else if(t_num == 0) 87 Outp32(rTCNTB0,((g_PCLK>>11)/1000) * ms ); 88 // 4. 对状态位写1,清除当前的未决中断: 89 uConValue = Inp32(rTINT_CSTAT); 90 uConValue |= (0x1<<(5+ t_num)); 91 Outp32(rTINT_CSTAT,uConValue); 92 93 94 95 // 5. 注册安装定时器的中断函数: 每个寄存器存有4个字节的地址所以乘以4 96 if(t_num == 0) 97 Outp32(rVIC0VECTADDR+4*NUM_TIMER0, (u32)Isr_Timer); 98 else if(t_num == 1) 99 Outp32(rVIC0VECTADDR+4*NUM_TIMER1, (u32)Isr_Timer); 100 // 6. 使能定时器的中断使能位: 101 uConValue = Inp32(rVIC0INTENABLE); 102 uConValue |= (0x1 << NUM_TIMER0) | (0x1 << NUM_TIMER1); 103 Outp32(rVIC0INTENABLE,uConValue); 104 105 106 // 7. 设置使得定时器可以发生中断: 107 uConValue = Inp32(rTINT_CSTAT); 108 uConValue |= 0x1 << (t_num); 109 Outp32(rTINT_CSTAT,uConValue); 110 111 // 8. 最后使定时器开始计数, 112 uConValue = Inp32(rTCON); 113 //第一步:设置手动装入定时器的计算缓冲器; 114 uConValue &= ~(0x1f << t_num*8); 115 uConValue |= 0x2 << (t_num * 8); 116 Outp32(rTCON,uConValue); 117 118 //第二步:设置自动装入,并开启定时器: 119 uConValue = Inp32(rTCON); 120 uConValue &= ~(0x1f << t_num * 8); 121 uConValue |= 0x9 << (t_num *8); 122 Outp32(rTCON,uConValue); 123 124 } 125 126 //延时函数; 127 void Delay(int time) 128 { 129 int i; 130 for(; time > 0; time--) 131 for(i=0; i < 3000; i++); 132 } 133 134 //led初始化; 135 void LedInit(void) 136 { 137 u32 uConValue; 138 139 uConValue = GPIO->rGPIOMCON; 140 uConValue &= ~(0xf | (0xf<<4) | (0xf<<8) | (0xf<<12)); 141 uConValue |= (0x1 | (0x1 << 4) | (0x1 << 8) | (0x1 << 12)); 142 GPIO->rGPIOMCON = uConValue; 143 144 } 145 //led工作,l_num为什么值就点着对应的led灯: 146 void LedPlay(u8 l_num) 147 { 148 u32 tmp; 149 tmp = GPIO->rGPIOMDAT; 150 tmp |= 0x1f; 151 tmp &= (~ (1 << (l_num - 1))); 152 GPIO->rGPIOMDAT = tmp; 153 }