Atmel MCU Application Skills

Publisher:明石轩Latest update time:2015-02-02 Source: laoguKeywords:Atmel Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Powerful clock interruption

  In program design, setting a good clock interruption can greatly facilitate and simplify program compilation and improve system efficiency and operability. The following takes the 89C51 system with a 6MHz clock as an example to illustrate the application of clock interruption.

  Timer initial value and interrupt cycle. The time interval of clock interrupt is generally 20ms (50Hz). If a time base signal of one hundredth of a second is required, 10ms (100Hz) can be used. Here we take 20ms. T0 works in 16-bit timer mode (mode 1). After each machine cycle, T0 automatically adds 1. When it reaches the next machine cycle of 0FFFFh, T0 overflows and generates an interrupt. The hardware sets the corresponding flag bit for software to query. That is, after N+1 machine cycles after the interrupt is started, T0 generates an interrupt. Therefore, as long as a number N less than 0FFFFh is stored in T0 first, and then the timer is started, an interrupt will be generated after N+1 machine cycles. This number is the so-called "initial value". If the clock is 6MHz, there are 10,000 machine cycles in 20ms. (10000)10=(2710)16, then 0FFFFh-2710h+1=0D8F0h. Since it takes 7-8 machine cycles to respond to interrupt, protect the scene and reload the initial value, the initial value that should be loaded into T0 is 0D8F7h. After each interrupt is entered, the values ​​of A and PSW are first stacked, and then 0D8F7h is loaded into T0.

  Set a unit, and add 1 for each interrupt. Name a unit in RAM INCPI. In the interrupt processing program, after loading the initial value of T0, use the "INC INCPI" instruction to add 1 to it. Whether it is an interrupt program or a main program, you can read any integer multiple of 20ms between 1 and 256 from INCPI. For example, if the program for sending display to the digital tube needs to refresh the display every 0.5 seconds, you can set a waiting unit W_DISP, use the three statements "MOV A, INCPI/ADD A, #25/MOV W_DISP, A" to make it 25 greater than the current INCPI value, and then check whether W-DISP is equal to the INCPI value in each interrupt. If they are equal, it means that 25 interrupt cycles have passed, execute the display sending program, and let W_DISP add 25 and wait for the next 0.5 seconds. Multiple waiting units can be set in the program to obtain multiple time base signals.

  Read keys and watch in interrupts. Usually, we read the keyboard in the main program, the steps are: scan the keyboard, if a key is pressed, delay for tens of milliseconds to debounce, confirm again that the key is indeed pressed, and then process the work corresponding to the key, and repeat the above steps again after completion. This method has two shortcomings: 1. The key input cannot be latched when processing the corresponding work, and the key may be missed. 2. The CPU cannot do other things during the delay debounce, which is not efficient. Putting the key reading in the clock interrupt can avoid the above shortcomings. The method is: if the same key is read as pressed in two adjacent interrupts, then this key is valid (the debounce purpose is achieved), latch it into the first-in-first-out (queue) keyboard buffer, and wait for the main program to process it. In this way, the main program can still respond to keyboard input while processing the key. The buffer depth can usually be set to 8 levels. If the number of latched keys has reached 8, the new key is ignored, and an alarm is issued to remind the user that the new key will be invalid. If the keyboard buffer queue stagnates for much longer than the maximum time required for the main program to process a key press, it means that the main program has made an error or run away. In this case, you can use an instruction in the interrupt to reset the system, thus fulfilling the purpose of a watchdog.

  Delay in the main program. Since there is a normally open clock interrupt, when a shorter delay with higher precision is required in the main program, the clock interrupt should be temporarily turned off. When a longer delay with lower precision is required in the program, the following example can be used to avoid multi-layer nested loop delays.

Example: Output a 1-second high-level pulse from P1.1 port
MOV A,INCPI
INC A
CJNE A,INCPI,$; Wait for an interrupt to complete
SETB P1.1; Set P1.1 to 1, pulse starts
ADD A,#50; 50 20ms is 1 second
CJNE A,INCPI,$; Wait for an interrupt to increase INCPII by 50 times
CLR P1.1; Set P1.1 to 0, pulse ends

  In summary, we can know that the clock interrupt should be used flexibly in the design, and the tasks should be reasonably allocated to the interrupt and the main program. The two should have clear division of labor and simple interface. And we should pay attention to shortening the execution time of the interrupt handler as much as possible, and it should not be longer than 20ms.

                         

