tag:blogger.com,1999:blog-8853820565211362966.post3515683797466255468..comments2023-08-30T04:01:25.904-04:00Comments on Scientific Instruments Using the TI MSP430: Tutorial 03: Flipping BitsUnknownnoreply@blogger.comBlogger21125tag:blogger.com,1999:blog-8853820565211362966.post-1459870588317764862014-11-11T13:00:58.240-05:002014-11-11T13:00:58.240-05:00Thanks for pointing out the confusing statement; e...Thanks for pointing out the confusing statement; exactly the kind of thing that will be fixed in the new website I'm working on.<br /><br />What you are changing is the register that holds the values of the output. Until the pin is configured as an output, it does nothing externally as it's not connected. Once the pin is configured, it takes on the value that is held in the output state register. Setting that value before making the pin an output guarantees that the pin starts in the state you want it to have.Anonymoushttps://www.blogger.com/profile/02144862918620225279noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-59354942096656619312014-05-21T14:51:51.220-04:002014-05-21T14:51:51.220-04:00I got lost somewhere. I can't make sense of t...I got lost somewhere. I can't make sense of the statement:<br /><br />"(A better practice is to initialize the output state before we change the pin to an output.)"<br /><br />If we haven't set a pin to an output first, how do we change its state?SBhttps://www.blogger.com/profile/17099981206094320869noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-3221263547771864872012-12-10T09:58:26.018-05:002012-12-10T09:58:26.018-05:00@billabot,
the documentation on TI devices is gen...@billabot,<br /><br />the documentation on TI devices is generally divided into<br /><br />* datasheets, containing hard number on one or a few microcontrollers properties such as maximum power supply voltage, sink current etc, as well as what peripherals are in that specific mcu (eg X timers, Y USCI etc etc)<br /><br />* the FUG/family user guide, that tells you how to use that family of mcu's, what bitfields are in what registers etc<br /><br />* application notes and white papers, outlining how to solve some specific task or use some peripherals etc.<br /><br />They are usually very, very easy to find. Just google for your device, eg msp430g2553, and on the TI page for that, there are links that are easy to find.<br /><br />Good luck.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-35052749611268776332011-10-31T08:29:18.138-04:002011-10-31T08:29:18.138-04:00The bit notation can be a bit confusing if you'...The bit notation can be a bit confusing if you're not familiar with programming. There are 8 bits, numbered 0-7, and counted from right to left. So BIT0 is the least significant bit. The name comes from the fact that when this bit is set, it represents a value of 2^0. Likewise, BIT3 represents 2^3, and so on.<br /><br />So when we say something like BIT4, it's not the 4th bit in, but rather the 5th bit from the right: 0b00010000.<br /><br />Just remember that the order always looks like this:<br />0b(7)(6)(5)(4)(3)(2)(1)(0)<br />where the values in parentheses represent the bit number.Anonymoushttps://www.blogger.com/profile/02144862918620225279noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-81642175115644532592011-10-31T04:31:56.062-04:002011-10-31T04:31:56.062-04:00Hello David,
"P1DIR = BIT4 + BIT6;
(adding t...Hello David,<br /><br />"P1DIR = BIT4 + BIT6;<br />(adding the two values together is easily verified to be 0b01010000)"<br /><br />How come that? I thought it would be 0x00010100.<br /><br />As well as:<br />"You certainly can use binary directly; P1OUT = 0b01000001 is equivalent to P1OUT = BIT6 + BIT0"<br /><br />Isn't it BIT2 + BIT8?<br /><br />thank you very muchUlricnoreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-72236733241136490682011-10-20T16:30:35.582-04:002011-10-20T16:30:35.582-04:00Oh, and now that I think about it, my suggestion o...Oh, and now that I think about it, my suggestion of toggling P1OUT ^= P1OUT won't work; that will always return 0. P1OUT = ~P1OUT would toggle it, however. Looks like I've learned a lot since I wrote this tutorial... it's probably time to go back and edit/improve everything.Anonymoushttps://www.blogger.com/profile/02144862918620225279noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-23893855439626750512011-10-20T16:29:00.417-04:002011-10-20T16:29:00.417-04:00I agree; though I have to admit I prefer using + i...I agree; though I have to admit I prefer using + in one situation: initializing peripherals. I think it makes more sense in my head (visually, at least) to say P1DIR = BIT0 + BIT2 rather than P1DIR = BIT0 | BIT2, even though they have the exact same result. It just makes the code more reasonable, in my opinion.<br /><br />Your comment made me go back and read the other comments, and I realized that I made a type too.. in my previous comment I intended to say it's easier to toggle P1OUT rather than P1DIR. Hopefully that was more obvious from the context. I wish there was a way to edit comments here!Anonymoushttps://www.blogger.com/profile/02144862918620225279noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-39454689301197906952011-10-20T05:06:26.133-04:002011-10-20T05:06:26.133-04:00When you want to toggle many bit in the same opera...When you want to toggle many bit in the same operation it is better to use | then + because<br /><br />P1OUT = BIT1 + BIT1 <br /><br />makes P1OUT to be equal to BIT2 and<br /><br />P1OUT = BIT1 | BIT1<br /><br />makes P1OUT to be equal to BIT1<br /><br />Personally, I would banish the use of + except if you really want an addition (which is often not the case when doing bits operations).<br /><br />Also, many thanks for these great tutos.metaonenoreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-79691329323761822342011-05-09T08:57:10.964-04:002011-05-09T08:57:10.964-04:00NKT,
You certainly can use binary directly; P1OUT...NKT,<br /><br />You certainly can use binary directly; P1OUT = 0b01000001 is equivalent to P1OUT = BIT6 + BIT0.<br /><br />Perhaps it was a typo, but I think it would make more sense to toggle P1DIR rather than P1OUT. Change P1OUT only if you really want to switch to inputs. However, you should be able to toggle all of P1OUT. Any pins configured as inputs are unaffected by changes to P1OUT. So P1OUT ^= P1OUT should toggle everything at once.<br /><br />There are potentially undefined states when pins are configured as inputs; TI recommends unused inputs be tied to pull-up or down resistors, which can be done internally. Another options is to make all of them outputs and ground the unused ones. Of course, that makes toggling P1OUT less useful.<br /><br />Hope these thoughts help some!Anonymoushttps://www.blogger.com/profile/02144862918620225279noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-28843007002999763522011-05-08T14:17:45.959-04:002011-05-08T14:17:45.959-04:00Hi, just working through this again.
Firstly, can...Hi, just working through this again.<br /><br />Firstly, can I input a binary number rather than using the BITx system?<br /><br />Also, if I use your code and make P1DIR = BIT6 + BIT0; then if I toggle P1DIR both LEDs flash together. <br /><br />However, could I toggle the whole of P1DIR rather than the BITs, to do the same thing? (And, save a line or two by only setting BIT6 and toggling the entire register to get alternate LED flashing?<br /><br />Or is there also an "undefined" state as well as 0 and 1?NKTnoreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-63055102304253658252011-03-07T09:29:56.166-05:002011-03-07T09:29:56.166-05:00Those are interesting points; I do think I need to...Those are interesting points; I do think I need to come in TI's defense on this one, however. <br /><br />First off, there's nothing magical about Rev. E itself, it's the Family User's Guide that's key. Rev. F is just as good, possibly better. Rev. C or D will also work, however later revisions will have corrections, additions for newer devices and technologies, etc.<br /><br />Second, I personally think the datasheets are very useful. As I've been learning to use the MSP430, I've found that I use both the Family Guide and the Datasheet extensively as I figure out how something works, how it's implemented on my specific device, and how to code it up.<br /><br />As a disclaimer, I do not work for TI at all... I'm simply an MSP430 user. I started using this uC as opposed to one of the others on the market simply because the chief electrical engineer on my project said to use it. As time has gone on, I've found there are some great features and advantages in this uC as opposed to some of the others, and I really enjoy using it. I'm not paid by TI in any way.Anonymoushttps://www.blogger.com/profile/02144862918620225279noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-8326632747541412562011-03-07T09:16:30.850-05:002011-03-07T09:16:30.850-05:00Me again. on another site I stumbled across the m...Me again. on another site I stumbled across the missing file. (REv. E) and I quote: <br />BigglesPiP said ...<br /><br />Coming from the Microchip PIC family of uCs it's nice to have real interrupts, I miss the RAM being flash a little.<br /><br />But one thing I'm finding hard to forgive is TI's diabolical datasheets, there's nothing immediately useful in either the MSP430 family datasheet or the datasheet speific to the MCU.<br /><br />After days of searching I came across a rumour that Revision E of something generic to MSP430 told me what I wanted to know (what every bit in every control register does and how to operate features). But the document is a 404 on TI's site. Eventually I found it, I shall host it while TI get their act together: http://iamtheb.org/me/MSP430x2xx_Family_Users_Guide_Rev_E.pdf<br /><br />TI, can we please have good datasheets, Microchip's are the business.<br /><br />--BigglesPiP 07:38, 18 January 2011 (CST)billabotthttp://www.knology.net/~billabottnoreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-24558144614367033042011-03-05T06:08:03.057-05:002011-03-05T06:08:03.057-05:00I agree, but with one exception: initialization. W...I agree, but with one exception: initialization. When your program starts, it can be helpful to leave no ambiguity as to the initial state of some peripherals. In the end, they both get you there, but I do tend to only use the direct assignment method at the beginning of my code. Everywhere else I'll use logic operators.<br /><br />Thanks for helping to make that point!Anonymoushttps://www.blogger.com/profile/02144862918620225279noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-22089042147489592452011-03-05T05:04:16.998-05:002011-03-05T05:04:16.998-05:00Thanks for the earlier clarification regarding P1D...Thanks for the earlier clarification regarding P1DIR= BIT4. <br />In light of the dangers involved in accidentally clearing bits using the above notation, wouldn't it be better if we always use logical operators | and & to manipulate bits?<br /><br />Just a thought..Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-46811889370663473922011-02-23T13:35:46.782-05:002011-02-23T13:35:46.782-05:00Keep in mind that in C a single = is not equating,...Keep in mind that in C a single = is not equating, but assigning. The statement P1DIR = BIT4; assigns the value BIT4 to the P1DIR register. BIT4 is a shorthand notation for an 8 bit value where all bits are 0 except bit 4 (counting from 0-7). So:<br /><br />BIT4 is 0b00010000, which is the same as 0x10. (If you look in the header file, you'll see a line that says #define BIT4 0x10.)<br /><br />The effect of the line P1DIR = BIT4; is to set the direction flags for each GIO in P1 to 0 (making them inputs) except for P1.4, which is set to 1 (making it an output).<br /><br />Keep in mind this is different from P1DIR |= BIT4;. This line is different in that it leaves all bits unchanged (be they 0's or 1's) except for bit 4--this line ensures that bit is set (to 1) without changing any other bits. P1DIR = BIT4; will set bit 4 and clear all others. You'll have to choose the assignment operator used according to the needs in the program.<br /><br />The whole point of the notation is that the port registers can be viewed as individual switches for each port pin-- BITx in each register controls each respective pin, eg. BITx in the P1 registers control P1.x.<br /><br />Hope that helps! Feel free to ask for any other clarification.Anonymoushttps://www.blogger.com/profile/02144862918620225279noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-595214890718819982011-02-23T13:12:41.811-05:002011-02-23T13:12:41.811-05:00Under the Direct Assignment section:
What does th...Under the Direct Assignment section:<br /><br />What does the following mean:<br />(Using the BITx notation, we would write P1DIR = BIT4.)<br /><br />What does equating P1DIR to BIT4 achieve?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-59129074472401515532010-12-22T16:53:24.103-05:002010-12-22T16:53:24.103-05:00I'm very curious as to what are the circumstan...I'm very curious as to what are the circumstances in which one should use the a + b or the a | b notationsm.Alinhttps://www.blogger.com/profile/17623693231254263797noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-22761517672330223002010-07-19T15:55:12.845-04:002010-07-19T15:55:12.845-04:00One other point... the LaunchPads I received have ...One other point... the LaunchPads I received have the red LED on P1.0 and the green LED on P1.6-- opposite what was expected from the release announcement.Anonymoushttps://www.blogger.com/profile/02144862918620225279noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-36342601926066080872010-07-19T15:54:02.193-04:002010-07-19T15:54:02.193-04:00I agree, which is why I brought it up so early in ...I agree, which is why I brought it up so early in the tutorial. Thanks for the wikipedia links!<br /><br />Another note on the LEDs, many LEDs can handle the currents from directly connecting to Vcc, but the MSP430 might not. In general, always limit the currents for LEDs in a design. Expect something around a few hundred ohms for the voltages we use to power these chips.Anonymoushttps://www.blogger.com/profile/02144862918620225279noreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-40832508541887086882010-07-19T15:17:29.940-04:002010-07-19T15:17:29.940-04:00Bit manipulation is a good thing to learn in the b...Bit manipulation is a good thing to learn in the beginning. It is used a lot when programming microcontrollers. Do the math with logic operators on papers and it's learned quite fast. Shifting << or >> is also good to learn.<br />http://en.wikipedia.org/wiki/Bitwise_operation<br />http://en.wikipedia.org/wiki/Bit_manipulationplazmanoreply@blogger.comtag:blogger.com,1999:blog-8853820565211362966.post-30224798552421561772010-07-13T00:08:12.148-04:002010-07-13T00:08:12.148-04:00I have some comments about LEDs.
1) Usually, LEDs...I have some comments about LEDs.<br /><br />1) Usually, LEDs need to have a resistor in series to limit the amount of current.<br /><br />2) In order to be able to turn on/off a LED, the LED can be either connected between an output pin and ground or between an output pin and Vcc. The voltage level at the output pin has opposite effect depending on which way the LED is connected.<br /><br />3) The LaunchPad has a Red LED controlled by P1.6 and a Green LED controlled by P1.0.OCYhttps://www.blogger.com/profile/05239312711831523449noreply@blogger.com