You are on page 1of 38

MICROCONTROLLER ASSIGNMENT 3

Question 1:- Explain the registers and pins of an LCD panel and write and 8051 C program to display the message HELLO on the LCD panel. LCD pin descriptions: Pin 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Symbol Vss Vcc VEE RS R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7 I/O I I I/O I/O I/O I/O I/O I/O I/O I/O I/O Description Ground +5V power supply power supply to control RS=0 to select code register RS=1 to select data register R/W=0 for write R/W for read Enable The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus

V CC , V SS and V EE :While VCC and VSS provide +5V and ground, respectively, VEE is used for controlling LCD contrast. RS, register select:If RS=0, the instruction command code register is selected, allowing the user to send a command such as clear display, return home etc. If RS=1 the data register is selected, allowing the user to send data to be displayed on the LCD.
5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

R/W, read/write: R/W input allows the user to write the information to the LCD or read the information from it. E, enable:The enable pin is used by the LCD to latch information presented to its data pins. When data is supplied to data pins, a high-to-low pulse must be applied to this pin in order for the LCD to latch in the data present at the data pins. This pulse must be a minimum of 450ns wide. D0-D7:The 8-bit data pins, D0-D7, are used to send information to the LCD or read the contents of the LCDs internal registers. To display the letters and numbers we send ASCII codes and also there are instruction command codes we send to these pins while making RS=1. We also use RS=0 to check the busy flag bit to see if the LCD is ready to receive information. The busy flag is D7 and can be read when R/W=1, RS=0.

Registers of LCD panel :There are main two registers inside the LCD 1. Data register: This register is selected when RS=1. Here LCD stores the message to be displayed i.e. allowing the user to send data to be displayed on the LCD. 2. Code register: This register is selected when RS=0. Allowing the user to send a command to LCD. The following table lists the command codes.

Code (hex) 1 2 4 5 6 7

Command to LCD instruction register Clear display screen Return home Shift cursor to left Shift display right Shift cursor to right Shift display left

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

8 A C E F 10 14 18 1C 80 C0 38

Display off, cursor off Display off, cursor on Display on, cursor off Display on, cursor blinking Display on, cursor blinking Shift cursor position to left Shift cursor position to right Shift entire display to the left Shift entire display to the right Force cursor to beginning of 1st line Force cursor to beginning of 2nd line 2 lines and 5*7 matrix

C-Program: #include <reg51.h> sfr ldata = 0x90; sbit rs = P2^0; sbit rw = P2^1;

//P1=LCD data pins

sbit

en =P2^2;

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

