Posts

Instruction Set and Timing

The 8051 operates based on an external crystal. This is an electrical device which, when energy is applied, emits pulses at a fixed frequency. One can find crystals of virtually any frequency depending on the application requirements. When using an 8051, the most common crystal frequencies are 12 megahertz and 11.059 megahertz with 11.059 being much more common.  Why would anyone pick such an odd-ball frequency?  There is a real reason for it :-   It has to do with generating baud rates and well talk more about it in the Serial Communication chapter. For the remainder of this discussion well assume that were using an 11.059Mhz crystal.  Microcontrollers (and many other electrical systems) use crystals to synchronize operations. The 8051 uses the crystal for precisely that: to synchronize its operation. Effectively, the 8051 operates using what are called "machine cycles." A single machine cycle is the minimum amount of time in which a single 8051 instru...

Common Problems with Interrupts

Interrupts are a very powerful tool available to the 8051 developer, but when used incorrectly they can be a source of a huge number of debugging hours. Errors in interrupt routines are often very difficult to diagnose and correct. If  interrupts are being used and the program is crashing or does not seem to be performing as expected, always review the following interrupt-related issues :- 1. Register Protection : Make sure you are protecting all your registers, as explained above. If forget to protect a register that the main program is using, very strange results may occur. In our example above we saw how failure to protect registers caused the main program to apparently calculate that 25h + 10h = 51h. If it witness problems with registers changing values unexpectedly or operations producing "incorrect" values, it is very likely that  forgotten to protect registers.  ALWAYS PROTECT YOUR REGISTERS. 2. Forgetting to restore protected values: Anot...

Serial Interrupts

Serial Interrupts are slightly different than the rest of the interrupts. This is due to the fact that there are two interrupt flags: RI and TI. If either flag is set, a serial interrupt is triggered. As it will recall from the section on the serial port, the RI bit is set when a byte is received by the serial port and the TI bit is set when a byte has been sent. This means that when the serial interrupt is executed, it may have been triggered because the RI flag was set or because the TI flag was set--or because both flags were set. Thus, the routine must check the status of these flags to determine what action is appropriate. Also, since the 8051 does not automatically clear the RI and TI flags this must clear these bits in the interrupt handler.                                JNB INT_SERIAL: RI,CHECK_TI    ; If the RI flag is not set, then jump to check TI MOV A, SBUF  ...

What happens when an interrupt ends?

An interrupt ends when your program executes the RETI (Return from Interrupt) instruction.  When the RETI instruction is executed the following actions are taken by the microcontroller :- 1. Two bytes are popped off the stack into the Program Counter to restore normal program execution. 2. Interrupt status is restored to its pre-interrupt status.

What happens when an interrupt occurs?

When an interrupt is triggered, the following actions are taken automatically by the microcontroller :- 1.  The current Program Counter is saved on the stack, low-byte first. 2.  Interrupts of the same and lower priority are blocked. 3.  In the case of Timer and External interrupts, the corresponding interrupt flag is cleared. 4.  Program execution transfers to the corresponding interrupt handler vector address. 5.  The Interrupt Handler Routine executes. Take special note of the third step: If the interrupt being handled is a Timer or External interrupt, the microcontroller automatically clears the interrupt flag before passing control to interrupt handler routine. This means it is not necessary that bit is cleared in the code.

Interrupt Priorities

Image
The 8051 offers two levels of interrupt priority: high and low. By using interrupt priorities it may assign higher priority to certain interrupt conditions. For example, you may have enabled Timer 1 Interrupt which is automatically called every time Timer 1 overflows. Additionally, you may have enabled the Serial Interrupt which is called every time a character is received via the serial port. However, you may consider that receiving a character is much more important than the timer interrupt. In this case, if Timer 1 Interrupt is already executing then it may wish that the serial interrupt itself interrupts the Timer 1 Interrupt. When the serial interrupt is complete, control passes back to Timer 1 Interrupt and finally back to the main program. This may accomplish this by assigning a high priority to the Serial Interrupt and a low priority to the Timer 1 Interrupt. When considering interrupt priorities, the following rules apply : - 1. Nothing can interrupt a high-prior...

Polling Sequence

The 8051 automatically evaluates whether an interrupt should occur after every instruction. When checking for interrupt conditions, it checks them in the following order : - 1.  External 0 Interrupt 2.  Timer 0 Interrupt 3.  External 1 Interrupt 4.  Timer 1 Interrupt 5.  Serial Interrupt This means that if a Serial Interrupt occurs at the exact same instant that an External 0 Interrupt occurs, the External 0 Interrupt will be executed first and the Serial Interrupt will be executed once the External 0 Interrupt has completed.