chetan microcontrollers assignment 2

24
Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069 1 MICROCONTROLLERS ASSIGNMENT Nitin.J.Sanket Sem. V Sec. “B” USN:1MS09EC069 MSRIT

Upload: sachin-shetty

Post on 25-Oct-2014

353 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

1

MICROCONTROLLERS

ASSIGNMENT

Nitin.J.Sanket

Sem. V Sec. “B”

USN:1MS09EC069

MSRIT

Page 2: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

2

1. Explain with an example, bit-wise logic operators for

8051 C.

Solution:

One of the most important features of C is its

ability to perform bit manipulation.

Some of the bitwise operators in C are,

1. AND (&)

2. OR (|)

3. EX-OR (^)

4. Inverter (~)

5. Shift Right (>>)

6. Shift Left (<<)

Bit-wise logic operators are very important in

embedded systems and most of the controlling

operations use one or more pins of a particular

port and not the whole port.

Now let us discuss each operator in a bit more

detail,

AND (&):

This instruction performs the logical AND of the

byte to the left and to the right of the operator.

For example, 0x35 & 0x0F = 0x05.

This instruction is generally is used to clear/mask

the particular bit(s) and to check the status of the

bit.

Page 3: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

3

OR (|):

This instruction performs the logical OR of the byte

to the left and to the right of the operands.

For example, 0x04 | 0x68 = 0x0C.

This instruction is generally used to set the

particular bit(s).

EX-OR (^):

This instruction performs the logical EX-OR of the

byte to the left and to the right of the operands.

For example, 0x54 ^ 0x78 = 0x2C.

This instruction is generally used to clear the port as

a whole or to set or invert particular bit(s) depending

upon some condition.

Inverter (~):

This instruction performs the one’s complement of

the operand on the right.

For example, ~ 0x55 = 0xAA.

This instruction is used to find the one’s

complement of a given number.

Shift Right (>>):

The format of this instruction is as follows,

Data>>Number of bits to be shifted Right.

For example, 0x9A >> 3 = 0x13.

In the upper example,

0x9A = 10011010

Shifting right by 3 bits we get,

Page 4: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

4

00010011

The bits on the right side are automatically zero

filled.

This is used for code conversions, division.

Shift Left (<<):

The format of this instruction is as follows,

Data<<Number of bits to be shifted Right.

For example, 0x06 << 4 = 0x60.

In the above example,

0x06 = 00000110

Shifting left 4 times we get,

0110000

The bits on the left side are automatically zero filled.

This is used for code conversions, multiplication.

The table of bit-wise logic operators in C is as shown

below:

A simple C program to illustrate the use of bit-wise logic

operators is shown below:

Page 5: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

5

#include<reg51xd2.h>

void main (void)

{

PO = 0x35 & 0x0F; //ANDing

P1 = 0x04 | 0x68; //ORing

P2 = 0x54 ^ 0x78; //XORing

PO = ~0x55; //Inverting

P1 = 0x9A >> 3; // Shift right by 3 times

P0 = 0x06 << 4; // Shift left by 4 times

}

2. Explain the steps to program timers in mode 1 and

write an 8051 program to generate a square wave of

50% duty cycle on the pin P1.5.

Solution:

Consider the following program in Assembly to

generate a square wave of 50% duty cycle on pin

P1.5 using the timer in mode 1:

ORG 0

Page 6: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

6

LJMP 8000H

ORG 8000H

MOV TMOD, #01H ;Timer 0,mode 1 (16-bit mode)

LOC1:

MOV TL0, #0F2H ;Start counting from FFF2H

MOV TH0, #0FFH

CPL P1.5 ;Toggle P1 to generate a square

wave

ACALL DELAY

SJMP LOC1 ;Load the initial value for count

again

;---------------Delay Subroutine--------------

DELAY:

SETB TR0 ;Start Timer 0

LOC2:

JNB TF0,LOC2 ;Monitor Timer 0 flag until it

rolls over

CLR TR0 ;Stop Timer 0

CLR TF0 ;Clear Timer 0 overflow flag

RET

Page 7: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

7

The following steps are used to write the program:

TMOD is loaded.

FFF2H is loaded into TH0:TL0.

P1.5 is toggled for high and low portions of the

pulse.

The DELAY subroutine using the timer is called.

In th DELAY subroutine, Timer 0 is started by the

“SETB TR0” instruction.

Timer 0 counts up with passing of each clock,

which is provided by the crystal oscillator. As the

timer counts up, it goes through the states of

FFF3H, FFF4H, FFF5H …. FFFBH …. FFFFH. One

more clock rolls it to 0, which enables the timer

overflow flag ( TF0 = 1 ). At that point, the JNB

