Google
Showing posts with label Program. Show all posts
Showing posts with label Program. Show all posts

Mar 10, 2010

PWM with 89S52

Here is the program for Pulse width modulation PWM on basic for 89s52.

You could generate pwm using the following psedocode

#define high_period
#define low_period

main()
{
!!Code to initiate the timer;
!!Timer counter register value = high_period;
!!set port pin high
!!start timer
while(1);
}

interrupt void time_interrupt_routine(void)
{
if(port pin is high)
!!Timer counter register value = low_period;

else
!!Timer counter register value = high_period;

!!Toggle port pin
!! start timer;
}

Here high_period + low_period gives the time period in terms of clock cycle and

high_period/(high_period + low_period) *100 gives the dutycycle.

Mar 5, 2010

How to access R0 to R7 registers in Keil C?

you can use the following Program to access R0 to R7 registers in Keil C


{

unsigned char R0Val;

_asm

MOV A, R0
MOV _R0Val, A

_endasm

}

OR

{

unsigned char R0Val;

#pragma ASM

MOV A, R0
MOV _R0Val, A

#pragma ENDASM

}

C variables have to be prefixed by '_' or underscore character, to be accessible in the assembly section of the C code.

Hope this helps.

Feb 28, 2010

Program to initialise the LCD in 4 bit mode using PIC microcontroller

It was for 16F72 at 4Mhz written in cc5x. LCD is connected to PORTB.RS at RB7,RW at RB1,EN at RB6. DB7 at RB2,DB6 at RB3, DB5 at RB4, and DB4 at RB5. A function delay_ms() is used , which is not shown here.

code will work for 2x16 character LCD modules.

void lcdw(unsigned char m)
{
PORTB.2=m.7;
PORTB.3=m.6;
PORTB.4=m.5;
PORTB.5=m.4;
en=1;
nop();
nop();
en=0;
}

void nbw(unsigned char m)
{
PORTB.2=m.7;
PORTB.3=m.6;
PORTB.4=m.5;
PORTB.5=m.4;
en=1;
nop();
nop();
en=0;
PORTB.2=m.3;
PORTB.3=m.2;
PORTB.4=m.1;
PORTB.5=m.0;
en=1;
nop();
nop();
en=0;
}

void goadr(unsigned char a)
{

rs=0;
nbw(0x80 + a);
delay_ms(5); // delay of 5msec

}


void main()
{
................

TRISB=0;
PORTB=0;
rw=0;
rs=0;
lcdw(0x28); // lcdw function is only used once to initialise the LCD to 4 bit mode.
delay_ms(5);
nbw(0x28);
delay_ms(5);
nbw(0x0c);
delay_ms(5);
nbw(0x06);
delay_ms(5);
nbw(0x01);
delay_ms(5);
goadr(0x02);
rs=1;
nbw('a'); //displays the character "a" at location 0x02
nbw('b'); //displays the character "b" at location 0x03
.........................


}

Feb 13, 2010

A microprocessor Program to convert BCD pack and Unpack number for 8085.

Please find the Below program to Pack the two unpacked BCD numbers stored in memory locations 4200H and 4201H and store result in memory location 4300H. Assume the least significant digit is stored at 4200H.


Here is the Sample problem:
(4200H) = 04
(4201H) = 09
Result = (4300H) = 94


Source program for the above logic

LDA 4201H : Get the Most significant BCD digit
RLC
RLC
RLC
RLC : Adjust the position of the second digit (09 is changed to 90)
ANI FOH : Make least significant BCD digit zero
MOV C, A : store the partial result
LDA 4200H : Get the lower BCD digit
ADD C : Add lower BCD digit
STA 4300H : Store the result
HLT : Terminate program execution

Oct 15, 2009

Dely loop in AVR

Amer:

Hello freinds.
Can anybody help me regarding how to write a assembly language delay loop (loop-in-loop method no use of internal timers) for AVR microcontroller and how to evaluate the time delay provided by a loop?
I have an example of such loop taken from EFY mag's Nov.05 issue. You can help me by explaining the calculations for that code. The code goes as follows:-


