51 microcontroller learning: timer 1 experiment

Publisher:灵感狂舞Latest update time:2023-07-19 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Experiment Name: Timer 1 Experiment
Wiring Instructions:
Experiment Phenomenon: After downloading the program, the D1 indicator light flashes at an interval of 1s.
Notes:
************************ *************************************************** *************/
#include "reg52.h"

typedef unsigned int u16; //Redefine the system default data type
typedef unsigned char u8;

//Define LED1 pin
sbit LED1=P2^0;

/****************************************************** ****************************
* Function name: delay_10us
* Function function: Delay function, when ten_us=1, the delay is approximately 10us
* Input: ten_us
* Output: None
**************************************** ***************************************/
void delay_10us(u16 ten_us)
{
while(ten_us --);
}

/****************************************************** ******************************
* Function name: time1_init
* Function function: Timer 1 interrupt configuration function, by setting TH and TL can determine the timing time
* Input: None
* Output: None
************************************ *******************************************/
void time1_init(void)
{
TMOD|=0X10;//Select timer 0 mode, working mode 1
TH1=0XFC; //Assign initial value to timer, timing 1ms
TL1=0X18;
ET1=1;//Turn on timer 1 interrupt enable
EA= 1;//Turn on the total interrupt
TR1=1;//Turn on the timer
}

/****************************************************** ******************************
* Function name: main
* Function function: main function
* Input: None
* Output: None
* *************************************************** ******************************/
void main()
{
time1_init();//Timer 1 interrupt configuration

while(1)
{

}
}

void time1() interrupt 3 //Timer 1 interrupt function
{
static u16 i; //Define static variable i
TH1=0XFC; //Assign initial value to timer, time 1ms
TL1=0X18;
i++;
if(i== 1000)
{
i=0;
LED1=!LED1;
}
}



Reference address:51 microcontroller learning: timer 1 experiment

Previous article:51 microcontroller learning: serial communication experiment
Next article:51 microcontroller learning: timer 0 experiment

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号