// In Embedded C:
#include <reg51.h>
void delay(unsigned int time)
{
unsigned int i,j;
for(i=0;i<time;i++)
for(j=0;j<0xFF;j++);
}
int main()
{
P1 = 0x00;
while(1)
{
P1 = ~P1;
delay(100);
}
}
// ----------------------------------------------------------------------------
// Using Timer generated delay
#include<reg51.h>
#define ON 1
#define OFF 0
sbit led=P2^0;
unsigned int i=0;
void delay();
void delay()
{
TMOD = 0x01; // Timer0 mode1
TH0 = 0xFC; //initial value for 1ms
TL0 = 0x66;
TR0 = 1; // timer start
while(TF0==0); // check overflow condition
TR0 = 0; // Stop Timer
TF0 = 0; // Clear flag
}
void main()
{
while(1)
{
led = ON;
for(i=0;i<1000;i++)
{
delay();
}
led = OFF;
for(i=0;i<1000;i++)
{
delay();
}
}
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// In Assembly
// ----------------------------------------------------------------------------
org 00h
ljmp start ;jump to start on reset
delay: ;delay routine approx. 1.5 seconds
mov r3,#0x02
mov r4,#0x0
mov r5,#0x0
loop:
djnz r5, loop
djnz r4, loop
djnz r3, loop
RET
start:
mov P2, #00h
acall delay
mov P2, #0FFh
acall delay
sjmp start
END
Comments
Post a Comment