37
8/19/2019 Using Push Button Switch With 8051 and Keil C http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 1/37 Using Push Button Switch with 8051 and Keil C – AT89C51  by Ebin George / 4 Comments This tutorial is for beginners in the field of microcontroller. In this case the microcontroller is AT89C5! a re"rogrammable deri#ati#e of 8$5. This "ur"ose of this tutorial is to familiari%e &ith the use of "ush button s&itch &ith the micro controller. It &ill be useful &hene#er a decision is to be made according to the "ress of a s&itch. If you are not yet started &ith 8$5 &ith 'eil C "lease refer this tutorial (lin)ing *ed using 8$5 and 'eil C. Circuit Diagra  

Using Push Button Switch With 8051 and Keil C

Embed Size (px)

Citation preview

Page 1: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 1/37

Using Push Button Switch with 8051 and Keil

C – AT89C51

 by Ebin George / 4 Comments 

This tutorial is for beginners in the field of microcontroller. In this case the microcontroller is

AT89C5! a re"rogrammable deri#ati#e of 8$5. This "ur"ose of this tutorial is

to familiari%e &ith the use of "ush button s&itch &ith the micro controller. It &ill be useful&hene#er a decision is to be made according to the "ress of a s&itch. If you are not yet started

&ith 8$5 &ith 'eil C "lease refer this tutorial (lin)ing *ed using 8$5 and 'eil C.

Circuit Diagra

 

Page 2: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 2/37

+sing ,ush (utton -&itch &ith 8$5 icrocontroller and 'eilC

!ote " CC and G01 of AT89C5 is not sho&n in the abo#e circuit diagram. CC should be

connected to 25 and G01 should be connected to Ground of the "o&er su""ly.

,ush button s&itch is connected to the first bit of ,3T $ ,$.$6 &hich is configured as an in"ut

 "in. 7hich is connected to a "ull u" resistor such that this "in is at cc "otential &hen the s&itch

is not "ressed. 7hen the s&itch is "ressed this "in ,$.$ &ill be grounded. The *E1 is connected

to the first bit of ,3T ,.6 and a resistor is connected in series &ith it to limit the current

Page 3: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 3/37

K#$% C Progra

#include<reg52.h> /* special function register declarations */  /* for the intended 8051 derivative */#include<stdio.h> /* prototype declarations for I/ functions */

s!it "$%pin & '2(0) //$efining "$ 'Is!it s+itch%pin & '0(0) //$efining ,+itch 'I

void $elay-int) //unction prototype declarationvoid ain -void  s+itch%pin & 1) // a3ing ,+itch 'I input  "$%pin & 0) //a3ing "$ pin output

  +hile-1 //infinite loop

  if-s+itch%pin && 0 //If s+itch pressed

   "$%pin & 1) //"$  $elay-1000) //$elay "$%pin & 0) //"$

  4  44

void $elay-int 3  int )  int i)  for-i&0)i<3)i66 

  for-&0)<100)66    4  44

Th

Page 4: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 4/37

In some electronic a""lications &e need to s&itch or control high #oltages or high currents. Inthese cases &e may use electromagnetic or solid state relays. or e:am"le! it can be used to

control home a""liances using lo& "o&er electronic circuits.

relay

An electromagnetic relay is a s&itch &hich is used to s&itch ;igh oltage or Current using *o&

 "o&er circuits. It magnetically isolates lo& "o&er circuits from high "o&er circuits. It is

acti#ated by energi%ing a electromagnet! coil &ounded on a soft iron core. or detailed &or)ingof relay "lease #isit this "age. A relay should not be directly connected to a microcontroller! it

needs a dri#ing circuit due to the follo&ing reasons.

• A microcontroller &ill not able to su""ly current re<uired for the "ro"er &or)ing of arelay. The ma:imum current that A89C5 microcontroller can source or sin) is 5mA&hile a relay needs about 5$ = $$mA current.

• A relay is acti#ated by energi%ing its coil. icrocontroller may sto" &or)ing by the

negati#e #oltages "roduced in the relay due to its bac) emf.

$nter&acing 'ela( with 8051 using Transistor

Transistor is &ired as a s&itch. &hich dri#es the relay. 7hen the first "in of "ort , goes high the

 base current flo&s through the $) resistor. This in turn s&itches the transistor 30. The relaygets enough current through the # su""ly to s&itch the secondary circuit 30.

