Hands-On Technical Training (hott) Workshop: Introduction to Microcontroller Programming in C



Download 110.29 Kb.
Page4/5
Date29.01.2017
Size110.29 Kb.
#11287
1   2   3   4   5

Launching & Exploring the IDE

Creating a new project


  1. Launch CodeWarrior IDE from desktop icon. This is the PC application for programming and debugging the microcontroller.

  2. If the Startup window dialog box appears, select Create New Project. If not, select File > Startup Dialog, then click Create New Project.

  3. Refer to the diagram below. In the New Project dialog box, expand the left-side tree and select the MC9RS08KA2 device. This is the microcontroller you’ll be programming.







  1. Select the SofTec RS08 for the default connection and click Next.

  2. Select C as the programming language and no others.

  3. For the project name, type in “Hello World.mcp” as shown below, then set the location to My Documents and click Next.





  1. The next step of the wizard asks you if you want to add existing files to your project. We don’t need any pre-existing files for this project, so click Next again.

  2. Recall that most of the pins on this microcontroller can serve multiple purposes. The Device Initialization tool (the GUI mentioned earlier) enables you to edit those pin assignments from within your project. Select Device Initialization as shown below and click Next.







  1. On the following screen, make sure the selections are set as illustrated below. This will cause the wizard to generate the necessary program files with code stubs to make your programming job easier. Also, floating point or real numbers will not be used in this project, so that option should be deselected. Floats require a large amount of memory and do not work well on the RS08 because of its very small memory area.






  2. Click Finish and the wizard will build your project.


Configuring the Microcontroller Chip for Proper Operation with the Development Board


  1. The right side of the CodeWarrior IDE should display the Device Initialization tool illustrated below. If not, select from the menu Device Initialization > Initialize Device.






  1. We will now make some basic CPU settings. Click the CPU block in the Device Initialization GUI, which should open a new window where you will make those changes.

  2. In the window that opens, expand the Internal Peripherals area and verify that BDM pin support is Enabled. This allows debugging the microcontroller operation from the PC, which is a very nice programming feature that we’ll be using. If you look at the development board schematic, you’ll see that the debug pin is shared with one of the LEDs, so it will blink when the PC is communicating with the microcontroller. Incidentally, “BKGD” and “BDM” are interchangeable terms that both refer to the background debug pin. If the BDM pin support is Disabled, click the button on that row to enable it.

  3. If the Reset pin support is Disabled, click the button on that row to enable it. This will assign the Reset pin on the development board to recognize the Reset button.

  4. Click OK to accept those CPU changes.

  5. The CPU contains a special section called a Watchdog or Computer Operating Properly (COP) that supervises operation of the CPU. It’s a handy feature, but we will not be using it at this time and so it must be disabled. Select the Device Initialization block labeled COP and change the Initialization option to Enable COP no. Click OK.

  6. PTA, or Port A, is Freescale’s name for the group of 6 I/O pins. Click the PTA block to select this port and configure the pins.

  7. Under Settings, Port Control, click the button on that row to change the setting from “Entire I/O port” to “Individual pins”. We will be configuring some pins as inputs and some pins as outputs.

  8. Referring to the information you studied above and/or the demo board schematic, note that SW0 is connected to microcontroller pin PTA1/KBIP1/ACMP. This pin can serve three purposes, as alluded to earlier. Since this pin is connected to switch SW0, set it as Enabled with Direction set to Input. Looking ahead, the name “PTAD_PTAD1” will be used in the program to refer to this input.

  9. Again referring to the previous information, set the appropriate pins as Outputs for LED1 and LED2. Your settings should appear as below.






  1. Click OK to accept the Port A changes. Notice that the Device Initialization blocks that have been changed from their default settings now appear aqua in color.

  2. Find and click the Generate Code button. This step is very important because it commands the IDE to convert the changes you just made into programming code that will be sent to the microcontroller. Read the additional dialog boxes that pop up, accept the defaults for now, and use common sense to respond.


Editing and Downloading a Program


  1. The left side of the IDE shows the overview of your new project. The main program file is appropriately named “main.c”. Double click main.c to open it for editing. In this example, you’ll be writing a program to turn LED1 on and off when switch SW0 is pressed.




