Interrupts and timers to implement clock functions

Publisher:DazzlingSpiritLatest update time:2018-05-13 Source: eefocusKeywords:Interrupt Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Interrupts and timers implement a 24-hour clock. The program is as follows:

#include

 

#define PORTLEN P0

 

sbit bit_select = P2^0;

sbit seg_select = P2^1;

 

unsigned char src[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

unsigned char qrc[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

unsigned char second1[8];

unsigned char shi = 0;

unsigned char fen = 0;

unsigned char miao = 0;

 

void timer0_init(void)

{

  EA = 1; // Enable general interrupt

  TMOD |= 0x01; //Set working mode

  TH0 = (65536 - 20000) / 256;

  TL0 = (65536 - 20000) % 256; //Set the initial value of the timer and generate an interrupt every 1ms

  ET0 = 1; // Enable timer interrupt

  TR0 = 1; //Turn on the timer and start adding 1 every 1ms

} // Turn on the timer and start adding 1. When TH0 = 0xff, TL0 = 0xff, TF0 (the overflow flag of the internal timer of the microcontroller, generally no need to set it manually) is set from 0 to 1, triggering an interrupt, and reset to 0.

Then request an interrupt from the CPU. If the CPU responds, it jumps to the interrupt function, i.e. void timer0_isr() interrupt 1

 

void timer1_init(void)

{

  EA = 1;

  TMOD |= 0x10;

  TH1 = (65536 - 1000) / 256;

  TL1 = (65536 - 1000) % 256;

  ET1 = 1;

  TR1 = 1;

}

 

void main()

{

      timer0_init();

  timer1_init();

  while(1);

}

//while(1) is equivalent to an infinite loop. There is a point to note here. The difference between putting the two timers inside and outside the while is explained in detail later.

 

void display()

{

    static unsigned char i = 0;

  PORTLEN = 0x0; // Eliminate ghosting

  seg_select = 1;

  seg_select = 0;

  PORTLEN = 0xff;

  bit_select = 1;

  bit_select = 0;

  PORTLEN = second1[i];

  seg_select = 1;

  seg_select = 0;

  PORTLEN = qrc[i];

  bit_select = 1;

  bit_select = 0;

  

  i++;

  if(8 == i)

{

  i = 0;

}

}

 

void timer0_isr() interrupt 1        

{

  static unsigned char i = 0;

    TH0 = (65536 - 20000) / 256;

    TL0 = (65536 - 20000) % 256; //Set the initial value of the timer and generate an interrupt every 20ms

  i++;

  if(50 == i)

{

  i = 0;

        miao++;

  if(60 == miao)

{   

  miao = 0;

  fen++;

  if(60 == fen)

{

  fen = 0;

  shi++;

  if(24 == shi)

{

  shi = 0;

}

}

}

    }

second1[0] = src[shi / 10];

second1[1] = src[shi % 10];

second1[2] = 0x40;

second1[3] = src[fen / 10];

second1[4] = src[fen % 10];

second1[5] = 0x40;

second1[6] = src[miao / 10];

second1[7] = src[miao % 10];

}

 

void timer1_isr() interrupt 3

{

    TH1 = (65536 - 1000) / 256;

  TL1 = (65536 - 1000) % 256;

  display();

}

 //interrupt x The x value following interrupt is the interrupt number, which means the interrupt port number to which this function corresponds. In 51, 0 is external interrupt 0; 1 is timer 0; 2 is external interrupt 1; 3 is timer 1; 4 is serial interrupt

In fact, when compiling, the entry address of your function is set to the jump address of the corresponding interrupt.

——————————————————————————————

Next, let’s talk about while. Use A and B to replace the two functions timer0_init and time1_init respectively. A+ and B+ represent two interrupt service routines.

①After A and B are executed, A continues to execute. However, it should be noted that the execution of A and B only turns on the timer at the end of the execution. It can be almost regarded as the end of A to start the timer accumulation, and then the same for B. However, as soon as B ends, it comes to A again. TH and TL in A are reset, resulting in interruption all the time and no entry into the interrupt service function. If you want to modify it here, you need to add your own delay function to give it time to enter the interrupt service program, but this is not as meaningful as the second method.

② In the second case, after A and B are executed, they stay in the while state, waiting for A and B to send out interrupt responses, entering the interrupt service routine according to the priority, and waiting for the interrupt service routine to end according to the internal interrupt, and returning to the while state. (The initial value has been given again in the interrupt service routine, and the timer will not be turned off automatically after being turned on). B has been refreshing the display, and A is giving values ​​to the temp array.

The execution time on the right side of the above figure is wrong. I forgot to change it. The 1ms and 20ms in the interrupt service program are just re-initialized.


Keywords:Interrupt Reference address:Interrupts and timers to implement clock functions

Previous article:51 MCU timer interrupt program
Next article:Interrupt timer and running light

Recommended ReadingLatest update time:2024-11-16 08:53

PIC16F877A microcontroller (interrupt and timer Timer1)
1 Basic principles In the figure above, if an external crystal oscillator is connected to the left side of RC0, then T1OSCEN must be set to 1. The frequency of this external crystal oscillator is generally low. The lower the crystal oscillator frequency, the lower the power consumption. Why is an external cry
[Microcontroller]
PIC16F877A microcontroller (interrupt and timer Timer1)
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号