from numpy import abs,real, imag
from numpy.fft import fft
from pylab import stem,xlabel,ylabel,subplot
        from scipy.fft import fft,ifft
                 x1=[1,2,3]
               x2=[1,2,3,4,5]
                 n1=len(x1)
                 n2=len(x2)
              N=max (n1,n2)
                 X1=fft(x1)
                 X2=fft(x2)
                 print(X1)
                 print(X2)
                 Y=X1*X2
                  print(Y)
                  y=ifft(Y)
               subplot(3,1,1)
                  stem(x1)
               subplot(3,1,2)
                  stem(x2)
               subplot(3,1,3)
                  stem(y)
                  print(y)
                                      Microcontrollers Lab
Course Code: BECL456A
Course objectives: This course will enable students to:
      Understand the basic programming of Microcontrollers.
      Develop the 8051 Microcontroller-based programs for various applications using Assembly
       Language & C Programming.
      Program 8051 Microcontroller to control an external hardware using suitable I/O ports.
Note: Execute the following experiments by using Keil Microvision Simulator (any 8051
Microcontroller can be chosen as the target) and Hardware Interfacing Programs using 8051 Trainer Kit.
                                  I. Assembly Language Programming
Data Transfer Programs:
1 Write an ALP to move a block of n bytes of data from source (20h) to destination (40h) using Internal-
RAM.
2 Write an ALP to move a block of n bytes of data from source (2000h) to destination (2050h) using
External RAM.
 3 Write an ALP To exchange the source block starting with address 20h, (Internal RAM) containing N
(05) bytes of data with destination block starting with address 40h (Internal RAM).
 4 Write an ALP to exchange the source block starting with address 10h (Internal memory), containing n
(06) bytes of data with destination block starting at location 00h (External memory).
Arithmetic & Logical Operation Programs:
5 Write an ALP to add the byte in the RAM at 34h and 35h, store the result in the register R5 (LSB) and
R6 (MSB), using Indirect Addressing Mode.
6 Write an ALP to subtract the bytes in Internal RAM 34h &35h store the result in register R5 (LSB) &
R6 (MSB).
 7 Write an ALP to multiply two 8-bit numbers stored at 30h and 31h and store16- bit result in 32h and
33h of Internal RAM.
8 Write an ALP to perform division operation on 8-bit number by 8-bit number.
9 Write an ALP to separate positive and negative in a given array.
10 Write an ALP to separate even or odd elements in a given array.
11 Write an ALP to arrange the numbers in Ascending & Descending order.
12 Write an ALP to find Largest & Smallest number from a given array starting from 20h & store it in
Internal Memory location 40h.
Counter Operation Programs:
13 Write an ALP for Decimal UP-Counter.
14 Write an ALP for Decimal DOWN-Counter.
15 Write an ALP for Hexadecimal UP-Counter.
16 Write an ALP for Hexadecimal DOWN-Counter.
                                           II. C Programming
1 Write an 8051 C program to find the sum of first 10 Integer Numbers.
2 Write an 8051 C program to find Factorial of a given number.
3 Write an 8051 C program to find the Square of a number (1 to 10) using Look-Up Table.
4 Write an 8051 C program to count the number of Ones and Zeros in two consecutive memory locations.
                      III. Hardware Interfacing Programs
1 Write an 8051 C Program to rotate stepper motor in Clock & Anti-Clockwise direction.
2 Write an 8051 C program to Generate Sine & Square waveforms using DAC interface.
                                I. Assembly Language Programming
Data Transfer Programs:
   1. Write an ALP to move a block of n bytes of data from source (20h) to destination (40h) using
      Internal-RAM.
ORG 0000H
MOV R0, #0X20                       ; source location (X)
MOV R1, #0X40                      ; Destination location (Y)
MOV R2, #0X05                      ; ARRAY COUNT OF 05
BACK: MOV A, @ R0                  ; Read data from @ R0 source location (X)
MOV @ R1, A                        ; Transfer data to @R1 Destination location (Y)
INC R0                             ; Point to next source location (X)
INC R1                             ; Point to next Destination location (Y)
DJNZ R2, BACK                      ; Repeat data transfer until all the data are transfered
NOP                                ; No operation
NOP                                ; No operation
NOP                                ; No operation
RET                                ; Return to main program
END                                ; End of the program
   2. Write an ALP to move a block of n bytes of data from source (2000h) to destination (2050h) using
      External RAM.