delay: clr r19
ldi r17,$ff
loop1: inc r17
brne loop1
inc r19
brne loop1
ret
Please include the step by step calculation procedure for the delay loop in your reply so that i will be able to write loops for desired delay.
Also does anybody has another type of such code to generate delay? please explain the evaluation of the same.
Thanks in advance...

Arun Dayal
May be my C code for PWM Servo controller routine help u! Just follow my web at http://www.dayalsoft.com . An ASM delay can be written similarly. The routine is not using any of the internal timers and produces a variable delay ranging from 800 to 1250 uS precisely.

sajiD
main:
ser r16
loop1:
dec r16
brnq loop1
ret
that is very ez ok

Prasanna.S

for calulation of delay loop you should know the clock cycle period(i.e1/frequency) once you get this if you are writing code in assembly language you have to check no. of cyles required for each instruction(but for avr most of the instruction are single cycle)
refer processor manual write a simlpe code with decrement in loops the value to be given is calculated
no to be decremented=sec required/(no of cycles /loop)*period in sec

to exact value you have to consider the jump instruction in the end of loop completion etc.

Oct 13, 2009

C Programming Options in AVR

I am new to AVRs
I want to know what all programming options are there for C.
I know about BASCOM, but dont personally like BASIC that much.

ans:

* hey try codvisionavr c compiler .....it good and easy to learn and when u become pro use
WinAVR GCC

* I think you should see the page http://www.lancos.com and everthing will be clear! U will be able to make ur own circuit to program downloader circuit! as well as software to download for free!
U should use IAR Embedded C Compiler! It is widely supported by C Libraries and Evaluation version if free and sufficient for new users!

One of the International Journal Paper of mine for Your Use
http://www.convergencepromotions.com/atmelonline/v_9/pdf/AtmelJournal_V9_ConstructingA16-Channel.pdf

Similar ones u could find at http://www.atmel.com web site

* You may try AVRstudio from http://www.atmel.com/dyn/products/tools_card.asp?tool_id=2725 along with WINAVR from winavr.sourceforge.net/ . AVRstudio serves as the IDE and Debugger/Simulator and WINAVR the C compiler. Both complement each other.

For the hardware part you could try the JTAG emulator described in this site.
http://www.scienceprog.com/build-your-own-avr-jtagice-clone/

I have made the emulator using GP board and it works perfectly.

* i recommend you to use C Language for programming AVR microcontroller
C is quite commonly used for programming microcontrollers and a lot of help is
available on programming microcontroller in C
you can design your own programmer for AVR because AVR microcontroller has SPI & other ports for In-Circuit programming

Dec 8, 2008

program to perform fibonacci series

following program is perform fibonacci series

In order to perform the fibonacci series, that is 1,1,2,3,5,8,13 .....
We initalize SI to 2000 and CX to 0, then we initilaize AX and BX registers. Then we increment SI Register by 1 and we move the contents of AL into SI register. Then we decrement cs by one and move the contents of BL o that of si and decrement CS. Then we add al and the bl registers.

Move al with contents of si +01 and move si with BL, then we increment si by one and repeat until cx=0 and we halt the program

CODE:

MOV SI,2000
MOV CX,0000
MOV CL,[SI]
MOV AX,0000
MOV BX,00
INC SI
MOV [SI], AL
DEC CX
INC SI
MOV [SI],BL
DEC CX
AGAIN ADD AL,BL
MOV [SI+01],AL
MOV BL,[SI]
INC SI
LOOP AGAIN
INT A5

----
code ends

Aug 30, 2008

check wether given 16 bit number is palindrome or not

Palindrome program taking 16 bits.

taking C7H,EBH as given data i have done the program
which is equivalant to 1101011111101011

ORG 2000H
LXI H,2100H ;DATAS ARE STORED AT LOCATIONS 2100H,21001H
MOV A,M
MVI C,07H

LOOP: RRC
CALL SBR ;SBR IS SUBROUTINE
DCR C
JNZ LOOP

ANI 01H
ADD D
INX H
CMP M
LXI H,2400H ;1 IS STORED AT MEM-LOC 2400H IF PALIIN ELSE 0
JZ PALIN
MVI M,00H
JMP FIN

PALIN: MVI M,01H
FIN: HLT

SBR: MOV B,A
MOV A,D
JNC BYPAS ;IF CARRY IS NOT 1 IS NOT AADED
ADI 01H
BYPAS: RLC
MOV D,A
MOV A,B
RET

