(4) Timer

51 The timer of the single-chip microcomputer belongs to the internal resource of the single-chip microcomputer, and the connection and operation of the circuit are all completed inside the single-chip microcomputer

effect:

  1. for timing systems
  2. Instead of a long delay, improve operating efficiency and speed
  3. task switching

STC89C52 timer resources:

Number of timers: 3 (T0, T1, T2), T2 is a new resource, T0T1 is compatible with the old version

The resource of the timer is associated with the model of the single chip microcomputer, and different models may have different numbers of timers and operation modes

According to the output signal of the clock, the value of the counting unit is increased by one every fixed period of time. When the value of the counting unit increases to the set value, the counting unit will send an interrupt request to the interrupt system to make the program jump to the interrupt service Execute in function

Both T0 and T1 of STC89C52 have four working modes:

  • Mode 0: 13-bit timer/counter
  • Mode 1: 16-bit timer/counter (commonly used)
  • Mode 2: 8-bit auto-reload mode
  • Mode 3: Two 8-bit counters

Please add a picture description

Counting unit: The clock sends pulses to the counting unit, and each pulse adds one. The counting unit has a total of 2*8=16 bits (maximum 65535), and overflows when the maximum value is exceeded. At this time, the flag bit TF0 sends an interrupt request

Clock: There are two sources

  • SYSclk system clock, that is, crystal oscillator period, 12MHz, at this time as a timer
  • T0 Pin external clock - at this time as a counter

Interrupt system: enable the CPU to have real-time processing capabilities for external emergency events, high-level interrupts are executed first, multi-level interrupts are called interrupt nesting, and the source of interrupt requests is called interrupt source

STC89C52 interrupt resources:

  • Number of interrupt sources: 8 (external interrupt 0, timer 0 interrupt, external interrupt 1, timer 1 interrupt, serial port interrupt, external interrupt 2, external interrupt 3)
  • Number of interrupt priorities: 4
  • interrupt number:

Please add a picture description

The register is the medium connecting software and hardware. The register in the microcontroller is a special RAM memory. On the one hand, the register can store and read data. On the other hand, a wire is connected behind each register to control the connection mode of the circuit. Registers are equivalent to the "action buttons" of a complex machine

TCON: Control Interrupt

Please add a picture description

Please add a picture description

TMOD: timing and counting function

Please add a picture description

Please add a picture description

12MHz adds one every microsecond, the counting unit (0~65535), and interrupts every second by assigning an initial value:

0~65535
总共定时65535us
计数单元初始值为64535,计时时间为1000us=1ms
所以TH0(高位)TL0(低位)应组合
TH0 = 64535/256; // 取出64535的高八位赋给TH0
TL0 = 64535%256; // 取出64535的低八位赋给TL0
1.因为64535是十进制数,要先转换为十六进制数所以除16;
2.然后还要取高8位,所以再除16,算到一起就是除256.
3.取模同理
void Timer0_Init(){
    
    
	TMOD = 0x01; // 0000 0001 定时模式1
    // 这里TMOD可以使用与或式赋值法,只操作特定位而不改变其他位
    // TMOD &= 0xf0;
    // TMOD |= 0x01;
	TF0 = 0; // 标志位初始化,防止产生中断
	TR0 = 1; // 运行控制位,开始计数工作
	TH0 = 64535 / 256; // 给计数单元赋初值
	TL0 = 64535 % 256;
	
	ET0 = 1;// 溢出中断允许位
	EA = 1; // 总中断允许控制位
	PT0 = 0; // 定时器0中断优先级控制位
}
void Timer0_Rountine() interrupt 1{
    
     // 中断号
    static unsigned int T0Count = 0;
	TH0 = 64535 / 256; // 给计数单元赋初值,保证每次移除后都是从64535开始重新计数
	TL0 = 64535 % 256;
	T0Count++;
	if(T0Count >= 1000){
    
     // 中断次数超过1000,即经过1秒后
		T0Count = 0;
		P2 = ~P2;
	}
}

A function header file: INTRINS.H

内部函数 描述
_crol_ 字符循环左移 溢出则从低位重新开始
_cror_ 字符循环右移
_irol_ 整数循环左移
_iror_ 整数循环右移
_lrol_ 长整数循环左移
_lror_ 长整数循环右移
_nop_ 空操作 8051 NOP 指令
_testbit_ 测试并清零位 8051 JBC 指令
_nop_ 空操作 8051 NOP 指令
_testbit_ 测试并清零位 8051 JBC 指令

Guess you like

Origin blog.csdn.net/Falling_Asteroid/article/details/130736668