Circuit Diagra

Page 5: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 5/37

Interfacing elay &ith 8$5 using Transistors = Circuit 1iagram

Keil C Progra

#include<stdio.h>

s!it relay%pin & '2(0)

void $elay%s-int)

void ain-

  do    relay%pin & 1) //7elay   $elay%s-1000)  relay%pin & 0) //7elay   $elay%s-1000)  4+hile-1)4

Page 6: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 6/37

void $elay%s-int 3  int )  int i)  for-i&0)i<3)i66    for-&0)<100)66    4  44

$nter&acing 'ela( with 8051 using U%!)00*

7hen &e need more than one relays! using transistors and diodes become bul)y. In these cases

&e can use +*0 dri#ers. These are monolithic IC s consists of ;igh oltage ;igh Current

1arlington transistor arrays. 7hen using these dri#er ICs &e don>t need to connect free&heeling

diode as they ha#e built in clam" diodes. ;ere &e are using +*0$$? for demostration.

 Circuit Diagra

Page 7: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 7/37

Page 8: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 8/37

Interfacing elay &ith 8$5 using +*0$$?

Keil C Progra

#include<stdio.h>

#define relayport '2

void $elay%s-int)

void ain-  do    relayport & 0) //9ll relays n  $elay%s-1000)  relayport & 000) //9ll relays ff  $elay%s-1000)  4+hile-1)4

void $elay%s-int 3  int )  int i)  for-i&0)i<3)i66    for-&0)<100)66    4  44

Page 9: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 9/37

Page 10: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 10/37

Circuit Diagra

Interfacing 1C otor &ith 8$5 using *9?1

The 1C otor is connected to the first "air of dri#ers and it is enabled by connecting E0 tologic ;IG; 56. -- "in is used to "ro#ide logic #oltage to *9?1. ;ere 8$5microcontroller! &hich &or)s at 5# is used to control *9?1! hence the logic #oltage is 5. The

motor su""ly is gi#en to s "in of the *9?1.

Keil C Progra

Page 11: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 11/37

#include<reg52.h>#include<stdio.h>

void delay-void)

s!it otor%pin%1 & '2(0)s!it otor%pin%2 & '2(1)

void ain-  do    otor%pin%1 & 1)  otor%pin%2 & 0) //7otates otor 9nit :loc3+ise  delay-)  otor%pin%1 & 1)  otor%pin%2 & 1) //,tops otor  delay-)  otor%pin%1 & 0)  otor%pin%2 & 1) //7otates otor :loc3+ise

  delay-)  otor%pin%1 & 0)  otor%pin%2 & 0) //,tops otor  delay-)  4+hile-1)4

void delay-  int i;)  for-i&0)i<1000)i66    for-&0)<1000)66    4  44

Control Signals and +otor Status

P),0-$!1 P),1-$!) +otor Status

*37 *37 -to"s

*37 ;IG; Cloc)&ise

;IG; *37 AntiBCloc)&ise

;IG; ;IG; -to"s

Dou can do&nload 'eil C files and ,roteus files here

v

Page 12: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 12/37

• Shop By Category

FOLLOW US ON:

7EFE -3CIA*

Search

All

Interfacing Stepper Motor with 8051 using Keil C – A8!C51

"y #"in $eorge % 8 Co&&ents 

-te""er otor 

A -te""er otor is a brushless! synchronous 1C otor. It has many a""lications in the field

of robotics and mechatronics. The total rotation of the motor is di#ided into ste"s. The angle of a

single ste" is )no&n as the ste""er angle of the motor. There are t&o ty"es of ste""er motors 

Uni.olar and Bi.olar. 1ue to the ease of o"eration uni"olar ste""er motor is commonly used by

electronics hobbyists. or more details "lease read the article -te""er otor or -te" otor.

-te""er otors can be easily interfaced &ith a microcontroller using dri#er ICs such as *9?1 

or +*0$$?.

'riving (nipolar Stepper Motor with 8051

Page 13: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 13/37

+ni"olar -te""er otor 7indings

+ni"olar ste""er motors can be used in three modes namely the /ae Drie! ull Drie and 

2al& Drie mode. Each dri#e ha#e its o&n ad#antages and disad#antages! thus &e should choose