Pulse constant current source with variable frequency and duty cycle controlled by 89C2051

  The load of this constant current source is purely resistive, with a resistance range of 100Ω~1kΩ, and an output constant current of 4mA. The constant current source provides three outputs, and the frequency of each output pulse constant current source is 12 times/min, 6 times/min, and 3 times/min, respectively, and the output pulse width is 300ms.

  The constant current source is mainly composed of two parts: the constant current source output circuit and the pulse control signal generation circuit with variable frequency and duty cycle.
The above pulse control signal can also be obtained by frequency division using TTL or CMOS circuit, but the circuit is more complicated, and the frequency and duty cycle are not easy to change. Using the output port of 89C2051 as the pulse control signal, the frequency and duty cycle of the pulse control signal can be changed through the program.

  The pulse constant current source circuit is shown in the attached figure. For the convenience of explanation, only one constant current source output circuit is drawn here.
Connector P1 is connected to the 9V battery, and P3 is connected to the load.

  The constant current source output circuit is composed of U1, T1, T2, D1 and other peripheral circuits. T1, R3, D1, RW1 and R2 constitute a reference voltage source. According to the design requirements, the reference potential added to the in-phase terminal of the op amp is determined by the output current and the resistance value of the sampling resistor R1. According to the parameters in the figure, when T1 is turned on, RW1 is adjusted to make the reference potential of the in-phase terminal of the op amp 0.4V. Obviously, when T1 is turned off, the reference potential is equal to 0V. When T1 is turned on, if the load current is lower than 4mA, the voltage on the sampling resistor R1 drops, the potential of the inverting input terminal of the op amp drops, the output potential of the op amp rises, and the base potential and collector current (load current) of T2 rise; when the load current is greater than 4mA, the base potential and collector current (load current) of T2 drop, and finally the output current is kept constant. When T1 is turned off, since the reference potential added to the in-phase terminal of the op amp drops to 0V, the output voltage of the op amp drops, T2 is turned off, and no current passes through the load.

  The pulse signal that controls the conduction and cutoff of T1 is output from the P10 port of 89C2051. The software that generates the pulse control signal mainly consists of a loop program and a delay subroutine.

If you want to expand the output current range, you only need to use a relatively high-power voltage regulator and replace T2 with a high-power composite tube.


AT89C series MCU encryption mode

  The simple decryption of the microcontroller is to erase the encryption lock bit in the microcontroller. Due to the unreasonable design of the erasing operation timing of the AT89C series microcontroller, it is possible to erase the encryption lock bit before erasing the on-chip program. The timing of the erasing operation of the AT89C series microcontroller is: erase start ----> erase operation hardware initialization (10 microseconds) ----> erase the encryption lock bit (50-200 microseconds) ---> erase the data in the on-chip program memory (10 milliseconds) -----> erase end. If the program is used to monitor the erasing process, once the encryption lock bit is erased, the erasing operation is terminated, and further erasing of the on-chip program memory is stopped. The encrypted microcontroller becomes an unencrypted microcontroller. The on-chip program can be read out through the bus. There are two unbreakable encryption methods for the AT89C series microcontroller.

1. An encryption method that permanently destroys the encryption bits of the microcontroller. It is referred to as OTP encryption mode.

2. A method to permanently destroy the encryption of the microcontroller's data bus, referred to as the burn bus encryption mode.

 

AT89C series MCU OTP encryption mode principle

  This programming encryption algorithm burns out the encryption lock bit (breaks through the silicon chip inside the chip) without damaging other parts and does not occupy any resources of the microcontroller. After the encryption lock bit is burned out, it no longer has the erase feature. The 89C51/52/55 has 3 encryption bits to further increase the reliability of encryption. Once encrypted in OTP mode, the encryption bit in the microcontroller chip and the data in the program memory cannot be erased again. The 89C51/52/55 microcontroller is like a one-time programmed OTP type microcontroller. If the user program length is greater than the capacity of the 89C51 microcontroller's on-chip memory, the OPT mode can also be used for encryption. The specific method is as follows:

1. Expand a large-capacity program memory as usual, such as 27C512 (64K).
2. Arrange the key program part in the first 4K of the program.
3. Write the entire program into 27C512, and then fill the first 4K of 27C512 with 0.
4. Fix the first 4K of the program into AT89C51 and encrypt it in OPT mode.
5. Connect the EA pin of the microcontroller to a high level.

  In this way, the first 4K of the program runs inside the microcontroller, and the last 60K runs outside the chip. Pirates cannot read the first 4K of the program, and even if they know the last 60K, it will be useless.

