Micro Controller Manual

97
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 //Source address MOV R1,#50H //Destination address MOV R3,#0AH //count BACK:MOV DPL,R0 //Get data from source MOVXA,@DPTR MOV DPL,R1 MOVX@DPTR,A //Move to destination INC R0 //Increment pointers INC R1 DJNZ R3,BACK HERE:SJMP HERE //wait here END 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. Dept of ECE Page 1

Transcript of Micro Controller Manual

Page 1: Micro Controller Manual

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 0HSJMP 30HORG 30HMOV DPH,#80HMOV R0,#35H //Source addressMOV R1,#50H //Destination addressMOV R3,#0AH //countBACK:MOV DPL,R0 //Get data from sourceMOVXA,@DPTRMOV DPL,R1MOVX@DPTR,A //Move to destinationINC R0 //Increment pointersINC R1DJNZ R3,BACK

HERE:SJMP HERE //wait hereEND

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

Page 2: Micro Controller Manual

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 0HSJMP 30HORG 30HMOV R0,#27H //Source addressMOV R1,#41H //Destination addressMOV R3,#05H //Count

BACK: MOVX A,@DPTR //Get data from sourceMOV R2,A //Save it in R2MOVX A,@R1 //Get data from destinationMOVX @DPTR ,A //Move it to sourceMOV A,R2 //Move source data(stored in R2 )to destinationMOVX @R1,AINC DPTR //Increment pointersINC R1DJNZ R3,BACK //Decrement count and repeat

HERE: SJMP HERE //Wait hereEND

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.

Dept of ECE Page 2

Page 3: Micro Controller Manual

Microcontroller Lab Manual

3) Assembly Language Program to Sort Numbers.//Bubble sort ProgramWrite 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 0HSJMP 30HORG 30HMOV R0,#05H //Count n-1 Array

//Size-n-Pass CounterL1: MOV DPTR,#9000H //Array stored from

//Address 9000HMOV A,R0 //Initialize exchange counterMOV R1,A

L2: MOVX A,@DPTR //Get numbers from arrayMOV B,A //& Store in BINC DPTRMOVX A,@DPTR //Next number in the arrayCLR C //Reset borrow flagMOV R2, A //Store in R2SUBB A, B //2nd -1st No.---No compare Instruction in 8051JC NOEXCHG //JNC- For ascending orderMOV A, B // Exchange the 2 numbers in the arrayMOVX @DPTR, ADEC DPL //DEC DPTR-Instruction Not PresentMOV A, R2MOVX @DPTR, AINC DPTR

NOEXCHNG: DJNZ R1,L2 // Decrement compare counter DJNZ R0, L1 //Decrement pass counter

HERE: SJMP HERE END

Algorithm:

1. Store the elements of the array from the address 9000h2. 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.

Dept of ECE Page 3

Page 4: Micro Controller Manual

Microcontroller Lab Manual

Result:

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 0HSJMP 30HORG 30HMOV R3,#6 //Length of the arrayMOV DPTR,#4000H //Starting address of the arrayMOVX A,@DPTR //R1 is to store

//Largest number MOV R1,A

NEXTBYTE:INC DPTRMOVX A,@DPTRCLR C //Reset borrow flagMOV R2,A //Next number in the arraySUBB A,R1 //Other Num-Previous Largest No.JC SKIP // JNC for smallest elementMOV A,R2 // UPDATE larger number in R1MOV R1,A

SKIP: DJNZ R3,NEXTBYTE MOV DPL,#62H // Location of the result -4062H MOV A,R1 //Largest number MOVX @DPTR,A //Store at #4062H OVER: SJMP OVER

END

Dept of ECE Page 4

Page 5: Micro Controller Manual

Microcontroller Lab Manual

Algorithm:

