0% found this document useful (0 votes)
30 views

MC Interfacing

The document contains code for initializing and writing text to an LCD display. It defines LCD functions for initialization, command writing, and data writing. It initializes the LCD, writes the string "E &C" to the first line, waits, then writes the string "BEC-BGK -02" to the second line, and loops continuously displaying this text.

Uploaded by

Suhas Hatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

MC Interfacing

The document contains code for initializing and writing text to an LCD display. It defines LCD functions for initialization, command writing, and data writing. It initializes the LCD, writes the string "E &C" to the first line, waits, then writes the string "BEC-BGK -02" to the second line, and loops continuously displaying this text.

Uploaded by

Suhas Hatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Winclude cat89c51xd2.

h>

woid delay(unsigned int time)


unsigned int i, i:

for (i=0; i<time; i++)


for (j =0; j<1275; j++);
-366
-180

yoid driveStepperMotorClockwise90Degrees() {

int steps =25; // Number of steps for 90-degree rotation


inti: // Variable declaration moved here
for (i =0; i< steps; it+)

{
PO = 0xBB; /* Initialize Port 0/

Pl = 0xBB;/ Initialize Port 1/


P2 = OxBB:/ Initialize Port 2/

P3 =OxBB; /* Initialize Port 3/


movement /
delay(15); / Delay for stepper motor

PO =0xDD; / Step 1*/

P1 =OxDD;

P2 = OxDD;

P3 =0xDD;

delay(15);
PO =0xEE; / Step 2/

P1= OxEE;
p2 =0xEE;
P3 = 0xEE;

delay(15);
PO = 0x77; /* Step 3*/
P1 = 0x77;

P2 = Ox77;

P3=0x77;

delay(15);

while(1)

PO = 0x00; /* Step 2*/

P1 = 0x00;

P2 =0x00;

P3 = 0x00;

int main(void) {
driveStepperMotorClockwise90Degrees):/ Rotate stepper motor clockwise for 90 degrees */

return 0;: //Optional: main function should return an int value


/*A Buzzer 1s
connected to portline PO.5. Timer) is used to generate
Droduce a tone. Vary dealy to
oFF time & ON timer to
different tones, */
Connections CNIto CN12 Buzzer*/

#include "'at89c51 xd2.h"


#define off time = 20

#define on time = 20

sbit buzz= P0^S: I/ Buzzer is connected to the portline PO.5

idata unsigned int i;


void main ()

TMOD =0x01; II Timer0 is configured in Mode-I,

TLO =0x00: I/ Load Timer-0 value for 2.2ms Delay

THO = 0xF8;

TCON = Ox00: | Clearing All Flags


TFO =0: / Clear Tiemr-0 overflow flag

while(1 )

buzz = 1: Il make 8Shigh i.e. Buzzer ON


for(i=0:i<on time;itt)

TLO = 0x00; /Reload timer count for 2msec

TH0 - 0xF8:
TRO 1; I|Turn ON timer-0

while(!TFO); I/ Wait untill Timer-0 overfolws


TFO =0; I/ Clear Tiemr-0 overflow flag
TRO-0; I/ Turn OFF timer-0

buzz =0; l make P2.4 low i.e Buzzer OFF

for(i-0;i<off_time;itt)

TLO =0x00; I/ Reload timer count for 2msec

THO = 0xF8:

TRO =1; I/Turn ON timer-0

while(!TFO); I/ Wait untill Timer-0 overfolws

TFO =0; I/ Clear Tiemr-0 overflow flag

TRO=0; I/ Turn OFF timer-0

}
27

+PROGRAME TO BUZZER ON-OFF USING DIP SWITCH/


CONNECTIONS CN3 TO CN12
I
NEXT CN1 TO CN9

#include <at89c51xd2.h>

#define OFF_TIME 20

#define ON_TIME 20

sbit buzz = P2^5:


is connected to P1.0, change it if needed
sbit button = P0^O;// Assuming the button

idata unsigned int i;

void main()

(Timer 0, Mode 1)
TMOD = Ox01; // Set TimerO in 16-bit mode
to Ox00
LO-Ox00: //!ritialize the low byte of Timer O
Timer 0 to OxF8
THO = OxF8: // initialize the high byte of

TCON = Ox00; // Clear the Timer 0 control register


TF0 = 0: /Clear the Timer 0 overflow flag

while(1)

i (button == 0) // If the button is pressed (logic low)


buzz = 1; I/ Turn ON the buzzer

I/Generate the ON time for the buzzer

for (i =0; i< ON_TIME; i++)

TLO = 0x00; // Reset the low byte of Timer 0 to start counting from 0
THO = 0xF8; // Reset the high byte of Timer 0 to the initial value

TRO = 1; I/Start Timer 0


while (!TFO): // Wait until Timer0 overflows
TFO = 0; I/Clear the Timer 0 overflow flag

TRO = 0; |/Stop Timer 0

| | buzz = 0: // Turn OFF the buzzer

I/Generate the OFF time for the buzzer

fcr {i - O;i<OFF_TIME; i+)


{
from 0
TLO = 0x00: // Reset the low byte of Timer 0 to start counting
initial value
THÌ = 0xF8: // Reset the high byte of Timer 0 to the

TRO = 1; I/Start Timer 0

while (!TFO): // Wait until Timer 0 overflows

TFO = 0: /Clear the Timer 0 overflow flag

TRO = 0: I/ Stop Timer 0


else // If the button is not pressed

buzz = 0; // Turn OFF the bUzzer


SSD O0-99.cont n
I/ ssp- o0-99
I/CNI CN7, CN2 ’ CNB
#inclydecat89c51xd2. h>

void msdelay(unsigned int time) I/ Function for creating delay in milfiseconds

unsigned int i,j ;

for(i=0;ictime;i++)

for(j=0;j<1275,j++):

void main()

unsigned char nums[l={0x3f,0xO6,0xSb,0x4f,0x66,0x6d,0x7d,Ox07,0x7f,Ox6f;: //Array for hex vaiues (0-9)


I/for common anode 7 segment

int loop=0,loop2;

while(1)

for(loop=0;loop<100;loop++)

loop2=loop %10;

PO=0x10;
Pl=numslloop2);
msdelay(30):
loop2=loop / 10;
PO=0x00;

P1=numslloop2):
msdelay(10);
I|SSD- 00-99
|/CN1 ’ CN7,CN2 ’ CN8

#include<at89c51xd2.h>

void msdelay(unsigned int time) // Function for creating delay in milliseconds.

unsigned int i,j;


for(i=0;i<time;i++)

for(j=0;j<1275;j++);

void main()

unsigned char nums[|={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7{,0x6f}; //Array for hex values (-9)


I/for common anode 7 segment

int loop=0,loop2;

while(1)

for(loop=0;loop<100;loop++)

loop2=loop %10;

PO=0x10;
P1=numslloop2);
msdelay(30);
loop2=loop /10;
PO=0x00;
Pl=nums[loop2]:
msdelay(10);
"Up-Counter 00---99 using Timer-0 for delay
Connection CNI To CN7 :# CN2 To CN8I

tincludecat89c51xd2.h>
void delay(int);

unsigned char temp1=0x00;

unsigned char temp2;


unsigned char dig1=0x00,dig2=0x00,dig3=0x00,dig4=Ox00,dig5=0x00,dig6-0x00,temp3=0x00,
unsigned char twenty_count=0x00,dig_count=0x00;
unsigned char array_dec[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,Ox6F};

unsigned char temp2=0x00;

unsigned int i=0;

bit tmr0_flg=0,one_sec_flg-0;
void timer0_init(void);

void display(void);

void main(void)

AUXR=0x10;
timer0_init():
EA=1;

while(1)
while(tmro_flg=-0)
tmr0_flg-0;
dig_count+=1;
if(dig_count=-0x06)
dig count-0x00;
if(one_sec_flg==1)
{

one_sec_flg-0;

dig1+=1;
if(digl==0xOA)

digl=0;
dig2+=1;

if(dig2==0x0A)

dig2-0;

dig3+=1;

display():

void timer0_init(void)
TMOD=0x01;
TLO=0x00;
THO=0X00;
TCON=0x00;

ETO=1;

TRO=1;

void timer0_isr(void)interrupt 1

{
TLO=0x00;

THO=0x00;
TFO=0;

tmro_flg=1;

if(twenty_count==11)

one_sec_flg=1;
twenty count=0x00;

else twenty count+=1;

}
void display(void)

PO=0xFO;

for(i-0;i<6;i++);

if(dig_count--0x00)

temp3=digl;
PO=0x50;

else if(dig_count==0x01)

temp3=dig2;

PO=0x40;

temp3 &-0x0F;

temp2=array_dec[temp3];

P1=temp2;

for(i=0;i<4;i++);
* Pogram to demonstrate led operation displays a message on LCD sereen
lines used: P2.5 RW P2.6 RS p2.7 EN data lines : P2.0 - P2.3 */
/ Connection CN3 to CN6 */

#include "at89cS I xd2.h"

I/LCD FUNCTION PROTOTYPE

void lcd_init(void);
void lcd comm(void);
void lcd data(void);
void wr cn(void);

void wr dn(void):
void delay(int);
unsigned char templ=0x00:

unsigned char temp2,var;


unsigned char arrl[17] ="E &C ";

unsigned char arr2|17] =" BEC-BGK -02 ":


void main ()

int i:

AUXR = 0x10: lIAccesiing Ful XRAM


lcd init():
while(1 )
of first line
templ = Ox80: I/ Display the data from first position
I/ Command Writing
lcd comm():

for(i=0;i<l6;i++)

temp2 = arrl [i];

lcd data(); II Data Writing

for(i-0;i<20000;i++);
of first line
templ = OxCO; II Display the data from first position
I| Command Writing
lcd comm();

for(i=0:i<l6;i++)

temp2 = arr2|i];
I/ Data Writing
lcd data():

while( 1):

lend of main()

void lcd init( void)

templ = Ox30:
wr cn();
delay( 500):

templ = Ox30;
WT_cn);
delay(500);

templ = 0x30:

Wr cn);