ORG 0000H
MOV DPTR, #2000H                   ; Load dptr with source address
MOV R0, #50H                       ; R0 is loaded with destination lower byte
MOV R1, #20H                       ; R1 is loaded with destination upper byte
MOV R2, #05H                       ; R2 is loaded with block size
BACK: MOVX A, @DPTR ; Accumulator is loaded with content of address present in dptr
PUSH DPH                           ; Save dptr content into stack
PUSH DPL                           ; Save dptr content into stack
MOV DPH, R1                        ; Load dph with R1 destination location upper byte
MOV DPL, R0                        ; Load dpl with R0 destination location lower byte
MOVX @DPTR, A                      ; Move accumulator content to destination location
INC DPTR                           ; Increment dptr (Destination location)
MOV R1, DPH                        ; Store dph content in R1
MOV R0, DPL                        ; Store dpl content in R0
POP DPL                            ; Restore dpl content of source location
POP DPH                            ; Restore dph content of source location
INC DPTR                           ; Increment dptr (Source by 1)
DJNZ R2, BACK                      ; Decrement R2, if not ‘0’, jump to BACK
END                                ; End of the program
  3. Write an ALP To exchange the source block starting with address 20h, (Internal RAM) containing
     N (05) bytes of data with destination block starting with address 40h (Internal RAM).
ORG 0000H
MOV R0, #20H              ; R0, Starting address (X)
MOV R1, #40H              ; R1, Destination address (Y)
MOV R2, #05H              ; N=05, Array count
UP: MOV A, @R0            ; A= Content of address present in R0 register
XCH A,@R1                 ; Exchange A with content of address present in R1 register
MOV @R0, A                ; Move register A content to location Starting address (X)
INC R0                    ; Increment R0 (R0=R0+1)
INC R1                    ; Increment R1 (R1=R1+1)
DJNZ R2, UP               ; Decrement R2, if not ‘0’ jump to UP
END                       ; End of the program
4. Write an ALP to exchange the source block starting with address 10h (Internal memory), containing n
(06) bytes of data with destination block starting at location 00h (External memory).
ORG 0000H
MOV R0, #10H                        ; Load R0 with source location address
MOV DPTR, # 0000H                   ; Load dptr to hold destination address
MOV R2, #06H                        ; Load R2 with block size
BACK: MOVX A,@DPTR                  ; Load accumulator the content address present in dptr
MOV B,@R0                           ; Load B with internal memory content
XCH A, B                            ; Exchange A & B content
MOVX @DPTR, A                ; From accumulator, move the content to dptr address [external]
MOV @R0, B
INC R0
INC DPTR
DJNZ R2, BACK
END
Arithmetic & Logical Operation Programs:
   1. Write an ALP to add the byte in the RAM at 34h and 35h, store the result in the register R5 (LSB)
      and R6 (MSB), using Indirect Addressing Mode.
ORG 0000H
MOV R0,#34H            ; R0 LOAD WITH 34H
BACK: MOV A,@R0 ; READ FIRST NUMBER INTO ACCUMULATOR
INC R0                 ; INCREMENT R0
MOV B,@R0              ; READ SECOND NUMBER INTO B REGISTER
ADD A,B                ; PERFORM ADDITION OPERATION
JNC SKIP               ; JUMP TO SKIP IF NO CARRY GENERATED
MOV R6,#01             ;
SKIP: MOV R5,A         ; STORE SUM VALUE INTO R5
EXIT: SJMP EXIT        ; SPECIFIES LOGICAL END OF THE PROGRAM
END
  2. Write an ALP to subtract the bytes in Internal RAM 34h &35h store the result in register R5 (LSB)
     & R6 (MSB).
