Professional Documents
Culture Documents
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.
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 ones complement of the operand on the right. For example, ~ 0x55 = 0xAA. This instruction is used to find the ones 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,
Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069
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:
#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
Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069
MOV TMOD, #01H LOC1: MOV TL0, #0F2H MOV TH0, #0FFH CPL P1.5 ACALL DELAY SJMP LOC1
;---------------Delay Subroutine-------------DELAY: SETB TR0 LOC2: JNB TF0,LOC2 CLR TR0 CLR TF0 RET ;Monitor Timer 0 flag until it rolls over ;Stop Timer 0 ;Clear Timer 0 overflow flag ;Start Timer 0
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.
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; auto reload TH1 = 0xFA; SCON = 0x50; TR1 = 1; //Run Timer //Use Timer 1, 8 bit //For 4800 Baud Rate
while(1) {
//Repeat forever
void SerTx ( unsigned char x ) { SBUF = x; while( TI == 0); TI = 0; } Output on Keil: //Place value in Buffer //Wait until transmitted
P.T.O
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;
10
void DelayTimer1 ( unsigned char ); void main (void) { SW = 1; while ( 1 ) { mybit = ~mybit; //Toggle P2.7 if ( SW == 0 ) //Check Switch //Make P1.7 as input pin
void DelayTimer1 ( unsigned char c ) { TMOD = 0x01; if ( c == 0 ) { TL0 = 0x67; TH0 = 0xFC; } //FC67
11
else { TL0 = 0x9A; TH0 = 0xFD; } TR0 = 1; while ( TF0 == 0 ); TR0 = 0; TF0 = 0; } //FD9A
Calculations: FFFFH FC67H = 398H 398H*1.085s = 998.2 s Hence the frequency is, 1/(2 * 998.2 s) = 500Hz
FFFFH FD9AH = 265H 265H*1.085s = 665.1 s Hence the frequency is, 1/(2 * 665.1 s) = 750Hz.
12
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;
13
void main ( ) { unsigned int char x; P1 = 0xFF; TMOD = 0x22; TH1 = 0xF6; SCON = 0x50; TH0 = 0xA4; IE = 0x92; TR1 = 1; //5 KHz has T = 200s //Enable Interrupts //Start Timer 1 //Make P1 as input //4800 Baud rate
14
//Start Timer 0
//Read value from pins //Put value in buffer //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 ) {
Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069
15
unsigned char i; unsigned char mess[ ] = Good Morning; TMOD = 0x20; TH1 = 0xFD; TR1 = 1; //Timer 1, 8 Bit Auto Reload //9600 Baud Rate //Run Timer
P.T.O
16
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 ) {
Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069
17
P0 = ~P0; P2 = ~P2;
//Invert P0 //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; TH0 = -23; TR0 = 1; while ( TF0 == 0 ); TR0 = 0;
Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069
18
TF0 = 0; }
Calculations: 256 23 = 233 23*1.085s = 25s 25s 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;
19
// 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:
20
The program is as shown below: ORG 0 LOC1: MOV A, P1 CPL A MOV P2, A LCALL DELAY50MS LJMP LOC1 DELAY50MS: MOV TMOD, #10H MOV TL1, #FDH MOV TH1, #4BH SETB TR1 LOC2: JNB TF1, LOC2 CLR TR1 CLR TF1 RET END ; Wait till Timer 1 Rolls Over ; Stop Timer 1 ; Timer 1, Mode 1 (16 bit Mode) ; Count = 4BFDH = 19453 ; ( 65536 19453) x 1.085s = 25ms ; Run Timer1 ; Store a copy of P1 in A ; Complement A ; Send out via P2 ; Call Delay Subroutine
21
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 timers 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
Nitin.J.Sanket, 5th Sem B Section, Assignment 2, USN:1MS09EC069
22
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.
23
Clear the TF flag. Go back to the monitor the TF flag as Mode 2 is auto reload.
24