Your main.c program file should appear something like this:




  1. In the main.c file, there are several lines you should basically understand but don’t need to change. First, there are some “#include” statements at the top of the file. These were inserted automatically when the project was created and direct the compiler to compile those other files before compiling the main file. These lines are necessary and should not be modified. Next, the line that begins with “void MCU_init(void);” is called a “function prototype” and tells the compiler the “signature” of function MCU_init, which will be invoked soon. This is necessary because the function has not been compiled yet at this point. Remember that every line of program code in C must end with a semicolon. The compiler will not be able to compile the files successfully if you don’t follow this rule.




  1. Next in the program is the line “void main (void) { ”. This is the beginning of the main program routine, or function. The main function ends with a closing curly brace, “}”.




  1. The first line within this main function tells the processor to run the hardware initialization routine, MCU_init.c. If you’re curious, double click that file name in the project tree on the left under the folder “Generated Code”. All of the MCU_init.c code was generated by the Device Initialization tool and would have to be generated by hand if that tool did not exist!




  1. In main.c, find the line that contains the text /* include your code here */. This is called a comment, which is purely for human benefit and will be ignored by the compiler. There are two types of comments used in C as shown in these two examples:
    // This is a comment. Double slashes tell the compiler to ignore
    // any text following the slashes on that one line only.
    /* This slash-and-asterisk type of comment may cross over multiple lines
    because the compiler treats everything within the opening and closing slash-
    and-asterisk comment symbols as comments rather than executable code*/



  2. Delete that “/* include your code here */” comment, then locate the line “for(;;) {”. This instruction defines the beginning of an infinite loop. Immediately following that line, insert the following lines of text, which are actual program code:

    if(!PTAD_PTAD1) // switch SW0 pressed?



PTAD_PTAD4 = !PTAD_PTAD4; // yes. Reverse the state of LED1
The whole purpose of this little program, as the comments show, is to reverse the state of LED1 each time SW0 is detected as being pressed – on, off, on, off, etc. The exclamation point basically says “take the inverse or opposite”.
Recall that switch SW0 is connected to input PTAD_PTAD1 and LED1 is connected to PTAD_PTAD4. The first line reads if(!PTAD_PTAD1). This means “if Port A Data Bit 1 is not on” and determines whether the next line of code will execute or not. If the parenthetical condition is true (i.e., if the pushbutton is pressed), then PTAD_PTAD4 = !PTAD_PTAD4; will run. If the SW0 button is not pressed, the “if” condition is false and the PTAD_PTAD4 line will be skipped over.
You may wonder why pressing the button makes the inverse of the input become true (input PTAD_PTAD1 = zero or logical FALSE if the button is pressed). If you look at the schematic, you’ll see that the switch is tied to ground and so it pulls the input low when it is pressed. Also, pullup resistor R103 pulls the input high when the button is not pressed. Therefore, !PTAD_PTAD1 is true when the button is pressed.
Now let’s take a closer look at the PTAD_PTAD4 = !PTAD_PTAD4; statement. This says “make output PTAD_PTAD4 equal to its inverse”, which reverses the state of the output bit. So, pressing the button reverses the LED state.



  1. Select from the menu Project > Make (or F7 or this little icon: ). This will compile the project files. No error messages should appear at this time.
    Optional: To create an error condition and see how the compiler responds, remove one of the semicolons in main.c and recompile the project.

  2. Select Project > Debug (or F5 or green right arrow icon). Note: Selecting Debug will automatically compile the project, so step #7 above was not really necessary, but was included for training purposes.

  3. In the MCU Configuration dialog window that pops up, select Hardware Model DEMO9RS08KA2 and click OK. This will send your Hello World program to the microcontroller on the demo board. The term for this is “downloading”.

  4. The True-Time Simulator and Real-Time Debugger tool should now open in a new window. This tool is basically a panel from which to control and monitor operation of the microcontroller. Once the program download is complete, look at the toolbar and find the Run button, which is a green right arrow icon. Click this icon or press the F5 button to start the program execution.

  5. Notice that the green Run button icon has become grayed out, indicating that the program is running. If it has not grayed-out, again click it or press F5.

  6. The program should now be running. To test it, press and release the SW0 pushbutton repeatedly while watching LED1. Note that LED1 seems to dim while SW0 is pressed, then when SW0 is released, LED1 may be either off or on randomly. LED1 is actually blinking on and off while SW0 is held, but much more quickly than the human eye can detect. The program isn’t quite right yet, so in the following steps you’ll modify it.



Modifying the Program to Slow Down the LED1 Blink Rate


  1. Close the debugger tool and edit the main.c file in the CodeWarrior IDE.

  2. Modify your existing program (use copy & paste from this document if you prefer) to look like this, with changes shown in bold text and pointed out with left angle brackets:



#include /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */


void MCU_init(void); /* Device initialization function declaration */
void main(void) {
int LoopCount = 0; // Declare and zero new integer variable

MCU_init(); /* call Device Initialization */


for(;;) {

for(LoopCount=0;LoopCount < 10000; LoopCount++); // delay here

if(!PTAD_PTAD1) // switch SW0 pressed?

PTAD_PTAD4 = ~PTAD_PTAD4; // yes. Reverse the state of LED1
} /* loop forever */

/* please make sure that you never leave main */

}



  1. Download and retest your modified program. Note that it may not yet be perfect, but it works much better now and that LED1 on the development board changes state each time SW0 is pressed. Also notice that LED1 blinks if SW0 is held down continuously. Why?

This program is not quite perfect yet, but it still works well enough for this example. The LED1 state change is not 100% predictable, but more code could be added to make it work better. Let’s just keep it simple at this point and enjoy the example for what it is.





  1. Modify and test your program again by replacing the “LoopCount < 10000” with each of the following to see which value works best:

    1. LoopCount < 32767

    2. LoopCount < 1000

    3. LoopCount < 100



These program additions work as follows. First, the new integer variable “LoopCount” is declared (i.e., created) and initialized or assigned a value zero here:
int LoopCount = 0;
Several important notes about declaring variables:

  1. The syntax is very important and must include the data type (“int” for integer in this example), variable name (e.g., LoopCount), and the ending semicolon. The initialization (“ = 0 ”) sets the variable equal to some value (zero in this case). Declarations do not need to include initialization, but many programmers prefer to always initialize variables at the same time they are declared because this will avoid certain programming problems later.

  2. Any variable declarations must precede any executable code in a function. Notice that the LoopCount declaration precedes the call to the MCU_init function within the main function. The order of these lines may not be reversed or the compile will fail. Try it right now if you’d like.

  3. It is necessary to declare a variable before it is used in a program. This new LoopCount variable is used in the main routine and therefore it needed to be declared first.

The new line “for(LoopCount=0;LoopCount < 10000; LoopCount++); // delay here” was added to slow down the program. The “for” instruction works this way:



  1. LoopCount=0 sets that variable to an initial value of zero,

  2. LoopCount < 10000 checks the value of the variable. If it is less than the target value (10000), then the next instruction(s) following the for instruction will be executed. In this case, the for instruction is followed by a semicolon rather than some instruction to be looped, so this loop does nothing but waste time. When LoopCount reaches 10000, the processor exits the loop and continues program execution at the next line following the for loop.

  3. LoopCount++ simply increments the LoopCount value by one after each loop is executed.

  4. The net result is that LoopCount starts at zero, counts up to 10000, then the loop instruction is finished and the program continues.



Directory: people
people -> Math 4630/5630 Homework 4 Solutions Problem Solving ip
people -> Handling Indivisibilities
people -> San José State University Social Science/Psychology Psych 175, Management Psychology, Section 1, Spring 2014
people -> YiChang Shih
people -> Marios S. Pattichis image and video Processing and Communication Lab (ivpcl)
people -> Peoples Voice Café History
people -> Sa michelson, 2011: Impact of Sea-Spray on the Atmospheric Surface Layer. Bound. Layer Meteor., 140 ( 3 ), 361-381, doi: 10. 1007/s10546-011-9617-1, issn: Jun-14, ids: 807TW, sep 2011 Bao, jw, cw fairall, sa michelson
people -> Curriculum vitae sara a. Michelson
people -> Curriculum document state board of education howard n. Lee, C
people -> A hurricane track density function and empirical orthogonal function approach to predicting seasonal hurricane activity in the Atlantic Basin Elinor Keith April 17, 2007 Abstract

Download 110.29 Kb.

Share with your friends:
1   2   3   4   5




The database is protected by copyright ©ininet.org 2022
send message

    Main page