1. Store the elements of the array from the address 4000H2. Store the length of the array in R3 and set it as Counter.3. DPTR is loaded with the starting address of the array.4. Store the first number of the array in R1(R1 is assigned to hold the largest number5. Increment DPTR6. 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.

Dept of ECE Page 5

Page 6: Micro Controller Manual

Microcontroller Lab Manual

Assembly Language Program Illustrating Addition, Subtraction, Multiplication and Division.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 0000HSJMP 30HORG 30HMOV DPTR,#9000HMOVX A,@DPTRMOV R0,AINC DPTRMOVX A,@DPTR

CJNE R0,#00,CKSUBADD A, R1 //Perform AdditionSJMP LAST

CKSUB:CJNE R0,#01,CKMUL CLR C // Clear Carry flagMOV A,R1SUBB A,R2 //FF indicates negative numberSJMP LAST

CKMUL:CJNE R0,#02,CKDIVMOV A,R1MOV B,R2MUL AB //16-Bit product in AB with A having lower byteSJMP LAST

CKDIV:CJNE R0,#03,OTHERMOV A,R1MOV B,R2DIV AB //Quotient in A & reminder in BSJMP LAST

OTHER:MOV A,#00MOV B,#00

LAST:INC DPTRMOVX @DPTR,A //Store A&B in the nextEND

Algorithm:

1. Store the condition X in R1.2. load the first and second numbers to A and B registers respectively.3. Compare the contents of R1 and perform the operations add, sub, etc accordingly.4. store the result present in A and B registers to the appropriate memory locations.

Dept of ECE Page 6

Page 7: Micro Controller Manual

Microcontroller Lab Manual

RESULT:Before Execution :ADD After Execution

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 0HSJMP 30HORG 30HMOV DPTR,#9000HMOVX A,@DPTR //Get number-XMOV R0,A // Store in R0MOV B,AMUL AB //Square it-X^2CLR C //For storing resultJB 01,LAST //If bit 01 is set then End,

Dept of ECE Page 7

Page 8: Micro Controller Manual

Microcontroller Lab Manual

//Else do cubePUSH B //Store upper part of squareMOV B,A //B-Lower part of X^2MOV A,R0 //A-XMUL AB //X*Lower X^2INC DPTRMOVX @DPTR,A //Store partial resultMOV A,BMOV R2,A //Upper part of X*Lower X^2 in R2POP B // Get back upper part of square MOV A,R0 //A-XMUL AB //X*Upper X^2ADD A,R2 // Add to partial resultLAST:INC DPTRMOVX @DPTR,AMOV A,BADDC A, # 00 //Add carry to B (For square result, C=0)INC DPTRMOVX @DPTR, AHERE: SJMP HEREEND

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.

Dept of ECE Page 8

Page 9: Micro Controller Manual

Microcontroller Lab Manual

Algorithm:

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 stack5. 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.

Dept of ECE Page 9

Page 10: Micro Controller Manual

Microcontroller Lab Manual

PROGRAM ILLUSTRATING BIT MANIPULATIONS

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 //Reset vectorSJMP 30HORG 30HMOV DPTR,#8000HMOVX A,@DPTR //Get number from 8000hMOV R0,A //R0=NUM1INC DPTRMOVX A,@DPTR //A=NUM2CLR C //Clear ‘C’ for SUBBSUBB A,ROJZ EQUALJNC BIGSETB 78H //Set bit at 78h bit addressSJMP END1

BIG: SETB 7FHSJMP END1

EQUAL: CLR 77HCLR 7FH

END1: SJMP END1 //wait hereEND

Algorithm:

1. Store the elements of the array from the address 4000H2. 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=012. Before Execution: X: 08000H=25 & X :8001=35 After Execution: D:02FH=803. Before Execution: X: 08000H=45 & X :8001=45

Dept of ECE Page 10

Page 11: Micro Controller Manual

Microcontroller Lab Manual

After Execution: D:02FH=00 LOGICAL INSTRUCTIONS

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 0HSJMP 30HORG 30HMOV A,20H //Do not use #,as data RAM 20H is to be accessedMOV R1,A //X in R1MOV A,21H //A has NUM1CJNE R1,#0,CKOR //Check if R1=0ANL A,22H //AND with contents of 22H Location=NUM2SJMP END1

CKOR: CJNE R1,#01,CKXOR // Check if R1=01ORL A,22H //Yes-do ‘OR’ operationSJMP END1

CKXOR: CJNE R1,#02,OTHERXRL A,22H //XOR OperationSJMP 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=022. Before Execution: D:020H=01,21=0F,22=12 After Execution D:023H=1F3. Before Execution: D:020H=02,21=0F,22=12 After Execution D:023H=1D4. Before Execution: D:020H=34, 21=0F,22=12 After Execution D: 023H=00

Dept of ECE Page 11

Page 12: Micro Controller Manual

Microcontroller Lab Manual

The above program can also be written as shown below (using indirect addressing)ORG 0HSJMP 30HORG 30HMOV R0,#2OhMOV A,@R0 //ON CHIP DATA RAM-DONOT USE MOVXMOV R1,A //X IN R1INC ROMOV A,RO //A-NUM1INC R0 //R0 POINTS TO NUM2CJNE R1, #0, CKORANL A,@ROSJMP END1

CKOR:CJNE R1,#01,CKXORORL A,@R0SJMP END1

CKXOR:CJNE R1,#02,OTHERXRL A,@R0SJMP 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 0HSJMP 30HORG 30HMOV R0,20H //R0=XCJNE R0,#0,CK1MOV C,08H //LSB of NUM1(21H) – BIT address-08ANL C,10H //LSB of NUM1(22H) – BIT address-10SJMP LAST

CK1: CJNE R0,#1,CK2MOV C,0FH //MSB of NUM1(21H) – BIT address-0FORL C,17H //MSB of NUM1(22H) – BIT address-17SJMP LAST

CK2: CJNE R0,#2,CK3CPL 0FH

Dept of ECE Page 12

Page 13: Micro Controller Manual

Microcontroller Lab Manual

MOV C,0FH //MSB of NUM1(21H) – BIT address-0FSJMP LAST

CK3: CLR CLAST: MOV 1FH,C //RES is MSB of 23H Location-1FHHERE: SJMP HERE

END

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.

Dept of ECE Page 13

Page 14: Micro Controller Manual

Microcontroller Lab Manual

COUNTERS:

Assembly Program Illustrating Hex Up/Down Counters10) 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 //Reset vectorSJMP 30HORG 30HMOV A, #00 //Initial count