ORG 2100H
DB 00C7H,00EBH ;DATA BYTE

END

here EBH is reversed and compared with C7H, if they are equal then plain else not palin


You might be also interested in:

:: Temperature Control system using 8086
:: Traffic light control system using 8086
:: Assembly Language Program to serve NMI
:: Interfacing Stepper Motor to 8086 using 8255

Jul 26, 2008

A C program for serial communation with pc using 8051

This program will read a complete line coming through my COM port in 8051.


This is the subroutine or function in c for accept line from serial port
void get_line(void)
{
char c, index=0;

while(c = getchar() != '\n')
{
buffer[index++] = c;
}
}


The main program
void main()
{
PCON = 0x80; // Double Baud Rate
SCON = 0x50; // SCON: mode 1, 8-bit UART, enable rcvr
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload
TH1 = BAUD_CONST; // TH1: reload value for desired baud. Calculate this for your crystal frequency
TR1 = 1; // TR1: timer 1 run
TI = 1;
RI = 0;

get_line(); //function call

}

You might be also interested in:

:: options available for int 21h instruction
:: Answers of Microprocessor(8085) & Electronics FAQ
:: The 8085 Microprocessor Architecture Microprocessors & Interfacing

Jul 22, 2008

8086 program to find GCD of two 2 numbers

Euclid (a Greek mathematicians and philosopher of about 300 BC) describes this algorithm in Propositions 1 and 2 of Book 7 of The Elements, although it was probably known to the Babylonian and Egyptian mathematicians of 3000-4000 BC also.
If we try it with an two numbers, the final non-zero remainder is the greatest number that is an exact divisor of both our original numbers (the greatest common divisor)


Here is the program


mov ax,4000h
mov ds,ax
mov si,0000h
mov al,num1 ;num1 is first no.
mov cl,num2 ;num2 is second no.
mov ah,00h
cmp al,cl
ja next
xchg al,cl
next: mov bl,cl
div cl
cmp ah,00h
je down
mov al,cl
mov cl,ah
mov ah,00h
jmp next
down mov result,bl ;result is the mem.loc.
;where gcd is to be stored
hlt

You might be also interested in:

:: options available for int 21h instruction
:: Answers of Microprocessor(8085) & Electronics FAQ
:: The 8085 Microprocessor Architecture Microprocessors & Interfacing

Jul 20, 2008

speed control of DC motor using pic microcontroller

this is the program for speed control of DC motor using 89c51 microcontroller

code !



$mod51

;***************************************************************************

;* LED PWM 2 *

;* Amateur World *

;***************************************************************************

; R7 is used to hold a value between 0 and 255 which will correspond to a
brightness

; level for the LED. Timer 0 will be used to create the Pulse Width Modulated
output.

; Timers are generally used to repeatedly measure the same fixed interval of
time.

; In this case we want to measure 2 periods of time. The high output period and
the

; low output period.

;**************************************************************************

; RESET ;reset routine

ORG 0H ;locate routine at 00H

AJMP START ;jump to START

;**************************************************************************

; INTERRUPTS (not used) ;place interrupt routines at appropriate

;memory locations

ORG 03H ;external interrupt 0

RETI

ORG 0BH ;timer 0 interrupt

AJMP TIMER_0_INTERRUPT ; go to interrupt routine (can be anywhere we want)

; since a jump does not return to this point we do not

; need a Return command

ORG 13H ;external interrupt 1

RETI

ORG 1BH ;timer 1 interrupt

RETI

ORG 23H ;serial port interrupt

RETI

ORG 25H ;locate beginning of rest of program

;**************************************************************************

INITIALIZE: ; set up control registers

MOV TMOD,#00H ; set timer 0 to Mode 0 (8 bit Timer with 5 bit prescalar)

SETB TR0 ; turn on timer 0


MOV PSW,#00H

SETB EA ; Enable Interrupts (each individual interrupt must also be enabled)

SETB ET0 ; Enable Timer 0 Interrupt

RET

;**************************************************************************

; Real code starts below.

;*************************************************************************

; The LED is off during the high section and on during the low section of each
cycle

; The Flag F0 is used to remember whether we are timing tlow or thigh.



