Vous êtes sur la page 1sur 69

Microcontroller Lab Manual 1) Write an assembly language Program to transfer n=10 bytes of data from location 8035H to location

8050H( without overlapping) ORG 0H SJMP 30H ORG 30H MOV DPH,#80H MOV R0,#35H MOV R1,#50H MOV R3,#0AH BACK:MOV DPL,R0 MOVXA,@DPTR MOV DPL,R1 MOVX@DPTR,A INC R0 INC R1 DJNZ R3,BACK HERE:SJMP HERE END

//Source address //Destination address //count //Get data from source //Move to destination //Increment pointers //wait here

Result: Before Execution:10 Locations X:8035H are filled up with data.

After Execution: 10 locations X:8050H are filled up with data from 8035H.

Algorithm: 1. Initialize registers to hold count data & also the source & destination addresses. 2. Get data from source location into accumulator and transfer to the destination location. 3. Decrement the count register and repeat step 2 till count is zero. Note: For data transfer with overlap start transferring data from the last location of source array to the last location of the destination array. Dept of ECE Page 1

Microcontroller Lab Manual 2) Assembly Language Program to Exchange a Block of Data. Write an assembly language program to Exchange n=5bytes 0f Data at location 0027H and at location 0041H ORG 0H SJMP 30H ORG 30H MOV R0,#27H //Source address MOV R1,#41H //Destination address MOV R3,#05H //Count BACK:MOVX A,@DPTR //Get data from source MOV R2,A //Save it in R2 MOVX A,@R1 //Get data from destination MOVX @DPTR ,A //Move it to source MOV A,R2 //Move source data(stored in R2 )to destination MOVX @R1,A INC DPTR //Increment pointers INC R1 DJNZ R3,BACK //Decrement count and repeat HERE: SJMP HERE //Wait here END RESULT: Before Execution:5 locations at x:0027H and x:0041H are filled up with data.

After execution: The data at x:0027H and x:0041H are exchanged

Algorithm: 1. Initialize registers to hold count data & also the source & destination addresses. 2. Get data from source location into accumulator and save in a register. 3. Get data from destination location into accumulator. 4. Exchange the data at the two memory locations. 5. Decrement the count register and repeat from step 2 to 4 till count is zero. 3) Assembly Language Program to Sort Numbers. Dept of ECE Page 2

Microcontroller Lab Manual //Bubble sort Program Write an assembly language program to sort an array of n-6 bytes of data in ascending order stored from location 9000H. (use bubble sort algorithm) ORG 0H SJMP 30H ORG 30H MOV R0,#05H L1: MOV DPTR,#9000H

L2:

NOEXCHNG: HERE: Algorithm:

MOV A,R0 MOV R1,A MOVX A,@DPTR //Get numbers from array MOV B,A //& Store in B INC DPTR MOVX A,@DPTR //Next number in the array CLR C //Reset borrow flag MOV R2, A //Store in R2 nd st SUBB A, B //2 -1 No.---No compare Instruction in 8051 JC NOEXCHG //JNC- For ascending order MOV A, B // Exchange the 2 numbers in the array MOVX @DPTR, A DEC DPL //DEC DPTR-Instruction Not Present MOV A, R2 MOVX @DPTR, A INC DPTR DJNZ R1,L2 // Decrement compare counter DJNZ R0, L1 //Decrement pass counter SJMP HERE END

//Count n-1 Array //Size-n-Pass Counter //Array stored from //Address 9000H //Initialize exchange counter

1. Store the elements of the array from the address 9000h 2. Initialize a pass counter with array size-1 count (for number of passes) 3. Load compare counter with pass counter contents and initialize DPTR to point to the start address of the array(here 9000H). 4. Store the current and the next array elements pointed by DPTR in registers B & R2 respectively. 5. Subtract the next element from the current element. 6. If the carry flag is set (for ascending order) then exchange the 2 numbers in the array. 7. Decrement the compare counter and repeat through step 4 until the counter becomes 0. 8. Decrement the pass counter and repeat through step 3 until the counter becomes 0.

Result:

Dept of ECE

Page 3

Microcontroller Lab Manual Before Execution: Unsorted array at 9000H

After Execution: Sorted array (Descending order) at 9000H

4.Write an assembly language program to find the largest element in a given string of n=6bytes at location 4000H.Store the largest element at location 4062H. ORG 0H SJMP 30H ORG 30H MOV R3,#6 MOV DPTR,#4000H MOVX A,@DPTR MOV R1,A NEXTBYTE:INC DPTR MOVX A,@DPTR CLR C MOV R2,A SUBB A,R1 JC SKIP MOV A,R2 MOV R1,A SKIP: DJNZ R3,NEXTBYTE MOV DPL,#62H MOV A,R1 MOVX @DPTR,A OVER: SJMP OVER END

//Length of the array //Starting address of the array //R1 is to store //Largest number

//Reset borrow flag //Next number in the array //Other Num-Previous Largest No. // JNC for smallest element // UPDATE larger number in R1 // Location of the result -4062H //Largest number //Store at #4062H

Algorithm: Dept of ECE Page 4

Microcontroller Lab Manual

1. 2. 3. 4. 5. 6.

Store the elements of the array from the address 4000H Store the length of the array in R3 and set it as Counter. DPTR is loaded with the starting address of the array. Store the first number of the array in R1(R1 is assigned to hold the largest number Increment DPTR Subtract the number pointed by DPTR from the contents of R1(to compare whether the next array elements is larger than the one in R1). 7. If the element pointed by DPTR is larger (than the one already present in R1) then load the larger number in R1. 8. Decrement the counter and repeat steps through 5 until the counter becomes 0. 9. Store the largest number in R1 address 4062H. RESULT: Before Execution:

After Execution: Location 4062 has the largest element.

Assembly Language Program Illustrating Addition, Subtraction, Multiplication and Division. Dept of ECE Page 5

Microcontroller Lab Manual 5) Write an ALP to perform the following: If X=0 perform w+v; else if X=1 perform w-v; else if X=2 perform w*v; else if X=3 perform w/v, where w & v are eight bit numbers. ORG 0000H SJMP 30H ORG 30H MOV DPTR,#9000H MOVX A,@DPTR MOV R0,A INC DPTR MOVX A,@DPTR CJNE R0,#00,CKSUB ADD A, R1 //Perform Addition SJMP LAST CKSUB:CJNE R0,#01,CKMUL CLR C // Clear Carry flag MOV A,R1 SUBB A,R2 //FF indicates negative number SJMP LAST CKMUL:CJNE R0,#02,CKDIV MOV A,R1 MOV B,R2 MUL AB //16-Bit product in AB with A having lower byte SJMP LAST CKDIV:CJNE R0,#03,OTHER MOV A,R1 MOV B,R2 DIV AB //Quotient in A & reminder in B SJMP LAST OTHER:MOV A,#00 MOV B,#00 LAST:INC DPTR MOVX @DPTR,A //Store A&B in the next END Algorithm: 1. 2. 3. 4. Store the condition X in R1. load the first and second numbers to A and B registers respectively. Compare the contents of R1 and perform the operations add, sub, etc accordingly. store the result present in A and B registers to the appropriate memory locations.

RESULT: Before Execution :ADD Dept of ECE

After Execution Page 6

Microcontroller Lab Manual

Before Execution :SUB

After Execution

Before Execution :MUL

After Execution

Before Execution :DIV

After Execution

6) An eight number X is stored in external memory location 9000H. Write an ALP to compute (i) The square of the X if D1 of data RAM 20H(bit address 01H) is set (ii) The cube of the X if D1 of data RAM 20H (bit address 01H) is reset. Store your result at locations 9001,9002,9003H ORG 0H SJMP 30H ORG 30H MOV DPTR,#9000H MOVX A,@DPTR MOV R0,A MOV B,A MUL AB CLR C JB 01,LAST PUSH B MOV B,A Dept of ECE

//Get number-X // Store in R0 //Square it-X^2 //For storing result //If bit 01 is set then End, //Else do cube //Store upper part of square //B-Lower part of X^2 Page 7

Microcontroller Lab Manual MOV A,R0 //A-X MUL AB //X*Lower X^2 INC DPTR MOVX @DPTR,A //Store partial result MOV A,B MOV R2,A //Upper part of X*Lower X^2 in R2 POP B // Get back upper part of square MOV A,R0 //A-X MUL AB //X*Upper X^2 ADD A,R2 // Add to partial result LAST:INC DPTR MOVX @DPTR,A MOV A,B ADDC A, # 00 //Add carry to B (For square result, C=0) INC DPTR MOVX @DPTR, A HERE: SJMP HERE END Result: CUBE OF 56H IS 9B498 WHICH IS STORED AS 98,B4,09(LOWER BYTE FIRST)

To get square make the D1 bit of data memory 20H high, say Ff,02,06.etc.The bit address is 01.Similarly bit address 78H correspond to D0 bit of data RAM location 2Fh.

Algorithm:

Dept of ECE

Page 8

Microcontroller Lab Manual 1. Store the 8 bit number X in A,R0 & B registers. 2. Multiply A & B registers to obtain the square (say SQH:SQL) of the number X. 3. Check if bit 01 is set. If set go to end (Storing the result), else do the cube operations. 4. The high part of the square result (SQH)is stored on the stack 5. Multiply the low part of the square result (SQL) with X (partial cube result). 6. Store the low part of the above result at 9001H & the high part in R2. 7. Retrieve the high part of square result(SQH) stored on the stack & multiply with X. 8. Add the low part of the above result (SQH*X) with R2 & store in 9002H. 9. Add the high part (SQH*X) with the resulting carry & store in 9003.