void lcdcmd(unsigned char value) { ldata = value; //put the value on the pins rs = 0; //to select code register rw =0; en = 1; //strobe the enable pin MSDelay (1); en=0; return; } void lcddata (unsigned char value) { ldata = value; //put the value on the pins rs = 1; //to select the data register rw =0; en = 1; //strobe the enable pin MSDelay (1); en=0; return; }

void {

MSDelay (unsigned int itime) Unsigned int i, j; for (i=0; i<itime; i++) for (j=0; j<1275; j++);

} void main() { unsigned char msg[] =HELLO; int k; lcdcmd (0x38); //2 lines and 5*7 matrix MSDelay (250);

lcdcmd (0x0E);

// Display on and cursor blinking

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

MSDelay (250); lcdcmd (0x01); // Clear display screen MSDelay (250); lcdcmd (0x06); // Shift cursor to right after inserting each //character MSDelay (250); lcdcmd (0x86); //line 1, position 6 MSDelay (250); for (k=0; k<5; k++) { lcddata (msg[k]); MSDelay(250); } }

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Q2. Draw the 8051 connection to DAC0808 at port P1 and write 8051 ALP to generate a sine wave

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Angle(Degrees)

Sin (angle)

Voltage Magnitude 5V + (5V*Sin(angle)) 5 7.5 9.33 10 9.33 7.5 5 2.5 0.67 0 0.67 205 5

Voltage sent to DAC (Voltage Mag. * 25.6) 128 192 238 255 238 192 128 64 17 0 17 64 128

0 30 60 90 120 150 180 210 240 270 300 330 360

0 0.5 0.87 1 0.87 0.5 0 -0.5 -0.87 -1 -0.87 -0.5 0

ALP: ORG 40 AGAIN: BACK: MOV MOV CLR MOVC DPTR, #TABLE R2, #COUNT A A,@A+DPTR ;Initialize DPTR to start of table ;Initialize count

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

MOV P1,A ;Move values from LUT to P1 INC DPTR DJNZ R2,BACK ;Loop the values SJMP AGAIN ;Looping sine wave ORG 300H ;Initializing LUT TABLE: DB 128,192,238,255,192,128,64,17,0,17,64,128 END

Question 3:-Show a simple keyboard interfacing with Port 1 and Port 2 of 8051 and explain its operation. Keyboards are organized in a matrix of rows & columns. The cup Accesses both rows & columns through ports; therefore, with two 8 bit Ports, an 8 8 matrix of keys can be connected to a microprocessor. When a key is pressed, a row & a column make a contact; otherwise, there is no connection between rows & columns. In IBM pc keyboards, a single microcontroller takes care of hardware & software interfacing of the keyboard. In such system, it is the function of programs stored in the EPROM of the microcontroller to scan the keys continuously, identify which one has been activated, & present it to the motherboard. Fig shows 44 matrix connected to two ports. The rows are connected to an output port & the columns are connected to an input port. If no key has been pressed, reading the input port will yield 1s for all columns since they are connected to high (Vcc). If all the rows are grounded & a key is pressed, one of the columns will have 0 since the key pressed provides the path to ground. It is the function of the microcontroller to scan the keyboard continuously to detect & identify the key pressed. Operation:To detect the pressed key, the microcontroller grounds all the rows by providing 0 to the output latch, then it reads the columns. If the data read from the columns is D3D0=1111, no key has been pressed and the process continues until a key press is
5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

detected. However, if one of the column bits has a zero, this means that a key press has occurred. After a key press is detected, the microcontroller will go through the process of identifying the key. Starting with the top row, the microcontroller grounds it by providing a low to row D0 only; then it reads the columns. If the data read is all 1s, no key in that row is activated and the process is moved to the next row. It grounds the next row, reads the columns, and checks for any zero. This process continues until the row is identified. After identification of the row in which the key has been pressed, the next task is to find out which column the pressed key belongs to. This should be easy since the microcontroller knows at any time which row and column are being accessed.

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

4. Show a scheme of interfacing ADC0809 to 8051 controller. Write an ALP to obtain the output from such an interface. Discuss practical application.
Ans.
5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

The ADC0809 chip has an 8 bit data output. The 8 analog input channels are multiplexed and selected according to the table(i) shown using 3 address pins A,B and C. Selected Analog Channel IN0 IN1 IN2 IN3 IN4 IN5 IN6 IN7

C 0 0 0 0 1 1 1 1

B 0 0 1 1 0 0 1 1

A 0 1 0 1 0 1 0 1

Table 1

IN0

gnd

Clk

Vcc

D0

Fig. 1

ADC0809
IN7 Vref(+) Vref(-) SC ALE C B A EOC OE D7

(LSB)

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Vref (V) Not connected 4.0 3.0 2.56 2.0 1.0

Vin (V) 0 to 5 0 to 4 0 to 3 0 to 2.56 0 to 2.0 0 to 1.0 Table 2

Step Size (mV) 5 / 256 = 19.53 4 / 256 = 15.62 3 / 256 = 11.71 2.56 / 256 = 10 2 / 256 = 7.81 1 / 256 = 3.90

Steps for programming the ADC0809: The following are the steps to get data from an ADC0809 1. Select analog channel by providing bits to A, B and C address according to table(ii). 2. Activate the ALE (address latch enable) pin. It needs an L-H pulse to latch in the address. 3. Activate SC (start conversion) by an L-H pulse to initiate conversion. 4. Monitor EOC (end of conversion) to see whether conversion is finished. H-L output indicates that the data is converted and is ready to be picked up. If we do not use EOC, we can read the converted digital data after a brief time delay. The delay size depends on the speed of the external clock we connect to CLK pin. 5. Activate OE (output enable) to read data out of the ADC chip. An L-H pulse to the OE pin will bring digital data out of the chip.

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

In ADC0809 there is no self clock and the clock must be prepared from an external source to CLK pin. The speed of conversion depends on the frequency of the clock connected to the CLK pin, it cannot be faster than 100 microseconds.

CLOCK

WR(SC)

RD(OE)

ALE

ADDR

EOC(INTR)

D0-D7

LATCH ADDRESS

Selecting a Channel and Read Timing for ADC0809

LATCH DATA

Programming ADC0809 in Assembly ALE OE SC EOC ADDR_A ADDR_B ADDR_C MYDATA ORG MOV SETB BIT P2.4 BIT P2.5 BIT P2.6 BIT P2.7 BIT 92.0 BIT P2.1 BIT P2.2 EQU P1 0H MYDATA, #0FFH EOC

;make P1 as input ;make EOC as input

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

CLR CLR CLR BACK: CLR CLR SETB ACALL SETB ACALL SETB ACALL CLR CLR HERE: JB HERE1:JNB SETB ACALL MOV CLR ACALL ACALL SJMP

ALE SC OE ADDR_C ADDR_B ADDR_A DELAY ALE DELAY SE DELAY ALE SC EOC, HERE EOC, HERE1 OE DELAY A, MYDATA OE CONVERSION DATA_DISPLAY BACK

;clear ALE ;clear WR ;clear RD ;C=0 ;B=0 ;A=1 (select channel 1) ;make sure address is stable ;latch address ;delay for fast DS89C4x0 Chip ;start conversion

;wait until done ;wait until done ;enable RD ;wait ;read data ;clear RD for next time ;hex to ASCII ;display the data

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Question 5:- Describe the 8051 connection to stepper motor. A switch is connected to pin p2.7. Write a c program to monitor the status of SW and perform the following If SW=0, the stepper motor moves clockwise. If SW=1, the stepper motor moves counter-clockwise.

The following steps show the 8051 connection to the stepper motor and its programming: 1. Use an ohmmeter to measure the resistance of the leads. This should identify which COM leads are connected to which winding leads.

2. The common wire(s) are connected to the positive side of the motors power supply. In many motors, +5V is sufficient.

3. The four leads of the stator winding are controlled by four bits of the 8051 port (P1.0 P1.3). However, since the 8051 lacks the sufficient current to drive the stepper motor windings, we must use a driver such as the ULN2003 to energize the stator. Instead of the ULN2003, we could have used transistors as drivers, as shown in the figure below. However, notice that if transistors are used as drivers, we must also use diodes to take care of inductive current generated when the coil is turned off. One reason that using the ULN2003 is preferable to the use of transistors as drivers is that the ULN2003 has an internal diode to take care of the back EMF.

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Fig. 8051 Connection to Stepper

// A C program to monitor P2.7 (SW) and rotate the stepper motor // clockwise if SW = 0; anticlockwise if SW = 1 #include <reg.h> sbit SW=P2^7; void main() { SW=1; while(1) { if(SW==0) {

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

P1=0x66; MSDelay(100); P1=0xCC; MSDelay(100); P1=0x99; MSDelay(100); P1=0x33; MSDelay(100); } else

{ P1=0x66; MSDelay(100); P1=0x33; MSDelay(100); P1=0x99; MSDelay(100); P1=0xCC; MSDelay(100); }


5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

} } void MSDelay(unsigned int value) { unsigned intx,y; for(x=0;x<1275;x++) for(y=0;y<value;y++); }