the re<uired dri#e according to the a""lication and "o&er consum"tion.

Wave Drive

In this mode only one electromagnet is energi%ed at a time. Generated tor<ue &ill be less &hen

com"ared to full dri#e in &hich t&o electromagnets are energi%ed at a time but "o&er

consum"tion is reduced. It has same number of ste"s as in the full dri#e. This dri#e is "referred

&hen "o&er consum"tion is more im"ortant than tor<ue. It is rarely used.

Wave Drive Stepping Sequence

Step A B C D

1 1 0 0 0

) 0 1 0 0

* 0 0 1 0

+ 0 0 0 1

Full Drive

In this mode t&o electromagnets are energi%ed at a time! so the tor<ue generated &ill be larger

&hen com"ared to 7a#e 1ri#e. This dri#e is commonly used than others. ,o&er consum"tion

&ill be higher than other modes.

Page 14: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 14/37

Page 15: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 15/37

Interfacing Using L293D

Interfacing +ni"olar -te""er otor &ith 8$5 using *9?1

This is the circuit diagram of dri#ing a bi"olar ste""er motor using 8$5 microcontroller using

*9?1. 4;% crystal is connected to "ro#ide the re<uired cloc) for the microcontroller. $

ca"acitor and $'J is used to "ro#ide ,o&er 3n eset ,36 for the 8$5 microcontroller.

*9?1 is connected to "ins ,.$! ,.! ,.! ,.? of the microcontroller and t&o "airs of *9?1

are enabled by tieing E0! E0 to 5. *ogic oltage 56 is connected to ss "in and otor

-u""ly 6 is connected to the s "in of *9?1. Center Ta" of each &indings of ste""er

motor is shorted and connected to the motor su""ly. 0o& &e can energi%e each &inding of the

motor by ma)ing corres"onding "in of *9?1 *37.

Page 16: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 16/37

Interfacing Using ULN23

Interfacing +ni"olar -te""er otor &ith 8$5 using +*0$$?

In this circuit instead of *9?1! +*0$$? is used. 7or)ing is similar to the "re#ious circuit!

&hen an in"ut say (6 is ;IG; corres"onding out"ut "in C6 &ill be grounded. Thus &e can

energi%e any &inding of ste""er motor.

!eil C C"#e F"r Wave Drive#include<reg52.h>

Page 17: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 17/37

#include<stdio.h>

void delay-int)

void ain-  do    '2&001) //0001  delay-1000)  '2&002) //0010  delay-1000)  '2&00) //0100  delay-1000)  '2&008) //1000  delay-1000)  4+hile-1)4

void delay-int 3  int i;)  for-i&0)i<3)i66    for-&0)<100)66  4  44

!eil C C"#e f"r Full Drive#include<reg52.h>#include<stdio.h>

void delay-int)

void ain-  do    '2 & 00=) //0011  delay-1000)  '2 & 00) //0110  delay-1000)  '2 & 00:) //1100  delay-1000)  '2 & 00?) //1001  delay-1000)  4+hile-1)4

void delay-int 3  int i;)  for-i&0)i<3)i66

Page 18: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 18/37

    for-&0)<100)66  4  44

!eil C C"#e f"r Half Drive#include<reg52.h>#include<stdio.h>

void delay-int)

void ain-  do    '2&001) //0001  delay-1000)  '2&00=) //0011  delay-1000)  '2&002) //0010  delay-1000)  '2&00) //0110  delay-1000)  '2&00) //0100  delay-1000)  '2&00:) //1100  delay-1000)  '2&008) //1000  delay-1000)  '2&00?) //1001  delay-1000)  4 +hile-1)

4

void delay-int 3  int i;)  for-i&0)i<3)i66    for-&0)<100)66  4  44

Dou can sim"lify these codes using the shift KK LL6 o"erators in C.

Interfacing Bipolar Stepper Motor

(i"olar ste""er motors ha#e no center ta" and ha#ing e<ual coil resistances. It can be easily

interfaced &ith a microcontroller using *9?1 1C otor 1ri#er IC.

Circuit 'iagra&

Page 19: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 19/37

Interfacing (i"olar -te""er otor &ith 8$5 using *9?1

!eil C C"#e

#include<reg52.h>#include<stdio.h>

void delay-int)

void ain-  do    '2&001) //0001

Page 20: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 20/37

  delay-1000)  '2&00) //0100  delay-1000)  '2&002) //0010  delay-1000)  '2&008) //1000  delay-1000)  4+hile-1)4

