51 MCU simple frequency meter source code

Publisher:平凡幸福Latest update time:2015-06-23 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//Timer 0 sets the timing time to 1 second Timer 1 sets the number of pulses counted within 1 second
#include
#define uchar unsigned char 
#define uint unsigned int
uint ff; //Accept frequency value
uchar start=1; //Timer, counting mode start variable
code uchar seg[]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60};
code uchar tab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe };
void delay(uint k) //delay function
{
 while(k--);      
}
void show(uint k) //Numerical display function
{
  static uchar i,j;
  uint ss[5];
  i=0;
  do
  {
   ss[i]=k%10;
   i++;
  }while(k/=10);
  for(j=0;j   {
   P0=seg[ss[j]];
   P2=tab[j];
   delay(100);
   P0=0xff;
   P2=0xff;
  }  
}

void time0_init()
{
 TMOD|=0x01; //Timer 0 timing mode count range 65536
 TH0=0x3c; //Timing initial value 50ms
 TL0=0xb0;
 TR0=0; //Pause timer 0
}
void time1_init()
{
 TMOD|=0x50; //Timer 1 counting mode, counting range 65536
 TH1=0; //Counting initial value 0
 TL1=0;
 TR1=0; //Pause counter
}
void str_init() //Timer interrupt
{
 EA=1;
 ET0=1;
}
void time0_event() interrupt 1
{
  static uchar i;
  i++;
  TH0=0x3c; //timing initial value 50ms
  TL0=0xb0;
 if(i==20)
 {
  i=0;
  TR1=0; //stop counting
  TR0=0; //stop timing
  ff=(TH1*256+TL1); //find the frequency value, which is the number of pulses in 1 second
  TH1=0; //clear the count value
  TL1=0;
  TH0=0x3c; //timing initial value 50ms
  TL0=0xb0;
  start=1; //start the timer and turn on the variable
 }
}
 
void main()
{
 time0_init();
 time1_init();
 str_init();
 while(1)
 {
  if(start==1)
  {
   TR0=1; //Start timer
   TR1=1; //Start counter
   start=0; //Close the start variable bit to ensure 1 second
  }
  show(ff);
 }
}
Reference address:51 MCU simple frequency meter source code

Previous article:PCF8591 (AD-DA) chip-51 single chip microcomputer program
Next article:51 MCU 18B20 temperature and serial port experiment

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号