"Beginner's C51 Self-study Notes" Timer Code (Part 3)

Publisher:彭勇Latest update time:2022-02-15 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Initialization procedure: 

 Assign values ​​to TMOD to determine how T0 and T1 work.

Calculate the initial value and write it into TH0, TL0 or TH1, TL1.

In interrupt mode, assign values ​​to EA and enable timer interrupt.

Set TR0 or TR1 to start the timer/counter timing or counting.

//Function realization: Digital tube increases by one per second;

#include

#define uchar unsigned char 

#define uint unsigned int 

 

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

 

void main()

{

TMOD=0x01; //Set timer 0 to working mode 1

TH0=(65536-50000)/256; //Set the initial value once every 50ms

TL0=(65536-50000)%256;

EA=1; //Turn on the general interrupt switch

ET0=1; //Enable timer interrupt

TR0=1; //Start the timer

while(1)

{

if(kk==20) // 20 times is 1 second

{

k=0;

P1=smg_du[num];

num++;

if(num==10) //Prevent array overflow

num=0;

}

}

 

}

 

void timer0 () interrupt 1

{

TH0=(65536-50000)/256; //Set the initial value

TL0=(65536-50000)%256;

k++;

 

}


Reference address:"Beginner's C51 Self-study Notes" Timer Code (Part 3)

Previous article:"Beginner's C51 Self-study Notes" - Four working modes of timer (Part 2)
Next article:"Beginner's C51 Self-study Notes" Serial Port

Recommended ReadingLatest update time:2024-11-24 20:36

C51 Programming 15-Interrupts (Timer Interrupts 2)
To use the timer/counter interrupt, the following conditions must be met. 1) Interrupts are always enabled EA = 1; 2) The interrupt source is allowed to be turned on ET0 = 1 or ET1 = 1; 3) Set the working mode of the timer/counter (set TMOD) 4) Load the timer and set the initial calculation value of the counte
[Microcontroller]
C51 Programming 15-Interrupts (Timer Interrupts 2)
[C51 code] Menu function [for LCD1602]
#ifndef __menu_h__ #define __menu_h__ #include "head.h" #include "LCD1602_2.h" #include "DS18B20.h" #define Null 0 /*********************** * Function declaration * ***********************/ void ShowMenu(void); void Menu_Change(volatile KeyNum); /*********************** * Key function key macro defi
[Microcontroller]
Introduction to C51/C52 MCU interrupt (EXTI) (I)
1. What is an interrupt? Interrupt: interrupt the current work, execute the program in the interrupt function, and then come back to execute the unfinished program after the execution is completed. As shown in the following figure: 2. Interrupt source and interrupt number Note: When writing an interrupt service
[Microcontroller]
Introduction to C51/C52 MCU interrupt (EXTI) (I)
Solution to directly use binary numbers in Keil C51
    In Keil C51, numbers cannot be assigned directly in binary form, although it is possible in 8051 assembly. Although binary numbers are long to write and prone to errors, because they are written one bit at a time, programmers can clearly see the status of each bit, which is more intuitive. Therefore, many people m
[Microcontroller]
The microcontroller part of the program for serial communication between C51 microcontroller and PC
Note that the baud rate is 9600 Function: The computer sends a byte to the microcontroller through the serial port, and the microcontroller sends it back to the computer after receiving it. 、#include #define uchar unsigned char #define uint unsigned int uint temp; bit flag; void init_ser1(); main() { init_ser1();
[Microcontroller]
C51 single chip counter experiment
Experimental requirements Implement a stopwatch display. Specifically, draw a MSC51 microcontroller and two eight-segment digital tubes. After running, the digital tubes will display 00-59 in sequence, display in a cycle, and jump back after 59. Interrupts must be used, T0 or T1 is not limited, mode 0, 1, 2 is not l
[Microcontroller]
C51 single chip counter experiment
C51 program-controlled laser advertising system
introduction At present, laser technology is developing day by day and is widely used in basic research, medical treatment, machinery, communication and other fields. Computers are often used for visual laser control because they have high speed and large storage space and can control large systems. When controlling
[Microcontroller]
C51 program-controlled laser advertising system
Support for Dual Data Pointers and Code Generation in Keil C51
In the 8051 system, the data pointer DPTR is a special 16-bit register used to address the 64 KB XDATA or CODE space. It is usually used as a 16-bit pointer to a constant table. Dual data pointers can improve the performance when two 16-bit pointers are used at the same time. As an enhanced feature, many 8051 deriva
[Microcontroller]
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号