Frequency meter based on STC8 microcontroller

Publisher:Blissful567Latest update time:2022-08-15 Source: csdnKeywords:STC8 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Frequency measurement principle: frequency is measured by counting method.

Implementation method: Using the feature of 51 MCU timer as external pulse counter, frequency counting can be realized. Specifically, two timers are used. In this example, timer 0 is used as external pulse counter to capture external pulse (falling edge trigger), and timer 3 generates 1s timing to extract counting frequency.

code show as below


//System clock frequency 24MHz

#include "STC8.H"


bit send_flag = 0;

unsigned int freq = 0; //frequency count

unsigned long cont_freq = 0; //Frequency count overflow

unsigned char res[6]; //Frequency conversion result


void TM0_Init();

void TM3_Init();

void UartSend(unsigned char dat);

void UartInit(void);

void UartSendStr(unsigned char *p);

void Num_Cut(unsigned long num);

void Uart_Send_Str(unsigned char str[],unsigned char len,bit mode);


void main ()

{

TM0_Heat();    

TM3_Init();    

UartInit();    

while(1)

    if(send_flag)

{

        Num_Cut(cont_freq+(unsigned long)freq); //Frequency value equals overflow value plus current value

  send_flag = 0;

  UartSendStr("Freq: ");

Uart_Send_Str(res,6,1);

Uart_Send_Str(res,6,0);

UartSendStr(" Hzn");

}

}

/******************************************************************************

 * Description: Timer 3 initialization function, timing time 1ms

 * Parameters: None

 * Return value: None

 ******************************************************************************/

void TM3_Init()

{

T3L = 0x35; //Set the initial timing value, 1ms

  T3H = 0xA2; //Set the initial timing value

    T4T3M |= 0x08; //Start the timer

  T4T3M |= 0x02;    //Timer clock 1T mode

    IE2 |= ET3; //Enable timer interrupt

    EA = 1; // Enable general interrupt

}

/******************************************************************************

 * Description: Timer 0 initialization function, external counting mode

 * Parameters: None

 * Return value: None

 ******************************************************************************/

void TM0_Init()

{

  TMOD |= 0x04; //Set external counting mode

  TMOD |= 0x01; //Working mode 1, 16-bit does not automatically reload

  TR0 = 1; //Open the counter

    TH0 = 0x00; //Counter reset

  TL0 = 0x00;                                

  ET0 = 1; //Open counter interrupt

}

/******************************************************************************

 * Description: Timer 0 interrupt service function 

 * Parameters: None

 * Return value: None

 ******************************************************************************/

void TM0_Isr() interrupt 1

{  

    cont_freq += 65536; // Overflow count, each overflow, the count times is TH0<<8|TL0 = 0xffff, after debugging, add 1

  TH0 = 0x00; //Reset counter

  TL0 = 0x00; //Reset counter

}

/******************************************************************************

 * Description: Timer 3 interrupt service function

 * Parameters: None

 * Return value: None

 ******************************************************************************/

void TM3_Isr() interrupt 19

{   

  static unsigned int ms_flag = 0; //1s count flag

if(ms_flag++ == 1000)

{

freq = (TH0<<8|TL0); //Get the current counter value          

ms_flag = 0; //1s count flag reset

send_flag = 1; //Serial port send flag is set

  TH0 = 0x00; //Reset counter

  TL0 = 0x00; //Reset counter

}

AUXINTIF &= ~T3IF; // Clear interrupt flag

}

/******************************************************************************

 * Description: Serial port 1 initialization function, baud rate 9600 

 * Parameters: None

 * Return value: None

 ******************************************************************************/

void UartInit(void) //9600bps@24.000MHz

{

SCON |= 0x50; //8-bit data, variable baud rate

AUXR |= 0x01; //Serial port 1 selects timer 2 as the baud rate generator

AUXR |= 0x04; //Timer 2 clock is Fosc, i.e. 1T

T2L = 0x8F; //Set the initial timing value

T2H = 0xFD; //Set the initial timing value

AUXR |= 0x10; //Start timer 2

}

/******************************************************************************

* Description: Serial port character sending and function 

* Input parameter: Send character

* Return value: None

******************************************************************************/

void UartSend(unsigned char dat)

{

    SBUF =that;     

    while(!IF);

  TI = 0;

}

/******************************************************************************

* Description: Serial port string sending function

* Input parameter: Send string

* Return value: None

******************************************************************************/

void UartSendStr(unsigned char *p)

{

    while (*p)

    {

        UartSend(*p++);

    }

}

 /******************************************************************************

 * Description: Number splitting function

 * Input parameter: Split number

 * Return value: None

 ******************************************************************************/

void Num_Cut(unsigned long num)

{ unsigned long temp = num; 

unsigned char cont = 5;

while(temp != 0)

{

res[cont--] = (unsigned char)(temp % 10);

temp /= 10;

}

  cont_freq = 0;

}

/******************************************************************************

* Description: Serial port digital array sending function

* Input parameters: str: array, len: array length or data length to be sent, mode: 1 to send, 0 to clear the array

* return value: 

******************************************************************************/

void Uart_Send_Str(unsigned char str[],unsigned char len,bit mode)

{

unsigned char k;

if(mode == 1)

{

for(k = 0;k < len; k++)

{

UartSend(48 + str[k]);

}

}else

{

for(k = 0;k < len; k++)

{

str[k] = 0;

}

}

}


The test results are analyzed as follows:

Keywords:STC8 Reference address:Frequency meter based on STC8 microcontroller

Previous article:Blue Bridge Cup STC15 MCU Review - Use of IO Ports
Next article:STC15 single chip microcomputer frequency measurement

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号