BACK: ACALL DELAY INC A //Dec a for binary down counter JNZ BACK

HERE: SJMP HERE DELAY: MOV R1,#0FFH //Delay subroutine DECR1: MOV R2,#OFFH DECR: MOV R3,#OFFH

DJNZ R3,$ //$ Indicate current address DJNZ R2,DECR DJNZ R1,DECR1 RET END

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).

Dept of ECE Page 14

Page 15: Micro Controller Manual

Microcontroller Lab Manual

Assembly Program Illustrating BCD UP/DOWN Counters.// Counter program – BCD up/down counters.

11) Write an ALP to implement (display) an eight bit UP/DOWN BCD counters on watch window.

ORG 0HSJMP 30HORG 30HMOV A,#00

BACK: ACALL DELAY //99H for BCD Down counterADD A,#99H //ADD 01 for BCD up counterDA A //for bed counterJNZ BACK

HERE: SJMP HEREDELAY: MOV R1, #0FFH //Delay subroutineDECR1: MOV R2,#0FFHDECR: MOV R3,#0FFHLOOP1: DJNZ R3,LOOP1

DJNZ R2,DECRDJNZ R1,DECR1RET END

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.

Dept of ECE Page 15

Page 16: Micro Controller Manual

Microcontroller Lab Manual

SERIAL DATA TRANSMISSIONProgram illustrating serial ASCII data transmission (data-yE)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 0HSJMP 30HORG 30HMOV TMOD,#20H //Timer 1; mode 2MOV TH1,#-3 //-3=FD loaded into TH1 for

//9600 baud, 11.0592MHz.MOV SCON,#50H //8-BIT, 1 stop bit, REN enabledSETB TR1 //Start timer !MOV A,#’Y’ //Transfer “Y”ACALL TRANSMOV A,#’E’ // Transfer “E”ACALL TRANS

AGAIN1: SJMP AGAIN1TRANS: MOV SBUF,A //LOAD SBUFHERE:JNB T1,HERE //Wait for last bit to transfer,until T1=1

CLR T1 //Get ready for next byteRET END

RESULT:yE is printed on the serial window each time the program is executed.

THEORY:

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)=28800Serial 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.

Dept of ECE Page 16

Page 17: Micro Controller Manual

Microcontroller Lab Manual

Algorithm:1. Initialize timer 1 to operate in mode 2 by loading TMOD register.2. Load TH1 with -3 to obtain 9600 baud.3. Initialize the asynchronous serial communication transmission (SCON) register.4. Start timer 1 to generate the baud rate clock.5. Transmit the characters “Y” & “E” by writing into the SBUF register and waiting

for the T1 flag.

TIMER DELAY PROGARMProgarm illustrating timer delay.

13) Generate a 1 second delay continuously using the chip timer in interrupt mode.

ORG 0H //Reset vectorSJMP 30H ORG 0BH //TFO vectorSJMP ISRORG30H //Main programMOV A, #00MOV R0,#0MOV R1,#0MOV TMOD, #02H //00000010-Run timer in mode 2MOV THO, #118 //Set up timer 0 to overflow in 0.05msecMOV IE,#82H //%10000010—Enable Timer0 interruptSETB TCON.4 //Start the timer0

HERE: SJMP HEREISR: CLR TCON.4 //Disable timer0

INC R1 //R1*R2=100*200=20000*0.05msec=1secCJNE R1,#100,SKIPMOV R1,#00INC R0CJNE R0,#200,SKIPMOV R0,#00HINC A

SKIP: SETB TCON.4 //Enable TimerRET1 //Return from interrupt subroutineEND

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. Stet up timer0 in mode 2 operation.2. Load TH1 with 118 to generate an every 0.05msec.3. Reset registers A,R1 & R0.4. Repeat step 4 continuously i.e., in main program just wait, do nothing.

Dept of ECE Page 17

Page 18: Micro Controller Manual

Microcontroller Lab Manual

5. In interrupt; ISR at 000B location goes to step 6which is TFO ISR.6. Disable timer07. Update R1 & R08. Check if 20000 interrupts (=1sec) over. Yes –increment accumulator A.9. Enable timer & return from ISR.

TMOD FUNCTION REGISTER7 6 5 4 3 2 1 0Gate C/T M1 M0 Gate C/T M1 M0

[TIMER 1 ][ TIMER0]

M1 M0 MODE0 0 0-13 bit0 1 1-16 bit1 0 2-8 bit

reloadable timer

1 1 3-timer0-2-8bit timers.Timer 1 stop

TCON FUNCTION REGISTER

7 6 5 4 3 2 1 0TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0

Dept of ECE Page 18

Bit Symbol Function 7/3

