15. Timer Interrupt

Publisher:山宝宝Latest update time:2022-04-21 Source: eefocusKeywords:Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Use timer interrupt 0 to flash LED1 (one second on and one second off)


#include

typedef unsigned char u8;

typedef unsigned int u16;


sbit led=P2^0;//D1


void Timer0Init()

{

TMOD|=0x01; //Select timer 0 mode, mode 1, only TR0 controls start

TR0=1; //Turn on timer 0

EA=1; //Total interrupt

ET0=1; //Turn on timer 0 interrupt

TH0=0xFC;

TL0=0x18; //Assign initial value to timing 1ms

}

void main()

{

Timer0Init(); //Timer 0 initialization

while(1);

}

void Timer0() interrupt 1

{

static u16 i;

TH0=0xFC;

TL0=0x18; //Assign initial value to the timer

i++;

if(i==1000)

{

led=~led;

i=0;

}

}


Use timer interrupt 1 to flash LED1 (one second on and one second off)


#include

typedef unsigned char u8;

typedef unsigned int u16;


sbit led=P2^0;//D1


void Timer1Init()

{

TMOD|=0x10; Select timer 1 mode, mode 1, only TR1 control start

TR1=1;

EA=1;

ET1=1;

TH1=0xFC;

TL1=0x18;

}

void main()

{

Timer1Init();

while(1);

}

void Timer1() interrupt 3 //Note that the interrupt number is 3

{

static u16 i;

TH1=0xFC;

TL1=0x18;

i++;

if(i==1000)

{

led=~led;

i=0;

}

}


The last digit of the digital tube displays 0-F in a cycle with an interval of one second.


#include

typedef unsigned char u8;

typedef unsigned int u16;


sbit LSA=P2^2;

sbit LSB=P2^3;

sbit LSC=P2^4;


u8 code smgduanxuan[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

                     0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

  

void Timer1Init()

{

TMOD=0x10; Select timer 1 mode, mode 1, only TR1 control start

EA=1; //Open the general interrupt

ET1=1; //Open timer interrupt 1

TR1=1; //Turn on timer interrupt 0

TH1=0xFC;

TL1=0x18; //Assign initial value, timing 1ms

}

void main()

{

LSA=0;

LSB=0;

LSC=0;

Timer1Init();

while(1);

}

void Timer1() interrupt 3

{

static u16 i;

u8 n;

TH1=0xFC;

TL1=0x18;

i++;

if(i==1000)

{

P0=smgduanxuan[n++];

if(n==16)

{

n=0; //Return to zero when the 16th number is displayed

}

i=0; //Return to zero at 1s

}

}

Keywords:Timer Reference address:15. Timer Interrupt

Previous article:16.Serial communication
Next article:14. Timers and Counters

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号