delay(500);

templ = 0x20;

wr_cn):
delay(500);

templ = Ox28;

lcd comm);
delay( 500):

temp] = Ox0f: l/display on,cursor on, cursor blinking

lcd comm():

delay( 500):

templ = 0x06: l/shift cursor right with auto increment

lcd comm():
delay( 500);:
templ = Ox80: l/clear display with cursor on first position

lcd comm();
delay(500);

templ = Ox01:

lcd_comm();
delay( 500);

/ Function to pass commands to LCD


void lcd comm(void)

var = templ;

templ =templ & OxfO:

templ = templ >> 4:


Wr en):

templ var & Ox0f:

WT Cn():

delay(60):
W
Functionnto pass data to LCD

oid lcd data(void)

var = temp2;
temp2 = temp2 &Oxf0; I/ convert the byte
into nibble
temp2 =temp2 >> 4;
wr_dn();
temp2 = var & Ox0f:

wr_dn():
delay(60);

W
Function to write to command reg of LCD
void wr_ cn(void)

RS(P2^7)0
templ = templ & Ox7f; //

templ = templ & OxDF;


EN(P2^6)1,
templ = templ |0x40; /

P2 = templ:
EN(P3^6F0,
temp] = templ & Oxbf; I/

P2 = templ:
UFunction to write to data reg of LCD