Gate OR gate enable bit which controls RUN/STOP of timer 1/0

6/2 C/T 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)

5/1 M1 Timer/counter operating mode select bit1

4/0 M0 Timer/counter operating mode select bit0

Page 19: Micro Controller Manual

Microcontroller Lab Manual

INTERRUPT ADDRESS(HEX)IE0 0003TFO 000BIE1 0013TF1 001BSERIAL 0023

Timerdelay=12*(257-delay)/frequencyTimerdelay=0.05msecDelay=256-((timerdelay*frequency)/12)

=256-(0.05*10-3*33*106)/12=256-137.5=118.5 //loaded in THO

To get 1sec delay1/0.05msec=200*100 in the ISR (assuming 33 MHZ crystal frequency. For 11 MHZ, the calculations change)

CONVERSION PROGRAMS14) Write an ALP to implement decimal to hex conversion

ORG 0000HSJMP 30HORG 30HMOV DPTR,#40H //2-digit decimal number to be converted is

//stored in data memory 40HMOVX A,@DPTRANL A,#0F0H //obtain upper decimal digit by masking lower oneSWAP A //bring to the units placeMOV B,#0AH //Multiply tens digit with #0A-to get tens in hexMUL ABMOV R1,A //Temporarily store the converted tens valueMOVX A,@DPTR //Get the decimal number againANL A,#0FH //Obtain the units digit by masking upper digitADD A,R1 //Add to the converted tens valueINC DPTR //Increment data addressMOVX @ DPTR,A // Converted hexadecimal number in next location

HERE: SJMP HERE

Dept of ECE Page 19

Bit Symbol Function77 TF1 Timer 1 overflow flag6 TR1 Timer 1 run control bit5 TF0 Timer 0 overflow flag4 TR0 Timer 0 run control bit3 IE1 External interrupt 1 edge flag2 IT1 External interrupt 1 signal type

control bit1 IE0 External interrupt 0 edge flag0 IT0 External interrupt 0 signal type

control bit

Page 20: Micro Controller Manual

Microcontroller Lab Manual

ENDRESULT:

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.

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 0000HSJMP 30HORG 30HMOV DPTR, #9000HMOVX A,@DPTR //Get hex numberMOV B,#10DIV AB //Divide by 10 (OAH)INC DPTRXCH A,BMOVX @DPTR,A //Store the remainder (in B) in units placeXCH A,BMOV B, #10 //Divide the quotient in A by 10DIV ABINC DPTRXCH A,BMOVX @DPTR,A //Store the remainder (in B) in tens placeXCH A,BINC DPTRMOVX @DPTR,A //Store the quotient (in A) in hundreds place

HERE:SJMP HEREEND

RESULT:

9000H-FF(HEX NUMBER)9001 to 9003- unpacked BCD number (decimal)-5,5,2(i.e.,255 stored Lower digit first)

Dept of ECE Page 20

Page 21: Micro Controller Manual

Microcontroller Lab Manual

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 BCD’s units value.4. Repeat the step 2 to obtain the BCD ten’s digit as remainder & quotient as

hundred’s digit.5. Store the tens and hundred’s BCD digits.

16) Write an ALP to implement BCD to ASCII conversion

ORG 0HSJMP 30HORG 30HMOV R1,#50HMOV A,@R1 //Get BCD data byte from RAM Location 50HMOV R2,A //Store in R2ANL A,#0FH //Get the lower nibbleORL A,#30H //Add/or with 30H i.e., 0-9 converted to 30-30HINC R1MOV @R1,A // Store the lower digit’s ASCII codeMOV A,R2 // Get back the numberSWAP A //Swap nibbles in AANL A,#0FH //Get the upper BCD digitORL A,#30H //Convert to ASCIIINC R1MOV @R1,A //Store the upper digit’s ASCII codeHERE:SJMP HEREEND

Result:The BCD code 28 at D:0050H is converted to 2 ASCII codes-38H-32H

Dept of ECE Page 21

Page 22: Micro Controller Manual

Microcontroller Lab Manual

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 30H5. Store the converted ASCII value.

17)Write an ALP to implement Hexadecimal to ASCII conversion.// This program also illustrates conditional branching (JNC), call and return instructions.

ORG 0HSJMP 30HORG 30HMOV R1,#50HMOV A,@R1 //Get hexadecimal data type from RAM/Location 50HMOV R2,A //Store in R2ANL A,#0FH //Get the lower nibbleACALL ASCII //Convert to ASCIIINC R1MOV @R1,A //Store the lower digit’s ASCII codeMOV A,R2 //Get back the numberSWAP A //Swap nibbles in AANL A,#0FH //Get the upper BCD digitACALL ASCIIINC R1MOV @R1,A //Store the upper digit’s in ASCII codeHERE:SJMP HEREASCII:MOV R4,A //Store ‘A’CLR CSUBB A,#0AH //Check if digit >=0AMOV A,R4 //Get back AJC SKIPADD A,#07H //Add 07 if >09SKIP:ADD A,#30H //Else add only 30H for 0-9RETEND

