For example, if we want to display binary count from 0(0x00) to 15(0x0F), we can hard code the values or another approach is use a variable and increment its value by 1 and display its value to PORT2.
Example 1: Hard Coded values
#include <at89x52.h>
void delay (void)
{
unsigned int i;
for(i=0; i<0x7FFF; i++);
}
void main (void)
{
P2 = 0; //initially turn LEDS off
while(1)
{
P2 = 0x00;
delay();
P2 = 0x01;
delay();
P2 = 0x02;
delay();
P2 = 0x03;
delay();
P2 = 0x04;
delay();
P2 = 0x05;
delay();
P2 = 0x06;
delay();
P2 = 0x07;
delay();
P2 = 0x08;
delay();
P2 = 0x09;
delay();
P2 = 0x0A;
delay();
P2 = 0x0B;
delay();
P2 = 0x0C;
delay();
P2 = 0x0D;
delay();
P2 = 0x0E;
delay();
P2 = 0x0F;
delay();
}
}
Example 2: Improved version
#include <at89x52.h>
void delay (void)
{
unsigned int i;
for(i=0; i<0x7FFF; i++);
}
void main (void)
{
unsigned char val = 0 //the value of this variable will be transferred to P2
P2 = 0; //initially turn off all LEDs
while(1)
{
P2 = val; //transfer val to P2
val++; //increment val by 1
if(val>=0x0F) //if we reach 0x0F then reset value
val = 0;
delay();
}
}
No comments:
Post a Comment