PROGRAM ILLUSTRATING BIT MANIPULATIONS

Dept of ECE

Page 9

Microcontroller Lab Manual 7) Two eight bit numbers NUM1 & NUM2 are stored in external memory locations 8000H & 80001H respectively. Write an ALP to compare the 2 Nos. Reflect your result as: if NUM1<NUM2, SET LSB of data RAM 2F(bit address 78H) IF NUM1>NUM2,SET MSB OF 2F(7FH). If NUM1=NUM2-Clear both LSB & MSB of bit addressable memory location 2Fh. ORG 0000h SJMP 30H ORG 30H MOV DPTR,#8000H MOVX A,@DPTR MOV R0,A INC DPTR MOVX A,@DPTR CLR C SUBB A,RO JZ EQUAL JNC BIG SETB 78H SJMP END1 SETB 7FH SJMP END1 CLR 77H CLR 7FH SJMP END1 END //Reset vector

//Get number from 8000h //R0=NUM1 //A=NUM2 //Clear C for SUBB

//Set bit at 78h bit address

BIG: EQUAL: END1:

//wait here

Algorithm: 1. Store the elements of the array from the address 4000H 2. Move the first number in R0 and the second in register A respectively. 3. Clear carry flag and subtract the two numbers, if the carry flag is 0 (if the nos are equal),clear both LSB & MSB of bit addressable memory location 2Fh. 4. If the carry flag is set then Set MSB of 2F(7FH), else LSB of data RAM 2F(bit address 78H). RESULT: 1. Before Execution: X: 08000H=45 & X :8001=35 After Execution: D:02FH=01 2. Before Execution: X: 08000H=25 & X :8001=35 After Execution: D:02FH=80 3. Before Execution: X: 08000H=45 & X :8001=45 After Execution: D:02FH=00 LOGICAL INSTRUCTIONS

Dept of ECE

Page 10

Microcontroller Lab Manual 8) Assembly Program Illustrating Logical Instruction (Byte Level) 3 eight bit numbers X,NUM1 & NUM2 are stored in internal data RAM locations 20H,21H & 22H respectively. Write an ALP to compute the following. IF X=0; THEN NUM1 (AND) NUM2, IF X=1; THEN NUM1 (OR) NUM2, IF X=2; THEN NUM1 (XOR) NUM2, ELSE RES=00, RES IS 23H LOCATION. ORG 0H SJMP 30H ORG 30H MOV A,20H //Do not use #,as data RAM 20H is to be accessed MOV R1,A //X in R1 MOV A,21H //A has NUM1 CJNE R1,#0,CKOR //Check if R1=0 ANL A,22H //AND with contents of 22H Location=NUM2 SJMP END1 CKOR: CJNE R1,#01,CKXOR // Check if R1=01 ORL A,22H //Yes-do OR operation SJMP END1 CKXOR: CJNE R1,#02,OTHER XRL A,22H //XOR Operation SJMP END1 OTHER: CLR A END1: MOV 23H,A //Result=00 HERE: SJMP HERE //Store result END Algorithm: 1. Point to the data RAM register 20H and store the condition X. 2. Point to 21H and 22H and move the number to A register. 3. Compare the contents of the R1 and perform the operation accordingly. 4. The result will be stored in 23H register. RESULT: 1. Before Execution: D:020H=00,21=0F,22=12 After Execution D:023H=02 2. Before Execution: D:020H=01,21=0F,22=12 After Execution D:023H=1F 3. Before Execution: D:020H=02,21=0F,22=12 After Execution D:023H=1D 4. Before Execution: D:020H=34, 21=0F,22=12 After Execution D: 023H=00 The above program can also be written as shown below (using indirect addressing) ORG 0H Dept of ECE Page 11

Microcontroller Lab Manual SJMP 30H ORG 30H MOV R0,#2Oh MOV A,@R0 //ON CHIP DATA RAM-DONOT USE MOVX MOV R1,A //X IN R1 INC RO MOV A,RO //A-NUM1 INC R0 //R0 POINTS TO NUM2 CJNE R1, #0, CKOR ANL A,@RO SJMP END1 CKOR:CJNE R1,#01,CKXOR ORL A,@R0 SJMP END1 CKXOR:CJNE R1,#02,OTHER XRL A,@R0 SJMP END1 OTHER:CLR A END1: INC R0 MOV @R0,A HERE: SJMP HERE //STORE RESULT END

9) 3 eight numbers X,NUM1& NUM2 are stored in internal data RAM locations 20H,21H & 22H respectively. Write an ALP to compute the following. IF X=0; THEN LSB OF NUM1 (AND) LSB OF NUM2, IF X=1; THEN MSB OF NUM1 (OR) MSB OF NUM2, IF X=2; THEN COMPIMENT MSB OF NUM1, STORE THE BIT RESULT IN RES, WHERE RES IS MSB OF 23H LOCATION. ORG 0H SJMP 30H ORG 30H MOV R0,20H CJNE R0,#0,CK1 MOV C,08H ANL C,10H SJMP LAST CK1: CJNE R0,#1,CK2 MOV C,0FH ORL C,17H SJMP LAST CK2: CJNE R0,#2,CK3 CPL 0FH MOV C,0FH SJMP LAST CK3: CLR C Dept of ECE

//R0=X //LSB of NUM1(21H) BIT address-08 //LSB of NUM1(22H) BIT address-10 //MSB of NUM1(21H) BIT address-0F //MSB of NUM1(22H) BIT address-17

//MSB of NUM1(21H) BIT address-0F

Page 12

Microcontroller Lab Manual LAST: MOV 1FH,C HERE: SJMP HERE END //RES is MSB of 23H Location-1FH

RESULT: 20H=00=>AND OF LSBs=1(hence 80 in 23H location)

20H=01=>OR of MSBs=0(hence 00 in 23Hlocation)

20H=01=>Compliment of MSB of 21H location. Hence 21H is changed to A1 and 23H location has 80H. Before Execution After Execution

Algorithm: 1. Move the condition X (from 20H location) into R0 register. 2. If X=0; then move LSB bit of 21H to carry flag and AND carry flag with LSB bit of 22H. Go to step 5. 3. If X=1; then move MSB bit of 21H to carry flag and OR carry flag with MSB bit of 22H. Go to step 5. 4. If X=2; then compliment MSB bit of 21H and move it to carry flag. Go to step 5. 5. Store Carry flag at MSB bit of 23H location.

COUNTERS: Dept of ECE Page 13

Microcontroller Lab Manual Assembly Program Illustrating Hex Up/Down Counters 10) Write an ALP to implement (display) an eight bit up/down binary (hex) counters on watch window. Note: To run this program, after selecting DEBUG session in the main menu use. View: Watch & call Stack window, in the Watches select watch 1 (or 2) and press F2 and enter a (for accumulator A) ORG 0H SJMP 30H ORG 30H MOV A, #00 ACALL DELAY INC A JNZ BACK SJMP HERE MOV R1,#0FFH MOV R2,#OFFH MOV R3,#OFFH DJNZ R3,$ DJNZ R2,DECR DJNZ R1,DECR1 RET END //Reset vector //Initial count //Dec a for binary down counter //Delay subroutine //$ Indicate current address

BACK: HERE: DELAY: DECR1: DECR:

RESULT: Accumulator A is incremented in binary from 00, 01, 02..09, 0A, 0B,..,0F,10,11,.FF

Algorithm: 1. Move 00 to A register. 2. Call the delay subroutine for 1 second, in delay program move FFH to registers in R1,R2 & R3, loop and decrement until 0. 3. Increment A register (decrement for down counter).

Assembly Program Illustrating BCD UP/DOWN Counters. // Counter program BCD up/down counters. Dept of ECE Page 14

Microcontroller Lab Manual 11) Write an ALP to implement (display) an eight bit UP/DOWN BCD counters on watch window. ORG 0H SJMP 30H ORG 30H MOV A,#00 ACALL DELAY ADD A,#99H DA A JNZ BACK SJMP HERE MOV R1, #0FFH MOV R2,#0FFH MOV R3,#0FFH DJNZ R3,LOOP1 DJNZ R2,DECR DJNZ R1,DECR1 RET END

BACK:

//99H for BCD Down counter //ADD 01 for BCD up counter //for bed counter //Delay subroutine

HERE: DELAY: DECR1: DECR: LOOP1:

Algorithm: 1. Move 00 to A register. 2. Call the delay subroutine for 1 second, (in delay program move FFH to registers in R1,R2 & R3, loop and decrement until 0). 3. Increment A register (add 99H for down counter). 4. Decimal adjust accumulator for the BCD up/down counter. RESULT: Accumulator A is increment in BCD from 00,01,02,..09,10,11,..99.

SERIAL DATA TRANSMISSION Program illustrating serial ASCII data transmission (data-yE) Dept of ECE Page 15

Microcontroller Lab Manual Note: To use result of this program, after selecting DEBUG session in the main menu use. View: Serial window #1. On running & halting the program, the data is seen in the serial window. 12).Conduct an experiment to configure 8051 microcontroller to transmit characters (yE) to a PC using the serial port and display on the serial window. ORG 0H SJMP 30H ORG 30H MOV TMOD,#20H MOV TH1,#-3