void delay-int 3  int i;)  for-i&0)i<3)i66    for-&0)<100)66  4  44

Dou can do&nload 'eil C files and ,roteus files here

• Shop By Category

Page 21: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 21/37

FOLLOW US ON:

7EFE -3CIA*

Search

All

Interfacing Servo Motor with 8051 using Keil C

"y #"in $eorge % 10 Co&&ents 

A ser#o motor uses ser#o mechanism! &hich is a closed loo" mechanism that uses "osition

feedbac) to control the "recise angular "osition of the shaft. -te""er otors! &hich is an o"en

loo" system can also be used for "recise angular control. (ut -er#o otors are "referred in

angular motion a""lications such as robotic arm. oreo#er controlling of ser#o motors are #erysim"le! easy and needs no e:tra hard&are li)e ste""er motor . +sually hobby circuit ser#o motors

ha#e three &ires. T&o of them are red and blac) &hich is used to gi#e "o&er to the motor and the

third &ire is used to "ro#ide control signal for angular "osition. It uses ,ulse 7idth odulated 

,76 &a#es as control signals. The angle of rotation is determined by the &idth of the "ulse at

the control "in. The ser#o motor used here is ha#ing angle of rotation from $ to 8$ degrees. 7e

can control the e:act angular "osition by #arying the "ulse bet&een ms to ms. (efore using

this in your a""lication! "lease refer the datasheet of your ser#o for angle and "ulse &idth

informations.

Page 22: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 22/37

Controlling Angular ,osition of -er#o otor using ,ulse 7idth odulation

Circuit 'iagra&

Page 23: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 23/37

Page 24: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 24/37

Interfacing -er#o otor &ith 8$5 = Circuit 1iagram

8;% crystal is used to "ro#ide the re<uired cloc) for 8$5 microcontroller and " ca"acitors

are used to stabili%e the o"eration of crystal. $'J resistor and $ ca"acitor is used to "ro#ide

the re<uired ,o&er 3n eset ,36 to the microcontroller. Control of -er#o otor is connected

to first "in of ,ort .

Keil C Co.e

#include<reg52.h>#include<stdio.h>#include <intrins.h>

s!it otor%pin & '2(0)void $elay-unsigned int)void $elay%servo-unsigned int)void ain-

  otor%pin & 0)

  do    //@urn to 0 degree  otor%pin & 1)  $elay%servo-50)  otor%pin & 0)

  $elay-1000)

  //@urn to ?0 degree  otor%pin&1)

  $elay%servo-82)  otor%pin&0)

  $elay-1000)  //@urn to 180 degree  otor%pin&1)  $elay%servo-110)  otor%pin&0)

  $elay-1000)  4+hile-1)4

void $elay-unsigned int s  unsigned long int us & s*1000)  +hile-usAA    %nop%-)  44

void $elay%servo-unsigned int us

Page 25: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 25/37

  +hile-usAA    %nop%-)  44

Dou can do&nload 'eil C files and ,roteus files here

nterfacing Keypa. with 8051 Microcontroller using Keil C

"y #"in $eorge % 1) Co&&ents 

Matri/ Keypa.

atri: 'ey"ads are commonly used in calculators! tele"hones etc &here a number of in"ut

s&itches are re<uired. 7e )no& that matri: )ey"ad is made by arranging "ush button s&itches in

ro& and columns. In the straight for&ard &ay to connect a 4M4 )ey"ad s&itches6 to a

microcontroller &e need in"uts "ins. (ut by connecting s&itches in the follo&ing &ay &e can

read the status of each s&itch using 8 "ins of the microcontroller.

4M4Batri:B'ey"ad

Page 26: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 26/37

The status of each )eys can be determined by a "rocess called -canning. or the sa)e of

e:"lanation lets assume that all the column "ins Col = Col46 are connected to the in"uts "ins

and all the ro& "ins are connected to the out"ut "ins of the microcontroller. In the normal case all

the column "ins are "ulled u" ;IG; state6 by internal or e:ternal "ull u" resistors. 0o& &e can

read the status of each s&itch through scanning.

