Thursday 23 April 2009

Reversing a String

;8086 program to reverse the entered string

data segment
msg1 db "Enter the string: $"
msg2 db 0ah,0dh,"Reversed string is: $"
ent db 0dh,0ah,"$"
str1 db 100 dup(?)
rev db 100 dup(?)
data ends

print macro msg
lea dx,msg
mov ah,09h
int 21h
endm

read macro
mov ah,01h
int 21h
endm

code segment
start:
mov ax,data
mov ds,ax

mov bx,0000h
lea si,str1
print msg1

L1:
read
mov [si],al
inc bx
inc si
cmp al,0dh
jnz L1


dec si
mov cx,bx
lea di,rev

L2:
mov al,[si]
mov [di],al
inc di

dec si
dec cx
jnz L2

print msg2
add si,bx

mov cx,bx
print ent

L3:
mov dl,[si]
mov ah,02h
int 21h
dec si
dec cx
jnz L3


en:

mov ah,4ch
int 21h
code ends
end start

;Program ends Here

querrymail@gmail.com

Getting an Input from Keyboard and Displaying it

;8086 program to get an Input from Keyboard and Displaying it

data segment
msg db "Enter a character: $"
msg1 db 0dh,0ah, "The Entered character is:$"
data ends
print macro msg
lea dx,msg
mov ah,09h
int 21h
endm
read macro
mov ah,01h
int 21h
endm
display macro
mov ah,02h
int 21h
endm
code segment
start:
mov ax,data
mov ds,ax
print msg
read
mov bl,al
print msg1
mov dl,bl
display
mov ah,4ch
int 21h
code ends
end start

;Program Ends Here

querrymail@gmail.com

Display a Message on DOS Prompt

This 8086 program compiled in MASM compiler displays the word welcome on the screen.

;Program Starts Here

msg1 db "hello$"
data ends
code segment
start:
mov ax,data
mov ds,ax
lea dx,msg1
mov ah,09h
int 21h
mov ah,4ch
int 21h
code ends
end start

;Program Ends Here

data segment

querrymail@gmail.com

Electronic Rain Guage

CIRCUIT DESCRIPTION

Electronic rain gauge can measure the rainfall of a day. The circuit consists of a sensing section, Micro controller section and a display section.

Sensing section

The sensing section includes a uniform cylindrical collector, 9V dc battery, the voltage regulator IC 7805 and the NOT gate IC 4069N. The 9V DC battery and 7805 regulator provide VCC supply of +5V. The connecting wires are used as the sensors. We use 7 wires and are cut 1cm apart and the longest wire is connected from VCC. fixed at the bottom base of the collector by drilling. Rest of six sensing wires are also fixed 1 cm apart from the base by drilling for recording 5cm and above rainfall and these wires are connected to corresponding inputs of the 6 NOT gates.



If there is no rain, collector is empty and no high signal is at the input of NOT gates. When the water level of collector reaches 1 cm, then the VCC wire at the base and first sensing wire fixed at 1 cm of collector is shorted. Now a high signal (+5V) reaches the input of the first NOT gate. Thus the output of the gate becomes low (zero) Similarly when water level at 2 cm of collector, second sensing wire also got connection with the VCC. Now out put of corresponding not gate also come low. Since the capacity of our rain gauge is 6 cm when the water reaches the top of the collector all the sensing wires are connected with VCC. Now all the inputs of NOT gates are feed from VCC. , out put will become low.

Micro controller section

In the rain gauge we use PIC 16F877A micro controller. It is clocked by crystal oscillator for getting maximum operating Frequency of 20 MHz. It also get supply from the same +5V VCC. Here D port is assigned as input port and C as the out put port.

The output from the NOT gates are connected to the input port lines. ie. out put of the 1st Not gate is connected to Port line D0 , Second gate to D1, like wise 6th gate to D5. All the D ports are set initially. When no rain, there is no change of out put from the NOT gate and initial condition of the D port remains as such; ie. all are set.

When water level rises to 1 cm the out put of first NOT gate become low and low signal reaches the port D0 and it is reset. (ie. Become zero). Similarly when water level at 2 cm the out put of second NOT also become low and gradually port D1, is reset. This will continue up to the capacity of the gauge. Ie. When water reaches top level then out put of all NOT gates will be zero and, the ports D0,D1,D2,D3,D4,D5 are set.



