<< Chapter < Page Chapter >> Page >
Programming basic C on an embedded processor.

With the MSP430, With the MSP430, the primary difference between "normal" C and programming C in the embedded space is that you will need to write to registers directly to control the operation of the processor. Fortunately, the groundwork has already been laid for you to make this easier. All of the registers in the MSP430 have been mapped to macros by Texas Instruments. Additionally, the important bit combinations for each of these registers have macros that use the same naming convention as the user’s guide. Other differences from the C used on most platforms include:

  • Most registers in the MSP are 16 bits long, so an int value is 2 bytes (16 bits) long.
  • Writing to registers is not always like writing to a variable because the register may change without your specific orders. It is always important to read the register description to see what the register does.
  • The watchdog timer will automatically reset the MSP unless you set the register not to.
  • There is only a limited "standard out" system. Standard out will typically print results to your computer screen. The board you have been provided does have 7 segment displays and LED’s but this does not allow for a full debugging display that a PC would have.
  • Floating-point operations cannot be efficiently performed. In general, you should avoid floating point decimal calculations on the MSP because it does not have special hardware to support the complicated algorithms used.

Code review

In this exercise, you may want to use some of the debugging tools to help you understand what the code is doing. You may use any of the following items:

  • Breakpoints - Left click on the arrow next to the line of code you want to break on. The program will stop when it gets to this point and you are able to view the contents of memory and current variable contents.
  • Watch Window - Right click on the variable you want to monitor and select Add "variable" to Watch . Variables that have been modified since the last time that the program was stopped will turn red.
  • Locals Window - From the View menu, click Other Windows then Locals . The Locals Window will automatically display the values for all local variables that are currently being used. Variables are considered "local" if they are within the function that is currently being processed.
Start a new project. Cut and paste the following code into main.c: #include <msp430x16x.h> #include <__cross_studio_io.h> void main(void){ int i,j,tmp; int a[20]={0x000C,0x0C62,0x0180,0x0D4A,0x00F0,0x0CCF,0x0C35,0x096E,0x02E4, 0x0BDB,0x0788,0x0AD7,0x0AC9,0x0D06,0x00EB,0x05CC,0x0AE3,0x05B7,0x001D,0x0000}; for (i=0; i<19; i++){ for (j=0; j<9-i; j++){ if (a[j+1] < a[j]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } while(1); }
  • Explain what this program is doing.
  • Use any of the methods listed above to show the updated array. What is the final result?
  • Modify the code so that it prints the final version of the array to standard out (the display window). What are the drawbacks and benefits of using this over setting a breakpoint?
To use the standard out, add the following line to the top of your code: #include<__cross_studio_io.h> The debug_printf() function will print to standard out. For example, debug_printf("x equals %d\n", x); will print out the value of x to the window. The %d means that x is a number, and \n will produce a line break.

Got questions? Get instant answers now!

Functions

Multiplications and division are very complex operations to do on any microprocessor. The operations should be avoided if possible or should be replaced with simpler, equivalent operations.

  • What do the operators<<and>>do?
  • How could you use these operators to perform multiplication and division?
  • Write the function multiply(int x, int y) that takes parameter x and multiplies it by y by using a bit shift. It must return an int . For symplicity, it is OK to assume that y is a power of 2.
  • Next, write the function divide(int x, int y) that takes parameter x and divides it by y by using a bit shift. It must also return an int .

Got questions? Get instant answers now!

Digital i/o registers

Open the file msp430x16x.h which should be located in C:\Program Files\Rowley Associates Limited\CrossWorks MSP430 x.x.x\include . This file contains the macros and register definitions for the MSP430F169 we use in this class. Using the MSP430 User’s Guide and the msp430x16x.h file, please answer the following questions.

  • The Watchdog Timer will automatically reset the hardware if it isn't periodically reset or disabled entirely. Usually, we will simply disable it. It can be disabled by writing to the WDTPW (watchdog timer password) and WDTHOLD (watchdog timer hold) section of the Watchdog Timer Control Register ( WDTCTL ). Refer to Section 10.3 of the User's Guide for more information. Find the macros for this register in the msp430x16x.h file. How are they different from their description in the User's Guide? Finally, write the C code required to disable it.
  • What are the differences among P1DIR, P1SEL, P1OUT, P1IN?
  • Some port pins have multiple functions to output and it is up to the user to select the appropriate signal. Write some code that would select the alternate function of P2.2 (pin 2 of port 2). What will the result be on our hardware?

Got questions? Get instant answers now!

Programming digital i/o