1 A logic 23 is given to 4ow1 an. others 4ow) – 4ow6+7 I$

) 9ow each Colu&n is scanne. If any switch "elongs to 1st  row is presse.correspon.ing colu&n will pulle. .own logic 237 an. we can .etect thepresse. :ey

*  his process is repeate. for all rows

If you need to sa#e more "ins of your microcontroller then you can interface )ey"ad using the

A1C module of your microcontroller.

Interfacing with 8051 Microcontroller

Page 27: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 27/37

Circuit #iagra$

Interfacing 'ey"ad &ith 8$5 icrocontroller using 'eil C

Page 28: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 28/37

$'J resistor and $ &ill "ro#ide the re<uired ,o&er 3n eset ,36 signal to the 8$5

microcontroller. ;% crystal is used to "ro#ide re<uired cloc) for the microcontroller and

" ca"acitors &ill stabili%e the oscillations of the crystal. AT89C5 can &or)s u"to 4;%.

7e can choose the re<uired fre<uency by changing the crystal and cloc) fre<uency in the "ro@ect

settings of 'eil C. 'ey"ad is connected to the ,ort , and column in"uts "ins are "ulled u"

internally. M *C1 is connected to ,ort , and ,$. ,$.$ and ,$. "ins are "ulled u" e:ternally

using $'J resistors since ,ort ,$ has no internal "ull u".

!eil C C"#e#include<reg52.h> //including sfr registers for ports of the controller#include<lcd.h>

//":$ odule :onnectionss!it 7, & '0(0)

s!it & '0(1)s!it $0 & '2(0)s!it $1 & '2(1)s!it $2 & '2(2)s!it $= & '2(=)s!it $ & '2()s!it $5 & '2(5)s!it $ & '2()s!it $B & '2(B)//nd ":$ odule :onnections

//Ceypad :onnectionss!it 71 & '1(0)s!it 72 & '1(1)

s!it 7= & '1(2)s!it 7 & '1(=)s!it :1 & '1()s!it :2 & '1(5)s!it := & '1()s!it : & '1(B)//nd Ceypad :onnections

void $elay-int a  int )  int i)  for-i&0)i<a)i66

    for-&0)<100)66    4  44

char 7ead%Ceypad-  :1&1)

Page 29: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 29/37

  :2&1)  :=&1)  :&1)  71&0)  72&1)  7=&1)  7&1)  if-:1&&0$elay-100)+hile-:1&&0)return DBD)4  if-:2&&0$elay-100)+hile-:2&&0)return D8D)4  if-:=&&0$elay-100)+hile-:=&&0)return D?D)4  if-:&&0$elay-100)+hile-:&&0)return D/D)4  71&1)  72&0)  7=&1)  7&1)  if-:1&&0$elay-100)+hile-:1&&0)return DD)4  if-:2&&0$elay-100)+hile-:2&&0)return D5D)4  if-:=&&0$elay-100)+hile-:=&&0)return DD)4  if-:&&0$elay-100)+hile-:&&0)return DED)4  71&1)

  72&1)  7=&0)  7&1)  if-:1&&0$elay-100)+hile-:1&&0)return D1D)4  if-:2&&0$elay-100)+hile-:2&&0)return D2D)4  if-:=&&0$elay-100)+hile-:=&&0)return D=D)4  if-:&&0$elay-100)+hile-:&&0)return DAD)4  71&1)  72&1)  7=&1)  7&0)  if-:1&&0$elay-100)+hile-:1&&0)return D:D)4  if-:2&&0$elay-100)+hile-:2&&0)return D0D)4  if-:=&&0$elay-100)+hile-:=&&0)return D&D)4  if-:&&0$elay-100)+hile-:&&0)return D6D)4  return 0)4

void ain-  int i&0)  char c;p)  "cd8%Init-)  +hile-1    "cd8%,et%:ursor-1;1)  "cd8%Frite%,tring-GCeys 'ressedHG)

  "cd8%,et%:ursor-2;1)  "cd8%Frite%,tring-G@iesHG)  +hile--c & 7ead%Ceypad-)  p&c)  +hile-p&&c    i66)  "cd8%,et%:ursor-1;1)  "cd8%Frite%:har-c)  "cd8%,et%:ursor-2;B)

Page 30: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 30/37

  "cd8%Frite%:har-i68)  $elay-100)  +hile--c & 7ead%Ceypad-)  4  i&0)  "cd8%:lear-)  44