MOV SCON,#50H SETB TR1 MOV A,#Y ACALL TRANS MOV A,#E // Transfer E ACALL TRANS AGAIN1: SJMP AGAIN1 TRANS: MOV SBUF,A //LOAD SBUF HERE: JNB T1,HERE //Wait for last bit to transfer,until T1=1 CLR T1 //Get ready for next byte RET END RESULT: yE is printed on the serial window each time the program is executed. THEORY:

//Timer 1; mode 2 //-3=FD loaded into TH1 for //9600 baud, 11.0592MHz. //8-BIT, 1 stop bit, REN enabled //Start timer ! //Transfer Y

In serial transmission as opposed to parallel transmission, one bit at a time is transmitted. In serial asynchronous transmission, the data consists of a Start bit (high), followed by 8-bits of data to be transmitted and finally the stop bit. The byte character to be transmitted is written into the SBUF register. It transmits the start bit. The 8-bit character is transferred one bit at a time. The stop bit is transferred. After the transmission, the T1 flag=1 indicating the completion of transmission. Hence in the subroutine wait until T1 is set. Later clear the T1 flag and continue with transmission of the next byte by writing into the SBUF register.(The program can also be written in interrupt mode).The speed of the serial transmission is set by the baud rate which is done with the help of timer 1. (Refer chapter 8). Timer1 must be programmed in mode 2(i.e.,8 bit, auto reload). Baud rate Calculation: Crystal freq/(12*32)=(11.0592MHz)/(12*32)=28800 Serial communication circuitry divides the machine cycle frequency (11.0592MHz)/(12) by 32 before it is being used by the timer to set the baud rate. To get 9600,28800/3 is obtained by loading timer 1 with -3( i.e.,FF-3=FD) for further clock division. For 2400 baud rate, 28800/12=>-12=F4 in TH1. Algorithm: 1. Initialize timer 1 to operate in mode 2 by loading TMOD register. Dept of ECE Page 16

Microcontroller Lab Manual 2. 3. 4. 5. Load TH1 with -3 to obtain 9600 baud. Initialize the asynchronous serial communication transmission (SCON) register. Start timer 1 to generate the baud rate clock. Transmit the characters Y & E by writing into the SBUF register and waiting for the T1 flag.

TIMER DELAY PROGARM Progarm illustrating timer delay. 13) Generate a 1 second delay continuously using the chip timer in interrupt mode. ORG 0H //Reset vector SJMP 30H ORG 0BH //TFO vector SJMP ISR ORG30H //Main program MOV A, #00 MOV R0,#0 MOV R1,#0 MOV TMOD, #02H //00000010-Run timer in mode 2 MOV THO, #118 //Set up timer 0 to overflow in 0.05msec MOV IE,#82H //%10000010Enable Timer0 interrupt SETB TCON.4 //Start the timer0 SJMP HERE CLR TCON.4 //Disable timer0 INC R1 //R1*R2=100*200=20000*0.05msec=1sec CJNE R1,#100,SKIP MOV R1,#00 INC R0 CJNE R0,#200,SKIP MOV R0,#00H INC A SETB TCON.4 //Enable Timer RET1 //Return from interrupt subroutine END

HERE: ISR:

SKIP:

RESULT: Accumulator A is incremented in binary from 00,01,01,.09,0A,10,11,.FF every 1 second for 33MHz clock setting(& every 3 seconds for 11.0598MHz) 33 MHz is the default setting, simulator which the user can change to 11.0598MHz. Algorithm: 1. 2. 3. 4. 5. 6. 7. Stet up timer0 in mode 2 operation. Load TH1 with 118 to generate an every 0.05msec. Reset registers A,R1 & R0. Repeat step 4 continuously i.e., in main program just wait, do nothing. In interrupt; ISR at 000B location goes to step 6which is TFO ISR. Disable timer0 Update R1 & R0 Page 17

Dept of ECE

Microcontroller Lab Manual 8. Check if 20000 interrupts (=1sec) over. Yes increment accumulator A. 9. Enable timer & return from ISR. TMOD FUNCTION REGISTER 7 6 5 4 3 2 Gate C/T M1 M0 Gate C/T [TIMER 1 Bit 7/ 3 6/2 Symbol Gate C/T ][ 1 0 M1 M0 TIMER0]

5/1 4/0 M1 0 0 1 1

M1 M0 M0 0 1 0 1

Function OR gate enable bit which controls RUN/STOP of timer 1/0 Set to 1-by program to make timer 1/0 act as a counter by counting pulses from external input pins 3.5(T1)or 3.4(T0) Timer/counter operating mode select bit1 Timer/counter operating mode select bit0 MODE 0-13 bit 1-16 bit 2-8 bit reloadable timer 3-timer0-2-8bit timers. Timer 1 stop

TCON FUNCTION REGISTER 7 6 5 4 3 TF1 TR1 TF0 TR0 IE1 Bit 7 6 5 4 3 2 1 0 Symbol TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0 Function7 Timer 1 overflow flag Timer 1 run control bit Timer 0 overflow flag Timer 0 run control bit External interrupt 1 edge flag External interrupt 1 signal type control bit External interrupt 0 edge flag External interrupt 0 signal type control bit ADDRESS(HEX) Page 18 2 IT1 1 0 IE0 IT0

INTERRUPT Dept of ECE

Microcontroller Lab Manual IE0 TFO IE1 TF1 SERIAL 0003 000B 0013 001B 0023

Timerdelay=12*(257-delay)/frequency Timerdelay=0.05msec Delay=256-((timerdelay*frequency)/12) =256-(0.05*10-3*33*106)/12 =256-137.5=118.5 //loaded in THO To get 1sec delay 1/0.05msec=200*100 in the ISR (assuming 33 MHZ crystal frequency. For 11 MHZ, the calculations change) CONVERSION PROGRAMS 14) Write an ALP to implement decimal to hex conversion ORG 0000H SJMP 30H ORG 30H MOV DPTR,#40H //2-digit decimal number to be converted is //stored in data memory 40H MOVX A,@DPTR ANL A,#0F0H //obtain upper decimal digit by masking lower one SWAP A //bring to the units place MOV B,#0AH //Multiply tens digit with #0A-to get tens in hex MUL AB MOV R1,A //Temporarily store the converted tens value MOVX A,@DPTR //Get the decimal number again ANL A,#0FH //Obtain the units digit by masking upper digit ADD A,R1 //Add to the converted tens value INC DPTR //Increment data address MOVX @ DPTR,A // Converted hexadecimal number in next location HERE: SJMP HERE END RESULT: Before Execution-X:0040H=45(Decimal/BCD) After Execution:X:0041H=2D(Hex value) Algorithm: 1. Move the decimal data to be converted from external memory 40H to accumulator. 2. AND A reg with 0F0H and obtain the upper MSB of the decimal digit and swap the LSB and MSB of accumulator to bring the same to unit place. Dept of ECE Page 19

Microcontroller Lab Manual 3. Move 0AH to B register and Multiply with A reg to convert to hex value, store the converted tens value in R1. 4. Get the LSB of the decimal number and add to the converted tens value. 5. Point to the next memory location and store the result (hexadecimal). 15) Write an ALP to implement hex to decimal conversion. ORG 0000H SJMP 30H ORG 30H MOV DPTR, #9000H MOVX A,@DPTR //Get hex number MOV B,#10 DIV AB //Divide by 10 (OAH) INC DPTR XCH A,B MOVX @DPTR,A //Store the remainder (in B) in units place XCH A,B MOV B, #10 //Divide the quotient in A by 10 DIV AB INC DPTR XCH A,B MOVX @DPTR,A //Store the remainder (in B) in tens place XCH A,B INC DPTR MOVX @DPTR,A //Store the quotient (in A) in hundreds place HERE:SJMP HERE END RESULT: 9000H-FF(HEX NUMBER) 9001 to 9003- unpacked BCD number (decimal)-5,5,2(i.e.,255 stored Lower digit first)

Algorithm: 1. Move the hex data to be converted to accumulator. 2. Move 10 to B register and divide A reg to get BCD units as the remainder. 3. Store the converted BCDs units value. 4. Repeat the step 2 to obtain the BCD tens digit as remainder & quotient as hundreds digit. 5. Store the tens and hundreds BCD digits.

Dept of ECE

Page 20

Microcontroller Lab Manual

16) Write an ALP to implement BCD to ASCII conversion ORG 0H SJMP 30H ORG 30H MOV R1,#50H MOV A,@R1 //Get BCD data byte from RAM Location 50H MOV R2,A //Store in R2 ANL A,#0FH //Get the lower nibble ORL A,#30H //Add/or with 30H i.e., 0-9 converted to 30-30H INC R1 MOV @R1,A // Store the lower digits ASCII code MOV A,R2 // Get back the number SWAP A //Swap nibbles in A ANL A,#0FH //Get the upper BCD digit ORL A,#30H //Convert to ASCII INC R1 MOV @R1,A //Store the upper digits ASCII code HERE:SJMP HERE END Result: The BCD code 28 at D:0050H is converted to 2 ASCII codes-38H-32H

Algorithm: //Converts the BCD byte in A into ASCII characters. 1. Move the BCD data to be converted to accumulator. 2. Get the lower nibble(BCD digit) & ADD (or ORL) with 30H. 3. Store the converted ASCII value. 4. Get the higher nibble (tens BCD digit) & Add (or ORL) with 30H 5. Store the converted ASCII value.

