Jp3 is connected to P0 port.
# Include <reg51.h> # define uchar unsigned charuchar flag, num; uchar code SMG [10] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99,0x92,0x82, 0xf8, 0x80, 0x90}; // declare the subfunction void Init (); void inittimer0 (); void main () {Init (); inittimer0 (); While (1) {// none of the Code} void timer0 () interrupt 1 {// re-install the initial value, because the cycle time is required, the time is 50 milliseconds th0 = (65536-50000) /256; tl0 = (65536-50000) % 256; flag ++; If (flag = 20) {// Add 1 flag = 0 every second; p0 = SMG [num]; num ++; If (num = 10) {num = 0 ;}} void inittimer0 () {// initial value, timing 50 ms th0 = (65536-50000)/256; tl0 = (65536-50000) % 256; // select the timer t0 Mode 1 tmod = 0x01; // enable/disable et0 = 1; // enable/disable total CPU Ea = 1; // start the timer tr0 = 1;} void Init () {flag = 0; num = 0 ;}
The two timers are used in the same way. Now let's summarize the usage steps of the Timer:
1. Initial Values
2. Determine the working method. The related register is tmod.
3. Enable the et0 or ET1, and EA
4. Start the timer
5. Write interrupt service programs
Void function name () interrupt 1 (if timer 1 is selected, the code is 3)
{
// Reinstall the Initial Value
}
MCU Lesson 10th: timer interruption