6. How can Microcontroller be used to control automatically the speed of a DC motor? Explain the concept clearly.
Ans: DC Motor: A direct current (DC) motor is a device that translates electrical pulses into mechanical movement. The maximum speed of a dc motor is indicated in rpm. The dc motor has two rpm no load and loaded. The normal voltage is the voltage for the motor in normal conditions, and can be from 1 to 150V depending on the motor. As we increase voltage, rpm goes up. The speed of the motor depends on 3 factors: (a) load, (b) voltage, and (c) current. For a given fixed load we can maintain a steady speed by using a method called pulse width modulation (PWM). B changing the width of the pulse applied to the DC motor we can increase or decrease the amount of power provided to the motor, thereby increasing or decreasing the motor speed. Although the voltage has affixed amplitude, it has a variable duty cycle. i.e, wider the pulse, higher the speed. For microcontrollers without PWM circuitry, we must create the various duty cycle pulses using software, which prevents the microcontroller from
5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

doing other things. The ability to control the speed of the dc motor using PWM is one reason that DC motors are preferred over AC motors. AC motor speed is dictated by the AC frequency of the voltage applied to the motor and frequency is generally fixed. Therefore we cannot control the speed of AC motor when the load is increased. We can also change the DC motors direction and torque.

1/4 POWER