TIMER_0_INTERRUPT:



JB F0, HIGH_DONE ; If F0 is set then we just finished the high section of the

LOW_DONE: ; cycle so Jump to HIGH_DONE

SETB F0 ; Make F0=1 to indicate start of high section

SETB P1.0 ; Turn off LED

MOV TH0, R7 ; Load high byte of timer with R7 (our pulse width control value)

CLR TF0 ; Clear the Timer 0 interrupt flag

RETI ; Return from Interrupt to where the program came from

HIGH_DONE:

CLR F0 ; Make F0=0 to indicate start of low section

CLR P1.0 ; Turn on LED

MOV A, #0FFH ; Move FFH (255) to A

CLR C ; Clear C (the carry bit) so it does not affect the subtraction

SUBB A, R7 ; Subtract R7 from A. A = 255 - R7.

MOV TH0, A ; so the value loaded into TH0 + R7 = 255

CLR TF0 ; Clear the Timer 0 interrupt flag

RETI ; Return from Interrupt to where the program came from

;**************************************************************************

START: ;main program (on power up, program starts at this point)

ACALL INITIALIZE ;set up control registers

MOV R7, #00H ; set pulse width control to dim

TEST: LCALL TIMER

INC R7 ;FROM HERE TO TEST IS THE TRICK TO VARY R7 AS WELL AS

;THE INTENSITY OF LED

CJNE R7,#0FFH,TEST

TEST1


TEST1: LCALL TIMER

DEC R7

CJNE R7,#00H,TEST1

LCALL TEST

;go to LOOP(always jump back to point labeled LOOP)



TIMER: push 00h

push 01h

push 02h ;store the internal location 00h,01h,02h contents

mov 02h,#03h

USER_L2:

mov 01h,#0ffh

USER_L1:

mov 00h,#0ffh

DJNZ 00h,$;decreament the internal location 00h

;content repeat until it becomes 0

DJNZ 01h,USER_L1 ;""""""



DJNZ 02h,USER_L2 ;""""""





pop 02h

pop 01h

pop 00h

RET

END ;end program

You might be also interested in:

:: Traffic light control system using 8086
:: Assembly Language Program to serve NMI
:: Interfacing Stepper Motor to 8086 using 8255



Jul 18, 2008

Edge triggeringin PIC16F877A

i am using the following program for edge triggering. i am using PIC16f877a Controller.

i put one key for the external interrupt.And using RB0/INT pin for the edge triggering concept.and i connected one led on porta to see the output.

i am using the HITECH c compiler

#include<pic.h>

#include<stdio.h>



#define ldata PORTD

#define RS RC4

#define RW RC5

#define EN RC6



unsigned int d;



void Init_Int(void);

void cmd(unsigned char);

void cardiac_lcd(unsigned int);

void Msdelay(unsigned int);

void convert_disp(unsigned char);





void Msdelay(unsigned int time)

{

unsigned int i,j;

for(i=0;i<time;i++)

for(j=0;j<1275;j++);

}



void Lcd_disp()

{

cmd(0x38);

Msdelay(5);

cmd(0x0e);

Msdelay(5);

cmd(0x01);

Msdelay(5);

cmd(0x80);

Msdelay(5);

}

void cardiac_lcd(unsigned int value1)

{

ldata=value1;

RS=1;

RW=0;

EN=1;

Msdelay(1);

EN=0;

}



void cmd(unsigned char value)

{

ldata=value;

RS=0;

RW=0;

EN=1;

Msdelay(1);

EN=0;

// return;

}



void convert_disp(unsigned char val)

{

unsigned int b,c;

b=val/10;

c=val%10;

d=b|0x30;

}



void Init_Int()

{

RP0=1;

INTEDG=1;

// RPBU=1;

RP0=0;

GIE=1;

INTE=1;

}

void interrupt intr_tc()

{

unsigned int a=0;

if(INTF==1)

a=a++;

// if(a>10)

{

// convert_disp(a);

RA1=1;

}

INTF=0;

}

void main()

{

PORTA=0x00;

TRISA=0x00;

PORTB=0x00;

TRISB=0xff;

PORTC=0x00;

TRISC=0x00;

PORTD=0x00;

TRISD=0x00;

Lcd_disp();

Init_Int();

cardiac_lcd(d);

while(1)

{

//cardiac_lcd(d);

//RA1=0;

}

}




