使用捕获功能测的脉宽后,如何计算她的频率呢?因为pca的时基是分频得来的,所以有些搞不清初如何计算
51cpu的捕获功能例程
void PCA0_ISR (void) interrupt 11
{
static unsigned int current_capture_value, previous_capture_value;
static unsigned int capture_period;
if (CCF0) // If Module 0 caused the interrupt
{
CCF0 = 0; // Clear module 0 interrupt flag.
// Store most recent capture value
current_capture_value = PCA0CP0;
// Calculate capture period from last two values.
capture_period = current_capture_value - previous_capture_value;
// Update previous capture value with most recent info.
previous_capture_value = current_capture_value;
}
else // Interrupt was caused by other bits.
{
PCA0CN &= ~0x86; // Clear other interrupt flags for PCA
}
}
还有下面一段pic的单片机测频的代码,我想转成51的
while( 1 )
{
setup_timer_1(T1_DISABLED); /* desabilita el timer 1 */
setup_ccp1(CCP_OFF); /* desabilita el modo capture */
set_timer1(0); / * timer 1 cero */
CCP1IF = 0; /* capture flag cero */
setup_timer_1(T1_INTERNAL); /* activa timer 1 */
setup_ccp1(CCP_CAPTURE_DIV_16); /* habilita modo capture */
while(!CCP1IF); /* Verifica si ocurrio captura */
temp1 = get_timer1(); /* Valor del TMR1 en temp1 */
CCP1IF = 0; / * capture flag = 0 */
while(!CCP1IF); /* Verifica si ocurrio captura */
temp2 = get_timer1(); /* Valor TMR1 en temp2 */
temp = temp2 - temp1; /* valor TRM1 entre dos capturas */
temp1 = (16*1.0e6)/temp; /* Temp1 = Frecuencia */
上面的pic代码十分简洁,大概意思和51cpu是一至的,关键在最后1行,频率是如何换算的? |