RESULT:

Dept of ECE Page 22

Page 23: Micro Controller Manual

Microcontroller Lab Manual

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. Move the hexadecimal data to be converted to accumulator.2. Get the lower nibble & call ASCII routine.3. Store the converted ASCII value.4. Get the higher nibble & call ASCII routine.5. Store the converted ASCII value.

ASCII subroutine1. If digit greater than 09,(for A-F) add 07H & 30H2. Else (i.e.,0-9)add only 30H3. Return.

18) Write an ALP to implement ASCII to Hexadecimal conversion.

ORG 0H SJMP 30HORG 30HMOV R1,#50HMOV A,@R1 //Get ASCII byte from RAM location 50HCLR CSUBB A,#41H //Compare with 41HMOV A,@R1 //Get back number into AJC SKIP //If number <41, subtract only 30HCLR CSUBB A,#07H //Subtract 07 & 30H if number >41SKIP:CLR CSUBB A,#30HINC R1MOV @R1,A //Store the hex codeHERE:SJMP HEREEND

Result:The ASCII code 45 at D:0050h is converted to hexadecimal 0E at 51H

Dept of ECE Page 23

Page 24: Micro Controller Manual

Microcontroller Lab Manual

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 & 30H3. Else (i.e. for 0-9) subtract only 30H4. Store the converted hexadecimal number.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.

Dept of ECE Page 24

Page 25: Micro Controller Manual

Microcontroller Lab Manual

.

Dept of ECE Page 25

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

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.

Page 26: Micro Controller Manual

Microcontroller Lab Manual

Dept of ECE Page 26

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.

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

Page 27: Micro Controller Manual

Microcontroller Lab Manual

.

Software Programs Using MSP430

1. DATA TRANSFER1a. ALP FOR BLOCK MOVE OPERATION

Dept of ECE Page 27

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

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

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

Page 28: Micro Controller Manual

Microcontroller Lab Manual

.cdecls C,LIST, "msp430g2452.h" // CALLING HEADER FILE FROM C LIBRARY;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop mov.W #0200h,R5 ; STARTING ADDRESS OF SOURCE mov.W #0250h,R6 ; STARTING ADDRESS OF DESTINATION

mov.w #0009h,R7 ; DATA LENGTH ( HERE 9)again mov.W @R5,0(R6) incd.w R5 incd.w R6 dec.w R7 jnz again ; GO TO LOOP AGAIN jmp Mainloop ; GO TO LOOP MAIN LOOP , KEEP BREAK POINT HERE ;------------------------------------------------------------------------------; Interrupt Vectors;------------------------------------------------------------------------------ .sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

1b. ALP FOR EXCHANGE OPERATION .cdecls C,LIST, "msp430g2452.h" // CALLING HEADER FILE FROM C LIBRARY;------------------------------------------------------------------------------.text ; Progam Start

Dept of ECE Page 28

Page 29: Micro Controller Manual

Microcontroller Lab Manual

;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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 LENGTHagain 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;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDT Mainloop mov.w #0009h,R7 LOOP2 mov.W #0200h,R5 ; STARTING ADDRESS

mov.w #0009h,R6 ; DATA LENGTHagain 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

Dept of ECE Page 29

Page 30: Micro Controller Manual

Microcontroller Lab Manual

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 LENGTHagain CMP.W R6,0(R5)

JC LOOP1 jmp LOOP2

LOOP MOV.W @R5,R6 ; RESULT IN R6LOOP2 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

2. Arithmetic operations

2a. ALP FOR ADDITION OPERATION .cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMain 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;------------------------------------------------------------------------------

Dept of ECE Page 30

Page 31: Micro Controller Manual

Microcontroller Lab Manual

.text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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;------------------------------------------------------------------------------

Dept of ECE Page 31

Page 32: Micro Controller Manual

Microcontroller Lab Manual

.text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop mov.w #06h,R5 ; DATA mov.w R5,R6

mov.w #00h,R7here 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;------------------------------------------------------------------------------

Dept of ECE Page 32

Page 33: Micro Controller Manual

Microcontroller Lab Manual

.text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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,R7LOOP2 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.cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY;------------------------------------------------------------------------------ .text ; Program Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop mov.w #0FFh,&P1DIR ; P1 IS OUT PUT mov.w #00h,R5Loop1 inc.w R5

mov.w R5,& P1OUT ; RESULT IN R5 AND PORT 1 Wait mov.w #0FFFFh,R15 ; DELAY TO R15L1 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

Dept of ECE Page 33

Page 34: Micro Controller Manual

Microcontroller Lab Manual

.cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY;------------------------------------------------------------------------------ .text ; Program Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT 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;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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

Dept of ECE Page 34

Page 35: Micro Controller Manual

Microcontroller Lab Manual

;------------------------------------------------------------------------------ .text ; Program Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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 POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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

Dept of ECE Page 35

Page 36: Micro Controller Manual

Microcontroller Lab Manual

