51 Microcontroller Basics: Timer Interrupt (2)

Publisher:勾剑寒Latest update time:2022-10-13 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

TMOD register:


Let’s talk about the TMOD register. This register can choose to turn on timer 0 or 1, and can also decide how the timer works. As shown in the picture:

Code:


#include


sbit LED=P2^0;


typedef unsigned char u8;

typedef unsigned int u16;


void time1_init(void)

{

        TMOD|=0x10; //TMOD is a register used to determine the working mode of the timer. The high four bits determine timer 1. Currently, it is working mode 1, which is 16-bit timing counting.

        TH1=0xfc; //High eight bits of timer

        TL1=0x18; //lower eight bits of timer

        ET1=1; //Interrupt enabled

        EA=1; //Total interrupt is turned on

        TR1=1; //Enable timer interrupt

}


void main(void)

{

        time1_init(); //Initialization

        while(1)

        {

        

        }

}


void tmie1(void) interrupt 3 //interrupt service function

{

        static u16 i; //No assignment required, automatically initialized to 0

        TH1=0xfc;

        TL1=0x18;

        i++;

        if(i==1000)

        {

            i=0;

            LED=!LED;

        }

}


operation result:


The result is the same as timer 0, it flashes once per second and no picture is sent.


Reference address:51 Microcontroller Basics: Timer Interrupt (2)

Previous article:51 Microcontroller Basics Dot Matrix LED8X8
Next article:51 Microcontroller Basics: Timer Interrupt (1)

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号