51 MCU【I】Background timer

Publisher:chunxingLatest update time:2020-03-15 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Microcontrollers MCU is an integrated circuit chip. It uses large-scale integrated circuit technology to integrate the central processing unit CPU with data processing capabilities, random access memory RAM, read-only memory ROM, multiple I/O ports and interrupt systems, timer/counter and other functions (may also include display drive circuit, pulse width modulation circuit, analog multiplexer, A/D converter and other circuits) on a silicon chip to form a small and complete microcomputer system. It is widely used in the field of industrial control. Since the 1980s, it has developed from the 4-bit and 8-bit microcontrollers at that time to the current 300M high-speed microcontrollers.


These are found on Baidu


Simply put, it is a micro and cheap logic circuit to control many electrical things around us. 


A single chip is a single silicon chip embedded in other systems, which is a simple and low-level embedded system. Embedded refers to embedded products. In a narrow sense, it is embedded in Linux systems such as Android phones.


The blogger is majoring in automation, so microcontrollers are very important. Since I am only a freshman now, I will start with the simple 51 and then further study arm. Embedded systems are also a good direction in the future, so I will record my learning process and welcome everyone to study with me.


The first step is to install the environment keil c51


Link: https://pan.baidu.com/s/1B-gWsxK2wiFtWQe5I4AUAA 

Extraction code: 8pro 


When activating, you need to run it as an administrator and then perform the activation instructions. There are


(Remember to wear headphones to activate some sexy music)


Install the download tool (download the program to the single-sided machine, this depends on the microcontroller you use, here is stc)


Link: https://pan.baidu.com/s/1vELF1DiPe3eykRs8HbWQYA 

Extraction code: 9pe4 


The 51 microcontroller was developed by Intel a long time ago and its core was open to the public, so anyone can see it. 


Also, you need to install the USB to serial port driver, which can be automatically installed by the driver master.


Link: https://pan.baidu.com/s/120E1RS-UD47PJLlO_SMb6A 

Extraction code: f0ci  

If you can see this in the device manager, it means it is installed, but it may not be COM4. It may be any COM.


MCU Timer


1. The meaning of CPU is like the alarm clock to people


Because the CPU can only do one thing at a time, it is necessary to set a time to end and start tasks.


2. Use the interrupt mechanism software and hardware agreement to let the CPU execute a specific program function that interrupts the program after a period of time.


3. There can be multiple timers that can work simultaneously. 51 is two 8-bit long time can be 16-bit short time can be 8-bit


4. Using the counter principle, the 51 single-chip microcomputer uses the addition timer overflow and then interrupts. Similar to data overflow, there is also a subtraction timer that decrements from the initial value.


5. Timing initial values ​​THn and TLn


The microcontroller wizard can input time, calculate initial time value and many other functions


Link: https://pan.baidu.com/s/1tWZo-ByP8J1TMj07gQ6miw 

Extraction code: z03r 


See the development manual for specific usage


void main(void)

{

    Timer0Init(); //Timer initialization

    while (1); //At this time, the CPU main task is 1 infinite loop

}

void Timer0Init(void)

{

TMOD &= 0xf0; // T0 is set to timer mode, only TR0 is used to control opening

TMOD |= 0x01; // T0 is selected as working mode 1: 16-bit timer, TH0 TL0 are fully used

 

TH0 = 0xd8; // Use the MCU Elf to calculate the initial values ​​of TH0 and TL0 for 10ms timing

TL0 = 0xf0;

ET0 = 1; // Enable timer 0 interrupt

EA = 1; // Enable general interrupt

TR0 = 1; // Start timing

}

void Timer0Isr(void) interrupt 1 //1 represents T0 timer

{

TH0 = 0xd8; // Use the MCU Elf to calculate the initial values ​​of TH0 and TL0 for 10ms timing

TL0 = 0xf0;

if (gCnt++ >= 50) // Every 50*10ms = 500ms, the LED will switch on and off

{

gCnt = 0; // Set gCnt to the initial value 0 to start the next interrupt count

gLed1 = !gLed1; // LED on and off conversion! It is logic inversion

}

}

Tips: Give an initial value every time


Experimental phenomenon: Flashes every 500ms

How to make the microcontroller run the program you wrote? You need to use the burning software to select the correct signal and serial port number, then open the program file and click download. After the words "detecting the target microcontroller" appear, turn on the microcontroller switch.


This is probably because the microcontroller memory cannot be modified when powered on.

Reference address:51 MCU【I】Background timer

Previous article:51 MCU 8*8 dot matrix snake
Next article:51 single chip microcomputer, dot matrix display Chinese characters (C language)

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号