25% DC

1/2 POWER

50% DC

3/4 POWER

75% DC

FULL POWER

100% DC

Pulse width Modulation Comparision

Question 7:- Explain the registers and pins of an LCD panel and write an 8051 ALP display message MSRIT on the LCD panel. The Optrex LCD Panel has 14 pins. The function of each pin is given in the table below.

Pin 1 2 3

Symbol VSS VCC VEE

I/O -

Description Ground +5V power supply Power supply to control contrast

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

RS

RS = 0 to control command register RS = 1 to select data register

5 6 7 8 9 10 11 12 13 14

R/W E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7

I I/O I/O I/O I/O I/O I/O I/O I/O I/O

R/W = 0 for write, R/W = 1 for read Enable The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus The 8-bit data bus

VCC, VSS and VEE :VCC and VSS provide +5V and ground respectively. VEE is used for controlling LCD contrast.

RS, register select:The RS pin is used to select between different registers of the LCD. If RS = 0, the instruction command code register is selected, allowing the user to send a

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

command such as clear display, cursor at home, etc. to the LCD. If RS = 1 the data register is selected, allowing the user to send data to be displayed on the LCD.

R/W, Read/Write:R/W input allows the user to write information to the LCD or read information from it. R/W=1 when reading; R/W=0 when writing.

E, Enable:The enable pin is used by the LCD to latch information presented to its data pins. When data is supplied to data pins, a high-to-low pulse must be applied to this pin in order for the LCD to latch in the data present at the data pins. This pulse must be a minimum of 450ns wide.

D0-D7:The 8-bit data pins, D0-D7, are used to send information to the LCD or read the contents of the LCDs internal registers.

To display letters and numbers, we send ASCII codes for the letters A-Z, a-z, and numbers 0-9 to these pins while making RS=1.

There also instruction command codes that can be sent to the LCD to clear the display or force the cursor to home position or blink the cursor. The table below lists the instruction command codes.
5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

RS = 0 is used to check the busy flag bit to find out if the LCD is ready to receive information. The busy flag is D7 and can be read when R/W = 1 and RS = 0. When D7 = 1, the LCD is busy taking care of internal operations and will not accept any new information. When D7 = 0, the LCD is ready to receive new information.

Code (Hex) 1 2 4 6 5 7 8 A C E F 10 14 18

Command to LCD instruction register

Clear display screen Return home Decrement cursor (shift cursor to left) Increment cursor (shift cursor to right) Shift display right Shift display left Display off, cursor off Display off, cursor on Display on, cursor off Display on, cursor blinking Display on, cursor blinking Shift cursor position to left Shift cursor position to right Shift the entire display to the left

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

1C 80 C0 38

Shift the entire display to the right Force cursor to beginning of 1st line Force cursor to beginning of 2nd line 2 lines and 5X7 matrix

The LCD has data and command (instruction) registers. The data register is used to store the data to be displayed on the LCD. The command (instruction) register is used to store the instructions to the LCD.

Program to display MSRIT on the LCD Panel ORG 0000H MOV DPTR, #MYCOM C1: CLR A

MOVC A,@A+DPTR ACALL COMNWRT ACALL DELAY JZ SEND_DAT INC DPTR SJMP C1 SEND_DAT: MOV DPTR, #MYDATA
5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

; call command subroutine

; give LCD some time

D1:

CLR A MOVC A,@A+DPTR ACALL DATAWRT ACALL DELAY INC DPTR JZ AGAIN SJMP D1 ; call command subroutine

; give LCD some time

AGAIN:

SJMP AGAIN

; stay here

COMNWRT: MOV P1, A CLR P2.0 CLR P2.1 SETB P2.2 ACALL DELAY

; SEND COMND to P1 ; RS=0 for command ; R/W=0 for write ; E=1 for high pulse ; give LCD some time

CLR P2.2

; E=0 for H-to-L RET

DATAWRT: MOV P1, A SETB P2.0 CLR P2.1 SETB P2.2

; SEND DATA to P1 ; RS=1 for data ; R/W=0 for write ; E=1 for high pulse

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

ACALL DELAY CLR P2.2 RET

; give LCD some time ; E=0 for H-to-L pulse

DELAY:

MOV R3, #250

; long delay for fast CPUs

HERE2: MOV R4, #255 HERE: DJNZ R4, HERE DJNZ R3, HERE2 RET