;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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 StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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

Dept of ECE Page 36

Page 37: Micro Controller Manual

Microcontroller Lab Manual

.cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRAY;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop mov.w #00F0h,R5 ;DATA clr R5 jmp Mainloop ;PUT BREAK POINT HERE;------------------------------------------------------------------------------; Interrupt Vectors;------------------------------------------------------------------------------

.sect ".reset" ; MSP430 RESET Vector .short RESET ; .end

4h. ALP FOR “BIT TEST “OPERATION

.cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRAY;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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

Dept of ECE Page 37

Page 38: Micro Controller Manual

Microcontroller Lab Manual

.cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRAY;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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

Dept of ECE Page 38

Page 39: Micro Controller Manual

Microcontroller Lab Manual

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 POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop mov.w #0000h,SR jmp LOOP0 LOOP5 clrc jnc LOOP1LOOP0 setz jz LOOP2LOOP1 clrn jmp LOOP6LOOP2 setc jc LOOP3LOOP6 clrz jnz LOOP4LOOP3 setn jmp LOOP5 LOOP4 jmp Mainloop ; PUT BREAK POINT HERE;--------------------------------------------------------------------------; Interrupt Vectors;-------------------------------------------------------------------------- .sect ".reset" ; MSP430 RESET Vector .short RESET ; .end

5. ALP FOR “CONDITIONAL CALL & RETURN “OPERATION

Dept of ECE Page 39

Page 40: Micro Controller Manual

Microcontroller Lab Manual

.cdecls C,LIST, "msp430g2231.h" // CALLING HEADER FILE FROM C LIBRARY;------------------------------------------------------------------------------ .text ; Program Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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 MainloopReturn mov.w #10,R6 ; SUBROUTINE FUNCTIONLoop2 dec.w R6 jnz Loop2 ret

;------------------------------------------------------------------------------; Interrupt Vectors;------------------------------------------------------------------------------ .sect ".reset" ; MSP430 RESET VECTOR .short RESET ; .end

6. CODE CONVERSION

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 POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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;------------------------------------------------------------------------------

Dept of ECE Page 40

Page 41: Micro Controller Manual

Microcontroller Lab Manual

.text ; Program Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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" //CALLING HEADER FILE FROM C LIBRARY;------------------------------------------------------------------------------ .text ; Program Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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 ; .end6d. ALP FOR “HEX TO DECIMAL “OPERATION

cdecls C,LIST, "msp430g2231.h" //CALLING HEADER FILE FROM C LIBRARY

Dept of ECE Page 41

Page 42: Micro Controller Manual

Microcontroller Lab Manual

;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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 ;

.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 POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTMainloop 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

Dept of ECE Page 42

Page 43: Micro Controller Manual

Microcontroller Lab Manual

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 rla R6 rla R6 rla R6 or R6,R8 rlc R7 rlc R7 rlc R7 rlc R7 or R8,R7 ;RESULT IN R7 jmp Mainloop PUT BREAK POINT HERE

7a. ALP FOR “DELAY “OPERATION

cdecls C,LIST, "msp430g2231.h" //CALLING HEADER FILE FROM C LIBRARY

Dept of ECE Page 43

;----------------------------------------------------------------------; Interrupt Vectors;---------------------------------------------------------------------- .sect ".reset" ; MSP430 RESET Vector

Page 44: Micro Controller Manual

Microcontroller Lab Manual

;------------------------------------------------------------------------------ .text ; Progam Start;------------------------------------------------------------------------------RESET mov.w #0280h,SP ; INITIALIZE STACK POINTERStopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTSetupP1 bis.b #001h,&P1DIR ; P1.0 OUTPUTMainloop xor.b #01h,&P1OUT ; TOGGLE P1.0

Wait mov.w #0FFFFh,R15 ; DELAY to R15L1 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 StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ; STOP WDTSetupTA mov.w #TASSEL_1+MC_2,&TACTL ; ACLK, CONTINOUS MODESetupC0 mov.w #OUT,&CCTL0 ; TXD IDLE AS MARKSetupP1 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 = idleTX_Wait bit.w #CCIE,&CCTL0 ; Wait for TX completion jnz TX_Wait ; ret ;;-------------------------------------------------------------------------------RX_Ready ; Subroutine Readies UART to Receive Character into RXTXData Buffer

Dept of ECE Page 44

Page 45: Micro Controller Manual

Microcontroller Lab Manual

;------------------------------------------------------------------------------- mov.w #8,BitCnt ; Load Bit Counter, 8 data bitsSetupRX 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 --> TXUART_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 bitRX_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;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<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 = 1TX_Space bis.w #OUTMOD2,&CCTL0 ; TX SpaceTX_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

Dept of ECE Page 45

Page 46: Micro Controller Manual

Microcontroller Lab Manual

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 ‘1’using a for loop.

Algorithm for Ramp wave generation:1. Output an initial value of 00H to P0 (for output at Yout use P1).

Dept of ECE Page 46

0

∏ 2∏t