ORG 0000H
MOV R0,#34H           ; R0 LOAD WITH 34H
MOV A,@R0             ; READ FIRST NUMBER INTO ACCUMULATOR
INC R0                ; INCREMENT R0
MOV B, @R0            ; READ SECOND NUMBER INTO B REGISTER
MOV R6,#00H
SUBB A, B             ; PERFORM SUBTRACT OF A AND B
MOV R5, A             ; CONTENT OF A LOADED TO R5
JNC EXIT              ; JUMP TO EXIT IF NO CARRY
DEC R6                ; DECREMENT R6
EXIT: SJMP EXIT       ; SPECIFIES LOGICAL END OF THE PROGRAM
END
  3. Write an ALP to multiply two 8-bit numbers stored at 30h and 31h and store16- bit result in 32h
     and 33h of Internal RAM.
ORG 0000H
MOV R0,#30H           ; LOAD FIRST NUMBER ADDRESS INTO R0
MOV A,@R0             ; MOVE CONTENT OF 30H INTO ACCUMULATOR
INC R0                ; INCREMENT R0
MOV B,@R0             ; MOVE CONTENT FO 31H INTO REGISTER R0
MUL AB                ; MULTIPLY CONTENT OF A AND B
INC R0                ; INCREMENT R0
MOV @R0,A             ; MOVE LOWER BYTE INTO 32H
INC R0                ; INCREEMNT R0
MOV @R0,B             ; MOVE UPPER BYTE INTO 33H
END
  4. Write an ALP to perform division operation on 8-bit number by 8-bit number.
ORG 0000H
MOV R0,#30H           ; DIVIDEND INTO 30H
MOV A,@R0             ; MOVE DIVIDEND INTO ACCUMULATOR
INC R0                ; INCREMENT R0
MOV B,@R0             ; LOAD CONTENT 31H TO B(DIVSOR LOADED TO B)
DIV AB                ; A DIVIDED BY B
INC R0                ; INCREMENT R0
MOV @R0,A             ; MOVE QUOTIENT INTO 32H
INC R0                ; INCREMENT R0
MOV @R0,B             ; MOVE REMAINDER INTO 33H
END
   5. Write an ALP to separate positive and negative in a given array.
POSITIVE NUMBER
ORG 0000H
MOV R0,#30H            ; STARTING ADDRESS OF ARRAY
MOV R1,#50H            ; ADDRESS FOR STORING POSITIVE NUMBER
MOV R2,#05             ; ARRAY SIZE 5
BACK: MOV A,@R0 ; TAKE DATA FROM ADDRESS PRESENT IN R0
JB ACC.7,SKIP          ; IF MSB BIT OF ACCU IS SET JUMP TO SKIP
MOV @R1,A              ; IF MSB BIT 0TH BIT IS SET ,THEN MOVE IT TO ODD ARRAY
INC R1                 ; INCREMENT R1
SKIP: INC R0           ; INCREMENT R0
DJNZ R2,BACK           ; DECREMENT R2 AND JUMP IF NOT 0 TO BACK
END
NEGATIVE NUMBER
ORG 0000H
MOV R0,#30H            ; STARTING ADDRESS OF ARRAY
MOV R1,#50H            ; ADDRESS FOR STORING POSITIVE NUMBER
MOV R2,#05             ; ARRAY SIZE 5
BACK: MOV A,@R0 ; TAKE DATA FROM ADDRESS PRESENT IN R0
JNB ACC.7,SKIP         ; IF MSB BIT OF ACCU IS NOT SET JUMP TO SKIP
MOV @R1,A              ; IF MSB BIT 0TH BIT IS SET ,THEN MOVE IT TO ODD ARRAY
INC R1                 ; INCREMENT R1
SKIP: INC R0            ; INCREMENT R0
DJNZ R2,BACK           ; DECREMENT R2 AND JUMP IF NOT 0 TO BACK
END
   6. Write an ALP to separate even or odd elements in a given array.