Dept of ECE

Page 21

Microcontroller Lab Manual 17)Write an ALP to implement Hexadecimal to ASCII conversion. // This program also illustrates conditional branching (JNC), call and return instructions. ORG 0H SJMP 30H ORG 30H MOV R1,#50H MOV A,@R1 //Get hexadecimal data type from RAM/Location 50H MOV R2,A //Store in R2 ANL A,#0FH //Get the lower nibble ACALL ASCII //Convert to ASCII INC R1 MOV @R1,A //Store the lower digits ASCII code MOV A,R2 //Get back the number SWAP A //Swap nibbles in A ANL A,#0FH //Get the upper BCD digit ACALL ASCII INC R1 MOV @R1,A //Store the upper digits in ASCII code HERE:SJMP HERE ASCII:MOV R4,A //Store A CLR C SUBB A,#0AH //Check if digit >=0A MOV A,R4 //Get back A JC SKIP ADD A,#07H //Add 07 if >09 SKIP:ADD A,#30H //Else add only 30H for 0-9 RET END RESULT: The BCD code 2C at D:0050H is converted to 2 ASCII codes-43H (for OC) & 32H(for 02). Another Example-BA is converted to 41H(A) & 42H(B).

Algorithm: //converts the hexadecimal byte in A into two ASCII characters. 1. 2. 3. 4. Move the hexadecimal data to be converted to accumulator. Get the lower nibble & call ASCII routine. Store the converted ASCII value. Get the higher nibble & call ASCII routine. Page 22

Dept of ECE

Microcontroller Lab Manual 5. Store the converted ASCII value. ASCII subroutine 1. If digit greater than 09,(for A-F) add 07H & 30H 2. Else (i.e.,0-9)add only 30H 3. Return. 18) Write an ALP to implement ASCII to Hexadecimal conversion. ORG 0H SJMP 30H ORG 30H MOV R1,#50H MOV A,@R1 //Get ASCII byte from RAM location 50H CLR C SUBB A,#41H//Compare with 41H MOV A,@R1 //Get back number into A JC SKIP //If number <41, subtract only 30H CLR C SUBB A,#07H//Subtract 07 & 30H if number >41 SKIP:CLR C SUBB A,#30H INC R1 MOV @R1,A //Store the hex code HERE:SJMP HERE END Result: The ASCII code 45 at D:0050h is converted to hexadecimal 0E at 51H

NOTE: For this program the input data should be only in the range 30H-39H & 41H to 46H. Algorithm: // Converts the ASCII characters into hexadecimal number. 1. Move the ASCII character to be converted to accumulator. 2. If character is greater than 41H,(for A-F), then subtract 07H & 30H 3. Else (i.e. for 0-9) subtract only 30H 4. Store the converted hexadecimal number.

Dept of ECE

Page 23

Microcontroller Lab Manual The following sections illustrates some C programs to interface 8051 chip to interfacing modules such as stepper motor, DC motor, DAC, Keypad, LCD, etc.

MSP 430 SOFTWARE FLOW


.

Step 1: Double click on Code Composer Studio V4 icon which will be situated at the desk top, then work launcher window will open

Step 2: The code composer studio V4 window will open in that we can view sub windows as Source window, Console window and Description window as shown below

Dept of ECE

Page 24

Microcontroller Lab Manual

Step 3: First we need to make target configuration settings, so go to Target and select New Target configuration.

Step 4: After selecting New Target Configuration the Target Configuration window will open, click on the finish.

Step 5: At the connection side under basic general setup select TIMSP430 USB 1.

Step 6: At the device side select MSP430G2452 and click on the save.

Step 7: Select a new file under that select CCS project.

Step 8: New CCs project window will open, give the project name and click on next.

Step 9: Select a project type as MSP430 and click on next.

Step 10: Additional project settings window will open , click on next for further settings.

Dept of ECE

Page 25

Microcontroller Lab Manual

Step 11: At the project settings select device variant as MSP430Gxxx Family.

Step 12: And the device as MSP430G2452 and click on next.

Step 13: At the Project Templates select Empty Assembly-only Project and click on finish

Step 14: After project settings go to new source file.

Step 15: If we are editing program in C-language we need to mention source file name as .c followed by file name or if we are editing program in assembly language we need to mention file name as .asm followed by file name .

Step 16: After editing the program save it.

Step 17: Go to project and Rebuild the Active Target.

Step 18: After rebuilding the target, click on Debug Active Project.

Dept of ECE

Page 26

Microcontroller Lab Manual

Step 19: Apply the break point and click on the run option to view specified output

Software Programs Using MSP430


1. DATA TRANSFER 1a. ALP FOR BLOCK MOVE OPERATION .cdecls C,LIST, "msp430g2452.h" // CALLING HEADER FILE FROM C
LIBRARY

;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK


POINTER

StopWDT Mainloop
SOURCE

mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT mov.W #0200h,R5 ; STARTING ADDRESS OF mov.W #0250h,R6 ; STARTING ADDRESS OF ; DATA LENGTH ( HERE 9)

DESTINATION

again

mov.w #0009h,R7 mov.W @R5,0(R6) incd.w R5 incd.w R6 dec.w R7 jnz again jmp Mainloop

; GO TO LOOP AGAIN ; GO TO LOOP MAIN LOOP , KEEP BREAK

POINT HERE

;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR Dept of ECE Page 27

Microcontroller Lab Manual .short RESET .end ;

1b. ALP FOR EXCHANGE OPERATION .cdecls C,LIST, "msp430g2452.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.W #0200h,R5 ; STARTING ADDRESS OF FIRST DATA BLOCK mov.W #0250h,R6 ; STARTING ADDRESS OF SECOND DATA BLOCK mov.w #0009h,R8 ; DATA LENGTH again mov.W @R5,R7 mov.W @R6,0(R5) mov.W R7,0(R6) incd.w R5 incd.w R6 dec.w R8 jnz again ; GO TO LOOP AGAIN jmp Mainloop ; GO TO LOOP MAINLOOP, KEEP BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end 1c. ALP FOR SORTING GROUP OF DATA IN ASCENDING ORDER .cdecls C,LIST, "msp430g2452.h" // CALLING HEADER FILE FROM C LIBRARY Dept of ECE Page 28

Microcontroller Lab Manual ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #0009h,R7 LOOP2 mov.W #0200h,R5 ; STARTING ADDRESS mov.w #0009h,R6 ; DATA LENGTH again CMP.W @R5,2(R5) JC LOOP1 MOV.W @R5,R8 mov.W 2(R5),0(R5) mov.W R8,2(R5) LOOP1 incd.w R5 dec.w R6 jnz again ; GO TO LOOP AGAIN dec.w R7 jnz LOOP2 jmp Mainloop ; GO TO LOOP MAINLOOP, KEEP THE
BREAK POINT HERE

;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end 1 d. ALP FOR FINDING THE LARGEST ELEMENT IN AN ARRAY .cdecls C,LIST, "msp430g2452.h"// CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.W #0200h,R5 ; STARTING ADDRESS OF DATA mov.w #0000h,R6 mov.w #0009h,R7 ; DATA LENGTH again CMP.W R6,0(R5) JC LOOP1 jmp LOOP2 LOOP MOV.W @R5,R6 ; RESULT IN R6 LOOP2 incd.w R5 dec.w R7 jnz again ; GO TO LOOP AGAIN jmp mainloop ; GO TO LOOP MAINLOOP, PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end Dept of ECE Page 29

Microcontroller Lab Manual

2. Arithmetic operations
2a. ALP FOR ADDITION OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESETmov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Main mov.w #08h,R5 ; DATA 1 mov.w #01h,R6 ;DATA 2 add.w R6,R5 ;RESULT IN R5 jmp Main ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end 2b. ALP FOR SUBTRACTION OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #08h,R5 ;DATA 1 mov.w #01h,R6 ;DATA 2 sub.w R6,R5 ; mov.w R5,R7 ;RESULT IN R7 jmp Mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

2c. ALP FOR MULTIPLICATION OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESETmov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Dept of ECE Page 30

Microcontroller Lab Manual Mainloop mov.w #05h,R5 ; DATA 1 mov.w #06h,R6 ; DATA 2 mov.w #00h,R7 here add.w R5,R7 ; RESULT IN R7 dec.w R6 jnz here jmp mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

2d. ALP FOR DIVISION OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESETmov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #1Bh,R5 ; DATA 1 mov.w #09h,R6 ; DATA 2 mov.w #00h,R7 here inc.w R7 sub.w R6,R5 jc here dec.w R7 ; RESULT , QUOTIENT IN R7 add.w R5,R6 ; REMINDER IN R6 jmp mainloop ; PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

2e. ALP FOR FINDING A SQUARE OF A NUMBER .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------Dept of ECE Page 31

Microcontroller Lab Manual .text ; Progam Start ;-----------------------------------------------------------------------------RESETmov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #06h,R5 ; DATA mov.w R5,R6 mov.w #00h,R7 here add.w R5,R7 ; RESULT IN R7 dec.w R6 jnz here jmp mainloop ; PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

2f. ALP FOR FINDING A CUBE OF A NUMBER .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #03h,R5 ; DATA mov.w #00h,R7 mov.w R5,R6 LOOP1 add.w R5,R7 dec.w R6 jnz LOOP1 mov.w R7,R8 mov.w #00h,R7 LOOP2 add.w R5,R7 ; RESULT IN R7 dec.w R8 jnz LOOP2 jmp mainloop ; PUT BREAK POINT HERE ; ----------------------------------------------------------------------------------Interrupt Vectors ; ----------------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end 3. ALP FOR FINDING A COUNTER OPERATION Dept of ECE Page 32

