51 MCU C language program (II) Timer/Counter interrupt

Publisher:廿由人Latest update time:2015-10-27 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Procedure 1

Use the timer/counter T0 to output a square wave with a period of 1s from P1.0
to make the light-emitting diode flash at 1HZ.
#include  //52 single-chip header file
#include //Library containing left and right circular shift sub-functions
#define uint unsigned int    //macro definition
#define uchar unsigned char  //macro definition
sbit P1_0=P1^0;
uchar tt;
void main()      //main function
{
 TMOD=0x01;//Set timer 0 to working mode 1
 TH0=(65536-50000)/256;
 TL0=(65536-50000)%256;
 EA=1;//Open the total interrupt
 ET0=1;//Open the timer 0 interrupt
 TR0=1;//Start timer 0
 while(1);//Wait for the interrupt to occur
 
}

void timer0() interrupt 1
{
 TH0=(65536-50000)/256;
 TL0=(65536-50000)%256;
 tt++;
 if(tt==20)
 {
  tt=0;
  P1_0=~P1_0;
 }
}

 

Procedure 2

Use timer/counter T1 to generate a timing clock, and
control 8 light-emitting diodes through P1 port,
so that the 8 indicator lights flash one by one in turn, with
a flashing frequency of 10 times/second (8 lights light up in turn for one cycle), in a cycle.
#include  //52 MCU header file
#include //Library containing left and right circular shift sub-functions
#define uint unsigned int    //Macro definition
#define uchar unsigned char  //Macro definition
sbit P1_0=P1^0;
uchar tt,a;
void main()      //Main function
{
 TMOD=0x01; //Set timer 0 to working mode 1
 TH0=(65536-50000)/256;
 TL0=(65536-50000)%256;
 EA=1; //Open total interrupt
 ET0=1; //Open timer 0 interrupt
 TR0=1; //Start timer 0
 a=0xfe;
 while(1); //Wait for interrupt to occur
 
}

void timer0() interrupt 1
{
 TH0=(65536-50000)/256;
 TL0=(65536-50000)%256;
 tt++;
 if(tt==2)
 {
  tt=0;
  P1=a;
  a=_crol_(a ,1);
 }
}

 

Procedure 3

 Two timers are used to control the buzzer sound at the same time. Timer 0 controls the frequency, and timer 1 controls the duration of
the same frequency. The square waves of 1, 10, 50, 100, 200, 400, 800, and 1k (hz) are output in turn at intervals of 2s.


#include  //52 MCU header file
#include //Library containing left and right circular shift sub-functions
#define uint unsigned int    //macro definition
#define uchar unsigned char  //macro definition
sbit beep=P2^3;
uchar tt;
uint fre,flag;

void main()      //Main function
{
 fre=50000;
 beep=0;
 TMOD=0x11;//Set timer 0 and timer 1 to working mode 1
 TH0=(65536-fre)/256;
 TL0=(65536-fre)%256;
 TH1=(65536-50000)/256;
 TL1=(65536-50000)%256;
 EA=1;//Open total interrupt
 ET0=1;//Open timer 0 interrupt
 ET1=1;
 TR1=1;
 TR0=1;//Start timer 0
 while(1);//Wait for interrupt to occur
 
}[page]

void timer0() interrupt 1   //Timer 0 interrupt
{
 TR0=0;    //After entering the interrupt, turn off the timer 0 interrupt to prevent the interrupt from being lost due to too many internal programs
 TH0=(65536-fre)/256;
 TL0=(65536-fre)%256;
 tt++;
 if(flag<40)    //The following ifs are used to select different frequencies
  if(tt==10)
   {
    tt=0;
    fre=50000;
    beep=~beep;
   }
 if(flag>=40&&flag<80)
  {
   tt=0;
   fre=50000;
   beep=~beep; 
  }
 if(flag>=80&&flag<120)
  {
   tt=0;
   fre=10000;
   beep=~beep; 
  }
 if(flag>=120&&flag<160)
  {
   tt=0;
   fre=5000;
   beep=~beep; 
  }
 if(flag>=160&&flag<200)
  {
   tt=0;
   fre=2500;
   beep=~beep; 
  }
 if(flag>=200&&flag<240)
  {
   tt=0;
   fre=1250;
   beep=~beep; 
  }
 if(flag>=240&&flag<280)
  {
   tt=0;
   fre=625;
   beep=~beep; 
  }
 if(flag>=280&&flag<320)
  {
   tt=0;
   fre=312;
   beep=~beep; 
  }
 if(flag>=320&&flag<360)
  {
   tt=0;
   fre=156;
   beep=~beep; 
  }
 TR0=1;
}
void timer1() interrupt 3  //Timer 1 interrupt is used to generate 2 seconds of time
{
 TH1=(65536-50000)/256;
 TL1=(65536-50000)%256;
 flag++;
 if(flag==360)
  {
    flag=0;
    fre=50000;
  }
}

 

Procedure 4


Use a timer to display 0, 1, 2, 3...C, D, E, F on the 6-digit digital tube in sequence at intervals of 500MS, and repeat.

#include  //52 MCU header file
#include //Library containing left and right circular shift sub-functions
#define uint unsigned int    //macro definition
#define uchar unsigned char  //macro definition
sbit dula=P2^6;       //digital tube segment selection latch end
sbit wela=P2^7;       ////digital tube bit selection latch end
uchar num,tt;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void main()
{
 num=0;
 tt=0;
 TMOD=0x01;//Set timer 0 to working mode 1
 TH0=(65536-50000)/256;
 TL0=(65536-50000)%256;
 EA=1;//Open the total interrupt
 ET0=1;//Open the timer 0 interrupt
 TR0=1;//Start timer 0
 dula=1;
 P0=0x3f;  //Send display 0 to the segment start
 . dula=0;//Close the segment selection latch to prevent garbled characters at the beginning.
 wela=1;//11101010
 P0=0xc0; //Open the six digital tubes and select
 wela=0;
 while(1)
 {
  if(tt==10)   //Every 10 interrupts are 500ms, and a display change is executed.
    {
    tt=0;
    num++;
    if(num==16)
    num=0;
    dula=1;
    P0=table[num];
    dula=0; 
   }
 }
}


void exter0() interrupt 1    // Timer 0 interrupt
{
 TH0=(65536-50000)/256;
 TL0=(65536-50000)%256;
 tt++;
}

Reference address:51 MCU C language program (II) Timer/Counter interrupt

Previous article:51 MCU C language program (III) Digital tube
Next article:Notes on making the minimum system of 51 single-chip microcomputer

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号