SEPEARATE ODD NUMBERS
ORG 0000H
MOV R0,#30H            ; STARTING ADDRESS OF ARRAY
MOV R1,#50H            ; ADDRESS STORING ODD NUMBERS AFTER SEPARATION
MOV R2,#05H            ; ARRAY SIZE 5
BACK: MOV A,@R0 ; TAKE DATA FROM ADDRESS PRESENT IN R0
JNB ACC.0,SKIP         ; IF LSB 0TH BIT OF ACCU IS NOT SET JUMP TO SKIP
MOV @R1,A              ; IF LSB OTH BIT OF ACCU IS SET,THEN MOVE IT TO ODD   ARRAY
INC R1                 ; INCREMENT R1
SKIP: INC R0           ; INCREMENT R0
DJNZ R2,BACK           ; DECREMENT R2 AND JUMP IF NOT 0 TO BACK
END
SEPARATE EVEN NUMBERS
ORG 000H
MOV R0,#30H            ; STARTING ADDRESS OF ARRAY
MOV R1,#50H            ; ADDRESS STORING ODD NUMBERS AFTER SEPARATION
MOV R2,#05             ; ARRAY SIZE 5
BACK: MOV A,@R0 ; TAKE DATA FROM ADDRESS PRESENT IN R0
JB ACC.0,SKIP          ; IF LSB 0TH BIT OF ACCU IS SET JUMP TO SKIP
MOV @R1,A              ; IF LSB OTH BIT OF ACCU IS SET,THEN MOVE IT TO ODD   ARRAY
INC R1                ; INCREMENT R1
SKIP: INC R0          ; INCREMENT R0
DJNZ R2,BACK          ; DECREMENT R2 AND JUMP IF NOT 0 TO BACK
END
   7. Write an ALP to arrange the numbers in Ascending & Descending order.
ASCENDING ORDER
ORG 0000H
MOV R7,#04H           ; LOAD R7 WITH 04
MAIN: MOV R0,#30H ; LOAD R0 WITH SOURCE LOCATION ADDRESS
MOV R6,#04H           ; LOAD R6 WITH 04
UP: MOV A,@R0         ; MOVE ACC WITH CONTENT OF ADDRESS PRESENT IN R0
INC R0                ; INCREMENT R0
MOV B,@R0             ; MOVE B REGISTER WITH CONTENT OF ADDRESS PRESNET IN R0
CJNE A,B,NXET         ; COMPARE TWO DATA’S
NEXT: JC NOEXCHANGE ; IF A< B JUMP TP NOEXCHANGE
MOV @R0,A             ; IF A> B MOVE ACCU CONTENT TO ADDRESS POINTED BY R0
DEC R0                ; DECREMENT R0
MOV @R0,B             ; MOVE B REGISTER CONTENT TO ADDRESS PRESENT IN R0
INC R0                ; INCREEMENT R0
NOEXCHANGE: DJNZ R6,UP            ; DECREMENT R6, IF NOT EQUAL TO 0,JUMP TO UP
DJNZ R7,MAIN          ; DECREMENT R6, IF NOT EQUAL TO 0,JUMP TO MAIN
END
DECENDING ORDER
ORG 0000H
MOV R7,#04H           ; LOAD R7 WITH 04
MAIN: MOV R0,#30H ; LOAD R0 WITH SOURCE LOCATION ADDRESS
MOV R6,#04H      ; LOAD R6 WITH 04
UP: MOV A,@R0    ; MOVE ACC WITH CONTENT OF ADDRESS PRESENT IN R0
INC R0           ; INCREMENT R0
MOV B,@R0        ; MOVE B REGISTER WITH CONTENT OF ADDRESS PRESNET IN R0
CJNE A,B,NXET    ; COMPARE TWO DATA’S
NEXT: JNC NOEXCHANGE      ; IF A> B JUMP TO NOEXCHANGE
MOV @R0,A        ; IF A< B MOVE ACCU CONTENT TO ADDRESS POINTED BY R0
DEC R0           ; DECREMENT R0
MOV @R0,B        ; MOVE B REGISTER CONTENT TO ADDRESS PRESENT IN R0
INC R0           ; INCREEMENT R0
NOEXCHANGE: DJNZ R6,UP    ; DECREMENT R6, IF NOT EQUAL TO 0,JUMP TO UP
DJNZ R7,MAIN     ; DECREMENT R6, IF NOT EQUAL TO 0,JUMP TO MAIN
END
  8. Write an ALP to find Largest & Smallest number from a given array starting from 20h & store it in
     Internal Memory location 40h.
