Monday, May 13, 2013

Pulse Width Modulation(PWM) using 8051

PWM has many applications in real world , for example:  to control the speed of a DC motor we use PWM signal. Here we are generating a PWM signal having  250ms , 125ms and 50ms pulse widths.


#include<reg51.h>
void T0delay(void);
sbit Mybit=P1^0;
// output PWM port
void main(void)
{
unsigned char x;
while(1)
{
Mybit=1;
for(x=0;x<10;x++) 
// generating 250ms delay for ON time
{
T0delay();
}
Mybit=0;
for(x=0;x<10;x++) 
// generating 250ms delay for OFF time
{
T0delay();
}
Mybit=1;
for(x=0;x<5;x++) 
// generating 125ms delay for ON time
{
T0delay();
}
Mybit=0;
for(x=0;x<5;x++) 
// generating 125ms delay for OFF time
{
T0delay();
}
Mybit=1;
for(x=0;x<2;x++) 
// generating 50ms delay for ON time
{
T0delay();
}
Mybit=0;
for(x=0;x<2;x++) 
// generating 50ms delay for OFF time
{
T0delay();
}
}
}

// 25ms delay function
void T0delay(void)
{
TMOD=0X01;
// timer-0 mode-1
TL0=0xFE;
TH0=0xA5;
TR0=1;
while(TF0==0);
TR0=0;
TF0=0;
}




Result: to see output

Pulse Generator using 8051


Providing Delay using internal timers of AT89c51. We are generating a pulse wave having 250ms ON time and 250ms OFF time. microcontroller clock time is 1.085us.
                                             25ms / 1.085us = 23041 = A5FE in hex
                                             25ms x 10 = 250ms                                             

#include<reg51.h>
void T0delay(void);
// delay function
sbit Mybit=P2^0;
// output port
void main(void)
{
unsigned char x;
while(1)
{
Mybit= ~Mybit;
// inverter
for(x=0;x<10;x++)
T0delay();
}
}
void T0delay(void)
{
TMOD=0x01;
// selecting timer-0 mode-1
TL0=0xFE;  
// count starts at A5FE
TH0=0xA5;
TR0=1;       
// timer-0 run
while(TF0==0);
//wait until timer-0 flag overflow
TR0=0;
// then stop timer-0
TF0=0;
// reset the overflow flag
}





  


Result:     to see output

Serial LEDs using 8051


connecting LEDS to P2 pins and SWITCHES to P1 pins.

#include<reg51.h>
sfr leds=0xA0;
sfr SW=0x90;
unsigned int x;
int main ()
{

while(1)
{
if(SW==0xFE)
{
      leds=
0x08 ;                       //"1000"
    for( x=0;x<20000;x++);      
// providing some random delay    
    leds=0x04;                          //"0100"
    for( x=0;x<20000;x++);
    leds=0x02;                          //"0010"
    for( x=0;x<20000;x++);
    leds=0x01;                          //"0001"
    for( x=0;x<20000;x++);
}
else if(SW==0xFD)
{   
    leds=0x01;                          //"0001"

    for( x=0;x<20000;x++);
    leds=0x02;                          //"0010"
    for( x=0;x<20000;x++);
    leds=0x04;                          //"0100"
    for( x=0;x<20000;x++);
    leds=0x08;                          //"1000"
    for( x=0;x<20000;x++);
 }
else if(SW==0xFB)
{   
    leds=0x09;                        //"1001"

    for( x=0;x<20000;x++);
    leds=0x06;                       //"0110"
    for( x=0;x<20000;x++);
  }
else if(SW==0xF7)
{
    leds=0x06;                      //"
0110"
    for( x=0;x<20000;x++);
    leds=0x09;                     //"1001"
    for( x=0;x<20000;x++);
}
else
    leds=0x00;
}
}

 
  to see output
  

Sunday, May 12, 2013

Wednesday, April 3, 2013

Automatic Room light Controller using OpAmp:

It is more important to save power. here is the one way to save the power by automatic controlling of room lights. This is also an application of OpAmp based comparator. This is based on the light intensity in the room, here we use LDR(light dependent resistor) who's resistance decreases with increase in room light intensity. There is a choice that you can change the threshold level to your desired level so that it can operate at that intensity level. It does not require any microcontroller or any other programmable devices.