Microcontroller Lab Manual .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Program Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #0FFh,&P1DIR ; P1 IS OUT PUT mov.w #00h,R5 Loop1 inc.w R5 mov.w R5,& P1OUT ; RESULT IN R5 AND PORT 1 Wait mov.w #0FFFFh,R15 ; DELAY TO R15 L1 dec.w R15 ; DECREMENT R15 jnz L1 jmp Loop1 ; DELAY OVER? jmp Mainloop ; GOTO MAIN LOOP ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end 4. LOGICAL OPERATIONS 4a. ALP FOR AND OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Program Start ;-----------------------------------------------------------------------------RESETmov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #06h,R5 ;DATA 1 mov.w #03h,R6 ;DATA 2 or R6,R5 mov.w R5,R7 ;RESULT IN R7 jmp Mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESETVECTOR .short RESET ; .end 4b. ALP FOR OR OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Program Start Dept of ECE Page 33

Microcontroller Lab Manual ;-----------------------------------------------------------------------------RESETmov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #06h,R5 ;DATA 1 mov.w #03h,R6 ;DATA 2 or R6,R5 mov.w R5,R7 ;RESULT IN R7 jmp Mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

4c. ALP FOR EXOR OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Program Start ;-----------------------------------------------------------------------------RESETmov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #06h,R5 ;DATA 1 mov.w #03h,R6 ;DATA 2 xor.w R6,R5 mov.w R5,R7 ;RESULT IS IN R7 jmp Mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end 4d. ALP FOR INVERT OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER Dept of ECE Page 34

Microcontroller Lab Manual StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #00FFh,R5 ;DATA inv.w R5 ; RESULT IN R5 jmp Mainloop ; PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

4e. ALP FOR BIT CLEAR OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #0FFFFh,R5 ; DATA bic.w #00FFh,R5 ; RESULT IN R5 jmp Mainloop ; PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

4f. ALP FOR BIT SET OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRAY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER Dept of ECE Page 35

Microcontroller Lab Manual StopWDT Mainloop mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT mov.w #0000h,R5 ;DATA bis.w #00FFh,R5 ;RESULT IN R5 jmp Mainloop ; PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

4g.ALP FOR REGISTER CLEAR OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRAY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #00F0h,R5 ;DATA clr R5 jmp Mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" .short RESET .end
; MSP430 RESET Vector

4h. ALP FOR BIT TEST OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRAY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------Dept of ECE Page 36

Microcontroller Lab Manual RESET StopWDT Mainloop mov.w #0280h,SP ; INITIALIZE STACK POINTER mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT mov.w #0000h,SR mov.w #0FFFFh,R5 ;DATA bit.w #0FFFFh,R5 ; jmp Mainloop ; PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

4i. ALP FOR RIGHT ROTATE OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRAY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #00F0h,R5 ; rra R5 ;DATA rra R5 rra R5 rra R5 jmp Mainloop ; PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

4j. ALP FOR RIGHT ROTATE OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRAY ;-----------------------------------------------------------------------------Dept of ECE Page 37

Microcontroller Lab Manual .text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #0Fh,R5 ; DATA rla R5 rla R5 rla R5 rla R5 jmp Mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

BOOLEAN OPERATION 4k. ALP FOR BIT MANIPULATION OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRAY ;-------------------------------------------------------------------------.text ; Progam Start ;-------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #0000h,SR jmp LOOP0 LOOP5 clrc jnc LOOP1 LOOP0 setz jz LOOP2 LOOP1 clrn jmp LOOP6 LOOP2 setc jc LOOP3 LOOP6 clrz jnz LOOP4 LOOP3 setn jmp LOOP5 LOOP4 jmp Mainloop ; PUT BREAK POINT HERE ;-------------------------------------------------------------------------Dept of ECE Page 38

Microcontroller Lab Manual ; Interrupt Vectors ;-------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET Vector .short RESET ; .end

5. ALP FOR CONDITIONAL CALL & RETURN OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Program Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #10,R5 ; Loop1 dec.w R5 jnz Loop1 ; CONDITION IS HERE, IF R5 IS ZERO CALL RETURN FUNCTION call #Return ; CALLING THE SUBROUTINE FUCTION(RETURN) jmp Mainloop Return mov.w #10,R6 ; SUBROUTINE FUNCTION Loop2 dec.w R6 jnz Loop2 ret ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end 6. CODE CONVERSION Dept of ECE Page 39

Microcontroller Lab Manual 6a. ALP FOR BCD TO ASCII OPERATION .cdecls C,LIST, "msp430g2231.h" //CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #1,R5 ;BCD DATA 1 mov.w #2,R6 ;BCD DATA 2 or #30h,R5 or #30h,R6 ;CONVERTED RESULT WILL REMAIN IN SAME REGISTER jmp Mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

6b.. ALP FOR ASCII TO DECIMAL OPERATION .cdecls C,LIST, "msp430g2231.h" //CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Program Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #'1',R5 ; ASCII DATA 1 mov.w #'2',R6 ;ASCII DATA 2 and.w #0Fh,R5 and.w #0Fh,R6 rla R5 rla R5 rla R5 rla R5 or R5,R6 ;RESULT WILL BE IN R6 jmp Mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end 6c.. ALP FOR DECIMAL TO ASCII OPERATION .cdecls C,LIST, "msp430g2231.h" Dept of ECE
//CALLING HEADER FILE FROM C LIBRARY

Page 40

Microcontroller Lab Manual ;-----------------------------------------------------------------------------.text ; Program Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #35,R5 ;DECIMAL DATA mov.w #0Ah,R6 mov.w #00h,R7 here inc.w R7 sub.w R6,R5 jc here dec.w R7 add.w R5,R6 or #30h,R7 ;FIRST DATA or #30h,R6 ;SECOND DATA jmp Mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end 6d. ALP FOR HEX TO DECIMAL OPERATION cdecls C,LIST, "msp430g2231.h" //CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #3Ah,R5 ;HEX DATA mov.w #0Ah,R6 mov.w #00h,R7 here inc.w R7 sub.w R6,R5 jc here add.w R5,R6 dec.w R7 rla.w R7 rla.w R7 rla.w R7 rla.w R7 or R7,R6 ;RESULT WILL BE IN R6 jmp Mainloop ;PUT BREAK POINT HERE ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR . short RESET ; Dept of ECE Page 41

Microcontroller Lab Manual .end 6e. ALP FOR DECIMAL TO HEX OPERATION cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #100,R5 ;DECIMAL DATA mov.w #00h,R8 mov.w #02h,R6 mov.w #00h,R7 clrc loop1 inc.w R7 sub.w R6,R5 jc loop1 add.w R5,R6 dec.w R7 or R6,R8 mov.w #02h,R6 mov.w R7,R5 mov.w #00h,R7 loop2 inc.w R7 sub.w R6,R5 jc loop2 add.w R5,R6 dec.w R7 rla R6 or R6,R8 mov.w #02h,R6 mov.w R7,R5 mov.w #00h,R7 loop3 inc.w R7 sub.w R6,R5 jc loop3 add.w R5,R6 dec.w R7 rla R6 rla R6 or R6,R8 mov.w #02h,R6 mov.w R7,R5 mov.w #00h,R7 loop4 inc.w R7 sub.w R6,R5 jc loop4 add.w R5,R6 dec.w R7 Dept of ECE Page 42

Microcontroller Lab Manual rla R6 rla R6 rla R6 or R6,R8 rlc R7 rlc R7 rlc R7 rlc R7 or R8,R7 jmp Mainloop
;---------------------------------------------------------------------; Interrupt Vectors ;---------------------------------------------------------------------.sect ".reset" ; MSP430 RESET Vector .short RESET ; .end

;RESULT IN R7 PUT BREAK POINT HERE

7a. ALP FOR DELAY OPERATION cdecls C,LIST, "msp430g2231.h" //CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Progam Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT SetupP1 bis.b #001h,&P1DIR ; P1.0 OUTPUT Mainloop xor.b #01h,&P1OUT ; TOGGLE P1.0 Wait mov.w #0FFFFh,R15 ; DELAY to R15 L1 dec.w R15 ; DECREMENT R15 jnz L1 ; DELAY OVER? jmp Mainloop ;AGAIN ;-----------------------------------------------------------------------------Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET Vector .short RESET ; .end 7b. ALP FOR SERIAL OPERATION cdecls C,LIST, "msp430g2231.h" //CALLING HEADER FILE FROM C LIBRARY ;-----------------------------------------------------------------------------.text ; Program Start ;-----------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTER Dept of ECE Page 43