if cant able to bring the no of counts in LCD display.and led is blinking then do the following
for the lcd line initialization (0x38) takes more time , so that it would be better to pass 0x38 three times. i had only checked that much and also increase the delay time

You might be also interested in:

:: Traffic light control system using 8086
:: Assembly Language Program to serve NMI
:: Interfacing Stepper Motor to 8086 using 8255

Jul 14, 2008

Three phase latching relay programming for pic microcontroller 16f873a

I m using pic16f873a microcontroller and ICD2 Debugger cum programmer

I will give you brief introduction about my application.I need to toggle the relay on and off continuously.During on condition i need to check the continuity of relay contacts.For that i gave +5v supply to the relay contacts.output of this contact is given to microcontroller 25th pin.If the relay contact is working i will increase the relay test count and this count should written into the EEPROM.I f relay contact is not working micrcontroller should wait until the contact works.


TEST BSF PORTB,2 ;Set Turn off Relay
CALL DELAY
BCF PORTB,2 ;Clear Turn off Rly
CALL DELAY
CALL DELAY
CALL DELAY
CLRF PORTB
BSF PORTB,1 ;Set Turn on Rly
CALL DELAY
BCF PORTB,1 ;Clear Turn on Rly
CALL DELAY
CALL DELAY
CALL DELAY
MOVF PORTB,W
MOVWF CTAT_STATUS
BTFSS CTAT_STATUS,4
GOTO $-3
MOVLW 01H
MOVWF DATA_EE_ADDR
CALL READ
MOVWF DATA_TCNTL
MOVLW 00H
MOVWF DATA_EE_ADDR
CALL READ
MOVWF DATA_TCNTH
INCFSZ DATA_TCNTL,1
GOTO STORE
INCFSZ DATA_TCNTH,1
GOTO STORE
STORE MOVF DATA_TCNTL,W
MOVWF DATA_EE_DATA
MOVLW 01H
MOVWF DATA_EE_ADDR
CALL WRITE
MOVF DATA_TCNTH,W
MOVWF DATA_EE_DATA
MOVLW 00H
MOVWF DATA_EE_ADDR
CALL WRITE
GOTO TEST



if the code do not work on the application then follow the instructions
connect ground to relay contact and check for 0 in program also have a pull up for microcontrolloer pin.
for current program go for pull down resistor.
that should solve your problem.

You might be also interested in:

:: MASM 611 SOFTWARE
:: bit reversal and sorting programs
:: Find Square Root of a hexadecimal number in assembly language
:: common intreview questions on 8086

Jul 6, 2008

decimal addition program for 8086

This program will add two decimal numbers

LXI H,2200H
MOV A,M
INX H
ADD M
DAA
STA 2300H
HLT

DAA will convert HEX to valid BCD number
now the program can be easily understood

You might be also interested in:


:: 8051 or PIC microcontroller which is better
:: Effective addresses
:: Floating Point Initializations

Jun 30, 2008

A subroutine program in assembly language

A subroutine based program in assembly language.

this is the MAIN PROGRAM

LXI SP 2400H
LXI H,2000H
LXI B,1020H
CALL SUB
HLT

this is the subroutine
SUB: PUSH B
PUSH H
LXI B,4080H
LXI H,4090H
DAD B
SHLD 2200H
POPH
POPB
RET

this subroutine is some thing like functions in c program language. here the main function is calling the subroutine SUB in the last but one line then the flow of the execution of the program is transfered to the subroutine once the program flow is reached to the end of the subroutine i.e RET statement then the execution will be going to the main program.


You might be also interested in:

:: free and open source 8086 Microprocessor Emulator
:: Invoke function

Jun 28, 2008

whether given number is palindrome or not

This is assembly language program for finding a given number is palindrome or not

Example:
3000 – AD
3001 – B5

AD = 10101101
B5 = 10110101 (D7=1; D6=0; D5=1; D4=1; D3=0; D2=1; D1=0; D0=1)

AD and B5 are palindrome

