Sei sulla pagina 1di 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 smo e 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 textboo s 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 1,111,1,. Step1+ 1rganise it into groups of ( bits starting from the rightmost bit, thus, giving 1,11 1,1, Step 2 + 3rite the hex e"uivalent for each group. 1,11 1,1, B A

4ence the hex e"uivalent of binary number 1,111,1, 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 ,111 6 11,1

!he binary e"uivalent of ,x56 is ,11111,1. 175

11 Low level programming and Interfacing

8our turn 1 2 'onvert the following binary numbers to their hexadecimal e"uivalent+ 9a: 111,1,11 9b: 1,1,1,1, 9c: ,,11,,11 9d: ,1,1,1,1 '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 11,,1,1, and you wish to invert it, this what you have to do + 'onvert it into hex format+ 11,,1,1, ,x'A 9' cannot represent data in binary format: 3rite <,x'A to get the one=s complement of the data. And the result will be ,,11,1,1 9>,x7/: 11.2.2 The % operator (& '$ 6o not confuse this bitwise ? operator 9single ampersand: with the logical ?? operator 9double ampersands:. !he bitwise ? operator wor s on two 9or more: bits. If both bits are 1, the ? expression will return a 1. If any one of the bit is ,, the ? expression will return a ,. .or example, if int variables num_1> ,xA' and num_2 > ,x*5, then result of num_1 ? num_2 will 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 wor s 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. 1Aing these two variables, we have

170

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 B expression returns a , if both the bits are , or both the bits are 1. 1therwise it returns a 1. !he B 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 << 2; means Eshift the value of num_1 left by 2 bit positionsF. Lost bits H shifted out num_1 > 1 , 1 , 1 1 , , num_1 <<2 > 1 , 1 1 , , , , .illed up with , !he GG operator shifts the data right by the number of bit positions specified by the operand immediately following it. .or example, the statement num_1 >> 4; means Eshift the value of num_1 right by ( bit positionsF.

17-

11 Low level programming and Interfacing

Lost bits H shifted out num_1 > 1 , 1 , 1 1 , , num_1 >>4 > , , , , 1 , 1 , .illed up with , Below is a program example that illustrates the use of these bitwise operators.
/* Program 11-1- Using bitwise operators #in !u"e <st"io#$> main%& ' int num_1()*+,; /* 1)1)11)) */ int num_2()*-.; /* )11))111 */ int /ot_ans0 +n"_ans0 1r_ans0 s$r_ans0 s$!_ans; /ot_ans ( 2num_1 +n"_ans ( num_1 & num_2; 1r_ans ( num_1 | num_2; s$!_ans ( num_1 <<2; s$r_ans ( num_2 >>3; print4%5/um 1 ( 6* $e* an" /um 2 ( 6* $e*#7n80num_10num_2&; print4%5,omp!ement o4 /um 1 is 6* $e*#7n80/ot_ans&; print4%59$en +/:e"0 t$e answer is 6* $e*#7n80 +n"_ans&; print4%59$en 1re"0 t$e anser is 6* $e*#7n801r_ans&; print4%5/um 1 s$i4t !e4t b; 2 bits ( 6* $e*#7n80 s$!_ans&; print4%5/um 2 s$i4t rig$t b; 3 bits ( 6* $e*#7n80 s$r_ans&; < */

!he printout will be /um 1 ( a $e* an" /um 2 ( -. $e*# ,omp!ement o4 /um 1 is 44=3 $e*# 9$en +/:e"0 t$e answer is 24 $e*# 9$en 1re"0 t$e answer is e4 $e*# /um 1 s$i4t !e4t b; 2 bits ( 2b) $e*# /um 2 s$i4t rig$t b; 3 bits ( $e*#

!he printout shows that the complement of )um 1 results in a (#digit hex number and the shifting of )um 1 by 2 bit positions gives a 7#digit hex number. !his is so because both

1(,

11 Low level programming and Interfacing

num_1 and num_2 are declared as type int which occupy 2 bytes each. 4ence num_1 > ,x,,A' and num_2 > ,x,,*5. 8our turn 1 Iive the one=s complement of the following 9int: numbers+ 9a: ,,,, ,,,, ,1,, 1111 9b: ,1,1 ,,11 11,, 1,1, 9c: ,,11 ,,11 9d: ,x2,*5 9h: 71,,, 2 9e: ,x*5af 9i: (/72 9f: ,x12 9J: 12( 9g: ,xcb 9 :0

!he following variables are declared+ int num1 ( 3420 num2 ( )*=40 num3 ( )*a=-; .ind the values of the following expressions+ %a& %b& % & %"& %e& %4& %g& %$& %i& %>& %?& %!& %m& %n& 2num2 num1 | num2 num1| num2| num3 num2 ^ num3 num1 ^ num2 ^ num3 num1 & num2 & num3 num1 << 1 num3 >>2 2%num2 << 2& 2num1 & %num3 >>1& ^ %num1 ^num2& << 2 %2%num2 | num3& << 1 & & %2num1& %%num1 ^num2& << 2& >(=)) %%num1<<1& & %num2 >>2& <( 2num3& %num2 <<1&

xx 11.( 1imple interfacing 1(1

11 Low level programming and Interfacing

!he computer communicates with the outside world through its input#output 9IK1: ports. Let us imagine that the computer you are wor ing 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. 1utput 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 ,,,,1,,1 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"ata to 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 <st"io#$> #in !u"e <"os#$> main%& ' int port_a"" ( )*13); out"ata ( )*)C; /* binar; pattern D ))))1))1 */ outportb%port_a""0 out"ata&; "e!a; %1)))&; outportb%port_a""0 )*))&; /*on AB:s at b3 an" b) */ /* 4or 1))) ms %1 s&*/ /* t$en turn t$em o44 */

<

1(2

11 Low level programming and Interfacing

!he delay() function, whose prototype is also found in the dos.h header 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 to be 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,,

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.+ 2a3ing 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

L;6 on for 1s L;6 off for 1s .ig 11.2


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

#in !u"e <st"io#$> #in !u"e <"os#$>

1(7

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&; "e!a; %1)))&; outportb%port_a""0 )*))&; "e!a; %1)))&; < <

/*on AB:s at b3 an" b) */ /* 4or 1))) ms %1 s&*/ /* t$en turn t$em o44 */

!he last delay91,,,: eeps the L;6s off for 1 second. 8our turn 1 2

3rite a program to ma

e L;6s at b2 and b/ flash at a rate of /,, 4%.

Lodify your program so that the L;6s at b1 and b/ flash alternately. !hat is, when b2
is 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.. 2a3ing 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>,x,1: 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 , , , , , , , 1 , b* , , , , , , 1 , , b/ , , , , , 1 , , , b( , , , , 1 , , , , b7 , , , 1 , , , , , b2 , , 1 , , , , , , b1 , 1 , , , , , , , b, 1 , , , , , , , , L;6 on b, b1 b2 b7 b( b/ b* b5

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

1((

11 Low level programming and Interfacing

/* Program 11-4 EoFing !ig$ts */ #in !u"e <st"io#$> #in !u"e <"os#$> main%& ' int port_a"" ( )*13); int out"ata ( )*)1 /*)))))))1 */ "o ' outportb%port_a""0out"ata&; out"ata ( out"ata <<1; "e!a; %1)))&; < w$i!e %out"ata&;

<

8our turn 1 2 Lodify $rogram 11#( to ma e the lights appear to move from left to right. ;xamine $rogram 11#/ below and answer the following "uestions.

/* Program 11-= */ #in !u"e <st"io#$> #in !u"e <"os#$> main%& ' int port_a"" ( )*13); int out"ata1 ( )*)1; int out"ata2 ( )*)1; "o ' outportb%port_a""0out"ata1&; "e!a;%1)))&; out"ata2 ( out"ata2 <<1; /*or use out"ata <<( 1 */ out"ata1 ( out"ata1 | out"ata2; < w$i!e %out"ata2&; <

/* binar; pattern D )))))))1 */ /* binar; pattern D )))))))1 */

Muestions 9a: 3hich L;6 is turned on when the do..while loop is run for the first time N 9b: 3hat will be the value of outdata2 after the statement out"ata2 ( out"ata2 <<1; is executed for the first timeN 9c: 3hat will be the value of outdata1 after the statement out"ata1(out"ata1 | out"ata2; is executed for the first timeN 1(/

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

/O 12O

.ig 11.7 Bytronic stepper connections !he meaning of the connections and the re"uired inputs to these connections are listed below Label 6 Leaning 6irection Ae"uired inputs A logic , applied to this input will cause the motor to rotate in the cloc wise direction A logic 1 will cause the motor to rotate in the anti cloc wise direction A rising edge of a pulse is re"uired to rotate the motor one step. !his stepper motor re"uires (0 pulses to ma e one revolution. 1ne related feature is that the specified maximum pull#in rate of the stepper motor is 7/, steps per second 9 7/, 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 . 'onnected to the I)6 or ,O terminal of the power supply. !o be connected to the / O power supply. !o be connected to the 12 O power supply.

$ulse

; /O 12O

;arth / volts 12 volts

1(*

11 Low level programming and Interfacing

8our turn 1 Iiven 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: 7,o, 9ii: *,o, 9iii: -,o, 9iv: 10,o, 9v: 25,o, 9vi: 77,o. 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.

Aepeat 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 i
7 7
periodic

or

ii
2 7
periodic

iii
7 2
periodic

1(5

11 Low level programming and Interfacing

iv
1 1
periodic

v
/ 2 7
aperiodic

.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.

$ower Supply / O 12 O I)6

/O 12O

!o data signal gnd

Bit .ig 11./

b5

PP

b7

b2

b1

b,

!o drive the motor one step a pulse is re"uired to be sent to bit ,. Let us create a pulse with the 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 ,x,1: and use the delay9: function to eep it high for 2 ms. After that, we set it bac to , by sending the binary pattern ,,,,,,,, 9,x,,: to the output port. !he statements are shown below+ 1(0

11 Low level programming and Interfacing

outportb9outCadd, ,x,1: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 cloc wise direction as b1 is maintained at logic ,. !he complete program to drive the stepper one step in the cloc wise direction is shown below.
/*Program 11-- :riFing t$e stepper motor one step */ #in !u"e <st"io#$> #"e4ine E+GH )*)1 #"e4ine IP+,B )*)) main%& '

int port_a"" ( )*13); outportb%port_a""0 E+GH&; "e!a;%2&; outportb%port_a""0 IP+,B&; "e!a;%2&;

<

In the above program, two constants LAAS and S$A'; are defined to represent the data to be sent to the output port. It is more meaningful to represent data with indicative names than Just writing out numbers. !o move the motor / steps, you will need to repeat the following statements
outportb%port_a""0 E+GH&; "e!a;%2&; outportb%port_a""0 IP+,B&; "e!a;%2&;

five times using one the looping control structures H for, doPwhile or while. !he modified program below moves the motor / steps in the cloc wise direction.
/*Program 11-. :riFing t$e motor = steps in t$e /* wit$ : ( ) */ #in !u"e <st"io#$> #"e4ine E+GH )*)1 #"e4ine IP+,B )*)) main%& ' !o ?wise "ire tion */

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

1(-

11 Low level programming and Interfacing

4or% ount ( ); ount <(=; ountJJ& ' outportb%port_a""0 E+GH&; "e!a;%2&; outportb%port_a""0 IP+,B&; "e!a;%2&; < <

8our turn 1 2 ;xpress LAAS and S$A'; in binary format. In the outportb9portCadd,LAAS: statement which bit of the output port is set to 1 and for how long does this bit remain high. !he bit mentioned in 2 is set to , with the outportb9portCadd,S$A';:. .or how long does it remain low. 4ow many pulses are sent to this port and what will be the approximate angular displacement of the motorN

S etch the pulse train and indicate on the drawing the approximate mar and space duration. 'alculate the fre"uency of the pulse train.

Lodify the program so that the mar


pulses are to be generated.

and space duration is about 7 ms each and 2, such

.ollow on from * above, modify the program so that (0 pulses will be generated. 3hat
will be the angular displacement after (0 pulses are sent to the motorN

xx Let us now ma e the motor rotates 7 complete revolutions. If you have completed the E8our turnF exercise 9no 5: above, you will have a program that ma e the motor rotate one complete revolution. 3ith slight modifications, you will be able to ma e the motor rotate 7 complete

1/,

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 ma e 1 revolution. 'ompare your program with the one below.
/*Program 11-K Eotor rotates 3 reFo!utions in t$e #in !u"e <st"io#$> #"e4ine E+GH )*)1 #"e4ine IP+,B )*)) main%& ' int port_a"" ( )*13); int ount0 no_o4_reF(3; w$i!e%no_o4_reF L()& ' 4or% ount ( ); ount <(4K; ountJJ& ' outportb%port_a""0 E+GH&; "e!a;%2&; outportb%port_a""0 IP+,B&; "e!a;%2&; < no_o4_reF --; < !o ?wise "ire tion */

<

11.9

#hanging the direction of rotation

Aecall that a , at the 6 input of the stepper motor will cause it to rotate in the cloc wise direction. Incidentally, all the programs examples listed so far cause the motor to rotate in a cloc wise direction because bit b1, which is connected to the 6 input, is always set to ,. !o ma e the motor to rotate in the anti cloc wise direction, bit b1 has to be set to 1. $rogram 11# - below ma es the motor rotate 7 revolutions in the anti cloc wise direction.
/*Program 11-C Gotates motor in t$e anti !o ?wise "ire tion */ #in !u"e <st"io#$> #"e4ine E+GH )*)1 #"e4ine IP+,B )*)) #"e4ine ,A1,H9MIB )*)) #"e4ine ,_,AH9MIB )*)2 /*))))))1) */ main%& ' int port_a"" ( )*13); int ount0 no_o4_reF(3; w$i!e%no_o4_reF L()& ' 4or% ount ( ); ount <(4K; ountJJ& ' outportb%port_a""0 E+GH|,_,AH9MIB &; "e!a;%2&; outportb%port_a""0 IP+,B|,_,AH9MIB&; "e!a;%2&; <

1/1

11 Low level programming and Interfacing

< <

no_o4_reF --;

!he LAAS value is 1Aed with the 'C'LS3IS; value resulting in the data shown below LAAS > ,x,1 > ,,,,,,,1 '#'LS3IS; > ,x,2 > , , , , , , 1 , CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC > ,,,,,,11 $ulse 6irection > 1 A'3 !he resulting data gives a pulse to the stepper motor and at the same time set the direction to anti cloc wise9A'3:. Similarly the expression S$A'; @ 'C'LS3IS; results in a data value of ,x,2 which eeps the direction A'3 because bit b1 is still at logic 1. Below is another program example which ma e the motor rotates / revolutions in the cloc wise dircection and then followed by 7 revolutions in the anti cloc wise 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 <st"io#$> #in !u"e < onio#$> #"e4ine E+GH )*)1 #"e4ine IP+,B )*)) #"e4ine ,A1,H9MIB )*)) #"e4ine ,_,AH9MIB )*)2 /*))))))1) */ #"e4ine BI, 2. main%& '

int port_a"" ( )*13); int ount0 no_o4_reF(=; $ar ?e;;

w$i!e %?e; L(BI,& ' "o' no_o4_reF(=; w$i!e%no_o4_reF L()& /* = ,9 reFo!utions */ ' 4or% ount ( ); ount <(4K; ountJJ& ' outportb%port_a""0 E+GH| ,AH9MIB &; "e!a;%2&; outportb%port_a""0 IP+,B| ,AH9MIB&; "e!a;%2&; < no_o4_reF --; < no_o4_reF ( 3;

1/2

11 Low level programming and Interfacing

w$i!e%no_o4_reF L()& /* 3 ,,9 reFo!utions */ ' 4or% ount ( ); ount <(4K; ountJJ& ' outportb%port_a""0 E+GH| ,_,AH9MIB &; "e!a;%2&; outportb%port_a""0 IP+,B| ,_,AH9MIB&; "e!a;%2&; < no_o4_reF --; < < w$i!e %L?b$it%&&; /* no ?e;boar" is $it */ ?e;(get $%&; /* rea" ?e; i4 ?e;boar" is $it */ < <

!he two sections


outportb%port_a""0 E+GH| ,AH9MIB &; "e!a;%2&; outportb%port_a""0 IP+,B| ,AH9MIB&; "e!a;%2&;

and
outportb%port_a""0 E+GH| ,_,AH9MIB &; "e!a;%2&; outportb%port_a""0 IP+,B| ,_,AH9MIB&; "e!a;%2&;

in the program loo s "uite similar except for output data. If we create a function with the above statements and when called with the appropriate arguments, then the above program can be made more compact. !he function is shown below.
Foi" pu!se_motor%int m?_"ata0 int sp_"ata0 int time_"e!a;& ' outportb%port_a""0 m?"ata&; "e!a;%time_"e!a;&; outportb%port_a""0 sp_"ata&; "e!a;%time_"e!a;&; <

!he program with the modifications incorporated is shown below.


/*Program 11- 12 N Eo"i4ie" Program 11-11 N Using user "e4ine 4un tion */ #in !u"e <st"io#$> #in !u"e < onio#$> #"e4ine E+GH )*)1 #"e4ine IP+,B )*)) #"e4ine ,A1,H9MIB )*)) #"e4ine ,_,AH9MIB )*)2 /*))))))1) */ #"e4ine BI, 2. main%& '

1/7

11 Low level programming and Interfacing

int port_a"" ( )*13); int ount0 no_o4_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_o4_reF(=; w$i!e%no_o4_reF L()& /* = ,9 reFo!utions */ ' m? ( E+GH | ,AH9MIB; sp ( IP+,B | ,AH9MIB; time_" ( 2; 4or% ount ( ); ount <(4K; ountJJ& ' pu!se_motor%m?0sp0time_"&; < no_o4_reF --; < no_o4_reF ( 3; w$i!e%no_o4_reF L()& /* 3 ,,9 reFo!utions */ ' m? ( E+GH |,_,AH9MIB; sp ( E+GH |,_,AH9MIB; time_" ( 2; 4or% ount ( ); ount <(4K; ountJJ& ' pu!se_motor%m?0sp0time_"&; < no_o4_reF --; < < w$i!e %L?b$it%&&; /* no ?e;boar" is $it */ ?e;(get $%&; /* rea" ?e; i4 ?e;boar" is $it */ < < /* User "e4ine" 4un tion */ Foi" pu!se_motor%int m?_"ata0 int sp_"ata0 int time_"e!a;& ' outportb%port_a""0 m?"ata&; "e!a;%time_"e!a;&; outportb%port_a""0 sp_"ata&; "e!a;%time_"e!a;&; <

8our turn 1

Lodify the above program so that the motor rotates ( revolutions in the cloc
direction and * revolutions in the anti cloc wise direction.

wise

1/(

11 Low level programming and Interfacing

and space duration in the pulseCmotor9: function is the same. Lodify the function so that the mar and space can have dissimilar duration. xx 11.: 2as3ing 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 mas ing operation is shown below.

!he mar

6ata Las 6ata ? Las

b5 , ,

b* , ,

b/ , ,

b( , ,

b7 1 b7

b2 , ,

b1 , ,

b, , ,

!he mas ing 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 <st"io#$> main%& ' int "ata0 mas?ing_b= ( )*2); i4 %"ata & mas?ing_b=& /* some Fa!ue- meaning b= is 1 */ print4 %5Oit = is one#7n8&; e!se print4%5Oit = is Pero#7n8&; <

8our turn 1 3hat are the mas ing patterns re"uired to chec the following bits of a data. 9a: b5 9b: b* 9c: b( 9d: b7 9e: b2 9f: b1 9g: b,

3rite a program to chec if b* and b2 are at logic 1 or ,.

1//

11 Low level programming and Interfacing

94int + 8ou need two mas ing patterns:

1/*

11 Low level programming and Interfacing

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

/*Program 11-14 */ #in !u"e <st"io#$> main%& ' int "ata ( )*,-; /*test "ata N11)) )11) */ int mas?ing_pattern ( )*)1; int bit (); "o ' i4 %"ata & mas?ing_pattern& print4 %Qbit 62" is 1#7nQ0bit&; e!se print4 %Qbit 62" is )#7nQ0bit&; bitJJ; < w$i!e %%mas?ing_pattern <<(1& <( )*K)&;

<

Muestions+ 9a: 3hen the do..while loop is run for the first time, what will be the value of the conditional expression (data & masking_pattern) and what will be printed N 9b: 3hat does the statement masking_pattern <<=1; do and what will be the value of mas ingCpattern after the first round of the loop. 3ill this value cause the program to brea out from the loop. 9c: 3hen will the program brea out from the loop N 9d: 3hen the loop is run for the second time, what are the values of 9i: data, 9ii: masking_pattern and 9iii: the conditional expression (data & masking_pattern)N 3hat will be printed out N 9e: Briefly describe what the program does. xx

1/5

11 Low level programming and Interfacing

Another of version of $rogram 11#1( which ma e use of the for loop is shown below+
/*Program 11-1=# +not$er Fersion o4 Program 11-. */ #in !u"e <st"io#$> main%& ' int "ata ( )*,-; int mas?ing_pattern ( )*)1; int bit (); 4or%mas?ing_pattern(1; mas?ing_pattern<()*K); mas?ing_pattern<<(10bitJJ& ' i4 %"ata& mas?ing_pattern& print4%QOit 62" is 1#7nQ0bit&; e!se print4%QOit 62" is )#7nQ0bit&; < <

11.1; *eading data from an inp8t port !he computer accepts input from the outside world via an input port. Imagine that the computer has an input port with address ,x11, and four switches are connected to the ( lower nibbles of the input port as shown in .ig 11.5. Input port + ,x11, Bit b5 b* b/ b( b7 b2 b1 b, S3, S31 S32 S37 /O

.ig 11.5

1/0

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 chec s if switch S3, are pressed.
/*Program 11-1- ,$e ? swit $ I9) */ #in !u"e<st"io#$> #in !u"e<"os#$> main%& ' int in_"ata0 mas? ( )*)1; int port_a"" ( )*11); in_"ata ( inportb%)*11)&; i4 %in_"ata & mas?& print4%5Iwit $ I9) is presse"#7n8&; e!se print4%5Iwit $ I9) is not presse"#7n8&;

<

!he above program can be easily modified to test all the switches. !he program to test all the switches is shown below.
/*Program 11-1. D ,$e ?ing a!! t$e swit $es */ #in !u"e<st"io#$> #in !u"e<"os#$> main%& ' int in_"ata0 sw()0 mas? ( )*)1; int port_a"" ( )*11); in_"ata ( inportb%)*11)&; 4or %mas?(1; mas? <()*)K; mas?<<(10swJJ& ' i4 %in_"ata & mas?& print4%5Iwit $ I962" is presse"#7n80sw&; e!se print4%5Iwit $ I962" is not presse"#7n80sw&; < <

1/-

11 Low level programming and Interfacing

11.11 & simple control system Let us use the computer in a simple pump control system as shown in .ig 11.0. L1, L2 and L7 are li"uid level sensors and $1 and $2 are pumps. 3hen li"uid level reaches L1, pump $1 will be turned off and pump $2 will be turned on. As the li"uid level reaches L2, pump $2 will be off and pump $1 will be on. And, if, for some reasons, pump $1 does not turn off when li"uid reaches L1 and the li"uid level continue to rise to sensor L7, an alarm will be sounded.

$1 L7 L1 L2 $2

.ig 11.0 Assume that the level sensors wor li e switches and each has a set voltage#free contacts. 3hen it is submerged in li"uid, it gives a closed contact, it is otherwise an open contact. 3e will use b,, b2 and b7 of the input port to monitor level sensors L1, L2 and L7 respectively. Similarly, we will use b,, b2 and b7 of the output port to drive pumps $1, $2 and the Alarm respectively. Assume also that the necessary actuation circuitry for the pumps are already available. A logic 1 is re"uired to turn on the pump and a logic , will turn it off. !he bloc diagram of the control system is shown in .ig 11.- below. /O
Input port+ ,x11, b, b1 b2 1utput port+ ,x17, b, b1 b2 Alarm $1 $2

L1 L2 L7

.ig 11.-

1*,

11 Low level programming and Interfacing

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

1n $1 1ff $2

Aead input

)o L1 reached N 8es
9L1 may be faulty so chec L7:

)o

L7 reached N

8es

1ff $1 1n $2

Aead input

)o 8es L2 reached N )o L7 reached N 8es

9$1 may not have turned off, so chec L7:

Sound Alarm 1ff $1 ? $2


93ait for the Aeset ey 9A: to be hit:

8es

Aeset N

)o

.ig 11.1,

1*1

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 b, Sensor L1 , H 9open contacts: Li"uid has not reached this level 1 H 9close contacts: Li"uid has reached level 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 b2 Sensor L7 , H 9open contacts: Li"uid has not reached this level 1 H 9close contacts: Li"uid has reached level 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 b2 b7#b5 $ump $2 Alarm Indicator , H !urn off pump 1 H !urn on pump , H 1ff annunciation 1 H 1n annunciation )ot used Action # 1n $2, 1ff $1 # 1n $1, 1ff $2 # 1ff $1 ? $2 1n Alarm

As the addresses of the IK1 ports are constants, we will define them to be so. 4ence Udefine I)CA66 ,x11, Udefine 1&!CA66 ,x17, 3e define as constants also the mas ing patterns to test bit b,,b1and b2 as follows+ Udefine LASSCL1 Udefine LASSCL2 Udefine LASSCL7 .or output control, we have Udefine 1)C$1 ,x,1 Udefine 1)C$2 ,x,2 Udefine 1)CALL ,x,( KR,,,,,,,1RK KR,,,,,,1,RK KR,,,,,1,,RK ,x,1 ,x,2 ,x,( KR,,,,,,,1RK KR,,,,,,1,RK KR,,,,,1,,RK

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

11 Low level programming and Interfacing

/* Program 11-1K Pump ,ontro! I;stem */ #"e4ine M/_+:: )*11) #"e4ine 1U@_+:: )*13) #"e4ine E+IH_A1 )*)1 /*)))))))1*/ #"e4ine E+IH_A2 )*)2 /*))))))1)*/ #"e4ine E+IH_A3 )*)4 /*)))))1))*/ #"e4ine 1/_P1 )*)1 /*)))))))1*/ #"e4ine 1/_P2 )*)2 /*))))))1)*/ #"e4ine 1/_+AE )*)4 /*)))))1))*/ #in !u"e <st"io#$> #in !u"e <"os#$> main%& ' int in_"ata0 !eFe!0a!arm_A3; !rs r%&; print4%5Pump ,ontro! I;stem7n8&; print4%5-------------------7n7n7n8&; w$i!e%1& ' !eFe! (); / *!eFe! 4!ag 1( !eFe! rea $e"0 ) !eFe! not rea $e" */ a!arm_A3 ( ); /* a!arm 4!ag D 1( a!arm0 ) no a!arm */ goto*;%=01)&;print4%5Itatus N /orma!7n8&; outportb%1U@_+::01/_P1&; goto*;%-01)&;print4%5Pump1 N 1/0 Pump 2N 1RR 7n8&; "o' in_"ata(inportb%M/_+::&; i4 %in_"ata&E+IH_A1& ' outportb%1U@_+::0 1/_P2&; !eFe! ( 1; goto*;%1)01)&; print4%5Pump 1N 1RR0 Pump 2N 1/7n8&; < i4 %in_"ata &E+IH_A3& ' a!arm_A3 ( 1; !eFe! ( 1; < <w$i!e %L!eFe!&; i4 %!eFe! & La!arm_A3& ' !eFe! (); a!arm_A3(); "o' in_"ata(inportb%M/_+::&; i4 %2in_"ata&E+IH_A2& !eFe! ( 1; i4 %in_"ata &E+IH_A3& ' a!arm_A3 ( 1; !eFe! ( 1; < <w$i!e %L!eFe!&; < i4 %a!arm_3& ' outport %1U@_+::0 1/_+AE&; !rs r%&; print4%5+!arm on"itionS7n7n8&;

/* /ee" to inFert b2 */

1*7

11 Low level programming and Interfacing

print4%5I;stem 4ai!ure#7n7n8&; print4%5,$e ? pumps an" sensors#7n8&; print4%59$en 4au!ts are re ti4ie"0 press G to reset s;stem#7n8&; Geset%&; a!arm_A3(); !eFe! ( ); < < /* en" o4 w$i!e%1& */ < /* en" o4 main%& */

/* User "e4ine" 4un tion */ int Geset%& ' $ar ?e;; < w$i!e %?e; L( TGU && ?e; L(UrU& ?e;(get $%&;

!he above program is by no mean the Tbest= program. 8ou may write a more compact program ma ing use of functions and pointers. !ry writing it. 8our turn 1 Lodify the program to print out the time at which the pumps are turned on. !he display may loo as follows+
Pump ,ontro! I;stem ------------------Itatus N /orma! 11N3= Pump 1N 1/ 12N=) Pump 1N 1RR Pump 2N 1RR Pump 2N 1/

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

xx

1*(

Potrebbero piacerti anche