Write a program to do the following:

  • When the program starts all three LED’s should light up for about a second then turn off.
  • Next, the green LED should blink for about 1/2 second on, 1/2 second off while the other two LED’s are off. Use long for-loops to generate a delay.
  • Pushing Button 1 should cause the green LED to stop blinking and cause the red LED to start the blinking pattern.
  • Pushing the button again should continue the pattern with the yellow LED, and pushing it more times should repeat the green, red, yellow pattern.
  • The current LED that's blinking should stop as soon as the button is pressed and the next LED should begin immediately.
Make sure you disable the watchdog timer at the very beginning of the program. Refer to Introduction to programming the MSP430 to learn how to enable and use the pushbuttons and LEDs.
You will need to demonstrate this code to the labbie, turn in a hard copy of your program, and post it online.

Got questions? Get instant answers now!

Questions & Answers

What are the factors that affect demand for a commodity
Florence Reply
differentiate between demand and supply giving examples
Lambiv Reply
differentiated between demand and supply using examples
Lambiv
what is labour ?
Lambiv
how will I do?
Venny Reply
how is the graph works?I don't fully understand
Rezat Reply
information
Eliyee
devaluation
Eliyee
t
WARKISA
hi guys good evening to all
Lambiv
multiple choice question
Aster Reply
appreciation
Eliyee
explain perfect market
Lindiwe Reply
In economics, a perfect market refers to a theoretical construct where all participants have perfect information, goods are homogenous, there are no barriers to entry or exit, and prices are determined solely by supply and demand. It's an idealized model used for analysis,
Ezea
What is ceteris paribus?
Shukri Reply
other things being equal
AI-Robot
When MP₁ becomes negative, TP start to decline. Extuples Suppose that the short-run production function of certain cut-flower firm is given by: Q=4KL-0.6K2 - 0.112 • Where is quantity of cut flower produced, I is labour input and K is fixed capital input (K-5). Determine the average product of lab
Kelo
Extuples Suppose that the short-run production function of certain cut-flower firm is given by: Q=4KL-0.6K2 - 0.112 • Where is quantity of cut flower produced, I is labour input and K is fixed capital input (K-5). Determine the average product of labour (APL) and marginal product of labour (MPL)
Kelo
yes,thank you
Shukri
Can I ask you other question?
Shukri
what is monopoly mean?
Habtamu Reply
What is different between quantity demand and demand?
Shukri Reply
Quantity demanded refers to the specific amount of a good or service that consumers are willing and able to purchase at a give price and within a specific time period. Demand, on the other hand, is a broader concept that encompasses the entire relationship between price and quantity demanded
Ezea
ok
Shukri
how do you save a country economic situation when it's falling apart
Lilia Reply
what is the difference between economic growth and development
Fiker Reply
Economic growth as an increase in the production and consumption of goods and services within an economy.but Economic development as a broader concept that encompasses not only economic growth but also social & human well being.
Shukri
production function means
Jabir
What do you think is more important to focus on when considering inequality ?
Abdisa Reply
any question about economics?
Awais Reply
sir...I just want to ask one question... Define the term contract curve? if you are free please help me to find this answer 🙏
Asui
it is a curve that we get after connecting the pareto optimal combinations of two consumers after their mutually beneficial trade offs
Awais
thank you so much 👍 sir
Asui
In economics, the contract curve refers to the set of points in an Edgeworth box diagram where both parties involved in a trade cannot be made better off without making one of them worse off. It represents the Pareto efficient allocations of goods between two individuals or entities, where neither p
Cornelius
In economics, the contract curve refers to the set of points in an Edgeworth box diagram where both parties involved in a trade cannot be made better off without making one of them worse off. It represents the Pareto efficient allocations of goods between two individuals or entities,
Cornelius
Suppose a consumer consuming two commodities X and Y has The following utility function u=X0.4 Y0.6. If the price of the X and Y are 2 and 3 respectively and income Constraint is birr 50. A,Calculate quantities of x and y which maximize utility. B,Calculate value of Lagrange multiplier. C,Calculate quantities of X and Y consumed with a given price. D,alculate optimum level of output .
Feyisa Reply
Answer
Feyisa
c
Jabir
the market for lemon has 10 potential consumers, each having an individual demand curve p=101-10Qi, where p is price in dollar's per cup and Qi is the number of cups demanded per week by the i th consumer.Find the market demand curve using algebra. Draw an individual demand curve and the market dema
Gsbwnw Reply
suppose the production function is given by ( L, K)=L¼K¾.assuming capital is fixed find APL and MPL. consider the following short run production function:Q=6L²-0.4L³ a) find the value of L that maximizes output b)find the value of L that maximizes marginal product
Abdureman
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Microcontroller and embedded systems laboratory. OpenStax CNX. Feb 11, 2006 Download for free at http://cnx.org/content/col10215/1.29
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Microcontroller and embedded systems laboratory' conversation and receive update notifications?

Ask