Hands-on Programming, A first course for Engineers: Course Notes 59 / 74
NSF CCLI Project # DUE 0737370
PART I:
Introduction: (FOR CANFIELD)
A Computer Program: What it is, what it looks like
High Level (e.g., C) int a = 10;
Assembly LDAA #$0A
Machine Level 10000110 00001010
Syntax (for C): Preliminary comments
Tools used in creating a program:
Editor
Complier à { Preprocessor Compiler Linker }
IDE – Integrated Development Environment
Introduction to the Motorola MHCS12 Processor and Evaluation Board:
Dragon12+ Board
Locate the parts listed below on your Dragon12+ board
A. Serial Port used to upload programs onto the Dragon12+
B. Power Input to the board.
C. Reset switch.
D. 2 button toggle switch.
D—For Serial Monitor, you only need to use the left most switch. You’ll notice that there are markings above and below the switch that say “LOAD” and “RUN”.
Flash memory is non-volatile memory. This means that the memory will not erase when the Dragon12+ board is powered down. Volatile memory is erased when it does not have power.
Connect the computer at your lab station to your Dragon12+ board with a serial cable (hint: remember to use A and not the other serial port).
Introduction to the FREESCALE IDE: (FOR NICK)
Layout of a Program:
1. Comments:
/* … */
Or
// (C++ only)
2. Preprocessor directives
Begin with #
# include
# define
3. prototype declarations
4. Main function
main() {
(your primary code goes in here)
}
Function calls, code
- (Start with calls to functions that already exist, later develop your own functions)
5. function definitions
(other than main)
How to create your first program:
Step 0: Plan your program
Outline, flow chart, pseudocode
Step 1: Create a project
(Use course stationary, HS12 CCLI, refer to “create new project” tutorial)
Step 2: Implement your program plan in source code (main.c)
Step 3: Compile your program, Correct errors
Step 4: Connect target, Build and download
Step 5: Run program, test its operation
Syntax: Fundamental requirements:
· Identify comments with:
o /* comment here */ (ANSI C)
o // C++ comment
· # indicates preprocessor directive (tells the preprocessor what to do)
· End each program line with ;
o except on lines that define a loop
· Place all contents of a function in braces:
o Void main( void ){
}
· declare what is sent and returned in a function
o void main (void)
· Making a function call
o writeTextLCD("Hello World");
· (In Codewarrior) Variables must be declared in the beginning of the main function or your code will not compile.
· Variables and functions are case sensitive .
· Performing an operation (adding, subtracting,setting values, etc) is different from comparing two values. Refer to the table below.
Operations / Comparisons=
+
-
*
/
% / ==
>=
<=
Style: General formula for good style:
· Use comments liberally
o At the beginning of each program
o For each function
o For significant steps in each function
· Always use descriptive variable names
o index, sum, result
o
· Suggest camel case (myVariable, myFunction) for variables and functions.
· Notes should be made about using proper indentation for loops…
· Use space to enhance readability
o X=5;
o X = 5;
Planning a Program:
Outline Pseudo code flow chart
Example: 2-digit counter initiated by input on PTH0
Example Program:
Create a program to provide a welcome message for a GPS navigation system
Planning the program (pseudo code or flow chart):
Program Analysis:
Compiling a Program – Handling errors and warnings: (FOR NICK)
How to use the the error warning tool
tips
what can / cannot be avoided
Lab Assignment #0:
Create your first engineering application: A teleprompter.
Steps:
1: Go through tutorial: “Creating and Running a New Project in Codewarrior”
2: Create your own project for lab 0
3: Create a teleprompter that displays up to a maximum of 25 words
Use the example program above and discussion below as aides in creating the program.
4: Demonstrate your project to 2 other people, record their response
5: Turn in:
1) program outline/psuedocode/flowchart
2) printout of main.c
3) description of response received (1 paragraph or less, handwritten or typed)
6: Extra credit
I will read up to 2 teleprompters in class for extra credit (5%, randomly selected)
Useful Functions:
ms_delay(int) – Pauses the code for the number of ms passed in the argument.
Functions: (FOR NICK – Can you fill in function boxes below?)
Preliminary information
Result = sin(angle)
Why functions?
main ()
The Programming Process (as seen by the computer)
How a program works: (FOR Canfield – To enter program execution process)
How a computer works: (FOR SHibakov)
Data Types: Integers and Floating Point Numbers
Know your numbers:
Integers non integers, Real complex numbers
For numbers:
Integers
- typically used for counting things
- 1 2 3 4
Floating point numbers (decimal floats)
- typically used for measuring things
- 1.0 2.2 3.14159 4.4
- Note: 1 is different from 1.0 to C
Why the distinction? Its partly an issue of size, partly an issue of how to represent the number
(think like a computer)
How does a computer think about numbers?
For a computer, everything is one of two states:
On/off (yes or no, true or false, high or low (voltage, light source))
These two states or binary lead to a natural numerical language for computers
Binary or base 2
Representing numbers:
Base 10 (decimal)
Base 2 (binary) 0b
Base 16 (hexadecimal) 0x
So Computers think about numbers (or any data/representation) as a binary number
Details on Creating Variables: First Integers
Declaring a variable (required)
int integer_number;
· Sets aside a portion of member (typically 32 bits),
· gives its address the name “integer_number”,
· allows integers to be stored there
· (anywhere within the current function, integer_number is replaced with memory address)
Assigning a Value to the Variable:
In the declaration statement:
int integer_number=100;
· Sets aside a portion of member (typically 32 bits),
· gives its address the name “integer_number”,
· allows integers to be stored there
· initializes its value to 100 (
Or after:
int integer_number;
integer_number = 100;
Details on Creating Variables: Floating Point Variables
float decimal_number;
· Sets aside a portion of memory (typically 32 bits),
· gives its address the name “decimal_number”,
· allows decimals to be stored there
Assigning a Value to the Variable:
In the declaration statement:
float decimal_number=3.14159;
· Sets aside a portion of member (typically 32 bits),
· gives its address the name “decimal_number”,
· allows integers to be stored there
· initializes its value to 3.14159
Or after:
int decimal_number;
decimal_number = 3.14159;
About Variable Names:
More About the Assignment Operator:
Int a,b;
a = 100;
b = a;
100 = a; (NO!)
b = b+1; (ok)
Character Data Types:
To handle Non-numbers (for example, characters)
char letter;
· Sets aside a portion of member (typically 8 bits),
· gives its address the name “letter”,
· allows integers to be stored there, 0 - 125
· Uses asci table if references as a character
letter = “a”;
· initializes the value of “letter” to “a”
string – collection (or array) of characters:
char text[30];
text = “a b c d e …1 2 3 4”;
The ASCII Table:
Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex
------
(nul) 0 0000 0x00 | (sp) 32 0040 0x20 | @ 64 0100 0x40 | ` 96 0140 0x60
(soh) 1 0001 0x01 | ! 33 0041 0x21 | A 65 0101 0x41 | a 97 0141 0x61
(stx) 2 0002 0x02 | " 34 0042 0x22 | B 66 0102 0x42 | b 98 0142 0x62
(etx) 3 0003 0x03 | # 35 0043 0x23 | C 67 0103 0x43 | c 99 0143 0x63
(eot) 4 0004 0x04 | $ 36 0044 0x24 | D 68 0104 0x44 | d 100 0144 0x64
(enq) 5 0005 0x05 | % 37 0045 0x25 | E 69 0105 0x45 | e 101 0145 0x65
(ack) 6 0006 0x06 | & 38 0046 0x26 | F 70 0106 0x46 | f 102 0146 0x66
(bel) 7 0007 0x07 | ' 39 0047 0x27 | G 71 0107 0x47 | g 103 0147 0x67
(bs) 8 0010 0x08 | ( 40 0050 0x28 | H 72 0110 0x48 | h 104 0150 0x68
(ht) 9 0011 0x09 | ) 41 0051 0x29 | I 73 0111 0x49 | i 105 0151 0x69
(nl) 10 0012 0x0a | * 42 0052 0x2a | J 74 0112 0x4a | j 106 0152 0x6a
(vt) 11 0013 0x0b | + 43 0053 0x2b | K 75 0113 0x4b | k 107 0153 0x6b
(np) 12 0014 0x0c | , 44 0054 0x2c | L 76 0114 0x4c | l 108 0154 0x6c
(cr) 13 0015 0x0d | - 45 0055 0x2d | M 77 0115 0x4d | m 109 0155 0x6d
(so) 14 0016 0x0e | . 46 0056 0x2e | N 78 0116 0x4e | n 110 0156 0x6e
(si) 15 0017 0x0f | / 47 0057 0x2f | O 79 0117 0x4f | o 111 0157 0x6f
(dle) 16 0020 0x10 | 0 48 0060 0x30 | P 80 0120 0x50 | p 112 0160 0x70
(dc1) 17 0021 0x11 | 1 49 0061 0x31 | Q 81 0121 0x51 | q 113 0161 0x71
(dc2) 18 0022 0x12 | 2 50 0062 0x32 | R 82 0122 0x52 | r 114 0162 0x72
(dc3) 19 0023 0x13 | 3 51 0063 0x33 | S 83 0123 0x53 | s 115 0163 0x73
(dc4) 20 0024 0x14 | 4 52 0064 0x34 | T 84 0124 0x54 | t 116 0164 0x74
(nak) 21 0025 0x15 | 5 53 0065 0x35 | U 85 0125 0x55 | u 117 0165 0x75
(syn) 22 0026 0x16 | 6 54 0066 0x36 | V 86 0126 0x56 | v 118 0166 0x76
(etb) 23 0027 0x17 | 7 55 0067 0x37 | W 87 0127 0x57 | w 119 0167 0x77
(can) 24 0030 0x18 | 8 56 0070 0x38 | X 88 0130 0x58 | x 120 0170 0x78
(em) 25 0031 0x19 | 9 57 0071 0x39 | Y 89 0131 0x59 | y 121 0171 0x79
(sub) 26 0032 0x1a | : 58 0072 0x3a | Z 90 0132 0x5a | z 122 0172 0x7a
(esc) 27 0033 0x1b | ; 59 0073 0x3b | [ 91 0133 0x5b | { 123 0173 0x7b
(fs) 28 0034 0x1c | < 60 0074 0x3c | \ 92 0134 0x5c | | 124 0174 0x7c
(gs) 29 0035 0x1d | = 61 0075 0x3d | ] 93 0135 0x5d | } 125 0175 0x7d
(rs) 30 0036 0x1e | > 62 0076 0x3e | ^ 94 0136 0x5e | ~ 126 0176 0x7e
(us) 31 0037 0x1f | ? 63 0077 0x3f | _ 95 0137 0x5f | (del) 127 0177 0x7f
Integer/Decimal Output with sprintf and writeline:
sprintf - Formated PRINT to a String
sprintf(text, “a b c …”);
or, with numbers:
sprintf(text,”the value of a is 10“);
To include numerical data types:
sprintf(text,”The value of a is %d.”,a);
%d = decimal
%3d = 3 digits for the decimal
Other examples:
For floats: % f
Arithmetic Operations (implicit):
+
-
*
/
% ß for integergers only
Discussion of their use with integers, with floats
Mixed Assignments:
Use caution to not mix variable types in assignment statements
Casting variables as alternative types:
Lab Assignment #2: Sound Lab – the new TTUpod
Create a product (a TTUpod) using your Dragon12+ board that plays a song of your choosing. The song needs to play a minimum of 10 tones using 3 or more distinct notes. The song should start whenever the board is reset. 80% of your grade will be based on meeting minimum requirements. 20% of your grade will be based on innovative creation of this product.
Steps:
1: Review the tutorial: “Digital Output on an MCU (example for Dragon 12)
2: Review the useful function, smDelay(int), use of for loop (below)
3: Plan your program
4: Create a new project for lab 2 (or modify your previous project for lab 2)
5: Write your program to create the required product (see example program below)
6: Demonstrate your project to 2 other people (non-engineering students), record their response
7: Turn in:
1) program outline/psuedocode/flowchart
2) printout of main.c
3) Demo to lab assistant and get printout initialized
4) description of response received (1 paragraph or less, handwritten or typed)
8: Due Date: ______
9: Extra credit
I will play up to 2 TTU-pods in class for extra credit (5%, randomly selected)
Useful Functions:
smDelay(int) – Pauses the code for a period of time (hint: smDelay(6000) is about 1 ms)
for(i=1; i<1000; i=i+1) {
xxx
}
// steps through the loop executing xxx 1000 times, then continues
Note/frequency - Hz/Wavelength - cm
(from http://www.phy.mtu.edu/~suits/notefreqs.html)
C4 / 261.63 / 132.C#4/Db4 / 277.18 / 124.
D4 / 293.66 / 117.
D#4/Eb4 / 311.13 / 111.
E4 / 329.63 / 105.
F4 / 349.23 / 98.8
F#4/Gb4 / 369.99 / 93.2
G4 / 392.00 / 88.0
G#4/Ab4 / 415.30 / 83.1
A4 / 440.00 / 78.4
A#4/Bb4 / 466.16 / 74.0
B4 / 493.88 / 69.9
C5 / 523.25 / 65.9
C#5/Db5 / 554.37 / 62.2
D5 / 587.33 / 58.7
D#5/Eb5 / 622.25 / 55.4
E5 / 659.26 / 52.3
F5 / 698.46 / 49.4
F#5/Gb5 / 739.99 / 46.6
G5 / 783.99 / 44.0
G#5/Ab5 / 830.61 / 41.5
A5 / 880.00 / 39.2
A#5/Bb5 / 932.33 / 37.0
B5 / 987.77 / 34.9
C6 / 1046.50 / 33.0
C#6/Db6 / 1108.73 / 31.1
D6 / 1174.66 / 29.4
D#6/Eb6 / 1244.51 / 27.7
E6 / 1318.51 / 26.2
F6 / 1396.91 / 24.7
F#6/Gb6 / 1479.98 / 23.3
G6 / 1567.98 / 22.0
G#6/Ab6 / 1661.22 / 20.8
A6 / 1760.00 / 19.6
A#6/Bb6 / 1864.66 / 18.5
B6 / 1975.53 / 17.5
Tutorial: “Digital Output on an MCU (example for Dragon 12)