void wr_ dn(void)

temp2 =temp2 |Oxc0; II


RS(P2^T)=1,
temp2 = temp2 &OxDF;
P2 =temp2;

temp2 = temp2 &0xbf; |/ EN=0

P2 = temp2;

void delay(int count)

int i;

for(i-0;i<count;i++);
I/sepper Motor Clock wise Intertace on flach Boart
/Connections CNI(port 0) TO CN12 CN18 TO CHAlport 3)
Mincdudeat89eS1vd2 h
vod delay).

void maintvoid)

int a-0,

UP PO-Ox0BB;

P1-0x0BB;

P2-0x0B8,

P3=Ox0BB;

delayl):
PO-0x0DD;

P1=0x0DD;

P2=0x0DD;

P3-0x0DD;

delay():

PO=0x0EE;

Pl=0x0EE;

P2=0x0EE:;
P3-0x0EE,

delayl).

PO-0x77,
P1=0x77;

P2=0x77;
P3=0x77;

delay():

a=a+1;

goto UP;

void delay()

int x=10000,y;

forly=O;y<=x;y++)
ytt;
I/Stepper Motor Clock wisse Interface on
Flash Board
WConnections CN1(port-0) TO CN12 :: CN18 TO CN4((port-3)
#include<at89c51xd2.h>
void delay):

void main(void)

int a=0;

UP: PO=0x0BB;

P1=0x0BB;

P2=0x0BB;

P3=0x0BB;

delay();
PO=0x0DD;

P1=0x0DD;

P2=0x0DD;

P3=0x0DD;

delay();
PO=0x0EE;

P1=0x0EE;

P2=0x0EE;

P3=0x0EE;

delay();
PO=0x77;

P1-0x77;

P2=0x77;
P3-0x77;

delayl):

a-a+1;

goto UP;

voId delay()

Int x 10000,y;

forly-0;y<=x;y++)

y+,
Board
Flash
on
Interface
wise
AntiClock
#include<at89c51xd2.h>
PO=0x77; P3=0x77; PO=0x0EE;
P2=0x77;
P1=0x77; P2=0x0EE;
P1=0x0EE; P1=0x0DD;
P3=0x0EE; PO=0x0DD; P2=0x0DD;
P3=0x0DD; PO=0x0BB; P2=0x0
P1=0x0BB;
Motor, void
main(void) a=0;
int delay(): delay(): delay():
delay();
void
I/Stepper
i
JP:

You might also like