Microcontroller Lab Manual StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT SetupTA mov.w #TASSEL_1+MC_2,&TACTL ; ACLK, CONTINOUS MODE SetupC0 mov.w #OUT,&CCTL0 ; TXD IDLE AS MARK SetupP1 bis.b #TXD+RXD,&P1SEL ; bis.b #TXD,&P1DIR ; Mainloop call #RX_Ready ; UART ready to RX one Byte bis.w #LPM3+GIE,SR ; Enter LPM3 w/ int until Byte RXed call #TX_Byte ; TX Back RXed Byte Received jmp Mainloop ; ;------------------------------------------------------------------------------TX_Byte ; Subroutine Transmits Character from RXTXData Buffer ;------------------------------------------------------------------------------TX_1 mov.w &TAR,&CCR0 ; Current state of TA counter cmp.w &TAR,&CCR0 ; !!Prevent async capture!! jne TX_1 ; add.w #Bitime,&CCR0 ; Some time till first bit bis.w #0100h, RXTXData ; Add mark stop bit to RXTXData rla.w RXTXData ; Add space start bit mov.w #10,BitCnt ; Load Bit counter, 8data + ST/SP mov.w #CCIS0+OUTMOD0+CCIE,&CCTL0 ; TXD = mark = idle TX_Wait bit.w #CCIE,&CCTL0 ; Wait for TX completion jnz TX_Wait ; ret ; ;------------------------------------------------------------------------------RX_Ready ; Subroutine Readies UART to Receive Character into RXTXData Buffer ;------------------------------------------------------------------------------mov.w #8,BitCnt ; Load Bit Counter, 8 data bits SetupRX mov.w #CM1+SCS+OUTMOD0+CAP+CCIE,&CCTL0 ; Neg Edge,Sync,cap ret ; ;------------------------------------------------------------------------------TA0_ISR ; RXTXData Buffer holds UART Data ;------------------------------------------------------------------------------add.w #Bitime,&CCR0 ; Time to next bit bit.w #CCIS0,&CCTL0 ; RX on CCI0B? jnz UART_TX ; Jump --> TX UART_RX bit.w #CAP,&CCTL0 ; Capture mode = start bit edge jz RX_Bit ; Start bit edge? RX_Edge bic.w #CAP,&CCTL0 ; Switch to compare mode add.w #Bitime_5,&CCR0 ; First databit 1.5 bits from edge ret ; RX_Bit bit.w #SCCI,&CCTL0 ; Get bit waiting in receive latch rrc.b RXTXData ; Store received bit RX_Test dec.w BitCnt ; All bits RXed? jnz RX_Next ; Next bit? ;>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< RX_Comp bic.w #CCIE,&CCTL0 ; All bits RXed, disable interrupt mov.w #GIE,0(SP) ; Decode byte = active in Mainloop

Dept of ECE

Page 44

Microcontroller Lab Manual ; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<< RX_Next reti ; UART_TX cmp.w #00h,BitCnt ; jne TX_Next ; Next bit? bic.w #CCIE,&CCTL0 ; All Bits TX or RX, Disable Int. reti ; TX_Next bic.w #OUTMOD2,&CCTL0 ; TX Mark rra.w RXTXData ; LSB is shifted to carry jc TX_Test ; Jump --> bit = 1 TX_Space bis.w #OUTMOD2,&CCTL0 ; TX Space TX_Test dec.w BitCnt ; All bits sent (or received)? reti ; ;-----------------------------------------------------------------------------; Interrupt Vectors ;-----------------------------------------------------------------------------.sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .sect ".int09" ; Timer_A0 Vector .short TA0_ISR ; .end DAC (Digital to Analog converter) Interfacing to 8051 A DAC as explained in chapter 11 converts a digital input word to an output analog voltage. The dual DAC interface considered below has 2,8-bit DACs, one connected to port P0 and the other to P1,with voltage outputs at X out and Y out. As the 8-bit digital code at P0(P1) varies from00H to FFH, the DAC output at X out(or Y out) varies from 0V to 5V respectively. Hence the resolution of this DAC is 19.6m (5V/225=0.0196V=19.6mV).The output analog voltage at X out (or Y out) is viewed on a CRO. (For CRO connections, each channel input has one pin connected to X out & the other input pin to GND of the DAC power supply. Similarly use the other channel of the CRO- with Y out & GND). Either only one DAC or both DACs at the same time can be used. Using the DAC interface to 8051, we can generate different types of wave-forms such as Square, triangular, ramp and sine. Note: The 8051 MC kit is connected to the DAC card through the 26-pin FRC connector. The following programs describe the generation of square, triangular, ramp and sine waveforms. RAMP WAVE GENERATION: A ramp waveform is shown in below figure. The voltage of the ramp increases from an initial value (say 00V) to a final value (say 5V) smoothly. This increase in voltage can be obtained from the output (say x out)of the DAC by changing the values sent at the port(P0) from 00 to FFH continuously in steps of 1using a for loop.

Dept of ECE

Page 45

Microcontroller Lab Manual

0 2

Algorithm for Ramp wave generation: 1. Output an initial value of 00H to P0 (for output at Yout use P1). 2. Increment the value at P0 in steps of 1 until FFH is reached. 3. Repeat step 1 and 2 continuously. Note: For stair-case waveform generation, the value at P0 instead of being incremented in steps of 1, is now incremented in steps of 33H(=1V) for a 5-step waveform obtained by FFH/5 steps=33H. Program for Ramp wave: #include <reg51xD2.h> main() {unsigned char i; while(1) {for (i=0;i<0xff;i++) P0=i; //use P1=I for yout } //end of while } //end of main Triangular waveform generation: It is same as that of the ramp wave generation except that at P0,i.e.,at the input of the DAC after being increased from 00 to FFH(in steps of 1) is brought down to 00,again in steps of 1. Dept of ECE Page 46 Program for Stair case wave: #include <reg51xD2.h> main() {unsigned char i; while(1) {for (i=0;i<0xff;i=i+0x33) P0=i; } //end of while } //end of main

Microcontroller Lab Manual

0 2

Algorithm forTriangular waveform generation: 1. Output an initial value of 00H at P0.(use P1 to get output at yout) 2. Increment the value of P0 in steps of 1(from 00H) until FFH. 3. Decrement the value at P0 in steps of 1(from FFH) until 00H. 4. Repeat steps 2 and 3. Procedure: 1. Compile the C program on the 8051 using Keil C compiler. 2. After build successful, download the hex file to the 8051 C kit connected to the PC(through the serial port). 3. Run the program and observe the waveform on the CRO.

Program for Triangular wave: # include<reg51xD2.h> main() { unsigned char i; while(1) { for (i=0;i<0xff;i++) P0=i; for (i=0xff;i>0;i--) P0=i; } }

//increasing ramp //decreasing ramp //end of while //end of main.

Sine waveform generation: A sine wave is generated by using a look up table. The values from the look up table when sent to P0 will generate a sine wave.Here let =30 0. For different values of , the voltages are V=2.5V+2.5Vsin.

Dept of ECE

Page 47

Microcontroller Lab Manual

Algorithm: 1. Initialize the index to zero. 2. Output the value from look up table corresponding to the index through P0. 3. Increment the index until 13(for 13 values stored in look up table)(after 13, bring the index back to zero). 4. Repeat steps 2 and 3. C- program for sine wave: #include<reg51xD2.h> ` main() { unsigned char table[i]={128,192,238,255,238,192,128,64,17,0,17,64,128}; unsigned char i; while(1) { for (i=0;i<13;i++) P0=table[i]; } //end of while } //end of main. Resolution of the DAC = Vmax =10V =19.5mV 2n 28 Angle Sin Vout (in degree) 2.5V+2.5 Sin o 0 0 2.5 30o 0.5 3.75 o 60 0.866 4.665 90o 1 5 o 120 0.866 4.665 o 150 0.5 3.75 180o 0 2.5 o 210 -0.5 1.25 240o -0.866 0.335 o 270 -1 0 o 300 -0.866 0.335 Dept of ECE

DAC i/p Vout/19.5m 128 192 238 255 238 192 128 64 17 0 17 Page 48

Microcontroller Lab Manual 330o 360o -0.5 0 1.25 2.5 64 128

SQUARE WAVE GENERATION: A square wave is obtained by generating a high voltage(say 5V) for certain time and then a low voltage(say 0V). This 5V and 0V are obtained by sending 00 and FFH to the input of the DAC.The on times and off times are generated using delay loop. For 50% duty cycle: on time=off time. For other values of duty cycles the on time and off times are varied.

0 2

Algorithm: 1. 2. 3. 4. 5.

Output a high value(say FFH=5V) on P0. Call delay. Output a low value(say 00H) on P0 Call delay. Repeat from step 5.

C- program for square wave: #include<reg51xD2.h> void delay(unsigned int x) {for(;x>0;x--); } void main {unsigned char on=0xff; unsigned char off=0x00; while(1) {P0=on; delay(300); P0=off; delay(300); } } //delay subroutble

//define amplitude during on time // amplitude during off time //make DAC output at xout=5V //on- time delay //generate the low of square wave //for 75% duty cycle use delay(100) //end of while //end of main

STEPPER MOTOR Dept of ECE Page 49

Microcontroller Lab Manual #include<REG52.h> //--------function declaration void MSDelay(unsigned int;) //Delay void send (unsigned char); //Serial data transfer void main() { unsigned char rdata; //serially received data unsigned char idata msg[8][30]={Sitec Electronics, Stepper Motor Test Program, Please choose an option, 1. Clockwise rotation, 2. Anti Clockwise rotation, 3. Clockwise xxRPM, 4. Clockwise xxRPM, 5. Clockwise xxRPM}, P1=0 ; //set port 1 as output TMOD=0x20; //Timer 1,mode2(auto-reload) TH1=-3; //9600 baud rate SCON=0x50; //8-bit,1 stop,REN enabled TR1=1; //start timer 1 While(1) { //--------Display message on hyper terminal unsigned char i,j; send(0x0C); for (i=0;i<8;i++) { for (j=0;j<30;j++) { send(msg[i][j]); } send(0x0D); send(0x0A); } while(RI= =0;) //wait until receive flag is set rdata=SBUF; //get data received RI=0; //clear flag next incoming data Switch(rdata) //loop to respective subroutine { case(49); { while(R1= = 0) //keep doing the function until next command is received { P0=0x66; //clockwise rotation MSDelay (1); P0=0xCC; MSDealy(1); P0=0x99; Dept of ECE Page 50

