Search This Blog

Tuesday, November 22, 2011

Baud rate in 8051

The 8051 transfers and receives data serially at many different baud rates. The baud rate in 8051 are completely programmable. This is done with help of timer 1.

8051 divides the crystal frequency by 12 to get machine cycle frequency. In case of XTAL = 11.0592 Mhz. so machine cycle here becomes 921.6 Khz. Now the 8051 UART circuitry divides the machine cycle frequency of 921.6 Khz by 32 once more before it is used by timer 1 to set the baud rate.

So 921.6Khz divided by 32 gives 28,800 hz. This is the value of frequency upon which we will perform operation to get variable baud rate. When timer 1 is used to set baud rate it must be programmed in mode 2 8 bit auto reload.
Timer 1 TH1 register values for various baud rates

Baud rate
TH1(decimal)
TH1(hex)
9600
-3
FD
4800
-6
FA
2400
-12
F4
1200
-24
E8

As 28,800/3 = 9600 where -3 is loaded into TH1.
28800/12 = 2400 where -12 is loaded into TH1.
28800/24 = 1200 where -24 = E8 is loaded into TH1.

WAP To generate frequencies of 2khz on pin P0.0 crystal frequency 12Mhz

calculations

2khz frequency means 2000 cycles / sec
2000 cycles     -    1 sec
1 cycle        -    ? secs
        -    1/2000
        =    .0005 secs
crystal frequency 12mhz
therefore used frequency 12/12 = 1 mhz
1000 cycles    -    1 sec
1 cycle        -    ? secs
        =    1/1000 secs
        =    1 usec

so each half cycle in 2khz will have a .00025 sec or 250 ms delay
so we need to create a 250ms delay using crystal frequency

DELAY SUBROUTINE

DELAY :

MOV R7, #DATA    ; 2 MACHINE CYCLE
DJNZ R7, $    ; 2 MACHINE CYCLE
RET        ; 1 MACHINE CYCLE

let x be our data
(2 + 2*x + 1)*1us = 500 us
2*x = 500 - 3
x = 497/2 = 249

DATA = 249 for 500 us but we needed for half its cycle that is 250 us
DATA = 125 for 250 us

SO PROGRAM

ORG 0000H    ;

BACK :
SETB P0.0    ;
ACALL DELAY    ;
CLR P0.0    ;
ACALL DELAY    ;
SJMP BACK    ;

DELAY :
MOV R7, #125    ;
DJNZ R7, $    ;
RET        ;


END        ;

assembly language subroutine code for serial data send and recieve

; SEND SERIAL DATA

SEND :
MOV SBUF, A        ; LOAD DATA IN BUFFER
HERE :
JNB TI, HERE        ; WAIT TILL LAST BIT IS GONE
CLR TI            ; GET READY FOR NEXT CHARACTER
RET            ; RETURN TO CALLER

; RECIEVE SERIAL DATA

RECV :
JNB RI, RECV        ; WAIT TILL LAST BIT HAS COME
MOV A, SBUF        ;
CLR RI            ; GET READY FOR NEXT CHARACTER
RET            ; RETURN TO NEXT CHARACTER

WAP to transfer WELCOME serially at 9600 baud rate do this continuously.

; USING TIMER 1 MODE 2 AUTO RELOAD MODE
; 8 BIT A STOP READ ENABLED
MOV TMOD, #0010 0000B    ; TIMER 1 MODE 2 FOR TIMER OPERATION
MOV TH1 #-3        ; AS 9600 BAUD RATE
MOV SCON #0101 0000B    ; SMODE 2 8 BIT 1 STOP BIT READ ENABLED
SETB TR1        ; START TIMER 1

MOV R7, #07H        ; AS WELCOME 7 LETTER WORD
MOV DPTR, #0300H    ; WELCOME STORED AT 300H

BACK :
CLR A            ;
MOV A, @A+DPTR        ;

HERE :
MOV SBUF,A        ;
JNB TI, HERE        ;
CLR TI            ;

INC DPTR        ;
DJNZ R7, BACK        ;
SJMP $            ;

ORG 300H        ;
DB 'W', 'E', 'L','C','O','M','E';

END            ;

WAP in 8051 to send "HI" serially at 4800 baud rate