components procured:
>LDR  
>OpAmp 741
>resistor 10k
>BC547 transistor
>SPDT relay 12V
>1N4001 diode 
>battery and a bulb

 here i connected the LDR output to the inverting terminal and a threshold voltage of 1.5v is connected to the non-inverting terminal. so that whenever the light intensity increases the LDR output will decrease and if it is less than the threshold the output of the opamp will be +Vsat=+5v.This will trun on the BC547 so that relay will be connected as shown here, bulb will not glow
 VLDR >1.5v ; transistor ON; Light OFF;       LDR sensor         
 when the room light intensity decreases LDR resistance will decrease and voltage across the resistor goes high. when it is greater than the threshold opamp output goes to -Vsat=-5v. then the BC547 will goes to the cutoff region (OFF state). so the light will glow.
                                  VLDR < 1.5v ; transistor OFF; light ON;               SPDT Relay

ADC and DAC using MATLAB

         Here we add some Gaussian noise to the input sine wave and then will will convert that to digital signal. This digital signal sampled data will be used in Modelsim. In the modelsim we develop moving average filter using VHDL, this will filters the sampled data and writes into another file. Using the new updated sampled data we will regenerate the Analog signal.
 
                  

%ADC & DAC
clear all;
close all;
clc;
fs=500000;
% taking sampling frequency as 500kHz
fm=10000;
% input signal frequency 10kHz
t=1:200;     % displaying 200 samples
x=5*cos(2*pi*(fm/fs)*t);
%input sinusoidal signal
z=awgn(x,1);
% adding white Gaussian noise to the input signal with S/N=1
h=1:1000;
plot(t,x,'g','LineWidth',2);
% plotting input signal
hold on;
plot(t,z,'r','linewidth',1.5);
% plotting noisy signal
hold on;
stem(t,z);
hold on;     
Vd=-5:0.0390625:5; % step size =0.0390625, when n=8 bits
for i=1:256  
Vdelta(i)=(Vd(i)+Vd(i+1))/2;
% Quantization levels
end

 i=0:255;
binary= dec2bin(i);
% decimal to binary conversion
% Quantization of input signal

for i=1:200
    for j=1:256
        if(z(i)< Vd(1))
            z(i) = Vdelta(1);
        end
        if (z(i) > Vd(257))
            z(i) = Vdelta(256);
        end
        if(z(i) <= Vd(j+1) && z(i) >= Vd(j))
            z(i) = Vdelta(j);
        end
    end
end

% Encoding the Quantized data
for i=1:200
    for j=1:256
        if (z(i)==Vdelta(j))
            B_data(i,1:8) = binary(j,1:8);
        end
    end
end
% representing binary data in decimal
figure
for i=1:200
 B(i)=bin2dec(B_data(i,1:8));    
end


% First solution; writing Encoded data into ADC.txt file. The we will perfom
% Moving average filter operation in VHDL
f = fopen('ADC.txt', 'w');
for n = 1:200
    fprintf(f, '%s\n', B_data(n,1:8));
end
fclose(f);
subplot(221);
plot(x);
title('original sinwave','fontsize',12);
xlabel('--->time in 2us');
ylabel('--->amplitude in volts');
subplot(222);
plot(z);
title('noise signal','fontsize',12);
xlabel('--->time in 2us');
ylabel('--->amplitude in volts');
% After the moving avg filter the filtered data has been written to vhdl_out.txt file
f=fopen('vhdl_out.txt','r');
A = fscanf(f,'%g',[1 inf]);
fclose(f);

subplot(224)
plot(B)
title('signal with white gaussian noise','fontsize',12);
xlabel('--->time in 2us');
ylabel('--->amplitude in decimal');
%Digital to Analog conversion
for i=1:192
    for j=1:256
      if(A(i)== j )
          outpt(i)=Vdelta(j);
      end
    end
end

subplot(223);
plot(outpt);
title('filtered sine wave sinewave output','fontsize',12);
xlabel('--->time in 2us');
ylabel('--->amplitude in decimal');

RESULT: 
Output data files 

ECG Filtering using MATLAB


 GoldStandard.mat
 % fourth order bandpass filter
 % GoldStandard.mat is a preloaded database ECG signal
 % the original signal is first combined with gaussian noise
 % after noise added the signal will pass through 0.03Hz-1.1Hz bandpass
 % filter,is a 4th order filter

 clear all;
 close all;
 load('GoldStandard.mat')
 subplot(211);
 plot(signal);
 sound('GlodStandard.mat');
 title('the original ECG signal');
 necg=awgn(signal,1,'measured');
 b1=[1 0 -1];
 a1=[1 -1.9955735726528454        0.99558400680448189        ];
 bp1=0.049039538429966834       *filter(b1,a1,necg);
 b2=[1 0 -1];
 a2=[1 -1.8603604222618464        0.87003045759154718        ];
 bp2=0.049039538429966834      *filter(b2,a2,bp1);
 subplot(212);
 plot(bp2);
 title('after filter');
 figure
 subplot(211);
 plot(necg);
 title('after noise adding');
 subplot(212);
 plot(bp1);
 title('after 1st section filter');


RESULT: