我用的是STM8S105K4 用TIM2进行外部捕获脉冲个数,源代码如下:
void Clk_set(void)
{
CLK_ICKR |=0X1;
CLK_CMSR=0Xe1;
//CLK_CKDIVR=0;
CLK_CKDIVR = 0x08;//设置为2分频,16M/2=8M
CLK_PCKENR1=0xff;//e6
}
void TIM_init2(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;// 允许更新中断
}
{0x82, Timer2com}, /* irq11 */
{0x82, Timer2Isr}, /* irq12 */
@far @interrupt void Timer2com(void)
{
if(TIM2_SR1&0x04==0x04)
{
TIM2_SR1&=0xFB;
test_var = TIM2_CCR2H;
test_var = test_var*256+ TIM2_CCR2L;
test_var = 0;
}
return;
}
@far @interrupt void Timer2Isr(void)
{
TIM2_SR1&=0xFE;//清除更新中断标志位,否则仿真连续进入中断程序
return;
}
我就是进不去中断,计不了输入的数据 |