The STC15W104 timer realizes the flashing of the light (explaining the principle of the timer)

One: STC15W104 MCU has several timers

The STC15W104 microcontroller has two timers in total, which are timer 0 and timer 2.

insert image description here

Two: The role of the timer

A timer is a commonly used peripheral in microcontrollers and is used to generate interrupts at certain time intervals.

Timers are commonly used in applications such as timing, measuring time intervals, generating PWM signals, etc.
In the STC15W104 microcontroller, the timer can be configured as a timer/counter mode.
In timer mode, the timer will generate an interrupt signal after a specified time interval;
in counter mode, the timer will count according to the external clock input, and when the count value reaches the specified threshold, the timer will generate an interrupt signal . The use of the timer needs to configure the corresponding registers, and the specific usage method can refer to the relevant data sheet.

Three: The working mode of the timer

The working mode of the timer can be configured through the corresponding registers, specifically, it can be configured through the following registers:

  • TCON: Timer control register, used to configure the working mode of the timer, interrupt flag bits, etc.
  • TMOD: Timer mode register, used to configure the working mode of the timer, the unit of measurement of the count value, count/timer selection, etc.
  • THx/TLx: Timer high/low count register, used to store the high 8 bits/low 8 bits of the timer count value.

In timer mode, the workflow of the timer is as follows:

  1. First, the TMOD register needs to be configured to select the timer’s working mode (0/1/2/3) and the counting unit of measurement (12-bit/16-bit).
  2. Then, THx/TLx registers need to be configured to set the initial count value of the timer.
  3. Next, the timer needs to be started to start timing. When starting the timer, the corresponding bit in the TCON register needs to be set to make the timer work.
  4. When the timer count value reaches the set threshold, the timer will generate an interrupt signal, and at this time, the interrupt flag position in the TCON register will be set, thereby triggering the interrupt service routine.

In the counter mode, the workflow of the timer is similar to that of the timer mode, the only difference is that the source of the timer count value is different. In counter mode, the timer counts according to the external clock input, so it is necessary to connect the external clock input to the timer.

Frequency division mode

In the timer of STC15W104 single-chip microcomputer, timer 0 and timer 1 support the frequency division function of the external clock source.
The frequency division function can divide the frequency of the external clock source by the specified coefficient, thereby reducing the counting speed of the timer. The frequency division coefficient can be configured through the corresponding registers, specifically, it can be configured through the following registers:

  • The upper 4 bits (M1, M0) in TMOD: used to select the frequency division coefficient.
  • The lower 2 bits in AUXR (T0x12, T1x12): used to select the multiplier of the frequency division coefficient.

Among them, the M1 and M0 bits of the TMOD register are used to select the frequency division coefficient, and the specific configuration method is as follows:

  • M1=0, M0=0: no frequency division.
  • M1=0, M0=1: divide by 2.
  • M1=1, M0=0: divide by 4.
  • M1=1, M0=1: divide by 8.

The T0x12 and T1x12 bits of the AUXR register are used to select the multiplier of the frequency division coefficient. The specific configuration method is as follows:

  • T0x12=0, T1x12=0: No multiplication.
  • T0x12=0, T1x12=1: Multiplier 2.
  • T0x12=1, T1x12=0: No multiplication.
  • T0x12=1, T1x12=1: No multiplication.

Through the frequency division function, the counting speed of the timer can be effectively reduced to adapt to different timing requirements.

Four: Calculation of timer timing time

ISP software find routine

insert image description here

program source code

/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC15F4K60S4 系列 定时器0的16位自动重装载模式举例---------------*/
/* --- Mobile: (86)13922805190 -------------- -------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966-------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.GXWMCU.com --------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序        */
/* 如果要在文章中应用此代码,请在文章中注明使用了STC的资料及程序        */
/*---------------------------------------------------------------------*/

//本示例在Keil开发环境下请选择Intel的8058芯片型号进行编译
//若无特别说明,工作频率一般为11.0592MHz


#include "reg51.h"

typedef unsigned char BYTE;
typedef unsigned int WORD;

//-----------------------------------------------

#define FOSC 11059200L

#define T1MS (65536-FOSC/1000)      //1T模式
//#define T1MS (65536-FOSC/12/1000) //12T模式

sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xC9;
sfr P5M0 = 0xCA;
sfr P6M1 = 0xCB;
sfr P6M0 = 0xCC;
sfr P7M1 = 0xE1;
sfr P7M0 = 0xE2;

sfr AUXR = 0x8e;                    //Auxiliary register
sbit P10 = P1^0;

//-----------------------------------------------

/* Timer0 interrupt routine */
void tm0_isr() interrupt 1
{
    
    
    P10 = ! P10;                    //将测试口取反
}

//-----------------------------------------------

/* main program */
void main()
{
    
    
    P0M0 = 0x00;
    P0M1 = 0x00;
    P1M0 = 0x00;
    P1M1 = 0x00;
    P2M0 = 0x00;
    P2M1 = 0x00;
    P3M0 = 0x00;
    P3M1 = 0x00;
    P4M0 = 0x00;
    P4M1 = 0x00;
    P5M0 = 0x00;
    P5M1 = 0x00;
    P6M0 = 0x00;
    P6M1 = 0x00;
    P7M0 = 0x00;
    P7M1 = 0x00;

    AUXR |= 0x80;                   //定时器0为1T模式
//  AUXR &= 0x7f;                   //定时器0为12T模式

    TMOD = 0x00;                    //设置定时器为模式0(16位自动重装载)
    TL0 = T1MS;                     //初始化计时值
    TH0 = T1MS >> 8;
    TR0 = 1;                        //定时器0开始计时
    ET0 = 1;                        //使能定时器0中断
    EA = 1;

    while (1);
}


Then the program is transplanted and modified

The following is our modified program

おすすめ

転載: blog.csdn.net/weixin_51624736/article/details/130472291