; USING TIMER 1 MODE 2 AUTO RELOAD MODE
MOV TMOD, #0010 0000B    ; TIMER 1 MODE 2 FOR TIMER OPERATION
MOV TH1, #-6        ; 4800 BAUD RATE
MOV SCON, #0101 0000B    ; SMODE 2 ,8 BIT ,1 STOP BIT, READ ENABLED
SETB TR1        ; START TIMER 1

BACK :
MOV SBUF, #"H"        ;

HERE :
JNB T1, HERE        ; WAIT FOR TRANSMISSION TO BE OVER
CLR T1            ;

MOV SBUF,#"I"        ;

HERE1 :
JNB T1, HERE1        ; WAIT FOR TRANSMISSION TO BE OVER
CLR T1            ;

SJMP BACK        ;
END

Monday, November 21, 2011

Pseudo Codes

In computer science and numerical computation, pseudocode is a compact and informal high-level description of the operating principle of a computer program or other algorithm. It uses the structural conventions of a programming language, but is intended for human reading rather than machine reading. Pseudocode typically omits details that are not essential for human understanding of the algorithm, such as variable declarations, system-specific code and some subroutines. The programming language is augmented with natural language descriptions details, where convenient, or with compact mathematical notation. The purpose of using pseudocode is that it is easier for persons to understand than conventional programming language code, and that it is an efficient and environment-independent description of the key principles of an algorithm. It is commonly used in textbooks and scientific publications that are documenting various algorithms, and also in planning of computer program development, for sketching out the structure of the program before the actual coding takes place.

No standard for pseudocode syntax exists, as a program in pseudocode is not an executable program. Pseudocode resembles, but should not be confused with, skeleton programs including dummy code, which can be compiled without errors. Flowcharts and UML charts can be thought of as a graphical alternative to pseudocode, but are more spacious on paper.

Application

Textbooks and scientific publications related to computer science and numerical computation often use pseudocode in description of algorithms, so that all programmers can understand them, even if they do not all know the same programming languages. In textbooks, there is usually an accompanying introduction explaining the particular conventions in use. The level of detail of the pseudo-code may in some cases approach that of formalized general-purpose languages.

A programmer who needs to implement a specific algorithm, especially an unfamiliar one, will often start with a pseudocode description, and then "translate" that description into the target programming language and modify it to interact correctly with the rest of the program. Programmers may also start a project by sketching out the code in pseudocode on paper before writing it in its actual language, as a top-down structuring approach.

Syntax

As the name suggests, pseudocode generally does not actually obey the syntax rules of any particular language; there is no systematic standard form, although any particular writer will generally borrow style and syntax for example control structures from some conventional programming language. Popular syntax sources include Pascal, BASIC, C, C++, Java, Lisp, and ALGOL. Variable declarations are typically omitted. Function calls and blocks of code, for example code contained within a loop, is often replaced by a one-line natural language sentence.

Depending on the writer, pseudocode may therefore vary widely in style, from a near-exact imitation of a real programming language at one extreme, to a description approaching formatted prose at the other.

This is an example of pseudocode (for the mathematical game bizz buzz):

For i = 1 to 100
    set print_number to true
    if i mod 3 = 0
        print "Bizz" and set print_number to false
    if i mod 5 = 0
        print "Buzz" and set print_number to false
    if print_number, print i
    print a newline

Sunday, November 20, 2011

Clock source for ADC0804

The speed at which analog input is converted to digital output depends on the speed of clk input.
According to datasheets of ADC0804 the typical operating frequency is 640khz at 5 volts. there are 2 ways to provide clock

source to ADc0804. as shown in fig.
ADC0804 with self clocking
ADC0804 with 8051 crystal clocking

Programming
; P2.5 CONNECTED TO RD
; P2.6 TO WR

; P2.7 TO INTR
; WR IS USED FOR STARTING CINVERSION
; SHOULD GO FROM L - H FOR CI=ONVERSION TO START
; CLR RD TO START READING DATA

ORG 0000H
RD BIT P2.5
WR BIT P2.6
INTR BIT P2.7
MYDATA EQU P1    ; DATA BUS OF ADC0804
MOV P1,#0FFH    ; MAKE P1 INPUT
SETB INTR

BACK :
CLR WR
SETB WR

