ࡱ> GIFg bjbjVV 44r<r<} 0008h$D07f($O"bK1VG07,":""7" : Addendum to Bit Manipulation Handout BJ Furman 25FEB2011 Registers To effectively and efficiently program microcontrollers, one needs to learn how to manipulate individual bits in registers, the special memory locations that the microcontroller uses to control and carry out its operations. The three registers that one must deal with for Input/Output (IO) operations with an ATmega microcontroller are: Data Direction (DDRx), Data (PORTx), and Port Input Pins (PINx). [The x represents the letter enumeration scheme for the associated ports. In the particular case of the ATmega328, x could be B, C, or D]. Each of these three registers is 8-bits (1 byte) wide. RegisterCommentsDDRxControls data direction: writing a 1 to the associated bit location makes the corresponding pin to be an OUTPUT; writing a 0 makes the pin to be an INPUT.PORTxFor a pin configured as an OUTPUT, the PORTx register provides the way to control the digital voltage of the pin: writing a 1 to the associated bit location drives the pin to logic HIGH; writing a 0 drives the pin to logic LOW. For a pin configured as an INPUT, the PORTx register provides the way to control the pullup resistor for the pin: writing a 1 to the associated bit turns the pullup resistor on; writing a 0 turns the pullup resistor off.PINxContains the almost current snapshot of the digital state of the pins in the associated port. This is the register used to read the digital state of a pin. Working with bits in registers bit masking The technique to access or change individual bits in registers takes some getting used to. The big idea underlying the technique is that when we work with a register, we always have to handle all 8 bits of the register together there is no direct way to work on a single bit level. Consequently, we will use bitwise logical operators and bit masks to drill down to an individual bit or groups of bits. A bit mask is a construct of 8 bits that is used in much the same way that a painter uses masking tape to keep sections of a surface from getting painted. As explained in the handout on Bit Manipulation ( HYPERLINK "http://www.engr.sjsu.edu/bjfurman/courses/ME106/lectures/handout_bit_manipulation.doc" http://www.engr.sjsu.edu/bjfurman/courses/ME106/lectures/handout_bit_manipulation.doc), you can use the _BV(n) macro to create a bit mask with a 1 in the bit position corresponding to the value of n. The macro for _BV(n) is a #define: #define _BV(n) (1<<(n)) which indicates the more direct way of building bit mask: using the bit shift left operator, << . To determine if a particular bit in a register is set, use a bit mask with a bit in the position of interest and perform a bitwise AND between the 8 bits in the register and the bit mask. If the resulting value is not zero, the bit is set, else the bit is clear. For example, to check if pin 5 of PORTD is at logic HIGH, use a bit mask with a 1 in the location for bit 5, _BV(5) or (1 << 5) and bitwise AND together with the PIND register contents. The table below illustrates what is happening, supposing bit 5 is set in PIND. (The Xs indicate dont care what the values are: it doesnt matter if they are 1 or 0). Go column by column doing a bitwise AND operation between the bit in the PIND register and the bit in the bit mask. The last row in the table is the result. bit76543210PINDXX1XXXXX(1 << 5) (this is the bit mask)00100000PIND & (1 << 5)00100000So, if you wanted to take a particular action if bit 5 was set, the following test would accomplish the selection: if( PIND & (1 << 5) ) { do stuff ; } else { do other stuff ; } Note that if bit 5 in the PIND register were clear, the value of the result from PIND & (1 << 5) would be zero. Exercises (write Arduino-style and port-style code snippets to accomplish the following tasks) Set bits 7, 4, and 1 of DDRD, but leave the other bits in the register undisturbed. Clear bits 4, 3, and 0 of PORTD, but do not disturb the other bits in the register. Suppose that pins 7, 6, 5, and 4 of port D on the Arduino are connected to digital sensors (like switches, where they present either logic HIGH or logic LOW to the pins), and pins 3, 2, 1, and 0 are connected to LEDs through series resistors to ground. Display the logic levels on pins 7-4 on pins 3-0. So if pins 7 and 4 are HIGH, and 6 and 5 are LOW, then the LEDs on pins 3 and 0 will be on, but those on 2 and 1 will be off.     Page  PAGE 2 of  NUMPAGES 2 BJ Furman | ME/EE 106 Introduction to Mechatronics| handout_bit_manipulation_addendum.doc | 25FEB2011 $%9C' ` e p ` o u K S j  6 7 N l     # ' X ˼ h"Xh0 h"Xh h"Xh[&] h"Xh' h"Xh- h"Xhihh'hZhgGhih=hh6h hgGhZh>* h"XhjhjhN 1h_ hWh"Xh3;5 h"Xh3; h"Xh_ hI1,,.02468:<=BDFHJLNPRSsuwyFf$d$Ifa$gd"X d$Ifgd"XFfr$$$d$Ifa$gd"Xy{}.0< d^gdj d^gdjdgdjgdI1Ff d$Ifgd"XFfh $d$Ifa$gd"X<>CEWY(|} *&#$gdU dgdU  & Fgd)|m$ & FgdY>gdN 1 d^gdj dgdj d^gdj  & Fgd)|m$ H$ D%$dNgdUhY>hqh{!hU21h:pU/ =!"#$%h $$If!vh#vJ#v!:V l t065J5!pyt"X$$If!vh#vJ#v!:V l t065J5!pyt"X$$If!vh#vJ#v!:V l t065J5!pyt"X$$If!vh#vJ#v!:V l t065J5!pyt"X$$If!v h#v#v#v#v#v :V l t0655555 pZyt"XZkdh$$Ifl  O%_!$ t06$$$$44 lapZyt"X$$If!v h#v#v#v#v#v :V l t0655555 / pZyt"XZkd$$Ifl  O%_!$ t06$$$$44 lapZyt"X$$$If!v h#v#v#v#v#v :V l t0655555 / / pZyt"XZkdB $$Ifl  O%_!$ t06$$$$44 lapZyt"X$$If!v h#v#v#v#v#v :V l t0655555 / pZyt"XZkd $$Ifl  O%_!$ t06$$$$44 lapZyt"Xj 666666666vvvvvvvvv6666>666666666666666666666666666666666666666666666666hH6666666666666666666666666666666666666666666666666666666666666666662 0@P`p2( 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p 0@P`p8XV~ OJPJQJ_HmH nH sH tH J`J ZNormal dxCJ_HaJmH sH tH 8@8 gG Heading 1$@&>*>@> ' Heading 2$d@&5DA`D Default Paragraph FontRi@R 0 Table Normal4 l4a (k ( 0No List tt i Table Grid7:V0 d8o8 gGHeading 1 Char>*2"@2 Qh 0Caption>*8o!8 'Heading 2 Char56U`16 '0 Hyperlink >*B*ph@@B@ Y> List Paragraph ^m$6>@6 @r> UHeaderdH$.. U0 Header Char> @> UFooterdH$.. U0 Footer Char.)@. U Page NumberR@R j0 Balloon Text dCJOJQJ^JaJN/N j0Balloon Text CharCJOJQJ^JaJPK![Content_Types].xmlN0EH-J@%ǎǢ|ș$زULTB l,3;rØJB+$G]7O٭V$ !)O^rC$y@/yH*񄴽)޵߻UDb`}"qۋJחX^)I`nEp)liV[]1M<OP6r=zgbIguSebORD۫qu gZo~ٺlAplxpT0+[}`jzAV2Fi@qv֬5\|ʜ̭NleXdsjcs7f W+Ն7`g ȘJj|h(KD- dXiJ؇(x$( :;˹! I_TS 1?E??ZBΪmU/?~xY'y5g&΋/ɋ>GMGeD3Vq%'#q$8K)fw9:ĵ x}rxwr:\TZaG*y8IjbRc|XŻǿI u3KGnD1NIBs RuK>V.EL+M2#'fi ~V vl{u8zH *:(W☕ ~JTe\O*tHGHY}KNP*ݾ˦TѼ9/#A7qZ$*c?qUnwN%Oi4 =3N)cbJ uV4(Tn 7_?m-ٛ{UBwznʜ"Z xJZp; {/<P;,)''KQk5qpN8KGbe Sd̛\17 pa>SR! 3K4'+rzQ TTIIvt]Kc⫲K#v5+|D~O@%\w_nN[L9KqgVhn R!y+Un;*&/HrT >>\ t=.Tġ S; Z~!P9giCڧ!# B,;X=ۻ,I2UWV9$lk=Aj;{AP79|s*Y;̠[MCۿhf]o{oY=1kyVV5E8Vk+֜\80X4D)!!?*|fv u"xA@T_q64)kڬuV7 t '%;i9s9x,ڎ-45xd8?ǘd/Y|t &LILJ`& -Gt/PK! ѐ'theme/theme/_rels/themeManager.xml.relsM 0wooӺ&݈Э5 6?$Q ,.aic21h:qm@RN;d`o7gK(M&$R(.1r'JЊT8V"AȻHu}|$b{P8g/]QAsم(#L[PK-![Content_Types].xmlPK-!֧6 0_rels/.relsPK-!kytheme/theme/themeManager.xmlPK-!0C)theme/theme/theme1.xmlPK-! ѐ' theme/theme/_rels/themeManager.xml.relsPK]  4  '  ,y<K X*,!L# @0(  B S  ?S_x~MRz]b1 3 }  02>BEG}333333333|},.z7h^h`o(.8^8`.L^`L. ^ `. ^ `.xL^x`L.H^H`.^`.L^`L.,         !_ Qh =1,N 1I13;gG"X_Y[&])|-3eqzXT}@XXXXX@UnknownG*Ax Times New Roman5Symbol3. *Cx Arial7.@ Calibri5. .[`)TahomaA$BCambria Math"qh FA F20uuKHX  $P2!xx Burford FurmanBurford Furman Oh+'0|   , 8 D P\dltBurford Furman Normal.dotmBurford Furman3Microsoft Office Word@J.@.b=2@|@xUG՜.+,D՜.+,4 hp   Microsoftu  Title, 8@ _PID_HLINKSACVhttp://www.engr.sjsu.edu/bjfurman/courses/ME106/lectures/handout_bit_manipulation.doc  !"#%&'()*+,-./012345789:;<=?@ABCDEHRoot Entry F OVGJData 81Table$"WordDocument44SummaryInformation(6DocumentSummaryInformation8>CompObjr  F Microsoft Word 97-2003 Document MSWordDocWord.Document.89q