LARGEST NUMBER
ORG 0000H
MOV R0,#20H           ; LOAD R0 WITH STARTING ADDRESS OF ARRAY 20H
MOV R1,#05H           ; ARRAY SIZE 5
MOV B,#00H            ; INITIALLY ASSUME LARGEST NUMBER IS 00 AND STORE IN B
BACK: MOV A,@R0 ; MOVE CONTENT OF ADDRESS PRESENT IN R0 INTO ACCU
CJNE A,B,LOOP         ; COMPARE A AND B, JUMP TO LOOP
LOOP:JC LOOP1         ; IF B> A THEN JUMP TO LOOP1
MOV B,A               ; IF A>B THEN MOVE A CONTENT TO B
LOOP1: INC R0         ; INCREMENT R0
DJNZ R1,BACK          ; DECREMENT R1, IF R1 IS NOT 0, JUMP TO BACK
MOV 40H,B             ; MOVE CONTENT OF B TO 40H
END
Counter Operation Programs:
   1. Write an ALP for Decimal UP-Counter.
ORG 0000H
MOV A,#00H                ; LOAD ACCUMULATOR WITH 00H
BACK: MOV P0.A            ;ACC CONTENT IS MOVED TO PORT P0
ACALL DELAY               ; CALL DELAY SUBROUTINE PROGRAM
ADD A,#01H                ; A=A+1
DA A                      ; DECIMAL ADJUST ACCUMULATOR
SJMP BACK                 ; SHORT JUMP TO LABEL
DELAY: MOV R1,#09FH
L1: MOV R2,#0FFH
L2: MOV R3,#0FFH
L3: DJNZ R3,L3
DJNZ R2,L2
DJNZ R1,L1
RET
END
   2. Write an ALP for Decimal DOWN-Counter.
ORG 0000H
MOV A,#99H               ; LOAD ACCUMULATOR WITH 99H
BACK: MOV P0.A           ;ACC CONTENT IS MOVED TO PORT P0
ACALL DELAY              ; CALL DELAY SUBROUTINE PROGRAM
ADD A,#99H               ; A=A+99 99+99=198
DA A                     ; DECIMAL ADJUST ACCUMULATOR
SJMP BACK                ; SHORT JUMP TO LABEL
DELAY: MOV R1,#09FH
L1: MOV R2,#0FFH
L2: MOV R3,#0FFH
L3: DJNZ R3,L3
DJNZ R2,L2
DJNZ R1,L1
RET
END
   3. Write an ALP for Hexadecimal UP-Counter.
ORG 0000H
MOV A, #00H          ; LOAD ACCUMULATOR WITH 00H
BACK: MOV P0,A       ; ACC CONTENT IS MOVED TO PORT 0
ACALL DELAY          ;CAL DELAY SUBROUTINE PROFRAM
ADD A,#01H           ; A=A+1
SJMP BACK            ; SHORT JUMP TO LABEL
DELAY: MOV R3,#09FH
L1: MOV R2,#0FFH
L2: MOV R3,#0FFH
L3: DJNZ R3,L3
DJNZ R2,L2
DJNZ R1,L1
RET
END
   4. Write an ALP for Hexadecimal DOWN-Counter.
ORG 0000H
MOV A,#0XFFH        ; LOAD ACCUMULATOR WITH 00H
BACK: MOV P0.A      ; ACC CONTENT IS MOVED TO PORTP0
ACALL DELAY         ; CALL DELAY SUBROUTINE PROGRAMS
ADD A,#0FFH         ; A=A-01
SJMP BACK           ; SHORT JUMP TO LABEL
DELAY: MOV R3,#05FH
L1: MOV R2,#0FFH
L2: MOV R3,#0FFH
L3: DJNZ R3,L3
DJNZ R2,L2
DJNZ R1, L1
RET
END
                                          II. C Programming