0 t

Page 47: Micro Controller Manual

Microcontroller Lab Manual

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: Program for Stair case wave:#include <reg51xD2.h> #include <reg51xD2.h>main() main(){unsigned char i; {unsigned char i;while(1) while(1){for (i=0;i<0xff;i++) {for (i=0;i<0xff;i=i+0x33)P0=i; //use P1=I for yout P0=i;} //end of while } //end of while} //end of main } //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.

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>

Dept of ECE Page 47

0 t

∏ 2∏

Page 48: Micro Controller Manual

Microcontroller Lab Manual

main(){ unsigned char i;while(1){ for (i=0;i<0xff;i++)P0=i; //increasing rampfor (i=0xff;i>0;i--)P0=i; //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 θ=300. For different values of θ, the voltages are V=2.5V+2.5Vsinθ.

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.

Dept of ECE Page 48

t

Page 49: Micro Controller Manual

Microcontroller Lab Manual

Resolution of the DAC = Vmax =10V =19.5mV 2n 28

Angle θ (in degree)

Sin θ Vout

2.5V+2.5 Sin θDAC i/pVout/19.5m

0o 0 2.5 12830o 0.5 3.75 19260o 0.866 4.665 23890o 1 5 255120o 0.866 4.665 238150o 0.5 3.75 192180o 0 2.5 128210o -0.5 1.25 64240o -0.866 0.335 17270o -1 0 0300o -0.866 0.335 17330o -0.5 1.25 64360o 0 2.5 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.

Algorithm:1. Output a high value(say FFH=5V) on P0.2. Call delay.3. Output a low value(say 00H) on P04. Call delay.5. Repeat from step 5.

C- program for square wave:

#include<reg51xD2.h>void delay(unsigned int x) //delay subroutble

Dept of ECE Page 49

t2∏

0

Page 50: Micro Controller Manual

Microcontroller Lab Manual

{for(;x>0;x--);}

void main{unsigned char on=0xff; //define amplitude during on timeunsigned char off=0x00; // amplitude during off timewhile(1)

{P0=on; //make DAC output at xout=5Vdelay(300); //on- time delayP0=off; //generate the ‘low’ of square wavedelay(300); //for 75% duty cycle use delay(100)} //end of while} //end of main

STEPPER MOTOR

#include<REG52.h>//--------function declarationvoid MSDelay(unsigned int;) //Delayvoid send (unsigned char); //Serial data transfervoid main(){unsigned char rdata; //serially received dataunsigned char idata msg[8][30]={“Sitec Electronics”,

“Stepper Motor Test Program”, “Please choose an option”, “1. Clockwise rotation”, “2. Anti Clockwise rotation”, “3. Clockwise ‘xx’RPM”, “4. Clockwise ‘xx’RPM”, “5. Clockwise ‘xx’RPM”},

P1=0 ; //set port 1 as outputTMOD=0x20; //Timer 1,mode2(auto-reload)TH1=-3; //9600 baud rateSCON=0x50; //8-bit,1 stop,REN enabledTR1=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);

Dept of ECE Page 50

Page 51: Micro Controller Manual

Microcontroller Lab Manual

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; 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);

Dept of ECE Page 51

Page 52: Micro Controller Manual

Microcontroller Lab Manual

} 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; } 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)

Dept of ECE Page 52

Page 53: Micro Controller Manual

Microcontroller Lab Manual

#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{ 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

Dept of ECE Page 53

Page 54: Micro Controller Manual

Microcontroller Lab Manual

void MSDelay(unsigned int;) //Delayvoid send (unsigned char); //Serial data transfersbit enablr=P0^6; //variables declaration for ADCsbit mtr_1=P0^7;sbit mtr_2=P0^4;void main(){unsigned char rdata; //serially received data the messageunsigned char idata msg[8][30]={“Sitec Electronics”,

“DC Motor Test Program”,“Please choose an option”,“1. AntiClockwise rotation”,“2. Clockwise rotation”,“3. AntiClockwise ‘xx’RPM”,“4. AntiClockwise ‘xx’RPM”,“5. AntiClockwise ‘xx’RPM”},

P1=00 ; //set port 1 as outputTMOD=0x20; //Timer 1,mode2(auto-reload)TH1=-3; //9600 baud rateSCON=0x50; //8-bit,1 stop,REN enabledTR1=1; //start timer 1While(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;

Dept of ECE Page 54

Page 55: Micro Controller Manual

Microcontroller Lab Manual

} 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; 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; } } }

Dept of ECE Page 55

Page 56: Micro Controller Manual

Microcontroller Lab Manual

}//------- Delay Routinevoid MSDelay (unsigned int value) { unsigned int x,y; for (x=0;x<50;x++0 for(y=0;y<value;y++); }//--------------Data Transfer Routinevoid Send (unsigned char x) { SBUF=x; While(TI= =0); TI=0;}

(Short Programs)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()