Microcontroller Lab Manual MSDelay (1); P0=0x33; MSDealy (1); } break; } case(50); { while (RI= =0) { P0=0x33; //Anticlockwise rotation MSDelay (1); P0=0x99; MSDelay(1); P0=0xCC; MSDelay (1); P0=0x66; MSDelay (1); } break; } case(51); { While (RI= =0) { P0=0x66; //clockwise rotation with reduced speed MSDelay (3); P0=0xCC; MSDealy(3); P0=0x99; MSDelay (3); P0=0x33; MSDealy (3); } break; } case(52); { while (RI= = 0) { P0=0x66; //clockwise Rotation with reduced speed MSDelay (6); P0=0xCC; MSDelay(6); P0=0x99; MSDelay (6); P0=0x33; MSDelay (6); } break; Dept of ECE Page 51

Microcontroller Lab Manual } case(53); { while (RI= = 0) { P0=0x66; //clockwise rotation with reduced speed MSDelay (9); P0=0xCC; MSDelay(9); P0=0x99; MSDelay (9); P0=0x33; MSDelay (9); } break; } } } } Void MSDealy (unsigned int value) // Delay routine { unsigned int x,y; for(x=0;x<600;x++); for (y=0;y<value;y++); } void send(unsigned char x) //Routine to send data serially { SBUF=x; While (TI= =0); TI=0; } Stepper motor(Clock wise Rotation) #include <REG52.H> void MSDelay (unsigned int); void main() { while(1) { P0 =0x66; MSDelay (1); P0 =0xCC; MSDelay (1); P0 =0x99; MSDelay (1); P0 =0x33; MSDelay (1); } } void MSDelay (unsigned int value) // Delay rouitne Dept of ECE Page 52

Microcontroller Lab Manual { unsigned int x,y; for (x=0;x<600;x++) for (y=0;y<value;y++); } Stepper motor(Anticlockwise Rotation) #include <REG52.H> void MSDelay (unsigned int); void main() { while(1) { P0 =0x33; MSDelay (1); P0 =0x99; MSDelay (1); P0 =0xCC; MSDelay (1); P0 =0x66; MSDelay (1); } } void MSDelay (unsigned int value) // Delay rouitne { unsigned int x,y; for (x=0;x<600;x++) for (y=0;y<value;y++); } DC MOTOR INTERFACE #include<REG52.h> //--------function declaration void MSDelay(unsigned int;) //Delay void send (unsigned char); //Serial data transfer sbit enablr=P0^6; //variables declaration for ADC sbit mtr_1=P0^7; sbit mtr_2=P0^4; void main() { unsigned char rdata; //serially received data the message unsigned char idata msg[8][30]={Sitec Electronics, DC Motor Test Program, Please choose an option, 1. AntiClockwise rotation, 2. Clockwise rotation, 3. AntiClockwise xxRPM, 4. AntiClockwise xxRPM, 5. AntiClockwise xxRPM}, P1=00 ; //set port 1 as output Dept of ECE Page 53

Microcontroller Lab Manual TMOD=0x20; //Timer 1,mode2(auto-reload) TH1=-3; //9600 baud rate SCON=0x50; //8-bit,1 stop,REN enabled TR1=1; //start timer 1 While(1) { //--------Display message on hyper terminal unsigned char i,j; send(0x0C); for (i=0;i<8;i++) { for (j=0;j<30;j++) { send(msg[i][j]); } send(0x0D); send(0x0A); } enablr=1; //enable H-Bridge while(RI= =0); //wait until receive flag is set radta=SBUF; //get data received RI=0; //clear flag for next incoming data Switch(rdata) //loop to respective subroutine { case(49); { while (RI= =0) // keep doing the function until next command is received { mtr_1=1; // Clockwise Rotation mtr_2=0; } break; } case(50); { while (RI= =0) { mtr_1=0; mtr_2=1; // Anticlockwise Rotation } break; } case(51); { while (RI= =0) { mtr_2=0; mtr_1=1; // Clockwise Rotation with PWM speed control MSDelay (5); Mtr_1=0; Dept of ECE Page 54

Microcontroller Lab Manual MSdelay(5); } break; } case(52); { while (RI= =0) { mtr_2=0; mtr_1=1; // Clockwise Rotation with PWM speed control MSDelay (1); mtr_1=0; MSdelay(20); } break; } case(53); { while (RI= =0) { mtr_2=0; mtr_1=1; // Clockwise Rotation with PWM speed control MSDelay (1); mtr_1=0; MSdelay(40); } break; } } } } //------- Delay Routine void MSDelay (unsigned int value) { unsigned int x,y; for (x=0;x<50;x++0 for(y=0;y<value;y++); } //--------------Data Transfer Routine void Send (unsigned char x) { SBUF=x; While(TI= =0); TI=0; } (Short Programs) Dept of ECE Page 55

Microcontroller Lab Manual DC motor(Clockwise Rotation) #include <reg52.h> void MSDelay (unsigned int); sbit enablr = P0^6; sbit mtr_1 = P0^7; sbit mtr_2 = P0^4; void main() { while(1) { enablr = 1; mtr_1 = 1; mtr_2 = 0; MSDelay (40); } } void MSDelay (unsigned int value) { unsigned int x,y; for (x=0;x<5000;x++) for (y=0;y<value;y++); DC motor(AntiClockwise Rotation) #include <reg52.h> void MSDelay (unsigned int); sbit enablr = P0^6; sbit mtr_1 = P0^7; sbit mtr_2 = P0^4; void main() { while(1) { enablr = 1; mtr_1 = 0; mtr_2 = 1; MSDelay (40); } } void MSDelay (unsigned int value) { unsigned int x,y; for (x=0;x<5000;x++) for (y=0;y<value;y++); Temperature sensor #include <reg52.h>

Dept of ECE

Page 56

Microcontroller Lab Manual sbit rd = P1^1; sbit wr = P1^2; sbit intr = P1^3; sbit aen = P1^0; sfr mydata1 = 0xA0; sfr ldata = 0x80; sbit rs = P3^2; sbit rw = P3^3; sbit en = P3^4; sbit busy = P0^7; //Variables declaration for ADC

//ADC data //Variables declaration for LCD

//--------------Funtion declaration void convert_display(unsigned char); //Hex to Binary converter void lcdcmd (unsigned char value); //LCD command void lcddata (unsigned char value); //LCD data void lcdready (); lines,5x7 matrix void MSDealy(unsigned int itime); //Delay void main() { unsigned char value; //ADC data recived lcdcmd(0x38); lcdcmd(0x0e); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x83); lcddata('T'); lcddata('e'); lcddata('m'); lcddata('p'); lcddata('e'); lcddata('r'); lcddata('a'); lcddata('t'); lcddata('u'); lcddata('r'); lcddata('e'); lcddata('r'); mydata1 = 0xff; ldata = 0x00; //---------ADC routine while (1) { aen = 0; wr = 0; Dept of ECE //enable ADC //Write=0 //LCD command

//Initialization LCD 2

//LCD data

//Set port 2 as input //Set port 0 as output

Page 57

Microcontroller Lab Manual wr = 1; //WR=1 L-TO-H TO START CONVERSION while (intr == 1) ; //WAIT FOR END OF CONVERSION rd = 0; //CONVERSION FINISHED,ENABLE RD value = mydata1; //READ THE DATA value = value - 114; lcdcmd(0xC7); convert_display(value); rd = 1; //MAKE RD=1 FOR NEXT ROUND MSDealy(10); } } //----------------Hex to Binary convertion routine void convert_display(unsigned char value) { unsigned char x, d1, d2, d3, data1, data2, data3; x = value / 10; //divide by 10 d1 = value % 10; //save low digit (reminder of division) d2 = x % 10; d3 = x / 10; //divide by 10 once more data1 = d1 | 0x30; //make it ASCII and save LSB data2 = d2 | 0x30; data3 = d3 | 0x30; lcddata(data3); //display converted output MSB first lcddata(data2); lcddata(data1); return; } //--------------LCD command void lcdcmd (unsigned char value) { lcdready(); ldata = value; rs = 0; rw = 0; en = 1; MSDealy(1); en = 0; return; } //-------------LCD data void lcddata (unsigned char value) { lcdready(); ldata = value; rs = 1; rw = 0; Dept of ECE

//is LCD ready? //issue command code //RS=0 for command //R/W=0 to write to LCD //E=1 for H-to-L pulse //E=0 ,latch in

//is LCD ready? //issue data //RS=1 for data //R/W=0 to write to LCD Page 58

Microcontroller Lab Manual en = 1; MSDealy(1); en = 0; return; } void lcdready () { busy = 1; rs = 0; rw = 1; //E=1 for H-to-L pulse //E=0, latch in

//make P1.7 input port //RS=0 access command reg //R/W=1 read command reg ;read command reg and check busy flag while (busy == 1) //stay until busy flag=0 { en = 0; //E=1 for H-to-L pulse MSDealy(1); en = 1; //E=0 H-to-L pulse } return;

} //------------Delay Routine void MSDealy(unsigned int itime) { unsigned int i,j; for (i=0;i<itime;i++) for (j=0;j<1275;j++); } C CODING FOR CALCULATOR #include <REG52.H> sfr ldata = 0x80; sbit rs = P3^2; sbit rw = P3^3; sbit en = P3^4; sbit busy = P0^7; //Variables declaration for LCD

//--------------Funtion declaration void convert_display(unsigned char); //Hex to Binary converter void MSDelay (unsigned int); //Delay void lcdcmd (unsigned char value); void lcddata (unsigned char value); void lcdready (); unsigned char getkey (); unsigned char j,countl,countr, key=0,Data1,Data2,Funct,Ans ; //Serially recieved data unsigned char idata msg[11] = {"Calculator "}; unsigned char idata keypad[4][4]= { '1','2','3','/', '4','5','6','*', '7','8','9','-', Dept of ECE Page 59

Microcontroller Lab Manual 'C','0','C','+',}; void main() { while(key!='C') { lcdcmd(0x38); lcdcmd(0x0e); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x83); //-------------Display message on lCD terminal for (j=0;j<11;j++) { lcddata(msg[j]); //Get data from lookup table } while(1) { lcdcmd(0xC3); do { getkey (); //get data from matrix key pad MSDelay(10); Data1 = key; key = key & 0xF0; }while (key!=0x30); lcddata(Data1); do { getkey ();//get data from matrix key pad MSDelay(10); Funct = key; key = key & 0xF0; }while (key!=0x20); lcddata(Funct); do { getkey (); //get data from matrix key pad MSDelay(10); Data2 = key; key = key & 0xF0; }while (key!=0x30); lcddata(Data2); lcddata('='); Data1= Data1 & 0x0F; Data2= Data2 & 0x0F; switch (Funct) //loop to respective subroutine { case ('+'): Dept of ECE Page 60

Microcontroller Lab Manual { Ans = Data1 + Data2; convert_display(Ans); break; } case ('-'): { Ans = Data1 - Data2; convert_display(Ans); break; } case ('*'): { Ans = Data1 * Data2; convert_display(Ans); break; } case ('/'): { Ans = Data1 / Data2; convert_display(Ans); break; } }}}} void MSDelay (unsigned int value) // Delay routine { unsigned int x,y; for (x=0;x<900;x++) for (y=0;y<value;y++); } void lcdcmd (unsigned char value) { lcdready(); ldata = value; rs = 0; rw = 0; en = 1; MSDelay(1); en = 0; return; } void lcddata (unsigned char value) { lcdready(); ldata = value; rs = 1; Dept of ECE Page 61