1 Write an 8051 C program to find the sum of first 10 Integer Numbers.
#INCLUDE<REG51.H>
VOID MAIN()
UNSIGNED CHAR SUM =0;
UNSIGNED CHAR I;
FOR (I=1;I<=10;I++)
SUM=SUM+I;
ACC=SUM;
P0=SUM;
}
2. Write an 8051 C program to find Factorial of a given number.
#INCLUDE <REG51.H>
#INCLUDE<STDIO.H>
VOID MAIN ( )
UNSIGNED INT I;
UNSIGNED CHAR NUM=5;
UNSIGNED LONG FACTORIAL = I;
FOR ( I=1;I<=NUM; I++)
FACTORIAL= FACTORIAL*I;
P1=FACTORIAL;
P2=(FACTORIAL& 0XFF00) >> 8;
}
3. Write an 8051 C program to find the Square of a number (1 to 10) using Look-Up Table.
#INCLUDE <REG51.H>
VOID MAIN ( )
UNSIGNED CHAR LUT[]={1,4,9,16,25,36,48,64,81,100};
UNSIGNED CHAR NUM, SQUARE;
FOR (NUM=1; NUM<11; NUM++)
SQUARE=LUT [NUM-1];
P0=SQUARE;
WHILE (1);
}
4. Write an 8051 C program to count the number of Ones and Zeros in two consecutive memory
   locations.
#INCLUDE <REG51.H>
VOID MAIN ( )
UNSIGNED CHAR ARRAY [ ] = {0X57,0XFC};
UNSIGNED CHAR I, ONES, ZEROS;
CY=0;
FOR(I=0; I<8; I++)
ARRAY [ 0]>>1;
IF (CY==1) ONES++;
ELSE ZEROS++;
FOR(I=0; I<8; I++)
ARRAY [1]>>1;
IF(CY==1) ONES++;
ELSE ZEROS++;
P0=ZEROS;
P1=ONES;
WHILE (1);
         }
                                   III. Hardware Interfacing Programs
   1. Write an 8051 C Program to rotate stepper motor in Clock & Anti-Clockwise direction.
#include "at89c51ed2.h"
static bit Dir=0;
//sbit buzzer = P0^5;
void ChangeDir(void) interrupt 0 /* Int Vector at 000BH, Reg Bank 1 */
{
  Dir = ~Dir;               /* Complement the Direction flag */
 }
void delay(unsigned int x)          /* Delay Routine */
{
 for(;x>0;x--);
}
main()
{
  unsigned char Val,i;
  EA=0x1;                 /* Enable Interrupt flag and Interrupt 0 & Serial Interrupt */
  EX0=0x1;
  ES=1;      /*since the monitor is using the serial interrupt it has to be enabled*/
 while(1)
 {
  if(Dir)                     //* If Dir Clockwise
  {
   Val = 0x08;
   for(i=0;i<4;i++)
   {
    P0 = Val;              //* Write data for clock wise direction
    Val = Val >> 1;
    delay(10000);
    }
  }
  else                  // AntiClockwise Direction
  {
         Val = 0x01;
         for(i=0;i<4;i++)
         {
          P0 = Val;         // Write data for anticlock wise direction
          Val = Val<<1;
          delay(10000);
         }
        }
    }
}
     2. Write an 8051 C program to Generate Sine & Square waveforms using DAC interface.
