Sei sulla pagina 1di 4

BCD UP/DOWN counter program in C.

To display the values with certain delay on the


onboard 7 – segment display unit of flash 8051 board.

// Follow indentation in every C program when practicing, writing programs in the record
and also on the data sheet.
#include<reg51.h> // to access ports, timer registers etc.,

unsigned char dig7[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};


void main(void)
{
void delay(unsigned int);
int i=0, i1, i2, k; // i = 99 for down
P11 =0, P10 = 0; // configured as output
while(1)
{
i2 = i%10; // LS digit, unit place
i1 = i/10; // MS digit, tenth place
i++; // decrement for DOWN
for(k=0;k<125;k++) // loop repeated for 0.5 s
{
P0= dig7[i1]; P2 = 0x01; // place data on P2 and enable right LED
delay(250); // data on the bus for 2 ms.
P0= dig7[i2]; P2 =0x00; // P1 – bit 0 enables right LED, bit 1
enables left LED (MSD)
delay(250);
}
if(i >99) // i < 0
i= 0;
else
;
}
}

void delay(unsigned int n)


{
unsigned int i; // can you write without i? Possible, How?
for (i=0; i<n;i++)
; // null executed. Is there a statement in C?
}

Stepper Motor Interface to Rotate in Clockwise Direction.

#include <reg51.h>

void main()
{
//void delay(unsigned int);
unsigned char x1[] = {0x06,0x0C,0x09,0x03};
void Delay(unsigned int);
unsigned char i,j;
P2 = 0x00; // configuring P2 as out

while(1)
{
for (j=0;j<=7;j++)
{
for(i=0;i<4;i++)
{
P2 = x1[i];
Delay(8000);
}

}
// forever wait until restart or reset
}
}

void Delay(unsigned int n)


{
unsigned int i;
for(i=0;i< n ;i++);
}
Stepper Motor Interface to Rotate in Clockwise/Anticlockwise Direction using the switch P1.7

#include <reg51.h>
unsigned char x1[] = {0x03,0x09,0x0C,0x06},temp;
//unsigned char x2[] = {0x03,0x01,0x09,0x08,0x0C,0x04,0x06, 0x02};
// x2 array for alternate phase excitation
void main()
{
void clk_wise();
void cclk_wise();
void delay(unsigned int);
P2 = 0x00; // configuring P2 as out
while(1)
{
temp = P1 & 0x80;
//delay(100);
if(temp!= 0)
clk_wise();
else
cclk_wise();
}
//while(1);

void delay(unsigned int n)


{
unsigned int i;
for(i=0;i< n ;i++);
}
void clk_wise(void) // note data array is global
{
unsigned int i;
for(i=0;i<4;i++)
{

P2 = x1[i];
delay(5000);
}
}
void cclk_wise(void)
{
unsigned int i;
for(i=0;i<4;i++)
{

P2 = x1[3-i];
delay(5000);
}
}

Potrebbero piacerti anche