vastpeak.blogg.se

Simple delay function for 8051 in c
Simple delay function for 8051 in c






Whenever we return from this blocking delay function, we toggle the GPIO_ODR bit, which causes the LED that is connected on that pin to be lit or not. In the case of the Blinky example, we remove the active polling of the GPIO_IDR that was used to read the button input, instead relying on the delay function that is added. In the Pushy example, the processor core would constantly query the GPIO peripheral’s incoming data register (GPIO_IDR) and write into the outgoing data register (GPIO_ODR) depending on those input values. Meanwhile the slower APB connects peripherals that are fine with less bandwidth and lower speeds, which includes the I2C, timers, USARTs and SPI peripherals. AHB connects the core to all peripherals that need the low latency and speed, such as RAM, ROM, GPIO banks and display controllers. This core is connected via a variety of AMBA (Advanced Microcontroller Bus Architecture) buses, with AHB being the fast bus. In Arm MCU architectures, generally the same Cortex-M processor core is used within the same family of MCUs by a manufacturer such as ST Microelectronics (‘ST’). In this article we’ll take a look at all three approaches, along with their advantages and disadvantages. In the latter two cases we also have to use interrupts. On an STM32 MCU, we get to choose between essentially an active delay ( while loop), one implemented using the SysTick timer and using one of the peripheral timers. The reason for this is that there are many ways to implement a delay function on a microcontroller (MCU), each of which comes with their own advantages and disadvantages. The reason for this is that there’s actually quite a story behind a simple call to delay() or its equivalent. This is actually a lot more complicated than the ‘ Pushy‘ example which we looked at in the first installment of this series. The same circuit can be used for generating any frequency but the program is different.One of the very first examples for an MCU or SoC usually involves the famous ‘ Blinky‘ example, where an LED is pulsed on and off with a fixed delay. Circuit diagram for generating square wave using 8051 is shown below. This will result in a square wave of the required frequency at the corresponding port pin. The idea is very simple, run a subroutine with delay equal to half the time period of the square wave, complement any port pin after the delay routine is finished, repeat the delay subroutine again, complement the same port pin again and repeat the cycle again and again over time.

Simple delay function for 8051 in c software#

Using software delay subroutines square waves over a wide frequency range (limited by the crystal frequency) can be produced using 8051. As result, a delay of 4 x 1mS x 250 = 1000mS = 1 second is produced. In this program subroutine for delaying 1mS (DELAY) is called 4 times back to back and the entire cycle is repeated 250 times. The program shown below produces a delay of around 1 second. You can make adjustments on the initial values of R6 and R7 to make the result more accurate. As I said earlier, this just a rough delay and when you test this program you may find slight differences in the output.

simple delay function for 8051 in c simple delay function for 8051 in c

This creates a loop of DJNZ Rx, LABEL repeating 500 times and the result will be a 1mS delay. Then DJNZ R6,LABEL1 is executed until R6 becomes zero and then DJNZ R7,LABEL2 is executed until R7 is zero. When called the sub routine DELAY, Registers R6 and R7 are loaded by 250D. The program is written as a subroutine and it works this way. So repeating this instruction 500 times will generate a delay of 500 x 2µS = 1mS. The instruction DJNZ R x, LABEL is a two cycle instruction and it will take 2µS to execute.

simple delay function for 8051 in c

The above program roughly produces a delay of 1mS. How ever software delay routines are very easy to develop and well enough for less critical and simple applications. Therefore it is better to use 8051 Timer for generating delay in time critical applications. Generally an instruction will be executed in the theoretical amount of time but some times it may advance or retard due to other reasons. Any way, keep one thing in mind that software delay is not very accurate because we cannot exactly predict how much time its takes for executing a single instruction. Thus a time delay of any magnitude can be generated by looping suitable instructions a required number of time. The shortest instructions will execute in 1µS and other instructions will take 2 or more micro seconds depending up on the size of the instruction. For an 8051 microcontroller clocked by a 12MHz crystal, the time taken for executing one instruction cycle is 1µS and it is according to the equation, Time for 1 instruction cycle= 12 /12MHz = 1µS. In an 8051 microcontroller, it requires 12 cycles of the processor clock for executing a single instruction cycle.






Simple delay function for 8051 in c