gatesphere 45 Posted August 21, 2010 Share Posted August 21, 2010 Hello all. To test out my LaunchPads, I decided to port a little white noise generator from the Arduino to the MSP430. This specific code is for the G2231 that comes preinstalled on the LP, but the code is light enough that it should work with any MSP430G2xxx uC. Just change the header file. The circuit is simple. A piezo buzzer or mono output from P1.0 to GND. If you want to, you can remove the P1.0 jumper from J5, though it's not necessary. The original Arduino sketch is available here. And on to my code: // white noise generator // written as a way to become familiar with the MSP430G2 Launchpad // based on an Arduino sketch available at the following address: // https://code.google.com/p/greennoise/downloads/detail?name=prbsGen.pde // // based on the idea of a linear feedback shift register // // written by ~suspended-chord (http://suspended-chord.info/) // placed into the public domain // 20 August 2010 #include "msp430g2231.h" // circuit: // piezo buzzer from P1.0 to gnd // definitions #define BUZZER BIT0 // buzzer on P1.0 // function prototypes void setup(); void loop(); // global variables unsigned long int reg; // Arduino-like paradigm void main() { setup(); for(; { // loop() indefinitely loop(); } } // end function main() // initialize the system void setup() { // kill watchdog timer WDTCTL = WDTPW + WDTHOLD; // set up pin direction and initialize register P1OUT = 0x00; // clear initial output, if any P1DIR |= BUZZER; // buzzer on P1.0 as output reg = 0x0badbeefL; // some arbitrary non-zero initial value } // end function setup() // the main workhorse of the program, loops continuously void loop() { unsigned long int newr; unsigned char lobit; unsigned char b31, b29, b25, b24; // extract four chosen bits from the 32-bit register b31 = (reg & (1uL << 31)) >> 31; b29 = (reg & (1L << 29)) >> 29; b25 = (reg & (1L << 25)) >> 25; b24 = (reg & (1L << 24)) >> 24; // XOR the four bits together lobit = b31 ^ b29 ^ b25 ^ b24; // shift and incorporate new bit at bit position 0 newr = (reg << 1) | lobit; // replace register with new value reg = newr; // drive buzzer from bit 0 of 'reg' if (reg & 1) { P1OUT |= BUZZER; } else { P1OUT &= ~BUZZER; } } // end function loop() The port was pretty simple, the only thing that threw me for a loop was the bit assignments. I'll admit, I'm used to high level languages and object oriented abstraction. That being said, I will probably end up writing an Arduino-like framework for the LaunchPad, and release that into the wild. digitalWrite() and delay() are sorely missed! Speaking of the bit assignments... is there any way to streamline the chunk of code: // drive buzzer from bit 0 of 'reg' if (reg & 1) { P1OUT |= BUZZER; } else { P1OUT &= ~BUZZER; } into a single statement? I tried working it out on paper, but i just couldn't figure it out with any of the bitwise operators. Oh, and sorry for the non-K&R bracket formatting. I'm used to Java, though I've done a lot of work in C/C++/PHP/Python, I just find that open brackets on a new line is distracting and consumes too much screen space. Anyways, sorry for the long winded post. Enjoy! -->suspended-chord EDIT: My blog is now up and running, this project can be found at its new home here: http://blog.suspended-chord.info/?c=2. Quote Link to post Share on other sites
gatesphere 45 Posted August 21, 2010 Author Share Posted August 21, 2010 Sorry for the double post, but I just wanted to say that I've been working on the Arduino-like framework I mentioned in the last post. I decided that it would be a good way to get my hands dirty quick with the internals of the MSP430 architecture, while at the same time potentially be a help to those new to the scene. So far, I have the following: --Ability to define just setup() and loop() within a source file. Simply #include "mspduino.h" and it will take care of the rest. --init() function which runs automatically to clear outputs and set all pins as inputs to default. --pinMode(), digitalRead(), and digitalWrite() working. I decided to map all the pins to a single number, like the Arduino language does. 0-7 are P1.0-P1.7 and 8-15 are P2.0-P2.7. So far, it's coming along nicely. One problem is code bloat. Just with this minimal framework, it's pushing the boundaries of what the .5k chips will be able to store. I'll be looking for ways to reduce this through addressing things differently, and modularizing the framework so users can take just the chunks they need. But yeah. I'll start another topic once I feel like it's "releaseable". Quote Link to post Share on other sites
GeekDoc 226 Posted August 21, 2010 Share Posted August 21, 2010 Thanks for the code example. I look forward to your framework. I too am used to the high-level, object-oriented world of Java/.Net. (Sorry to put those two together. I know that causes a gut response in some, but I think they both have their place.) Quote Link to post Share on other sites
gatesphere 45 Posted August 22, 2010 Author Share Posted August 22, 2010 Thanks for the comments, and good to know there's some interest! It's what keeps me motivated... Quote Link to post Share on other sites
bluehash 1,581 Posted August 22, 2010 Share Posted August 22, 2010 Good porting gatesphere! I think if you modify it a bit, you can get actual tunes. Each note has a different frequency. Maybe this post may be of help. Quote Link to post Share on other sites
gatesphere 45 Posted August 22, 2010 Author Share Posted August 22, 2010 Oh, I intend to make instruments with these. The majority of my experience with microcontrollers involves making synthesizers and step sequencers. I know my way around PWM with regards to tone Quote Link to post Share on other sites
NJC 17 Posted August 23, 2010 Share Posted August 23, 2010 gatesphere, To answer your question about condensing that bit of code you posted, it is possible but not necessary. All of this gets translated to assembly, and there would be no space saving done by modifying that bit of code in any way (other than maybe using assembly directly, and even then not likely). TI does a pretty good job of converting C to assembler. So, very interesting idea with the Arduino like interpreter for the MSP430. I would name it something other than mspduino, lol, maybe mspsphere :-P. Thats just a personal preference, I guess I just don't like the Arduino haha. This task will be very difficult without knowing assembler. It is not that hard to pick up though; the major difference is that you have 100% control over memory allocation and things like that. It does get quite complicated when writing larger parts of code. I really dont like assembly and use C, but I do admit that there is definitely a use for assembly. That being said, don't give up if you really want to do this. Once you think it is somewhat ready to be released I can help you out with making a boot loader or for it, which is fairly important i think. Just be prepared to either have a TON of overhead, or to use assembly. I don't know much about writing interpreters though, and know less about computer language theory. Good luck! Making an interpreter for the MSP430 which has the same ease of use as the Arduino could make the LaunchPad the most popular thing ever. Keep up the cool projects. -NJC _________________________ http://msp430launchpad.com Quote Link to post Share on other sites
gatesphere 45 Posted August 23, 2010 Author Share Posted August 23, 2010 Thanks for the advice, NJC! But I was actually just thinking of releasing it as a C library under the GPL, for use with whichever IDE/toolchain the user was already used to. An Arduino/Wiring/Processing-like environment would be a later thing for sure. Much later. If at all. And yeah, I didn't really like the name myself, it just seemed fitting. I do like the idea of MSPhere. When capitalized like that, it emphasizes the MSP, and the word "here", implying ease and control, while sneakily hiding "sphere" in there as a bit of an ego boost. I think I'll name it that I was going to go through the Arduino language reference work through section by section, omitting the ones that wouldn't be simple to port initially (like the entire serial stack, for one) and possibly later revisiting them and releasing them as time permits. I believe that I can get quite a bit of the basic Arduino functionality covered in library form without touching assembly, although I've always been looking for a reason to learn it. Anyways, before any serious progress on this happens, I have to finish writing the alpha version of my blogging software so I can get my dev blog up and running! That's tonight's project, mostly. Then comes the work on this. Also, classes start up for me again in one week, so I will have less time to tinker around with it, but I do intend to get this to a usable, helpful state. Thanks again! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.