Display Section


Display section include 16 x 2 LCD. The LCD contrast is adjusted by using a 10K port. Here LCD is interfaced with PIC 16F877A. In spite of assigning Port C as out put the port A is also assigned as output by resetting it. Now from Port C connections are made to LCD for data part and from port A connections are made for control part ie. Port A2 is connected to the 4th Pin (RS pin) of LCD to Select command or data register, and A5 is connected to 6th pin (E Pin) to disable or enable LCD. Port C0 to C4 are connected to LCD for display data part.


PIC 16F877A and LCD are interfaced and programmed such that, LCD should display the required information about the rain fall. LCD is 16 x 2, the first line of LCD is made permanent display as “RAIN GAUGE”, using program. Second line is for display information about rain fall. The PIC is [ programmed such a way that, when port D is in unchanged stage ie. all are set then LCD will display LOW/NO RAIN. When port D0 become Low (Zero) then “1 cm Rain” will be displayed on LCD. Similarly when D1, is reset, then LCD will display “2cm Rain”. Likewise when water level reaches top of the collector, all D0.,D1,D2,D3,D4 and D5 Ports are reset, then it is programmed such that “Heavy rain” will be the display.

Thus sensor section, PIC section and display section, perform their work synchronically to display the accurate rainfall and do its duty as a “Electronic Rain Gauge”.

Program

;Program Starts Here

#include <16F877a.h>
#include
#include
main()
{ char no[]="Low/No ",hi[]="Heavy ",cm[]=" cm ",clr[]=" ";
char po[]="Rain Guage",ra[]="Rain";
int val,set,i;
trisd=0xff;
lcdinit();
///misc
go(0);
printc(po);
delay_ms(300);
ra2=0;
for(i=0;i<5;i++)
{
portc=0x1c;
ok();
delay_ms(500);
}
for(i=0;i<3;i++)
{
portc=0x18;
ok();
delay_ms(500);
}
for(i=0;i<4;i++)
{

portc=0x8;
ok();
delay_ms(500);
portc=0x0C;
ok();
delay_ms(1000);
}
portc=1;
ok();
ra2=1;
///end
// position 0, 1... first row, 64,65,....Second row
go(3);
printc(po);

while(1)

{


// delay_ms(1000);
val=portd;

switch(val)
{
case 0xfe:
printc(clr);
go(67);
printh(1); //1
printc(cm);
printc(ra);
break;
case 0xfc:
printc(clr);
go(67);
printh(2);
printc(cm);
printc(ra); //2
break;

case 0xf8:
printc(clr);
go(67);
printh(3);
printc(cm);
printc(ra); //3
break;
case 0xf0:
printc(clr);
go(67);
printh(4);
printc(cm);
printc(ra); //4
break;
case 0xe0:
printc(clr);
go(67);
printh(5);
printc(cm); //5
printc(ra);
break;
case 0xc0:
printc(clr);
go(67);
printh(6);
printc(cm); //6
printc(ra);
break;
case 0x80:
printc(clr);
go(67);
printc(hi); //7
printc(ra);
break;

default:
printc(clr);
go(67);
printc(no); //No
printc(ra);
}
}
}

;Programmed in C
;Program Ends Here

Source: We got this idea from EFY Magazine.

PC Based Oscilloscope Using Matlab


Visit this link:

http://www.mathworks.com/matlabcentral/fileexchange/13165

You can download a .zip file. Unpack and use the oscilloscope.
But it uses your PC's sound card as interface. And also its frequency range is limited to 20Hz-20kHz. Higher frequencies can damage your sound card.


Octave: Open Source Alternative for Scilab



Octave, a freely redistributable software, is used for numerical computation with an interactive environment. Students pick up the basics quickly, comfortably using it within just a few hours. High-level language application intended primarily for numerical computations and other numerical experiments using a language that is compatible with Matlab. It is customizable with user-defined functions written in Octave's language or C++, C, Fortran or others.


For more:

http://www.osalt.com/octave

querrymail@gmail.com

Built-in Functions in Scilab

Most built in functions are identical in Matlab and Scilab. Some of them have a slightly different syntax. Here is a brief, partial list of commands with significant different syntax.

Matlab Scilab "equivalent"

all and

any or

balance balanc

clock unix('date')

computer unix_g('machine')

cputime timer

delete unix('rm file')

dir unix_g('ls')

echo mode

eig spec or bdiag

eval evstr

exist exists + type

fclose file('close')

feof

ferror

feval evstr and strcat

filter rtitr

finite (x < %inf)
fopen file('open') fread read fseek file ftell fwrite writeb global home isglobal isinf(a) a == %inf isnan(a) a ~= a isstr(a) type(a) == 10 keyboard pause + resume lasterr lookfor apropos more lines pack stacksize pause halt qz gspec+gschur randn rand rem modulo setstr code2str strcmp(a,b) a == b uicontrol uimenu getvalue unix unix_g version which whereis nargin [nargout,nargin]=argn(0) nargout

You will get a detailed tutorial on Scilab from: http://www-irma.u-strasbg.fr/~sonnen/SCILAB_HELP/frame.html

querrymail@gmail.com

Scilab: An Alternative to MATLAB

What is Scilab?

Developed at INRIA, SCILAB has been developed for system control and signal processing applications. It is freely distributed in source code format. The following points will explain the features of SCILAB. It is a similar to MATLAB, which is not freely distributed. It has many features similar to MATLAB.
Scilab is made of three distinct parts:

  • An interpreter
  • Libraries of functions (Scilab procedures)
  • Libraries of Fortran and C routines.

SCILAB has an inherent ability to handle matrices (basic matrix manipulation, concatenation, transpose, inverse etc.,)

Scilab has an open programming environment where the creation of functions and libraries of functions is completely in the hands of the user.
Scilab has an open programming environment where the creation of functions and libraries of functions is completely in the hands of the user

Download Scilab:
http://www.scilab.org/download/index_download.php?page=release#windows

What are the main differences between Scilab and MATLAB?
Functions

Functions in Scilab are NOT Matlab m-files but variables. One or several functions can be defined in a single file (say myfile.sci). The name of of the file is not necessarily related to the the name of the functions. The name of the function(s) is given by

function [y]=fct1(x)
...
function [y]=fct2(x)
...

The function(s) are not automatically loaded into Scilab. Usually you have to execute the command getf("myfile.sci") before using it.

Functions can also be defined on-line (or inside functions) by the command deff.

To execute a script file you must use exec("filename") in Scilab and in Matlab you just need to type the name of the file.
Comment lines

Scilab comments begins with: //

Matlab comments begins with: %

Variables

Predefined variables usually have the % prefix in Scilab (%i, %inf, ...). They are write protected.

Strings

Strings are considered as 1 by 1 matrices of strings in Scilab. Each entry of a string matrix has its own length.

Boolean variables

Boolean variables are %T, %F in Scilab and 0, 1 in Matlab. Indexing with boolean variables may not produce same result. Example x=[1,2];x([1,1]) [which is NOT x([%T,%T])] returns [1,1] in Scilab and [1,2] in Matlab. Also if x is a matrix x(1:n,1)=[] or x(:)=[] is not valid in Matlab.

Polynomials

Polynomials and polynomial matrices are defined by the function poly in Scilab (built-in variables). They are considered as vectors of coefficients in Matlab.

Empty matrices

[ ]+1 returns 1 in Scilab and [ ] in Matlab.

Plotting

Except for the simple plot and mesh (plot3d) functions, Scilab and Matlab are not compatible.

Scicos

Scicos (Scilab) and Simulink (Matlab) are not compatible.

querrymail@gmail.com


IR Tracking Robot

I got idea from EFY magazine and implemented along with my friends. It works well.

AMV: A Monostable Multivibrator

The robot described here senses a 38 kHz IR radiations and moves towards that direction. The system consists of three sections viz., sensor, controller and driver. The sensing section detects the 38 kHz IR radiation. The controller section processes the information from the sensor and provides the input to the driver section which has stepper motors for driving the robot.

The output of the sensors is fed to the monostable multivibrator which serves as the input to the microcontroller. Depending on the input sequence obtained the microcontroller performs sequential operation and gives out its decisions, which is a sequence of bits to drive stepper motors.

Since the microcontroller output is not sufficient to drive the stepper motor, a high voltage, high current Darlington array has been used to drive the motors. In the process of reaching the target, if an obstacle is encountered, the robot changes its path and again starts tracking the incoming IR radiation.

Robot Sensing Section


Robot Driving Section







Program

;Program Starts Here
$MOD51
ORG 0000H
CLR A
MOV P1, A
MOV P2, A
MOV P3, #0FFH
MOV R1, #11H
MOV R2, #11H
BACK: MOV C, P3.2
JB P3.0, NEXT
CPL C
NEXT: ANL C, P3.1
JC STRAIGHT
MOV C, P3.1
ANL C, P3.2
CPL C
ANL C, P3.0
JC LEFT
MOV C, P3.2
ORL C, /P3.1
ANL C, /P3.0
JC RIGHT
LJMP BACK
LEFT: MOV R3, #04H
FIRST: MOV A, R2
MOV P2, A
ACALL DELAY
RL A
MOV R2, A
DJNZ R3, FIRST
LJMP BACK
RIGHT: MOV R4, #04H
SECOND: MOV A, R1
MOV P1, A
ACALL DELAY
RR A
MOV R1, A
DJNZ R4, SECOND
LJMP BACK
STRAIGHT: MOV R3, #04H
THIRD: MOV A, R1
MOV P1, A
RR A
MOV R1, A
MOV A, R2
MOV P2, A
RL A
MOV R2, A
ACALL DELAY
DJNZ R3, THIRD
LJMP BACK
DELAY: MOV R6, #64
H1: MOV R7, #255
H2: DJNZ R7, H2
DJNZ R6, H1
RET
END

;Program Ends Here

Source: EFY Magazine

querrymail@gamail.com

Thursday 16 April 2009

Analyzing a .wav file

In the previous article we found how to create a simple audio music file. Here, we are going to analyze it, i.e., to find out the fundamental frequency and other parameters. The code given below plots the .wav file and it power spectrum.

%Code Starts here

[y, Fs] = wavread(file); % y is sound data, Fs is sample frequency.
t = (1:length(y))/Fs; % time

ind = find(t>0.1 & t<0.12); % set time duration for waveform plot
figure; subplot(1,2,1)
plot(t(ind),y(ind))
axis tight
title(['Waveform of ' file])

N = 2^12; % number of points to analyze
c = fft(y(1:N))/N; % compute fft of sound data
p = 2*abs( c(2:N/2)); % compute power at each frequency
f = (1:N/2-1)*Fs/N; % frequency corresponding to p

subplot(1,2,2)
semilogy(f,p)
axis([0 4000 10^-4 1])
title(['Power Spectrum of ' file])

%Code ends here

querrymail@gmail.com

Generate Music using MATLAB

This is an interesting, but simple experiment which has many utility. The following code can genrate a .wav file to play a sound of required frequency.

Before proceeding you should set the following:
filename---> give a name to your fantastic music file (say 'mymusic.wav')
f-----------> fundamental frequency (say f=256) in Hz.
d----------> time duration (say t =5) in seconds.
p----------> it is vector of amplitudes of length n. (say it values be [1 0.8 0.1 0.04]

%Code starts here

Fs=22050; nbits=8; % frequency and bit rate of wav file
t = linspace(1/Fs, d, d*Fs); % time
y = zeros(1,Fs*d); % initialize sound data
for n=1:length(p);
y = y + p(n)*cos(2*pi*n*f*t); % sythesize waveform
end
y = .5*y/max(y); % normalize. Coefficent controls volume.
wavwrite( y, Fs, nbits, filename)

%Code ends here

querrymail@gmail.com

Addition of two numbers

data segment
msg1 db "Enter the first 2 digit number: $"
msg2 db 0dh,0ah,"Enter the second 2 digit number: $"
msg3 db 0dh,0ah,"Sum = $"
data ends

print macro msg
lea dx,msg
mov ah,09h
int 21h
endm

read macro
mov al,01h
int 21h
endm

display macro num
mov dl,num
mov al,02h
int 21h
endm

code segment
start:mov ax,data
mov ds,ax

print msg1
read
mov bh,al
read
mov bl,al
print msg2
read
mov ch,al
read
mov cl,al

mov ax,bx
add al,cl
aaa
mov bl,al
mov al,ah
add al,ch
aaa
mov cx,ax

add cl,30h
add ch,30h
add bl,30h
print msg3
display ch
display cl
display bl

code ends
end start

querrymail@gmail.com

Palindrome

This is a program in 8086 ASM to check the given string is palindrome or not.
The program is compiled in MASM compiler.

data segment
msg1 db "Enter the string: $"
msg2 db 0ah,0dh,"Reversed string is: $"
msg3 db 0dh,0ah,"Not palindrome...........$"
msg4 db 0dh,0ah," palindrome...........$"
ent db 0dh,0ah,"$"
str1 db 100 dup(?)
rev db 100 dup(?)
data ends

print macro msg
lea dx,msg
mov ah,09h
int 21h
endm

read macro
mov ah,01h
int 21h
endm

code segment
start:
mov ax,data
mov ds,ax
mov bx,0000h
lea si,str1
print msg1

L1:
read
mov [si],al
inc bx
inc si
cmp al,0dh
jnz L1


dec si
mov cx,bx
lea di,rev

L2:
mov al,[si]
mov [di],al
inc di

dec si
dec cx
jnz L2

print msg2
add si,bx

mov cx,bx
print ent

L3:
mov al,[si]
mov dl,al
mov ah,02h
int 21h
dec si
dec cx
jnz L3

sub di,bx
mov cx,bx

L4:
dec cx
jz p
inc si
inc di
mov al,[si]
cmp al,[di]
jz L4
print msg3 ;not paliandrome
jmp en

p:
print msg4 ;paliendrome
en:
mov ah,4ch
int 21h
code ends
end start

ret


querrymail@gmail.com

Multiplication

This 8086 program multiplies two numbers.

data segment
msg1 db "Enter the first number: $"
msg2 db 0dh,0ah, "Enter the second number: $"
msg3 db 0dh,0ah, "Product= $"
data ends

print macro msg
lea dx,msg
mov ah,09h
int 21h
endm

read macro
mov ah,01h
int 21h
endm

display macro num
mov dl,num
mov ah,02h
int 21h
endm

code segment
start:mov ax,data
mov ds,ax
print msg1 ;read 1st number
read
mov bh,al
read
mov bl,al
print msg2 ;read 2nd number
read
mov ch,al
read
mov cl,al
sub bx,3030h
sub cx,3030h
mov dl,bh
rol dl,4
add bl,dl
mov dl,ch
rol dl,4
add cl,dl
and bx,00ffh
and cx,00ffh
mov ax,bx ;multipilication starts
mul cx
mov bx,dx
mov cx,ax
add bx,3030h
add cx,3030h
print msg3
display bh
display bl
display ch
display cl
mov ah,4ch
int 21h
code ends
end start


querrymail@gmail.com

Wednesday 8 April 2009

Square Root

;8086 program that computes the square root of a perfect square number

data segment
msg1 db "Enter the number:$"
msg2 db 0ah,0dh, "The entered number is not a perfect square$"
msg3 db 0ah,0dh, "Square root is:$"
data ends

print macro msg
lea dx,msg
mov ah,09h
int 21h
endm

read macro
mov ah,01h
int 21h
sub al,30h
mov bh,0ah
mul bh
mov bl,al
mov ah,01h
int 21h
sub al,30h
add bl,al
endm

disp macro reg
mov dl,reg
mov ah,09
int 21h
endm

code segment
start:
mov ax,data
mov ds,ax

print msg1
read

mov cl,00h
mov ch,01h
l3:
sub bl,ch
jc l2
jz l1
;aas
inc cl
inc ch
inc ch
jmp l3
l1:
inc cl
print msg3
add cl,30h
mov dl,cl
mov ah,02h
int 21h
jmp k
l2:
print msg2
k:
mov ah,4ch
int 21h
code ends
end start

;Program Ends Here

querrymail@gmail.com