instruction falls through.

Timer 0 is stopped by the instruction “CLR TR0”.

The DELAY subroutine ends, and the process is

repeated.

NOTES:

For the process to repeat, we must reload the TL

and TH registers and start again.

Here polling mode of timer is used, which is not

very useful as the microcontroller cannot perform

any other operation when the timer is running, In

practice Interrupt mode of Timers is used which is

very effective.

Page 8: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

8

3. Write an 8051 C program to send the message “The

Earth is beautiful”, to the serial port continuously.

Assume XTAL = 11.0592MHz, set the baud rate at

4800.

Solution:

The baud rate is set using the formula,

Baud Rate = K x Oscillator Frequency

32 x 12 x [256-(TH1)]

Now, We have to set the baud rate to 4800, so

the value for TH1 becomes 0xFA.

Here let the value of SMOD be 0, hence K = 1.

The program is as shown below:

#include<reg51xd2.h>

void SerTx (unsigned char); //Function to Send

data out of Serial port.

void main (void)

{

unsigned char dat[ ] = “The Earth is

beautiful”; //Data

unsigned char i;

TMOD = 0x20; //Use Timer 1, 8 bit

auto reload

TH1 = 0xFA; //For 4800 Baud Rate

SCON = 0x50;

TR1 = 1; //Run Timer

Page 9: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

9

while(1) //Repeat forever

{

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

{

SerTx(dat[i]);

}

}

}

void SerTx ( unsigned char x )

{

SBUF = x; //Place value in Buffer

while( TI == 0); //Wait until transmitted

TI = 0;

}

Output on Keil:

P.T.O

Page 10: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

10

4. A switch is connected to the pin P1.2. Write an

8051 C program to monitor the switch and create

the following frequencies on pin P1.7.

(i) When SW = 0; 500Hz

(ii) When SW = 1; 750Hz

Use timer 0,mode 1 for both of them.

Solution:

The program is as shown below:

#include<reg51xd2.h>

sbit mybit = P1^2;

sbit SW = P1^7;

Page 11: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

11

void DelayTimer1 ( unsigned char );

void main (void)

{

SW = 1; //Make P1.7 as input pin

while ( 1 )

{

mybit = ~mybit; //Toggle P2.7

if ( SW == 0 ) //Check Switch

DelayTimer1 ( 0 );

else

DelayTimer1( 1 );

}

}

void DelayTimer1 ( unsigned char c )

{

TMOD = 0x01;

if ( c == 0 )

{

TL0 = 0x67; //FC67

TH0 = 0xFC;

}

Page 12: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

12

else

{

TL0 = 0x9A; //FD9A

TH0 = 0xFD;

}

TR0 = 1;

while ( TF0 == 0 );

TR0 = 0;

TF0 = 0;

}

Calculations:

FFFFH – FC67H = 398H

398H*1.085µs = 998.2 µs

Hence the frequency is, 1/(2 * 998.2 µs) = 500Hz

FFFFH – FD9AH = 265H

265H*1.085µs = 665.1 µs

Hence the frequency is, 1/(2 * 665.1 µs) = 750Hz.

Page 13: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

13

5. Write a C program using interrupts to do the

following:

(i) Receive data serially and send it to P0.

(ii) Read port P1, transmit data serially, and

give a copy to P2.

(iii) Make timer 0 generate a square wave of

5KHz frequency on P0.1

Assume that XTAL = 11.0592 MHz. Set the baud

rate at 4800.

Solution:

#include <reg51xd2.h>

sbit SQ = P0^1;

void timer0 ( ) interrupt 1

{

SQ = ~SQ; //Toggle Pin

}

void serial0 ( ) interrupt 4

{

Page 14: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

14

if ( TI == 1)

{

TI = 0; //Clear Interrupt

}

else

{

P0 = SBUF; //Put value on pins

RI = 0; //Clear Interrupt

}

}

void main ( )

{

unsigned int char x;

P1 = 0xFF; //Make P1 as input

TMOD = 0x22; //4800 Baud rate

TH1 = 0xF6;

SCON = 0x50;

TH0 = 0xA4; //5 KHz has T =

200µs

IE = 0x92; //Enable Interrupts

TR1 = 1; //Start Timer 1

Page 15: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

15

TR0 = 1; //Start Timer 0

while ( 1 )

{

x = P1; //Read value from pins

SBUF = x; //Put value in buffer

P2 = x; //Write value to pins

}

}

6. Write a C program to send the message “Good

Morning” serially at 9600 Baud, 8 bit, 1 stop bit.

Solution:

The baud rate is set using the formula,

Baud Rate = K x Oscillator Frequency

32 x 12 x [256-(TH1)]

Now, We have to set the baud rate to 9600, so

the value for TH1 becomes 0xFD.

Here let the value of SMOD be 0, hence K = 1.

The program is as shown below:

#include<reg52xd2.h>

void main ( void )

{

Page 16: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

16

unsigned char i;

unsigned char mess[ ] = “Good Morning”;

TMOD = 0x20; //Timer 1, 8 Bit Auto

Reload

TH1 = 0xFD; //9600 Baud Rate

TR1 = 1; //Run Timer

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

{

SBUF = mess [ i ];

while ( TI == 0 );

TI = 0;

}

}

Output on Keil:

P.T.O

Page 17: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

17

7. Write a C program to toggle all bits of P0 and P2

continuously with 250mSec delay. Use the inverting

operator.

Solution:

The program is as shown below:

#include<reg51xd2.h>

void DelayTimer1 ( void );

void main (void)

{

unsigned char i, j;

while ( 1 )

{

Page 18: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

18

P0 = ~P0; //Invert P0

P2 = ~P2; //Invert P2

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

{

//Due to for loop overhead we put

//36 and not 40

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

{

DelayTimer1 ( );

}

}

}

}

void DelayTimer1 ( void )

{

TMOD = 0x02; //Timer 0, Mode 2

TH0 = -23; //Auto Reload Value

TR0 = 1;

while ( TF0 == 0 ); //Wait till TF0 rolls

over

TR0 = 0;

Page 19: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

19

TF0 = 0;

}

Calculations:

256 – 23 = 233

23*1.085µs = 25µs

25µs x 250 x 40 = 250 ms by calculation

But due to the overhead of the “for” loop in C we put 36

instead of 40.

8. Write a 8051 C program to convert a given hex –

data FDH into its equivalent decimal data and

display the result digits on P0, P1 and P2.

Solution:

The program is as shown below:

#include<reg51xd2.h>

void main (void)

{

unsigned char hex;

Page 20: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

20

hex = 0xFD; //Data given

P0 = hex/100; //MS digit in decimal

hex = hex%100;

P1 = hex/10; //Middle digit in decimal

hex = hex%10;

P2 = hex; // LS digit in decimal

}

Output on Keil:

9. Write an ALP to read the input from PORT 1,

complement it and to output via PORT 2. This

transfer is to be done once in 50msec. Use timer 1

to generate the delay.

Solution:

Page 21: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

21

The program is as shown below:

ORG 0

LOC1:

MOV A, P1 ; Store a copy of P1 in A

CPL A ; Complement A

MOV P2, A ; Send out via P2

LCALL DELAY50MS ; Call Delay Subroutine

LJMP LOC1 ; Keep doing this

continuously

DELAY50MS:

MOV TMOD, #10H ; Timer 1, Mode 1 (16 bit

Mode)

MOV TL1, #FDH ; Count = 4BFDH = 19453

MOV TH1, #4BH ; ( 65536 – 19453) x 1.085µs

= 25ms

SETB TR1 ; Run Timer1

LOC2:

JNB TF1, LOC2 ; Wait till Timer 1 Rolls Over

CLR TR1 ; Stop Timer 1

CLR TF1

RET

END

Page 22: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

22

Here the timer is used in polling mode, however

in practice Interrupt mode is preferred.

Output on Keil:

10. Explain Mode 2 programing of timers with a

neat sketch and specify the programming steps.

Solution:

Features of Mode 2 Programming:

It is an 8 bit timer and hence allows only values

00H to FFH to be loaded into the timer’s register

TH.

After TH is loaded with the 8 bit value, 8051

gives a copy of it to TL. Then the timer must be

started. This is done be SETB TRn where n is 0

or 1 for Timer 0 or 1 respectively.

After the timer is started, it starts incrementing

Page 23: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

23

TL, wherein it counts till FFH. When it rolls over

from FFH to 00H it sets high the TF ( Timer flag

).

When TF goes high TL is automatically reloaded

with the original value kept by the TH register.

Here we only have to clear TF and there is no

need to reload the original value, hence Mode 2

of the Timer is called as an Auto Reload 8 bit

Timer Mode.

Steps to program in Mode 2:

To generate a desired delay using Mode 2, one

must use the following procedure:

Load TMOD with a value indicating the timer to

be used and select Mode 2.

Load TH with initial count value.

Start the Timer.

Keep monitoring the timer flag TF with the

“JNB TFx, target” instruction to see whether it

has gone high. When TF is high come out of the

loop.

Page 24: Chetan Microcontrollers Assignment 2

Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069

24

Clear the TF flag.

Go back to the monitor the TF flag as Mode 2 is

auto reload.