CAP function frequency measurement

Publisher:太和清音Latest update time:2016-12-10 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The STC12C5608AD microcontroller has a CAP function, which can be used to measure the frequency of a signal. The principle is to measure the time between two rising edges or falling edges. This time is the period of the signal, and the reciprocal of the period is the frequency. The following is the program to achieve this:

/****************************************************** *
 * Name: CAP function measures signal frequency
 * Main frequency: 12MHz
 * Author: Ration
 * Date: December 7, 2013 (edited)
 * Copyright: Copyright belongs to "MCU Learning Network". Please keep integrity when disseminating.
 *************************************************/
#include //Call the header file, you can download the header file of STC12C5608AD from the official website

unsigned char counter=0;
unsigned char out_data[2]={0x00};

void UART_Init()
{
    TMOD=0x22;
    TH1=0xF3;
    TL1=0xF3; //Set the baud rate to 2400    
    TH0=0XEC; //Value under 12M
    TL0=0XEC;
    SCON=0X50;
    TR1=1; 
    TR0=1; 
}
void CAP_Init()
{
    CMOD = 0X04; // ECF = 1; Enable PCA interrupt flag
    CCAPM1 = 0X11; // CAPN0 = 1; Falling edge capture. ECCF0 = 1; Enable capture interrupt.
    CL = 0;
    CH = 0; // PCA counter cleared
    CCAP1L = 0;
    CCAP1H = 0; // capture register clear
    EPCA_LVD = 1; // Enable PCA interrupt
    CR = 1;
}

void send232byte(unsigned char bytebuf)
{
    TI=0;
     SBUF=bytebuf;
    while(!TI);           
}
void delay()
{
    unsigned int x;

    for(x=0;x<65535;x++);    

}
void PCAIRQ(void) interrupt 6
{      
 if(CCF1==1) // If the interrupt is generated by PCA capture
 {
        CF=0;
        CCF1=0;                
        out_data[0]=CCAP1L;
        out_data[1]=CCAP1H;
        CH=0;
        CL=0;     
 }        
}

void main(void)
{    
    unsigned int tmp;
    unsigned long dat;

    UART_Init();
    CAP_Init();
    EA = 1;

    while(1) // infinite loop
    {
        tmp=out_data[1]<<8;
    tmp+=out_data[0];
        dat=50000/tmp; //Calculate frequency value
        send232byte(dat>>8);
      send232byte(dat);
        delay();
    }
}

In the program, T1 timer is used as serial port baud rate generator to send frequency value to serial port, T0 is used as PCA clock generator, and it is set to interrupt once every 20us. Therefore, the measured value tmp*20 is the period in microseconds, which is converted into frequency, that is, 1/tmp*20/1000000, that is, 50000/tmp.


Reference address:CAP function frequency measurement

Previous article:51 MCU driver 1602 program
Next article:Solution to 51 MCU PWM duty cycle less than 0

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号