{

Dept of ECE Page 56

Page 57: Micro Controller Manual

Microcontroller Lab Manual

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>

sbit rd = P1^1; //Variables declaration for ADCsbit wr = P1^2;sbit intr = P1^3;sbit aen = P1^0;sfr mydata1 = 0xA0; //ADC data

sfr ldata = 0x80; //Variables declaration for LCDsbit rs = P3^2;sbit rw = P3^3;sbit en = P3^4;sbit busy = P0^7;

//--------------Funtion declaration void convert_display(unsigned char);//Hex to Binary convertervoid lcdcmd (unsigned char value); //LCD command void lcddata (unsigned char value); //LCD datavoid lcdready (); //Initialization LCD 2 lines,5x7 matrixvoid MSDealy(unsigned int itime); //Delay

void main(){

unsigned char value; //ADC data recived

lcdcmd(0x38); //LCD command lcdcmd(0x0e);lcdcmd(0x01);lcdcmd(0x06);lcdcmd(0x83);lcddata('T'); //LCD datalcddata('e');

Dept of ECE Page 57

Page 58: Micro Controller Manual

Microcontroller Lab Manual

lcddata('m');lcddata('p');lcddata('e');lcddata('r');lcddata('a');lcddata('t');lcddata('u');lcddata('r');lcddata('e');lcddata('r');

mydata1 = 0xff; //Set port 2 as input ldata = 0x00; //Set port 0 as output

//---------ADC routine while (1)

{aen = 0; //enable ADC

wr = 0; //Write=0wr = 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)

{

Dept of ECE Page 58

Page 59: Micro Controller Manual

Microcontroller Lab Manual

lcdready(); //is LCD ready?ldata = value; //issue command coders = 0; //RS=0 for command

rw = 0; //R/W=0 to write to LCDen = 1; //E=1 for H-to-L pulseMSDealy(1);en = 0; //E=0 ,latch inreturn;}

//-------------LCD datavoid lcddata (unsigned char value)

{lcdready(); //is LCD ready?ldata = value; //issue data rs = 1; //RS=1 for datarw = 0; //R/W=0 to write to LCDen = 1; //E=1 for H-to-L pulseMSDealy(1);en = 0; //E=0, latch in return;

}

void lcdready (){

busy = 1; //make P1.7 input port rs = 0; //RS=0 access command reg rw = 1; //R/W=1 read command reg ;read command reg and check

busy flagwhile (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>

Dept of ECE Page 59

Page 60: Micro Controller Manual

Microcontroller Lab Manual

sfr ldata = 0x80; //Variables declaration for LCDsbit rs = P3^2;sbit rw = P3^3;sbit en = P3^4;sbit busy = P0^7;

//--------------Funtion declarationvoid convert_display(unsigned char);//Hex to Binary convertervoid 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','-','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);

Dept of ECE Page 60

Page 61: Micro Controller Manual

Microcontroller Lab Manual

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 ('+'):{ 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++);

}

Dept of ECE Page 61

Page 62: Micro Controller Manual

Microcontroller Lab Manual

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;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

Dept of ECE Page 62

Page 63: Micro Controller Manual

Microcontroller Lab Manual

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); //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;

Dept of ECE Page 63

Page 64: Micro Controller Manual

Microcontroller Lab Manual

}

//get the coloum pressesif (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 10d1 = value % 10; //save low digit

(reminder of division)d2 = x % 10;d3 = x / 10; //divide by 10

once moredata1 = d1 | 0x30; //make it ASCII

and save LSBdata2 = d2 | 0x30;data3 = d3 | 0x30;

lcddata(data3); //display converted output MSB first

lcddata(data2); lcddata(data1);

return; }

Elevator

#include <REG52.H> sfr ldata = 0x80; //Variables declaration for LCDsbit rs = P3^2;sbit rw = P3^3;sbit en = P3^4;sbit busy = P0^7;sbit carry = PSW^7;

//--------------Funtion declaration

Dept of ECE Page 64

Page 65: Micro Controller Manual

Microcontroller Lab Manual

void convert_display(unsigned char);//Hex to Binary convertervoid 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',

'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

Dept of ECE Page 65

Page 66: Micro Controller Manual

Microcontroller Lab Manual

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');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++;

Dept of ECE Page 66

Page 67: Micro Controller Manual

Microcontroller Lab Manual

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++);}

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;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;

Dept of ECE Page 67

Page 68: Micro Controller Manual

Microcontroller Lab Manual

}

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 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;

Dept of ECE Page 68

Page 69: Micro Controller Manual

Microcontroller Lab Manual

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 pressesif (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 10d1 = value % 10; //save low digit

(reminder of division)d2 = x % 10;d3 = x / 10; //divide by 10

once moredata1 = d1 | 0x30; //make it ASCII

and save LSBdata2 = d2 | 0x30;data3 = d3 | 0x30;

lcddata(data3); //display converted output MSB first

lcddata(data2); lcddata(data1);

Dept of ECE Page 69

Page 70: Micro Controller Manual

Microcontroller Lab Manual

return; } */

Dept of ECE Page 70