我的代码如下
/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
#define _MAIN_C
#include "common.h"
byte i = 0;
byte j = 0;
word test_var;
word temp[10];
dword tcont[10];
dword peri;
byte time4_cout = 0;
void CLK_Init(void){
CLK_CKDIVR = 0x08;//设置为2分频,16M/2=8M
CLK_CSSR = 0x00;
CLK_SWCR = 0x02;
CLK_SWR = 0xE1;//设置HSI为主时钟源
while(!(CLK_ICKR&0x02));//等待HSI准备就绪
while(!(CLK_CMSR&0XE1));
CLK_ECKR = 0x00;//停止外部时钟
CLK_PCKENR1 =0xFF;//开启所有外设的时钟
CLK_PCKENR2 =0xFF;//开启所有外设的时钟
}
void TIME2_Init(void){
TIM2_CR1 = 0x04;
TIM2_EGR = 0x00;
TIM2_CCER1 = 0x00;
TIM2_CCER1 = 0x00;
TIM2_PSCR = 0x03;//fcnt=8M/(8)=1000kHz=1us
TIM2_ARRH = 0xFF;// 自动重载寄存器ARR=0x01F4=50000
TIM2_ARRL = 0xFF;// 每记数50000次产生一次中断,即50ms
TIM2_CCMR2 = 0x51;
TIM2_CCER1 = 0x20;
TIM2_IER |= 0x05;// 允许捕获,计数器使能,开始计数
TIM2_SR1 = 0;
TIM2_SR2 = 0;
TIM2_CCER1 = 0x10;
TIM2_CR1 |= 0x11;// 允许更新中断
}
@far @interrupt void Timer2com (void){
if(TIM2_SR1&0x04){
TIM2_SR1&=0xFB;
if(i<9){
test_var = TIM2_CCR2H;
test_var = test_var*256+ TIM2_CCR2L;
temp = test_var;
if(i>=1){
if(temp[i-1]<=temp){
tcont[i-1]=temp-temp[i-1];
}else{
tcont[i-1]=(~temp[i-1])+temp+1;
}
}
i++;
}
}
}
@far @interrupt void Timer2Isr (void){
static byte time4_cout = 0;
TIM2_SR1&=0xFE;//清除更新中断标志位,否则仿真连续进入中断程序
}
void init_devices(void){
DisableInterrupts;
CLK_Init();
TIME2_Init();
EnableInterrupts;
}
main()
{
init_devices();
while (1){
if(i==9){
for(j=0;j<9;j++){
peri += tcont[j];
}
peri = peri>>3;
i++;
}
}
}
|