Microcontroller Lab Manual rw = 0; en = 1; MSDelay(1); en = 0; return; } void lcdready () { busy = 1; rs = 0; rw = 1; while (busy == 1) { en = 0; MSDelay(1); en = 1; } return; } unsigned char getkey () // Routine to read matrix keyboard { unsigned char colloc, rowloc; TMOD = 0x20; TH1 = -3; SCON = 0x50; TR1 = 1; P2 = 0xff; do{ P2 = 0x0f; //Wait untill all keys are released colloc = P2; colloc &= 0x0f; } while (colloc != 0x0f); do{ do { MSDelay (1); colloc = P2; colloc &= 0x0f; } while (colloc == 0x0f); //Check whether any key is pressed MSDelay (1); colloc = P2; colloc &= 0x0f; //Confirm whether any ket is pressed after delay } while (colloc == 0x0f); aviod spikes //to

Dept of ECE

Page 62

Microcontroller Lab Manual while(1) { P2 = 0xfE; //get the row presses colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 0; break; } P2 = 0xfd; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 1; break; } P2 = 0xfb; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 2; break; } P2 = 0xf7; colloc = P2; colloc &= 0xf0; rowloc = 3; break; } //get the coloum presses if (colloc == 0xe0) key = (keypad[rowloc][0]); else if (colloc == 0xd0) key = (keypad[rowloc][1]); else if (colloc == 0xb0) key = (keypad[rowloc][2]); else key = (keypad[rowloc][3]); return(key); } //----------------Hex to Binary convertion routine void convert_display(unsigned char value) { Dept of ECE Page 63

Microcontroller Lab Manual unsigned char x, d1, d2, d3, data1, data2, data3; x = value / 10; //divide by 10 d1 = value % 10; //save low digit (reminder of division) d2 = x % 10; d3 = x / 10; once more data1 = d1 | 0x30; and save LSB data2 = d2 | 0x30; data3 = d3 | 0x30; lcddata(data3); converted output MSB first lcddata(data2); lcddata(data1); return; } Elevator #include <REG52.H> sfr ldata = 0x80; sbit rs = P3^2; sbit rw = P3^3; sbit en = P3^4; sbit busy = P0^7; sbit carry = PSW^7; //Variables declaration for LCD //display //make it ASCII //divide by 10

//--------------Funtion declaration void convert_display(unsigned char); //Hex to Binary converter void MSDelay (unsigned int); //Delay void lcdcmd (unsigned char value); void lcddata (unsigned char value); void lcdready (); void down (); void up (); unsigned char getkey (); unsigned char i,j,countl,countr, key=0,Data1='0',Data2='0',Funct,Ans ; //Serially recieved data //----------------The message unsigned char idata msg[9] = {"Floor No:"}; unsigned char idata keypad[4][4]= { '1','2','3','X', '4','5','6','X', Dept of ECE Page 64

Microcontroller Lab Manual

'7','8','9','X', 'X','0','X','X',}; void main() { lcdcmd(0x38); lcdcmd(0x0e); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x80); //-------------Display message on hyper terminal for (j=0;j<9;j++) { lcddata(msg[j]); //Get data from lookup table } lcdcmd(0x8A); lcddata('0'); lcddata('0'); while(1) { do { getkey (); //get data from matrix key pad MSDelay(10); Data1 = key; key = key & 0xF0; }while (key!=0x30); carry =0; if (Data1<Data2) down(); else up(); }} void down() { for(i=(Data2-Data1);i>0;i--) { for (j=3;j>0;j--) { lcdcmd(0x8E); lcddata('v'); lcddata('v'); Dept of ECE Page 65

Microcontroller Lab Manual MSDelay(30); lcdcmd(0x8E); lcddata(' '); lcddata(' '); MSDelay(30); } Data2--; lcdcmd(0x8b); lcddata(Data2); } } void up() { for(i=(Data1-Data2);i>0;i--) { for (j=3;j>0;j--) { lcdcmd(0x8E); lcddata('^'); lcddata('^'); MSDelay(30); lcdcmd(0x8E); lcddata(' '); lcddata(' '); MSDelay(30); } Data2++; lcdcmd(0x8b); lcddata(Data2); } } void MSDelay (unsigned int value) // Delay routine { unsigned int x,y; for (x=0;x<900;x++) for (y=0;y<value;y++); } { lcdready(); ldata = value; rs = 0; Dept of ECE Page 66

void lcdcmd (unsigned char value)

Microcontroller Lab Manual rw = 0; en = 1; MSDelay(1); en = 0; return; } void lcddata (unsigned char value) { lcdready(); ldata = value; rs = 1; rw = 0; en = 1; MSDelay(1); en = 0; return; } void lcdready () { busy = 1; rs = 0; rw = 1; while (busy == 1) { en = 0; MSDelay(1); en = 1; } return; } unsigned char getkey () // Routine to read matrix keyboard { unsigned char colloc, rowloc; TMOD = 0x20; TH1 = -3; SCON = 0x50; TR1 = 1; P2 = 0xff; do {

P2 = 0x0f; //Wait untill all keys are released colloc = P2; colloc &= 0x0f; } while (colloc != 0x0f); do Dept of ECE { Page 67

Microcontroller Lab Manual do { MSDelay (1); colloc = P2; colloc &= 0x0f; } while (colloc == 0x0f); //Check whether any ket is pressed MSDelay (1); colloc = P2; colloc &= 0x0f; //Confirm whether any ket is pressed after delay } while (colloc == 0x0f); //to aviod spikes while(1) { P2 = 0xfE; //get the row presses colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 0; break; } P2 = 0xfd; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 1; break; } P2 = 0xfb; colloc = P2; colloc &= 0xf0; if (colloc != 0xf0) { rowloc = 2; break; } P2 = 0xf7; colloc = P2; colloc &= 0xf0; rowloc = 3; break; } //get the coloum presses

Dept of ECE

Page 68

Microcontroller Lab Manual if (colloc == 0xe0) key = (keypad[rowloc][0]); else if (colloc == 0xd0) key = (keypad[rowloc][1]); else if (colloc == 0xb0) key = (keypad[rowloc][2]); else key = (keypad[rowloc][3]); return(key); } /*----------------Hex to Binary convertion routine void convert_display(unsigned char value) { unsigned char x, d1, d2, d3, data1, data2, data3; x = value / 10; //divide by 10 d1 = value % 10; //save low digit (reminder of division) d2 = x % 10; d3 = x / 10; //divide by 10 once more data1 = d1 | 0x30; //make it ASCII and save LSB data2 = d2 | 0x30; data3 = d3 | 0x30; lcddata(data3); converted output MSB first lcddata(data2); lcddata(data1); return; } */ //display

Dept of ECE

Page 69

Vous aimerez peut-être aussi