Nmbmnbmnb - Tennessee Technological University



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

[pic]

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

[pic]

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 ()

[pic] [pic]

[pic] [pic]

The Programming Process (as seen by the computer)

[pic]

How a program works: (FOR Canfield – To enter program execution process)

How a computer works: (FOR SHibakov)

[pic][pic][pic]

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= Greater than or equal to

!= No equal to

An Example using input switches

The Dragon 12 has a number of input switches (attached to PORT H)

These switches could be attached to a door, window, proximity sensor or keyboard (for example).

Conditional statements provide a way to evaluate if a switch has been activated or not.

If – else – else if Statements

For multiple alternatives where only one is to be selected, the else statement can be used in conjunction with the if.

By using the else, statement, only one of the two alternatives would be selected (true_condition_statements or false_condition_statements). Note that if the else had not been used, then a true condition would result in both true_condition_statements and false_condition_statements being executed.

Finally, if more than two alternatives exist, the else if statement may be used:

Conditional Execution – Some advanced issues:

Consider first the condition in the conditional statement. The condition is either true or false (evaluates to a non-zero or zero value respectively). Thus, the statement:

if (12) {

statements

}

is always true and statements always get executed.

Logical Operators:

Multiple conditions can be combined in several ways. One technique is nested if statements:

This program will turn on the alarm if both switch 1 and 2 are pressed. However, these could be combined into a single statement through the use of logical operators, as the following example shows.

where && is the logical AND operator. The logical operators generally used are:

|A |B |A&B |A|B |!A&B |

|0 |0 |0 |0 |1 |

|0 |1 |0 |1 |1 |

|1 |0 |0 |1 |1 |

|1 |1 |1 |1 |0 |

&& AND

|| OR

! NOT

Advanced Examples Using If statements

1. Check if SW 3 is pressed (PH2 is low)

What are potential problems with code above?

2. What if multiple switches are pressed (but want to check SW3 only)?

(makes use of AND / OR operators)

3. Using else if to check multiple switches

4. Using nested ifs to look for advanced switch information

[pic]

Lab Assignment #3: Counting Money

Create a product to count money using your Dragon12+ board. The product will assist you in keeping track of your loose change, quarters, dimes, nickels and pennies. The product should have four buttons, one for each type of coin, and will add up money as you push buttons corresponding to the coins you have. The current total amount in dollars and cents should be displayed on the lcd. 95% of your grade will be based on meeting minimum requirements of adding coins and displaying the amount accurately. 5% of your grade will be based on improvements to this basic setup.

Steps:

1: Review the tutorial: “Digital input on an MCU (example for Dragon 12)

2: Review the useful function, sprintf, if statements

3: Plan your program (see example flowchart below)

4: Create a new project for lab 3 (or modify your previous project for lab 3)

5: Write your program to create the required product (see example program below)

6: Have one friend use your product to count their change. Record their input relative to the value and quality of this money counting machine.

7: Turn in:

5) program outline/psuedocode/flowchart

6) printout of main.c

7) Demo to lab assistant and get printout initialized

8) description of response received (1 paragraph or less, handwritten or typed)

8: Due Date: _______________________

9: Extra credit

I will use up to 1 money counters in class for extra credit (5%, randomly selected)

Useful Functions:

sprintf(text, "You have $ %d.%d ",dollars,cents);

if( PORTH == 0b11111110) {

xxx

}

// executes xxxx if condition is met.

[pic]

Tutorial: “Digital Output on an MCU (example for Dragon 12)

I/O Port:

• connects a computer to the outside world

• 1 byte in memory (8 bits)

• To the computer it looks like a memory address, to the outside work it looks like a voltage signal (5volt source or a ground).

PORTH (Port H)

• Port that can be configured as general I/O (or other functions)

• Connected to SW2 – SW5 on Dragon 12 (see below)

DDRH (Data Direction Register for port H)

• Determines which bits of PORTH are input/output

DDRH = 0xFF;

• Makes all bits of port H output

PTH

• Variable name for Port H.

[pic]

Repetition in programming via the while loop and for statement:

The while loop and for loop allow a structure approach to repetition or looping in a program. The structure of the while loop is:

The while loop:

1) Checks the statement in the parentheses (condition)

2) if (condition) is true the statements inside the braces will be executed

3) It repeats steps 1 and 2 until (condition) is false. When the condition is false, flow control continues after the while loop

The for loop naturally contains a counter, step size and maximum value defining a fixed number iterations through the loop. The structure of the for loop is:

The for loop:

1) initializes the counter at the start of the for loop

2) executes statements b/n braces if i ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download