MCU Learning Thirteen: Flowing Light Pattern Change (Interruption)

Publisher:TranquilBreezeLatest update time:2016-09-09 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Experimental phenomenon:

         After entering the program, the program will operate as a two-way marquee; press the k0 key and the program will operate as a left-hand marquee; press the k1 key and the program will operate as a right-hand marquee; press the k2 key and the two bright spots will move from both sides to the middle; press the k3 key and the two bright spots will move from the middle to both sides.

2. Experimental Purpose:
       Master the response method of processing multiple key actions in the interrupt program

3. Experimental task analysis:
       (Note: When doing this experiment, don't forget to put the JMP0 jumper in the position of 1, 2, and select the diode display unit.)
       In the previous two interrupt modes to respond to key actions, we only responded to one key action, which was relatively easy.
       In this program, our interrupt program needs to respond to multiple key actions. There are 4 kinds of light changes, which are switched by different keys. So, how should this problem be handled?
       We can do it like this: In the main program, we make left and right marquees, which I believe everyone is already familiar with.
        In the interrupt service program, we first read the key status, then delay 10ms, and read the key status again. Compare the key status obtained twice. If they are different, it means jitter, and exit the interrupt; otherwise, determine which key is pressed. If it is k0, execute the left marquee; if it is k1, execute the right marquee; if it is k2, execute the bright spot to move to the middle; if it is k3, execute the bright spot to move to both sides.
         This idea is generally correct, but there is one thing to note: when we write programs, in the interrupt service program, we should try to simplify the tasks, do not let the interrupt service program do too many complex tasks, and try to put these complex tasks in the middle of the main program to complete.
According to this idea, our approach is as follows:
          set 5 flags in the main program, and then constantly query these flags: if it is flag 1, execute the two-way marquee; if it is flag 2, execute the left marquee; if it is flag 3, execute the right marquee, and so on.
          In the interrupt service program, we only need to do the following work: de-jitter, key recognition, change the flag, that's it.
          In this program, we use the content in register r0 as a flag. When the content in it is 0ffh, execute the left and right marquee program; when it is 00h, execute the left marquee; when it is 01h, execute the right marquee; when it is 02h, execute the two bright spots to move to the middle; when it is 03h, execute the two bright spots to move to both sides.
         Now let's take a look at the flowchart of the main program and the interrupt service program:

 

 

         The following is a program written according to this idea. The structure of this program is slightly complicated, so please pay attention.

4. The experimental procedure is as follows:
                org 0000h

           ajmp start

      

           org 0013h

           ajmp ext1

       

           org 0020h

start: clr p1.5    

           mov r0,#0ffh; Assign an initial value to r0, r0 is the flag we set,

         

           setb ea ; enable interrupt

           setb ex1 ; allow external interrupt 1 to request interrupt

           setbit1 ; Set external interrupt 1 jump mode trigger

           mov sp,#70h ;Set up the stack

                

loop0: cjne r0,#0ffh,loop1 ; If the content in r0 is not 0ffh, go to loop1 

           ajmp main_light ; Otherwise, execute the left and right marquee

loop1: cjne r0,#00h,loop2; if the content in r0 is not 00h, go to loop2

           ajmp k0_light ; Otherwise, execute the left marquee

loop2: cjne r0,#01h,loop3 ; if the content in r0 is not 01h, go to loop3

           ajmp k1_light ; Otherwise, execute the right marquee

loop3: cjne r0,#02h,loop4; if the content in r0 is not 02h, go to loop4

           ajmp k2_light ; Otherwise, execute the double bright spot to move to the middle

loop4: cjne r0,#03h,loop5 ;If the content in r0 is not 03h, return and restart the query

           ajmp k3_light ; Otherwise, execute double bright spots to move to both sides

loop5: ajmp loop0 ; return and restart the query

     

ext1: clr ea ; disable interrupts

        

           push acc ; scene protection

           push psw

      

           mov a,p1 ; read key status

           anl a,#0fh ; shield the upper four bits

          mov 30h,a ; save the keyboard status value in 30h 

           mov a,p1 ; read the key status again

anl a,#0fh ; shield the upper four bits

cjne a,30h,pass ;If the two key values ​​are not equal, it means jitter, exit interrupt

           ajmp k0_check ; if equal, jump to key recognition program

 

; The following is the key recognition program

k0_check: cjne a,#0dh,k1_check; If k0 is not pressed, check whether k1 is pressed

           ajmp k0_manage ; Otherwise, jump to the key processing procedure of k0

k1_check: cjne a,#0eh,k2_check; If k1 is not pressed, check whether k2 is pressed

           ajmp k1_manage ; Otherwise, jump to the key processing procedure of k1

k2_check: cjne a,#0bh,k3_check; If k2 is not pressed, check whether k3 is pressed

           ajmp k2_manage ; Otherwise, jump to the key processing procedure of k2

k3_check: cjne a,#07h,pass ;If k3 is not pressed, exit interrupt

           ajmp k3_manage ; Otherwise, jump to k3's key processing procedure

     

         ; The following are the corresponding processing procedures for each key,

k0_manage: mov r0,#00h ; set flag 2

            ajmp pass        

         

k1_manage: mov r0,#01h ; set flag 3

            ajmp pass       

 

k2_manage: mov r0,#02h ; set flag 4

            ajmp pass     

 

k3_manage: mov r0,#03h : set flag 5

            ajmp pass    

 

pass: pop psw ; restore the scene

           pop acc

         

           setb ea ; enable interrupt

           reti ;Interrupt return

           

main_light: mov r7,#08h ;main_light is the left and right running light program

            mov r6,#06h

            mov a,#0feh

l_loop: mov r1,a ; Use r1 to save the current bright spot position, so that you can start from this position when changing the light

            mov p0,a

            lcall del100ms

            rl a

            djnz r7,l_loop

           

            mov a,#0bfh

r_loop: mov r1,a

            mov p0,a

            lcall del100ms

            rr a

            djnz r6,r_loop

            ljmp loop0

           

k0_light: mov a,r1; k0_light is the left-hand marquee program

            mov p0,a

            lcall del100ms

            rl a

            mov r1,a

            ajmp loop0

        

k1_light: mov a,r1; k1_light is the right-hand marquee program

            mov p0,a

            lcall del100ms

            rr a

            mov r1,a

            ajmp loop0

           

k2_light: mov p0,#7eh; k2_light is a double bright spot to the middle program

            lcall del100ms

            mov p0,#0bdh

            lcall del100ms

            mov p0,#0dbh

            lcall del100ms

            mov p0,#0e7h

            lcall del100ms

            ajmp loop0

           

k3_light: mov p0,#0e7h; k3_light is a program for double bright spots to both sides

            lcall del100ms

            mov p0,#0dbh

            lcall del100ms

            mov p0,#0bdh

            lcall del100ms

            mov p0,#7eh

            lcall del100ms

            ajmp loop0

 

        

del10ms: mov r5,#20 ;10ms delay program

del1: mov r4,#0ffh

del2: djnz r4,del2

           djnz r5,del1

           ret;

 

del100ms: mov r3,#200 ;100ms delay program

del3: mov r2,#0ffh

del4: djnz r2,del4

           djnz r3,del3

           ret

        

           end;       

 

 MCU Learning Thirteen: Flowing Light Pattern Change (Interruption) - Polaris - To Be a Good Seed

MCU Learning Thirteen: Flowing Light Pattern Change (Interruption) - Polaris - To Be a Good Seed

Keywords:MCU Reference address:MCU Learning Thirteen: Flowing Light Pattern Change (Interruption)

Previous article:MCU Learning 15: Timer Application 2 (Mode 2)
Next article:MCU Learning 12: Button Control Marquee (Interrupt)

Recommended ReadingLatest update time:2024-11-23 16:33

Introduction to the internal RAM of the 80C51 microcontroller
The 80C51 internal RAM has 256 units, which are usually divided into two areas in space. The internal data RAM area of ​​the lower 128 units (00H~7FH) and the special register SFR area of ​​the upper 128 units (80H~0FFH). Internal RAM low 128 cells The lower 128 units of 80C51 are the real internal data RAM area. It
[Microcontroller]
Introduction to the internal RAM of the 80C51 microcontroller
Avoid MCU or programming language interference in design
As an experienced embedded systems developer, with experience in both large systems (Boeing 777 flight controls) and small one-person projects (laptop thermal fan control), one should avoid the specific pros and cons of a single machine or language, spend more time on application design and construction, and be inde
[Analog Electronics]
Avoid MCU or programming language interference in design
Detailed explanation of the definitions, characteristics and relationships of microcontrollers, ARM, MCU, DSP, FPGA and embedded systems
First of all, "embedded" is a concept. There is no accurate definition. Different books have their own definitions. But the main idea is the same. Compared with general systems such as PCs, embedded systems are special systems with a streamlined structure. Only the necessary parts are retained in both hardware and sof
[Microcontroller]
AVR microcontroller I²C bus experiment
/* AVR microcontroller I2C bus experiment. 1. Use 24C02 to record the number of CPU startup times and display it on the PB port. 2. Internal 1 M crystal oscillator, the program adopts single-task mode and software delay. 3. To conduct this experiment, please plug in all 8 short-circuit blocks of JP1 and the shor
[Microcontroller]
MCU ISP download circuit diagram (74hc244)
MCU ISP download circuit diagram (74hc244)
[Analog Electronics]
MCU ISP download circuit diagram (74hc244)
51 MCU lights up the first LED
First, we need to prepare the 51 development board, code software keil4, and burning software (I use the Puzhong Technology burning software). First, we need to understand the working principle. The following is the circuit diagram of the development board. It can be seen that the cathode of the LED is connected to
[Microcontroller]
51 MCU lights up the first LED
Mobile Wireless Data Transmission System Designed Based on 51 Single Chip Microcomputer
introduction    At present, there are three main ways of mobile wireless data transmission: GSM short message, GPRS and CDMA. Their characteristics are compared as follows: ① GSM short message method of mobile communication network. Short message is one of the telecommunications services of GSM network. The so-calle
[Microcontroller]
Mobile Wireless Data Transmission System Designed Based on 51 Single Chip Microcomputer
Hardware Design of LED Display Screen Based on 51 Single Chip Microcomputer and Programmable Logic Devices
0 Introduction The LED display screen is mainly composed of three parts: current drive circuit and LED dot matrix array, control system and PC management software (Figure 1). The control system is responsible for receiving, converting and processing various external signals, and realizing scanning control, and t
[Microcontroller]
Hardware Design of LED Display Screen Based on 51 Single Chip Microcomputer and Programmable Logic Devices
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号