HERE :
JB INTR, HERE    ; WAIT FOR END OF CONVERSION
CLR RD        ; START READING
MOV A, MYDATA    ;
ACALL CONVERSION; YOUR CONVERSION HEX - ASCCII
ACALL DISPLAY    ; DISPLAY DATA
SETB RD        ; RD =1 FOR NEXT ROUND
SJMP BACK    ;

CONVERSION :
; ANY HEX TO ASCII CONVERSION PROGRAM WILL DO.

DISPLAY : 
; AS NO DISPLAY THEREFORE NOT TO BE USED

8051 interfacing with stepper motor

8051 Interfacing with stepper motor
fig shows interfacing connection
-------------------------------
; 4 PINS OF STEPPER MOTOR CONTROLLED BY 4 BITS OF 8051 P1.0 - P1.3
; IT IS INTERFACED USING DARLINGTON ARRAYS SUCH AS ULN2003 AS
; 8051 LACKS CURRENT TO RUN THE MOTOR. SO ULN2003 IS USED FOR ENERGIZING STATOR.
; PROGRAM BELOW SHOWS JUST ROTATING THE MOTOR STEP WISE
; BUT STEP WIDTH IS UNKNOWN

MOV A, #66H    ; LOAD THE STEP SEQUENCE

BACK :
MOV P1, A    ; LOAD SEQUENCE TO PORT
RR A        ; CHANGE SEQUENCE ROTATE CLOCKWISE
ACALL DELAY    : WAIT FOR IT
SJMP BACK    ; NOW KEEP GOING

DELAY :
MOV R2, #100

H1 :
MOV R3, #255

H2 :
DJNZ R3, H2
DJNZ R2, H1
RET

END

; THIS OTHER PROGRAM CODE HOW IN SAME INTERFACING TO ROTATE A STEPPER MOTOR
; 64 DEGREES IN CLOCKWISE DIRECTION USING 4 STEP SEQUENCE
; IT TAKES SOME CALCULATIONS
; CONSIDER A MOTOR WITH STEP ANGLE 2 DEGREE
; SO STEPS PER REVOLUTION = 180
; NO OF ROTOR TEETH = 45
; MOVEMENT PER 4 STEP SEQUENCE IS 8 DEGREES
; FOR 64 DEGREE MOVEMENT WE NEED TO SEND 8 CONSECUTIVE 4 STEP SEQUENCE
; THAT IS IT WILL COVER 32 STEPS

ORG 0000H
MOV A, #66H
MOV R1, #32H    ; 32 STEPS TO BE TAKEN

BACK :
RR A        ; ROTATE CLOCKWISE
MOV P1, A    ;
ACALL DELAY    ;
DJNZ R1, BACK    ;
END
-----------------

Now few question arises
q1) why did we took 66h as step value??
Ans))

A normal 4 step sequence is like below.
Step #
Winding A
Winding B
Winding C
Winding D
1
1
0
0
1
2
1
1
0
0
3
0
1
1
0
4
0
0
1
1
Going from step 1 to 4 we rotate motor clockwise
Going from step 4 to 1 we rotate motor counter clockwise

The stepper motor discussed here has total 6 leads. 4 leads representing 4 windings and 2 commons for centre tapped leads. Here we have used 2 phase 4 step sequence. It must also be noted we can start from any step.  Be it step 1,2,3 or 4. But we must continue in proper order for proper rotation.
In programs on blog I have mostly begun from step 3.
 other step sequences are as below

Half Step 8 Step Sequence
Step #
Winding A
Winding B
Winding C
Winding D
1
1
0
0
1
2
1
0
0
0
3
1
1
0
0
4
0
1
0
0
5
0
1
1
0
6
0
0
1
0
7
0
0
1
1
8
0
0
0
1



Wave drive 4 step sequence

Step #
Winding A
Winding B
Winding C
Winding D
1
1
0
0
0
2
0
1
0
0
3
0
0
1
0
4
0
0
0
1





 q2) how much movement is associated with a step angle??

Step angle
Steps per revolution
.72
500
1.8
200
2
180
2.5
144
5
72
7.5
48
15
24


 ------------------

Edit - 1: Changed the Line DJNZ R0 to R1 as it was a mistake in typing.