11_lowlevel

Upload: syukz

Post on 04-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 11_LowLevel

    1/28

    11 Low level programming and Interfacing

    11 Low level programming and Interfacing

    Learn

    Binary to hex and hex to binary conversion.

    Bitwise operators

    Simple interfacing

    11.1 Binary to hex and hex to binary conversion.

    A computer may be programmed to control external devices, for example, turning on or off

    lights, pumps, motors or to monitor sensors such as smoe detectors or burglar alarms etc.

    !hese types of control systems usually re"uire the computer to process incoming signals and

    output control signals to external devices almost instantaneously. Because of its immediate

    response such a system is called a real#time control system. $rogramming a real#time control

    system will usually involve a substantial amount of low level programming where data are

    manipulated at bit level. !he best way to visuali%e a low level operation is to express the data

    in binary form. &nfortunately ' cannot represent data in binary form. But data can, however,

    be represented in hex format. !he conversions between these two number systems are

    relatively easy. !he conversion methods can be found in most textboos on digital electronics

    but are repeated here as a reminder.

    !o convert a binary number to a hex number, you must first organise the binary number in a

    group of ( bits starting from the rightmost bit. )ext, for each group, write its hex e"uivalent

    using the 1* symbols+ to - and A to where A to represent values from 1 to 1/.

    or example, consider an 0#bit binary number 11111.

    Step1+ rganise it into groups of ( bits starting from the rightmost bit, thus, giving

    1 1 1 1 1

    Step 2 + 3rite the hex e"uivalent for each group.

    1 1 1 1 1

    4ence the hex e"uivalent of binary number 11111 is xBA.

    !o convert a hex number to it binary e"uivalent, write the (#bit binary e"uivalent of each hex

    digit.

    or example, to convert hex number x56 to binary, write the (#bit binary e"uivalent for hex

    digits 5 and 6.

    5 6

    !he binary e"uivalent of x56 is 111111.

    175

    AB

    111111

  • 8/14/2019 11_LowLevel

    2/28

    11 Low level programming and Interfacing

    8our turn

    1 'onvert the following binary numbers to their hexadecimal e"uivalent+

    9a: 111111 9b: 1111 9c: 1111 9d: 1111

    2 'onvert the following hexadecimal numbers to their binary e"uivalent.9a: x(/ 9b: x* 9c: x-6 9d: xA 9e: x;

    xx

    11.2 Bitwise operators

    ' comes with a set of operators that allow us to manipulate data bitwise. !hese operators is a

    great help in low level programming which deals mainly with data bitwise !hese bitwise

    operators are described below.

    11.2.1 The ~ operator (!T"#omplement$

    !he < operator performs the one=s complement function. It inverts the bits of the data so that

    a 1=s becomes a and a becomes a 1. Assuming that you have an 0#bit data represented by

    the following binary pattern 1111 and you wish to invert it, this what you have to do +

    'onvert it into hex format+ 1111 x'A 9' cannot represent data in binary

    format:

    3rite xA' and num_2> x*5, then result of num_1?

    num_2will yield x2(. !he operation is illustrated below.

    num_1> xA' > 1 1 1 1

    num_2> x*5 > 1 1 1 1 1

    ####################################################

    num_1 & num_2 > 1 1 > x2(

    11.2. The ) operator (!*$

    !he @ operator wors on two 9or more: bits and returns a 1 if at least one bit is 1 and returns a

    if all the bits are %ero. 'onsider again the two variables num_1> xA' and num_2> x*5.

    ing these two variables, we have

    170

  • 8/14/2019 11_LowLevel

    3/28

    11 Low level programming and Interfacing

    num_1> xA' > 1 1 1 1

    num_2> x*5 > 1 1 1 1 1

    ####################################################

    num_1 | num_2 > 1 1 1 1 1 1 1 > x;

    11.2.+ The , operator (-!*$

    !he expression returns a if both the bits are or both the bits are 1. therwise it returns a

    1. !he operation on numC1 and numC2 are shown below.

    num_1> xA' > 1 1 1 1

    num_2> x*5 > 1 1 1 1 1

    ####################################################

    num_1 ^ num_2 > 1 1 1 1 1 > x'B

    11.2. The // (shift left$ and 00 (shift right$ operators

    !he DD operator shifts the data left by the number of bit positions specified by the operand

    immediately following it.

    or example, the statement

    num_1 1 1 1 1

    num_1 > 4;

    means Eshift the value of num_1right by ( bit positionsF.

    17-

    Lost bits H shifted out

    illed up with

  • 8/14/2019 11_LowLevel

    4/28

    11 Low level programming and Interfacing

    num_1> 1 1 1 1

    num_1 >>4 > 1 1

    Below is a program example that illustrates the use of these bitwise operators.

    /* Program 11-1- Using bitwise operators */

    #in!u"e

  • 8/14/2019 11_LowLevel

    5/28

    11 Low level programming and Interfacing

    num_1and num_2are declared as type intwhich occupy 2 bytes each. 4ence num_1>

    xA' and num_2> x*5.

    8our turn

    1 ive the one=s complement of the following 9int: numbers+9a: 1 1111 9b: 11 11 11 11 9c: 11 11

    9d: x2*5 9e: x*5af 9f: x12 9g: xcb

    9h: 71 9i: (/72 9J: 12( 9: 0

    2 !he following variables are declared+int num1 ( 3420 num2 ( )=0 num3 ( )a=;

    ind the values of the following expressions+

    %a num2

    %b num1 | num2

    % num1| num2| num3

    %" num2 num3

    %e num1 ^ num2 ^ num3

    % num1 & num2 & num3

    %g num1 >2

    %i %num2 >1 %num2

  • 8/14/2019 11_LowLevel

    6/28

    11 Low level programming and Interfacing

    !he computer communicates with the outside world through its input#output 9IK: ports. Let

    us imagine that the computer you are woring on has an 0#bit output port with address x17.

    urther assume that this port is used to control a set of L;6s as shown in ig 11.1.

    utput port + x17

    Bit b5 b* b/ b( b7 b2 b1 b

    ig 11.1

    Bit b5 is the most significant bit and b is the least significant bit. An L;6 is turned on if the

    bit to which it is connected is asserted high 9at logic 1: and is turned off if it is pulled low 9at

    logic :. or example to turn on L;6s connected to bits b and b7, the binary pattern

    11 9>x-: has to be sent to the output port x17. !urbo ' has the following

    function to output data to an output port.

    outportb%port_a""0 out"ata;

    !his function outputs a byte 9hence the b after the word outport: of out"atato a hardware

    port with address port_a"". Its definition is found in dos.h header file. In this example

    port_a""> x17 and out"ata> x-. !he program to turn on L;6s at b7 and b is

    shown below.

    /* Program 11-2 @urn on AB:s at b3 an" b) */

    #in!u"e

  • 8/14/2019 11_LowLevel

    7/28

    11 Low level programming and Interfacing

    !he delay()function, whose prototype is also found in the dos.hheader file, suspends

    program execution for the interval of time 9in ms: specified in the parenthesis.

    8our turn

    1 3ith reference to ig 11.1, give the binary patterns and their hex e"uivalents re"uired tobe sent to the output port to turn on L;6s connected to bits

    9a: b5 and b*,

    9b: b5, b/, b7 and b1,

    9c: b*, b( and b2,

    9d: b5, b7, and b,

    2 Assume that L;6s at b5, b*, b2 and b are now on, what is the value9 in hex: re"uired to

    turn L;6s at b5 and b off while L;6s at b2 and b* remain on.

    xx

    11.+ a3ing L4's flash

    ;xamine $rogram 11#2 again. If the last three statements are put in a loop, then the L;6s at

    b7 and b will be turned on and off repeatedly at a rate of about 1 second. 3e would have

    effectively send a train of pulses with a fre"uency of / 4% to these two bits and cause the

    L;6s to flash. See ig 11.2

    ig 11.2

    /* Program 11-3 Ea?ing AB:s at b3 an" b) !as$*/

    #in!u"e

  • 8/14/2019 11_LowLevel

    8/28

    11 Low level programming and Interfacing

    main%'

    int port_a"" ( )13);int out"ata ( ))C; /* binar pattern D ))))1))1 */

    w$i!e%1'

    outportb%port_a""0 out"ata; /*on AB:s at b3 an" b) */"e!a %1))); /* or 1))) ms %1 s*/outportb%port_a""0 ))); /* t$en turn t$em o */"e!a %1)));

    !he last delay91: eeps the L;6s off for 1 second.

    8our turn

    1 3rite a program to mae L;6s at b2 and b/ flash at a rate of / 4%.

    2 odify your program so that the L;6s at b1 and b/ flash alternately. !hat is, when b2is on b/ is off and when b2 is off, b/ is on.

    94int+ &se two binary data patterns to be sent to the output port.:

    xx

    11. a3ing the lights move

    3e now want to turn the L;6s on one at a time starting with the L;6 at b. !his will give

    the illusion that the light is moving from right to left. 3e will have to start with data pattern

    1 9>x1: and send it out to the output port. !o turn on the next L;6, the pattern is

    shifted one bit position to the left before it is sent out to the output port . !he shifting process

    is repeated until all the L;6s have been lit up. !he shifting binary patterns to be sent to the

    output port are shown below.

    b5 b* b/ b( b7 b2 b1 b L;6 on

    1 b

    1 b1 1 b2

    1 b7

    1 b(

    1 b/

    1 b*

    1 b5

    $rogram 11#( below EmovesF the lights from is left to right.

    1((

  • 8/14/2019 11_LowLevel

    9/28

    11 Low level programming and Interfacing

    /* Program 11-4 EoFing !ig$ts */#in!u"e

  • 8/14/2019 11_LowLevel

    10/28

    11 Low level programming and Interfacing

    9d: 3hich L;6s are turned on when the loop is entered for the second time N

    9e: 3hen will the program brea out from the loop N

    9f: 6escribe, briefly, what the program does.

    xx

    11.5 &nother interfacing example 6 driving a stepper motor

    !his interfacing example uses the Bytronic stepper motor. !he Bytronic stepper motor comes

    with five connections and are labelled as 6, $, ;, /O and 12O as shown in ig.11.7

    ig 11.7 Bytronic stepper connections

    !he meaning of the connections and the re"uired inputs to these connections are listed below

    Label eaning e"uired inputs

    6 6irection A logic applied to this input will cause the motor

    to rotate in the clocwise direction

    A logic 1 will cause the motor to rotate in the anti

    clocwise direction

    $ $ulse A rising edge of a pulse is re"uired to rotate the

    motor one step. !his stepper motor re"uires (0

    pulses to mae one revolution.

    ne related feature is that the specified maximum

    pull#in rate of the stepper motor is 7/ steps per

    second 97/ pulsesKsec.:. $ull#in rate is the

    maximum switching rate at which a motor can

    move without losing a step. If the pulse rate exceed

    this figure, the motor will not wor.

    ; ;arth 'onnected to the )6 or O terminal of the power

    supply.

    /O / volts !o be connected to the / O power supply.

    12O 12 volts !o be connected to the 12 O power supply.

    1(*

    6 $ ; /O 12O

  • 8/14/2019 11_LowLevel

    11/28

    11 Low level programming and Interfacing

    8our turn

    1 iven above that (0 pulses are re"uired to rotate the motor through one complete

    revolution.

    9a: 3hat is the step angle N

    9b: If 7 pulses are applied to the stepper motor, find the angular displacement.

    9c: 4ow many pulses are re"uired to give an angular displacement of 9i: 7o, 9ii: *o,

    9iii: -o, 9iv: 10o, 9v: 25o, 9vi: 77o.

    2 If a continous train of pulses with a fre"uency of (0 4% is applied to the $ connection,

    what is the rotational speed 9in rpm: of the motor.

    7 epeat 2 for a fre"uency of 1 4%.

    ( &sing the maximum pull#in rate of 7/ stepsKs,

    9a: state the maximum fre"uency a pulse train that can be fed to this stepper motor

    9b: calculate the minimum duration between each pulse

    9c: show that the maximum speed is about (( revolutions per minute 9rpm:.

    9d: state if the following pulse trains shown in ig 11.( are suitable to drive the motor. If

    not, explain why. All duration shown are in milliseconds 9ms:.

    $ulse waveform or

    i

    ii

    iii

    1(5

    7 7periodic

    2 7 periodic

    27 periodic

  • 8/14/2019 11_LowLevel

    12/28

    11 Low level programming and Interfacing

    iv

    v

    ig 11.(

    xx

    11.7 #onnection to the comp8ter

    Let us use bit and bit 1 of the output port to drive the stepper motor. Bit is connected to

    the $ input and bit 1 is connected to the 6 input of the stepper motor. !he connection

    between the computer and stepper is shown in ig 11./ below.

    Bit b5 PP b7 b2 b1 b

    ig 11./

    !o drive the motor one step a pulse is re"uired to be sent to bit . Let us create a pulse withthe mar and space ratio of 1+1 and a width of ( ms, i.e, mar > 2 ms and space > 2ms. See

    ig 11.*

    ig 11.*

    !o set b to 1, we output the following binary pattern 1 9or x1: and use the delay9:

    function to eep it high for 2 ms. After that, we set it bac to by sending the binary pattern 9x: to the output port. !he statements are shown below+

    1(0

    6 $ ; /O 12O

    / O 12 O )6

    !o data signal

    gnd

    $ower Supply

    1 1 periodic

    aperiodic/ 2 7

    2 2

  • 8/14/2019 11_LowLevel

    13/28

    11 Low level programming and Interfacing

    outportb9outCadd, x1:Q

    delay92:Q

    outportb9outCadd, x:Q

    delay92:Q KR not necessary if we only want to drive the motor one step RK

    )ote also that the above instructions drives the stepper motor one step in the clocwise

    direction as b1 is maintained at logic .

    !he complete program to drive the stepper one step in the clocwise direction is shown

    below.

    /*Program 11- :riFing t$e stepper motor one step */#in!u"e #"eine E+GH ))1#"eine IP+,B )))

    main%

    'int port_a"" ( )13);int ount;

    1(-

  • 8/14/2019 11_LowLevel

    14/28

    11 Low level programming and Interfacing

    or%ount ( ); ount

  • 8/14/2019 11_LowLevel

    15/28

    11 Low level programming and Interfacing

    revolutions. All you have to do is to add an outer loop to repeat the statements which cause

    the motor to mae 1 revolution. 'ompare your program with the one below./*Program 11-K Eotor rotates 3 reFo!utions in t$e !o?wise "iretion */#in!u"e #"eine E+GH ))1#"eine IP+,B )))#"eine ,A,H9MIB )))#"eine ,_,AH9MIB ))2 /*))))))1) */

    main%'

    int port_a"" ( )13);int ount0 no_o_reF(3;

    w$i!e%no_o_reF L()'

    or%ount ( ); ount

  • 8/14/2019 11_LowLevel

    16/28

    11 Low level programming and Interfacing

    no_o_reF --;

    !he A value is ed with the 'C'L3IS; value resulting in the data shown below

    A > x1 > 1

    '#'L3IS; > x2 > 1

    CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

    > 1 1

    !he resulting data gives a pulse to the stepper motor and at the same time set the direction to

    anti clocwise9A'3:. Similarly the expression S$A'; @ 'C'L3IS; results in a data value

    of x2 which eeps the direction A'3 because bit b1 is still at logic 1.

    Below is another program example which mae the motor rotates / revolutions in the

    clocwise dircection and then followed by 7 revolutions in the anti clocwise direction. !he

    cycle repeats until the ;S' ey is pressed.

    /Program 11-1) N = reFo!utions ,9 an" 3 reFo!utions +,9 */#in!u"e

  • 8/14/2019 11_LowLevel

    17/28

    11 Low level programming and Interfacing

    w$i!e%no_o_reF L() /* 3 ,,9 reFo!utions */'

    or%ount ( ); ount

  • 8/14/2019 11_LowLevel

    18/28

    11 Low level programming and Interfacing

    int port_a"" ( )13);int ount0 no_o_reF0 m?0sp0time_";$ar ?e;

    Foi" pu!se_motor %int m?_"ata0 int sp_"ata0 int time_"e!a;

    w$i!e %?e L(BI,'

    "o'no_o_reF(=;w$i!e%no_o_reF L() /* = ,9 reFo!utions */'

    m? ( E+GH | ,AH9MIB;sp ( IP+,B | ,AH9MIB;time_" ( 2;or%ount ( ); ount

  • 8/14/2019 11_LowLevel

    19/28

    11 Low level programming and Interfacing

    2 !he mar and space duration in the pulseCmotor9: function is the same. odify thefunction so that the mar and space can have dissimilar duration.

    xx

    11.: as3ing o8t 8nwanted bits

    Sometimes, we wish only to focus our attention on certain bits in a data and ignore the others.

    3e can Tmas= out the unwanted bits by setting these bits to . !his is achieved by A)6ing

    9with the ? operator: the unwanted bits with and the re"uired bits with 1. or example, to

    chec b7, we A)6 the data with this binary pattern 1 9>x(:. !he masing

    operation is shown below.

    6ata b5 b* b/ b( b7 b2 b1 b

    as 1

    6ata ?

    as

    b7

    !he masing operation shows that all the unwanted bits are set to while the re"uired bit

    9b7: remains.

    4ere is a program example that chec if b/ is at logic or 1. !he mas to chec b/ is

    1 9>x2:.

    /*Program 11-13 mas?ing operation */#in!u"e

  • 8/14/2019 11_LowLevel

    20/28

    11 Low level programming and Interfacing

    94int + 8ou need two masing patterns:

    1/*

  • 8/14/2019 11_LowLevel

    21/28

    11 Low level programming and Interfacing

    7 ;xamine $rogram 11#1( and answer the following "uestions.

    /*Program 11-14 */#in!u"e

  • 8/14/2019 11_LowLevel

    22/28

    11 Low level programming and Interfacing

    Another of version of $rogram 11#1( which mae use of the for loop is shown below+

    /*Program 11-1= +not$er Fersion o Program 11-. */#in!u"e

  • 8/14/2019 11_LowLevel

    23/28

    11 Low level programming and Interfacing

    3hen the switch is pressed, a logic 1 9/ O: is applied to the bit of the port to which this

    switch is connected, otherwise a logic 9 O: is applied to it.

    !o read the status of the switch, we use the function

    inportb%port_a"";

    which reads a byte from a hardware port with address port_a"". !he prototype is found in

    the "os$header file.

    !he program below checs if switch S3 are pressed.

    /*Program 11-1 ,$e? swit$ I9) */#in!u"e#in!u"e

  • 8/14/2019 11_LowLevel

    24/28

  • 8/14/2019 11_LowLevel

    25/28

    11 Low level programming and Interfacing

    !he algorithm for the pump control system is shown in the flow chart in ig 11.1 below.

    ig 11.1

    1*1

    n $1ff $2

    ead input

    L1reached N

    ff $1n $2

    L7reached N

    L7reached N

    Sound Alarmff $1 ? $2

    ead input

    Start

    eset N

    L2reached N

    8es

    )o

    8es

    8es

    8es

    8es

    )o

    )o

    )o

    )o

    9L1 may be faulty so

    chec L7:

    9$1 may not have turned

    off, so chec L7:

    93ait for the esetey 9: to be hit:

  • 8/14/2019 11_LowLevel

    26/28

    11 Low level programming and Interfacing

    Before we write the program, let us summarise the values that will be obtained at the input

    and the values re"uired at the output.

    At the input port x11, the input assignments are as follows+

    bit device Oalues and meanings Action

    b Sensor L1 H 9open contacts: Li"uid has not reachedthis level

    1 H 9close contacts: Li"uid has reached

    level

    #

    n $2, ff $1

    b1 Sensor L2 H 9open contacts: Li"uid has fallen below

    this level

    1 H 9close contacts: Li"uid has not fallen

    below this level

    #

    n $1, ff $2

    b2 Sensor L7 H 9open contacts: Li"uid has not reached

    this level

    1 H 9close contacts: Li"uid has reached

    level

    #

    ff $1 ? $2

    n Alarm

    b7#b5 )ot used

    At the output port x17, the bit assignment are as follows+

    bit device Oalues and meanings

    b $ump $1 H !urn off pump

    1 H !urn on pump

    b1 $ump $2 H !urn off pump

    1 H !urn on pump

    b2 Alarm

    Indicator

    H ff annunciation

    1 H n annunciation

    b7#b5 )ot used

    As the addresses of the IK ports are constants, we will define them to be so. 4ence

    Udefine I)CA66 x11

    Udefine &!CA66 x17

    3e define as constants also the masing patterns to test bit b,b1and b2 as follows+

    Udefine ASCL1 x1 KR1RK

    Udefine ASCL2 x2 KR1RK

    Udefine ASCL7 x( KR1RK

    or output control, we have

    Udefine )C$1 x1 KR1RK

    Udefine )C$2 x2 KR1RK

    Udefine )CAL x( KR1RK

    !he program for the control system is shown in $rogram 11#11.

    1*2

  • 8/14/2019 11_LowLevel

    27/28

    11 Low level programming and Interfacing

    /* Program 11-1K Pump ,ontro! Istem */#"eine M_+:: )11)#"eine U@_+:: )13)#"eine E+IH_A1 ))1 /*)))))))1*/#"eine E+IH_A2 ))2 /*))))))1)*/

    #"eine E+IH_A3 ))4 /*)))))1))*/#"eine _P1 ))1 /*)))))))1*/#"eine _P2 ))2 /*))))))1)*/#"eine _+AE ))4 /*)))))1))*/#in!u"e

  • 8/14/2019 11_LowLevel

    28/28

    11 Low level programming and Interfacing

    print%5Istem ai!ure7n7n8;print%5,$e? pumps an" sensors7n8;print%59$en au!ts are retiie"0 press G to reset sstem7n8;Geset%;a!arm_A3();!eFe! ( );

    /* en" o w$i!e%1 */ /* en" o main% */

    /* User "eine" untion */

    int Geset%'

    $ar ?e;

    w$i!e %?e L( TG && ?e L(r ?e(get$%;

    !he above program is by no mean the Tbest= program. 8ou may write a more compact

    program maing use of functions and pointers. !ry writing it.

    8our turn

    1 odify the program to print out the time at which the pumps are turned on. !he display

    may loo as follows+

    2 &se graphics, display the control system on the computer. &se a colour code to indicate ifa pump is on or off.

    xx

    Pump ,ontro! Istem-------------------

    Itatus N orma!

    11N3= Pump 1N Pump 2N RR12N=) Pump 1N RR Pump 2N