The code consists of t&o user defined functions. The Dela(34 is used to ma)e delay in the

 "rogram e:ecution. The 'eadKe(.ad34 reads the )ey"ad. If any )ey is "ressed it &aits until the

)ey is released and returns the corres"onding character. If no )ey is being "ressed it returns %ero.

As told before one of the "ins of the ro& is )e"t at a logic $ and the columns are chec)ed for a

logic $. If a logic $ is found the function returns a #alue according to the )ey "ressed. This

 "rocess is re"eated for each ro& until a "ressed )ey is found. If not found it &ill return %ero.

This e:am"le "ro@ect &ill sho& the character of )ey "ressed and the number of times that )ey is

 "ressed on the *C1 screen.

while363c 7 'eadKe(.ad3444 is used in the "rogram to read )ey"ad. It is a loc)ing call as it &ill

&ait until a )ey is "ressed and released.

!ote " The lcd is interfaced using the Nlcd.h> header file &hich is to be included in the "ro@ect

folder. or more details about *C1 interfacing "lease read our article Interfacing *C1 &ith 8$5

icrocontroller .

Dou can do&nload 'eil C files and ,roteus files here

Interfacing 'ey"ad &ith 8$5 using 'eil C

• Shop By Category

FOLLOW US ON:

7EFE -3CIA*

Search

All

Page 31: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 31/37

Interfacing C' with 8051 using Keil C – A8!C51

"y igo $eorge % !+ Co&&ents 

*i<uid Crystal 1is"lay *C16 is #ery commonly used electronic dis"lay module and ha#ing a

&ide range of a""lications such as calculators! la"to"s! mobile "hones etc. M character lcd

dis"lay is #ery basic module &hich is commonly used in electronics de#ices and "ro@ects. It can

dis"lay lines of characters. Each character is dis"layed using 5M or 5M$ "i:el matri:.

M Character *C1

Interfacing M *C1 &ith 8$5 using 'eil C is bit com"le: because there is no "o&erful

libraries in 'eil C. To sol#e this "roblem &e ha#e de#elo"ed a *C1 library &hich includes

commonly used features! you @ust need to include our header file and use it. Dou can do&nload

the header file at the bottom of this article.

*C1 can be interfaced &ith microcontroller in 4 (it or 8 (it mode. These differs in ho& data is

send to *C1. In 8 bit mode to &rite a character! 8 bit A-CII data is send through the data lines

1$ = 1 and data strobe is gi#en through E of the *C1. *C1 commands &hich are also 8 bit are

&ritten to *C1 in similar &ay.

(ut 4 (it ode uses only 4 data lines 14 = 1. In this mode 8 bit character A-CII data and

command data are di#ided into t&o "arts and send se<uentially through data lines. The idea of 4

 bit communication is used sa#e "ins of microcontroller. 4 bit communication is a bit slo&er than

8 bit communication but this s"eed difference can be neglected since *C1s are slo& s"eed

de#ices. Thus 4 bit mode data transfer is most commonly used.

i"rary ;unctions in lc.h

Page 32: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 32/37

%cd8$nit34 %cd$nit34 " These function &ill initiali%e the *C1 module &hich is connnected

to "ins defined by follo&ing bit addressable #ariables.

or 8 (it odeO

//":$ odule :onnectionss!it 7, & '0(0)s!it & '0(1)s!it $0 & '2(0)s!it $1 & '2(1)s!it $2 & '2(2)s!it $= & '2(=)s!it $ & '2()s!it $5 & '2(5)s!it $ & '2()s!it $B & '2(B)//nd ":$ odule :onnections

or 4 (it ode O

//":$ odule :onnectionss!it 7, & '0(0)s!it & '0(1)s!it $ & '2()s!it $5 & '2(5)s!it $ & '2()s!it $B & '2(B)//nd ":$ odule :onnections

These connections must be defined for the "ro"er &or)ing of the *C1 library.

%cd8Clear34 %cdClear34 " These functions &ill clear the *C1 screen &hen interfaced

&ith 8$5 in 8 bit and 4 bit mode res"ecti#ely.

%cd8SetCursor34 %cdSetCursor34 " These functions are used to set the cursor "osition

on the lcd screen. (y using this function &e can change the "osition of character and string

dis"layed by the follo&ing functions.

%cd8/riteChar34 %cd/riteChar34 " These functions are used to &rite a character to

the *C1 screen.

%cd8/riteString34 %cd8/riteString34 " These functions are used to &rite a string or

te:t to the *C1 screen.

%cd8Shi&t%e&t34 %cdShi&t%e&t34 " These functions are used to shift dis"lay left &ithout

changing the data in dis"lay A.

%cd8Shi&t'ight34 %cd8Shi&t'ight34 " These functions are used to shift dis"lay right

&ithout changing the data in dis"lay A.

Page 33: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 33/37

8 Bit Mo.e C' Interfacing

Circuit Diagra$

*C1 Interfacing &ith 8$5 using 'eil C = 8 (it ode Circuit 1iagram

!eil C C"#e#include<reg52.h> //including sfr registers for ports of the controller#include<lcd.h> // :an !e do+nload fro !otto of this article

Page 34: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 34/37

//":$ odule :onnectionss!it 7, & '0(0)s!it & '0(1)s!it $0 & '2(0)s!it $1 & '2(1)s!it $2 & '2(2)s!it $= & '2(=)s!it $ & '2()s!it $5 & '2(5)s!it $ & '2()s!it $B & '2(B)//nd ":$ odule :onnectionsvoid $elay-int a  int )  int i)  for-i&0)i<a)i66    for-&0)<100)66

    4  44void ain-  int i)  "cd8%Init-)  +hile-1    "cd8%,et%:ursor-1;1)  "cd8%Frite%,tring-Gelectro,oe ":$ Jello ForldG)  for-i&0)i<15)i66    $elay-1000)  "cd8%,hift%"eft-)  4  for-i&0)i<15)i66    $elay-1000)  "cd8%,hift%7ight-)  4  "cd8%:lear-)  "cd8%Frite%:har-DeD)  "cd8%Frite%:har-D,D)  $elay-=000)  4