Algorithm
1. Take the 2nd value B5
2. The binary equivalent of it (10110101), is cut into bits n each bit is put in address ranging from 8001 to 8008 in the reverse order. That is, D0 is put in 8001; D1 in 8002 and so on—until D7 in 8008
3. This is done by rotating 10110101 right thro carry
4. Thus the carry values are stored into the addresses (8001 – 8008)
5. Hence we get, 8001 – 00000001
8002 – 00000000
8003 – 00000001
8004 – 00000000
8005 – 00000001
8006 – 00000001
8007 – 00000000
8008 – 00000001
6. Each of these values hafta b shifted so as to get 10000000 of 8001
00000000 of 8002
00100000 of 8003
00000000 of 8004
00001000 of 8005
00000100 of 8006
00000000 of 8007
00000001 of 8008
7. Add the values present in 8001 to 8008 n store it in the register C. Hence in register C, we have the value AD after rotating B5)
8. Get the 1st value from 3000 n store it in accumulator (register A)
9. Subtract A and C registers
10. If the numbers are palindrome, 01h gets stored in memory 4000
11. If the numbers r not palindrome, 02h is stored in memory 4001
12. end

the program

Label Mnemonics

MVI H, 08h
MVI L, 01h
MVI C, 08h
LDA 3001
rotate: RAR
JC one
JNC zero
one: MVI M, 01h
JMP next
Zero: MVI M, 00h
Next: INC L
DCR C
JNZ rotate
MVI L, 00h
Inc: INC L
MOV D, L
MOV A, L
SUI 09h
JZ fini
MOV A, M
Hey: RRC
DCR D
JZ inc
JNZ hey
Fini: MVI L, 00h
MVI C, 00h
Other: INC L
MOV A, M
MOV B, A
INC L
MOV A, M
ADD B
ADD C
MOV C, A
MOV A, L
SUI 08h
JNZ other
MVI H, 30h
MVI L, 00h
MOV A,M
SUB C
JZ palin
JNZ nopalin
Palin: MVI A, 01h
STA 4000
JMP over
Nopalin: MVI A, 02h
STA 4001
Over: HLT

You might be also interested in:

:: Assembly Language Programs to compute an expression
:: Interfacing Analog-to-Digital converter to 8086 using 8255
:: Interfacing Digital-To-Analog converter to 8086 using 8255
:: Temperature Control system using 8086

Jun 26, 2008

seperate the digits of hexa decimal number assembly language program

seperate the digits of a hexa decimal numbers and store in different locations

first get the packed BCD number and mask the lower nibble then move to the required position in the number and adjust the higher BCD digit as a lower digit then store the partial result now get the orignal BCD number and mask the higher nibble store the result and stop the program

LDA 2200H
ANI F0H
RRC
RRC
RRC
RRC
STA 2300H
LDA 2200H
ANI 0FH
STA 2301H
HLT

You might be also interested in:

:: Assembly Language Source Codes
:: why there are two ground pins in 8086
:: Program to display ASCII characters on the display unit

Jun 25, 2008

assambley Language program for swapping two numbers

swap the two numbers
the following program will swap the two numbers
eg; 23 will be changed to 32

LDA 2000H
MOV B,A
LDA 2200H
STA 2000H
MOV A,B
STA 2200H
HLT
the algorithm of the program is simple just copy the fist number in to register B and second number in to register A while storing store them in the reverse way that id first store the contents in the A first and then store the contents in register B

Jun 23, 2008

Adding the 4 four most significant bit to a hexa no

this is the program to add 4 most significant bit to a hexa decimal number

first get most significant BCD Digit
then move to the location by RLC command
store the temporary number in C
get the lower BCD digit and add it
store the result and stop the program.

LDA 2201H
RLC
RLC
RLC
RLC
MOV C,A
LDA 2200H
ADD C
STA 2300H
HLT

Jun 21, 2008

once (1's) and twos (2's) complement of a number

1's complement of the number

LDA 2200H
CMA
STA 2300H
HLT
cma will give 1's complement of a number


2's complement of a number


LDA 2200H
CMA
ADI 01H
STA 2300H
HLT
here first the once's complent of the number is determined and then one is added to it

You might be also interested in:

:: Traffic light control system using 8086
:: Assembly Language Program to serve NMI
:: Interfacing Stepper Motor to 8086 using 8255