ORG 0300H

; lookup table

MYCOM: DB 38H,0EH,01H,06H,84H,00H ; commands and null MYDATA: END DB MSRIT, 00H ; data and null

8. DRAW 8051 CONNECTION T0 DAC0808 AT PORT P1 AND WRITE A C PROGRAM TO GENERATE SINE WAVE.
Ans.

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

In DAC 808 the digital inputs are converted to current (Lout) and by connecting a resistor to the Lout pin we convert the result to voltage. Lout is a function of digital inputs and reference current as below.

The Lref current is generally 2mA.The maximum output current, (if all inputs to DAC are high) is 1.99mA (from the above figure). Generating Sine Wave: Full scale output of DAC is achieved when all data inputs of DAC are high. Therefore, to achieve full-scale output we use the following equation. Vout=5V*(1 + sin).
5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Vout for DAC for various angles is calculated as below at increment of 30 degrees. Angle @ (degrees) 0 30 60 90 120 150 180 210 240 270 300 330 360 sin@ Vout(voltage mag) 5V*(1 + sin) 5 7.5 9.33 10 9.33 7.5 5 2.5 0.669 0 0.699 2.5 5 Values sent to DAC(decimal) (Voltage mag * 25.6) 128 192 238 255 238 192 128 64 17 0 17 64 128

0 0.5 0.866 1.0 0.866 0.5 0 -0.5 -0.866 -1.0 -0.866 -0.5 0

C program to generate sine wave: #include <reg51.h> sfr DACDATA =P1; void main() { Unsigned char table[12]={128,192,238,255,238,192,128,64,17,0,17,64}; Unsigned char x; while(1) { for(x=0;x<=12;x++) { DACDATA=table[x]; } } }
5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Question9:- How can a microcontroller be used to control automatically the speed of a dc motor. Explain the concept clearly. The speed of motor depends on three factors: a)load b) Current c) Voltage For a fixed load we can maintain a steady speed using a method called pulse width modulation. By changing (modulating) the width if the pulse applied to the DC motor we can increase or decrease the amount power provided to the motor, thereby increasing o decreasing the motor speed. Although the voltage has a fixed amplitude, it has a variable duty cycle. That means the wider the pulse, the higher the speed.PWM is so widely used in DC motor control that some microcontrollers come with PWM circuitry embedded in the chip. In such microcontrollers all we have to do is to load the proper registers with values of high and low portions of the desired pulse, and rest is taken care by the microcontroller. This allows the microcontroller to do other things. For microcontroller without PWM circuitry, we must create the various duty cycle pulses using software which prevents the microcontroller from doing other things. Pulse width modulation comparison: .25 power 25% DC .50 power 50% DC .75 power 75% DC Full power 100% DC

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

EX: C program to control speed of DC motor based switches connected to p2.6 and p2.7 as per table below SW2(p2.7) 0 0 1 1 SW(p2.6) 0 1 0 1 Comments DC motor moves slowly(25% duty cycle) DC motor moves moderately(50% duty cycle) DC motor moves fast(75% duty cycle) DC motor moves very fast(100% duty cycle)

#include<reg51.h> sbit mtr=P1^0; void msdelay(unsigned int value); void main() { Unsigned char x; P2=0xFF; z=P2; z=z&0x03; mtr =0; while (1) { switch (z)

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

{ case(0): { mtr=1; msdelay(25); mtr=0; msdelay(75); break; } case(1): { mtr=1; msdelay(50); mtr=0; msdelay(50); break; } case(2): { mtr=1;

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

msdelay(75); mtr=0; msdelay(25); break; } default: mtr=1; } } }

Q10. Show a scheme of interfacing an 8-bit ADC to 8051 controller. Write the code required in C to obtain the output from such an interface. Discuss practical application.
Ans.

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Code: #include <reg51.h> sbit RD = P2^5; sbit WR= P2^6; sbit INTR = P2^7; sfr MYDATA = P1; void main() { unsigned char value; MYDATA = 0xFF; INTR = 1;

/* make P1 as input */ /* make INTR input */

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