SQUARE WAVE GENERATION
#include<at89c51xd2.h>
// variables used:
// msg : a character array contains the string to output
//     on to lcd
// cptr : character pointer used to point to msg array
//      required to pass to function lcd_outstr()
void delay(void);
//unsigned char xdata msg[]={"Square"};
//unsigned char xdata *cptr;
void main ()
{
  // the program is in a endless loop
  // ie it executes the code within while(1) for ever
  // program ouputs 0 and 0xff alternatively to port P0
  // with a delay in between
   // user can observe square waveform at dac output
 while(1)
  {
      P0 = 0x0;
      delay();
      P0 = 0xff;
      delay();
// function to generate delay
// input: none
// returns : none
void delay(void)
   {
   int i;
   for(i=0;i<=300;i++);
    }
                             Microcontroller Lab (BECL456A)
Sine wave
#include <at89c51xd2.h>
// variables used:
// sine_tab: array of sine values in hex for different angles.
//         See the attached note for how these values are derived.
// count : index variable for loop count
xdata unsigned char sine_tab[49]=
{ 0x80,0x90,0xA1,0xB1,0xC0,0xCD,0xDA,0xE5,0xEE,0xF6,0xFB,0xFE,
0xFF,0xFE,0xFB,0xF6,0xEE,0xE5,0xDA,0xCD,0xC0,0xB1,0xA1,0x90,
      0x80,0x70,0x5F,0x4F,0x40,0x33,0x26,0x1B,0x12,0x0A,0x05,0x02,
0x00,0x02,0x05,0x0A,0x12,0x1B,0x26,0x33,0x40,0x4F,0x5F,0x70,0x80}
;
idata int count;
void main ()
{
AITM ECE                               Page 28
                              Microcontroller Lab (BECL456A)
    // the program is in a endless loop
    // ie it executes the code within while(1) for ever
    // program ouputs different values correspoding
    // different sine angles in hex
    // to port P0
    // user can observe sine waveform at dac output
 while(1)
{
         for(count=0;count<49;count++)
      {
          P0 = sine_tab[count];
      }
     }
Triangular wave
#include <at89c51xd2.h>
AITM ECE                                Page 29
                              Microcontroller Lab (BECL456A)
// variables used:
// count : unsigned char to output the value to P0 port
idata unsigned char count;
void main ()
{
    // the program is in a endless loop
    // ie it executes the code within while(1) for ever
    // program ouputs 0 and 0x7f to port P0            and
    // 0x7f to 0 to port P0
    // user can observe triangular waveform at dac output
      while(1)
     {
     // output 0 to 0x7f   to P0 port
         for(count=0;count!=0xff;count++)
         {
             P0=count;
AITM ECE                                Page 30
                               Microcontroller Lab (BECL456A)
        // output 0x7f to 0   to P0 port
        for(count=0xff; count>0;count--)
        {
            P0=count;
}
Ramp wave
#include <at89c51xd2.h>
// variables used:
// count : character variable used to output count to digital input
idata unsigned char count;
void main ()
AITM ECE                                 Page 31
                               Microcontroller Lab (BECL456A)
// initialise count varaible to 0x0
        count = 0x0;
    // the program is in a endless loop
    // ie it executes the code within while(1) for ever
    // program ouputs 0 to 0xff to port P0
    // it increments the count variable from 0 to 0xff
    // user can observe ramp waveform at dac output
    while(1)
    {
            P0 = count;    // output count to Port P0
             count++;     // increment count
        }
}
DC motor
#include <at89c51xd2.h>
sbit DC1 = P0^6;
sbit DC2 = P0^7;
AITM ECE                                 Page 32
                             Microcontroller Lab (BECL456A)
idata unsigned char off_time,on_time;
idata unsigned int i=0;
void main ()
{
                   off_time = 50;
                   on_time = 10;
                   //DC2= 1;                                  //For clockwise
direction
                   DC2= 0;                                    //For anticlockwise
direction
                   while(1)
                   {
                   DC1 = 0;
                   for(i=0;i<on_time;i++)
                   {
                          TL0 = 0x00;                         //timer count set for
2msec
                          TH0 = 0xF8;
                          TR0 =1;                             // Timer-0 ON
AITM ECE                               Page 33
                   Microcontroller Lab (BECL456A)
                while(!TF0);                        // Wait untill Timer-0
Overflows
                TF0 = 0;                    // Clear Overflow flag
                TR0=0;                              // Timer-0 OFF
                on_time=on_time+10;
            }
                if(on_time>30)
                on_time=10;
                DC1 = 1;
                for(i=0;i<off_time;i--)
            {
                TL0 = 0x00;                         //timer count set for
2msec
                TH0 = 0xF8;
                TR0 =1;                             // Timer-0 ON
                while(!TF0);                        // Wait untill Timer-0
Overflows
                TF0 = 0;                    // Clear Overflow flag
                TR0=0;                              // Timer-0 OFF
                off_time=off_time-10;
            }
                if(off_time<10)
AITM ECE                     Page 34
                  Microcontroller Lab (BECL456A)
               off_time=30;
           }
           }
AITM ECE                    Page 35