4+ Bit Mo.e C' Interfacing

Page 35: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 35/37

Circuit Diagra$

*C1 Interfacing &ith 8$5 using 'eil C 4 (it ode = Circuit 1iagram

Page 36: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 36/37

!eil C C"#e#include<reg52.h> //including sfr registers for ports of the controller#include<lcd.h> //:an !e do+nload fro !otto of this article

//":$ odule :onnectionss!it 7, & '0(0)

s!it & '0(1)s!it $ & '2()s!it $5 & '2(5)s!it $ & '2()s!it $B & '2(B)//nd ":$ odule :onnectionsvoid $elay-int a  int )  int i)  for-i&0)i<a)i66    for-&0)<100)66    4  44void ain-  int i)  "cd%Init-)  +hile-1    "cd%,et%:ursor-1;1)  "cd%Frite%,tring-Gelectro,oe ":$ Jello ForldG)  for-i&0)i<15)i66 

  $elay-1000)  "cd%,hift%"eft-)  4  for-i&0)i<15)i66    $elay-1000)  "cd%,hift%7ight-)  4  "cd%:lear-)  "cd%,et%:ursor-2;1)  "cd%Frite%:har-DeD)  "cd%Frite%:har-D,D)  $elay-2000)

  44

ow to use the ea.er ;ile <

Co"y and ,aste the header file lcd.h to your "ro@ect folder. Then add lcd.h to your source grou".

. ight Clic) on -ource Grou"

Page 37: Using Push Button Switch With 8051 and Keil C

8/19/2019 Using Push Button Switch With 8051 and Keil C

http://slidepdf.com/reader/full/using-push-button-switch-with-8051-and-keil-c 37/37

. Clic) on PAdd iles to NGrou" -ource Grou" Q

?. -elect lcd.h and Clic) Add

2pti&i=e Co.e for &ore #>ciency

Dou already seen that by using our header file lcd.h you can connect *C1 to any of the out"ut

 "ins of the microcontroller. or this more coding is re<uired in the header file &hich ma)es the

generated he: file less efficient and large in si%e. Dou can sol#e this "roblem by ma)ing some

changes in the header file according to your hard&are connections. or eg O in the abo#e sam"le

 "rograms I ha#e used "ort , for sending data and command. Rust re"lace the functions

 Lcd4_Port(data) and Lcd8_Port(data) &ith P2 = data.

Dou can do&nload header file! )eil c files! "roteus files etc here