在一个小项目里面,用PIC16F913来主控,用来记录N个脉冲的时间,时间精确到10us,看了下资料,晶振可以最高20M 20M/4分频可以到5M。但是看了下第定时器的中断,软件模拟了下,进中断和出中断就要3.XX个US,如果加上脉冲捕获进中断的时间,一起的误差会不会超出10US的范畴???(思路是:一边捕获脉冲。一边开个5US的定时器片,一直累加。当脉冲捕获到N值时,关定时器。看定时了多少个5US) 刚接错PIC,不知道这样可行不?高手路过,喵喵。。。
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
the output is on P2 and as you can see, it is 0x32 = 50 ticks -> 50us for the pulse width - the correct answer.
the only difference here is that the gate on a 12F675 is active low and the gate on the 8051 is active high.
(原文件名:C51 Gated TMR0.PNG)
========code==============
#include <regx51.h>
#include "gpio.h"
//__CONFIG(MCLRDIS & BORDIS & WDTDIS & PWRTEN & INTIO);
#define IN_PORT P3
#define IN_DDR P3
#define IN_GATE (1<<2) //pulse gate on int0=p3.2
#define OUT_PORT P2
#define OUT_DDR P2
#define OUT_PINs 0xff //all pins on p2 are output
void mcu_init(void) { //initiate the mcu
//ANSEL=0x00; //all pins digital
//CMCON=0x07; //analog comparators off
IO_IN(IN_DDR, IN_GATE); //in_gate as input
IO_OUT(OUT_DDR, OUT_PINs); //out_pins as output
}
void tmr0_init(void) { //initiate the tmr1
//TMR1GE=1; //enable tmr1 gate control - tmr1 starts counting if t1g is low
//T1CKPS1=0, T1CKPS1=0; //tmr1 prescaler 1:1
//TMR1CS=0; //tmr1 source is internal clock
TMOD &=0xf0; //reset tmod0..3
TMOD |=0x09; //tmr0 =gated timer, and in mode 1 (16-bit timer)
//TMR1ON=0; //tmr0 off
TR0=0;
}
void
main(void)
{
unsigned short count=0;
mcu_init(); //initiate the mcu
tmr0_init(); //initiate the tmr1
while (1){
//TODO Auto-generated main function
while (IO_GET(IN_PORT, IN_GATE)); //wait for in_gate to go low
//now in_gate is low
TH0=0, TL0=0; //reset tmr0 counters
//TMR1ON=1; //turn on tmr1
TR0=1;
while (!IO_GET(IN_PORT, IN_GATE)); //wait for IN_gate to go high
//in_gate is high -> counting starts high
while (IO_GET(IN_PORT, IN_GATE)); //wait for in_gate to go low
//TMR1ON=0; //turn off tmr1
TR0=0;
count=(TH0<<8) | TL0; //count contains tmr1h:tmr1l
IO_CLR(OUT_PORT, OUT_PINs); //clear output pins
IO_SET(OUT_PORT, count); //output
}
}
一周热门 更多>