AT89C series single chip microcomputer bus encryption mode principle

  Because the program code in the microcontroller chip must eventually be read out through the data bus, if one of the lines of the data bus that guides the microcontroller is permanently damaged, the decryptor cannot read the correct code of the program in the chip even if the encryption bit is erased. The data bus of 89C1051/2051 is P1 port. The bus burning mode burns the P1.0 port of 89C2051. The original program code is 02H, 01H, 00H. The read data is 03H, 01H, 00H. The lowest bit is always 1, and the program code read out is obviously wrong. This encryption mode is used to encrypt 89C1051/2051 microcontrollers. The disadvantage is that it occupies the resources of the microcontroller. When designing the microcontroller hardware system, the development and design personnel only need to reserve the export line P1.0 for use, and then use the bus burning mode to encrypt the microcontroller.

Keywords:Atmel Reference address:Atmel MCU Application Skills

Previous article:Using the MCU serial port to realize the display of multiple LEDs
Next article:Application of Single Chip Microcomputer in Furnace Temperature Control

Recommended ReadingLatest update time:2024-11-16 13:47

MSP430 low power consumption structure principle
After the basic functions of the system clock generator are established, the SCG1, SCG0, CPUOFF, and OSCOFF bits of the CPU status register SR are important low-power control bits. As long as any interrupt is responded to, the above control bits are pushed into the stack for storage, and the previous working mode can
[Microcontroller]
Microcontroller C language programming: timer controls 4 LEDs to scroll and flash
/* Name: Timer controls 4 LEDs to scroll and flash Description: 4 LEDs flash in a rolling manner under the control of the timer. */ #include reg51.h #define uchar unsigned char #define uint unsigned int sbit B1=P0^0; sbit G1=P0^1; sbit R1=P0^2; sbit Y1=P0^3; uint i,j,k; //Main program void main
[Microcontroller]
Microcontroller C language programming: timer controls 4 LEDs to scroll and flash
The single chip microcomputer controls the DC motor to achieve various motion combinations
project description: A single chip microcomputer (STC89C52RC) is used to control two DC motors to achieve various forms of motion. The functions implemented in this example are: forward, backward, turn right and turn left. The C language program is as follows: /*------------------------------------ FileName: main.c
[Microcontroller]
Single-chip multi-microcomputer system sharing RAM circuit
  At present, multi-microcomputer systems have been applied in many aspects, such as industrial robot control, CNC machine tool control, etc. Therefore, communication between multiple machines has become the technical key of multi-machine systems. Especially in occasions with special requirements for data transmission
[Microcontroller]
Single-chip multi-microcomputer system sharing RAM circuit
Design of intelligent high-power DC power supply based on single chip microcomputer
introduction In high-power DC power supplies, the main circuit generally uses a thyristor three-phase fully controlled bridge rectifier circuit. The key lies in how to accurately, reliably and stably control the conduction angle of the thyristor. At present, the most common control method in the field appl
[Power Management]
Design of intelligent high-power DC power supply based on single chip microcomputer
【C51 MCU】99-second countdown with digital tube
C51 MCU digital tube to achieve 99 seconds countdown (combined with timer) - STC89C52 MCU code show as below: #include reg52.h typedef unsigned char u8; typedef unsigned int u16; sbit b1 = P2^2; sbit b2 = P2^3; sbit b3 = P2^4; u8 duan = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39
[Microcontroller]
Traffic light system 51 microcontroller design
Require (1) Use the 51 microcontroller to design a traffic light control system to achieve the purpose of dividing pedestrians and traffic flow. This system is mainly used at intersections, with main roads in the longitudinal direction and branch roads in the horizontal direction; (2) The main and branch roads pass a
[Microcontroller]
Traffic light system 51 microcontroller design
Frequency conversion speed regulation system based on C504 single chip microcomputer
The application of variable frequency speed regulation in control is more and more extensive. The traditional variable frequency speed regulation control is realized by ordinary single-chip microcomputer system, which has complex circuit and troublesome programming. This system adopts C504 special control single-c
[Microcontroller]
Frequency conversion speed regulation system based on C504 single chip microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号