RD = 1; /* set RD high */ WR = 1; /* set WR high */ while(1) { WR = 0; /*send WR pulse */ WR = 1; while(INTR==1); /* wait for EOC*/ RD = 0; /*send RD pulse value = MYDATA; // read value ConvertAndDisplay(value); //function to convert and display the output RD = 1; } } Practical Applications: Analog-to-digital converters are among the most widely used devices for data acquisition. Digital computers use binary (discrete) values, but in the physical world everything is analog (continuous). Temperature, pressure (wind or liquid), humidity, and velocity are a few examples of physical quantities that we deal with every day. A physical quantity is converted to electrical (voltage, current) signals using a device called a transducer. Transducers are also referred to as sensors. Sensors for temperature, velocity, pressure, light, and many other natural quantities produce an output that is voltage (or current). Therefore, we need an analog-to-digital converter to translate the analog signals to digital numbers so that the microcontroller can read and process them. An ADC has n-bit resolution where n can be 8, 10, 12, 16 or even 24 bits. The higher-resolution ADC provides a smaller step size, where step size is the smallest change that can be discerned by an ADC.

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Question 11:- Describe the 8051 connection to stepper motor. A switch is connected to pin p2.7. Write a c program to monitor the status of SW and perform the following If SW=0, the stepper motor moves clockwise. If SW=1, the stepper motor moves counter-clockwise.

The following steps show the 8051 connection to the stepper motor and its programming: 1. Use an ohmmeter to measure the resistance of the leads. This should identify which COM leads are connected to which winding leads.

2. The common wire(s) are connected to the positive side of the motors power supply. In many motors, +5V is sufficient.

3. The four leads of the stator winding are controlled by four bits of the 8051 port (P1.0 P1.3). However, since the 8051 lacks the sufficient current to drive the stepper motor windings, we must use a driver such as the ULN2003 to energize the stator. Instead of the ULN2003, we could have used transistors as drivers, as shown in the figure below. However, notice that if transistors are used as drivers, we must also use diodes to take care of inductive current generated when the coil is turned off. One reason that using the ULN2003 is preferable to the use of transistors as drivers is that the ULN2003 has an internal diode to take care of the back EMF.

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Fig. 8051 Connection to Stepper ORG 0H SETB P2.7 MOV A, #66H MOV P1, A JNB P2.7, CW RR A ACALL DELAY MOV P1, A SJMP TURN RL A ACALL DELAY MOV P1, A SJMP TURN MOV R2, #100 MOV R3, #255 DJNZ R3, H2 DJNZ R2, H1 RET END ; starting address ; make an input ; starting phase value ; send value to port ; check switch results ; rotate right ; call delay ; write value to port ; repeat ; rotate left ; call delay ; write value to port ; repeat

MAIN:

TURN:

CW:

DELAY: H1: H2:

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Q12. Show a simple keyboard interface with port P1 and P2 of 8051 and explain its operation.
Ans: Keyboards are organized in a matrix of rows & columns. The cup Accesses both rows & columns through ports; therefore, with two 8 bit Ports, an 8 8 matrix of keys can be connected to a microprocessor. When a key is pressed, a row & a column make a contact; otherwise, there is no connection between rows & columns. In IBM pc keyboards, a single microcontroller takes care of hardware & software interfacing of the keyboard. In such system, it is the function of programs stored in the EPROM of the microcontroller to scan the keys continuously, identify which one has been activated, & present it to the motherboard. Fig shows 44 matrix connected to two ports. The rows are connected to an output port & the columns are connected to an input port. If no key has been pressed, reading the input port will yield 1s for all columns since they are connected to high (Vcc). If all the rows are grounded & a key is pressed, one of the columns will have 0 since the key pressed provides the path to ground. It is the function of the microcontroller to scan the keyboard continuously to detect & identify the key pressed.

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

Operation:
To detect the pressed key, the microcontroller grounds all the rows by providing 0 to the output latch, then it reads the columns. If the data read from the columns is D3-D0=1111, no key has been pressed and the process continues until a key press is detected. However, if one of the column bits has a zero, this means that a key press has occurred. After a key press is detected, the microcontroller will go through the process of identifying the key. Starting with the top row, the microcontroller grounds it by providing a low to row D0 only; then it reads the columns. If the data read is all 1s, no key in that row is activated and the process is moved to the next row. It grounds the next row, reads the columns, and checks for any zero. This process continues until the row is identified. After identification
5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

of the row in which the key has been pressed, the next task is to find out which column the pressed key belongs to. This should be easy since the microcontroller knows at any time which row and column are being accessed.

5th Sem, C, Assignment 3, Srikanth TR, 1MS09EC119

You might also like