Download presentation
Presentation is loading. Please wait.
1
ECE 3567 Microcontrollers Syllabus – Spring 2019
2
ECE 3567 Microcontrollers
3
ECE 3567 Microcontrollers Laboratory Schedule:
Week # Dates Material * Week /7, 1/11 (M, F) Lecture: Review: C Programming, Microcontroller Architecture Week /14, 1/18 (M, F) Lab1/Quiz 1: Code Composer, Project Creation, GPIO, Interrupts / Dr. Martin Luther King Jr. Day, No Lab. NOTE: Lab dates switch order Week 3/ /25, 1/28 (F, M) Lab 2/Quiz 2: Serial Communications, LCD Display /1, 2/ (F, M) Lab 2 (continued) Week 5/ /8, 2/11 (F, M) Lab 3/Quiz 3: Command Structure 2/15, 2/18 (F, M) Lab 3 (continued) Week 7/ /22, 2/25 (F, M) Lab 4/Quiz 4: Timers, PWM, LED Colors 3/1, 3/ (F, M) Lab 4 (continued)
4
ECE 3567 Microcontrollers Laboratory Schedule:
* Week # Dates Material * Week / (F Lab) Lab 5/Quiz 5: The Analog to Digital Converter, RC Feedback 3/11 – 3/15 Week of Spring Break, No Labs Week / (M Lab) Lab 5/Quiz 5: The Analog to Digital Converter, RC Feedback Week /22, 3/25 (F, M) Lab 5 (continued) Week 12/ /29, 4/1 (F, M) Lab 6/Quiz 6: The Temperature Sensor 4/5, 4/ (F, M) Lab 6 (continued) Week 14/ /12, 4/15 (F, M) Lab 7/Quiz 7: Heating Element, Temperature Feedback, Mapping LED 4/19, 4/22 (F, M) Lab 7 (continued)
5
Module 1: A Review of the C Programming Language
6
C Standards ANSI C – (1972), Dennis M. Ritchie, Bell Labs. One of the Authors of the original book “The C Programming Language”. American National Standards Institute defined the C Language officially in 1989. ISO 9899 (International Organization of Standards) has assumed responsibility for the “official” version of “C”. The ISO 9899 version is arrogantly referred to as “The C Language”, and ISO claims to “own” the language. ISO 9899 demagogues have released two newer versions of “C”, in 1999 and Both have additional keywords which are generally useless in an embedded environment or for portability. Most C compliers have been upgraded to C99 but not C11. The original 32 keywords defined in 1989 are sufficient for embedded design, and will make the code portable across more platforms than either C99 or C11.
7
A Good C Programming Reference
8
ANSI C Keywords Originally, these 32
9
“C” (Code Composer uses ‘ 1999 C’)
Has fewer structures than C++ Very few structures in Embedded C because it is highly dependent on predefined addresses and hardware names, included in HEADER files provided by the manufacturer. Consists of: Compiler directive – #include, #define Declarations/ Prototypes – void func(char, unsigned int), char, etc. Functions – void func(char x, unsigned int t) Statements – switch(X), for(i=1;i<11;i++), if(x=3) Variables Constants
10
Data Types C Programming Language
11
Data Types (Sizes are PROCESSOR DEPENDENT)
MSP430 Series Microcontrollers
12
Variable Types
14
The ASCII Character Set
15
Escape Sequences used in Formatting
16
Storage Classes auto (default) – The variable is only defined in the current function. register – The variable is still local, but is not stored in memory, it is stored in a register of the MCU, making access VERY FAST. volatile – A global variable that can be changed by multiple processes in the operating environment. static – This is still a local variable! It’s value remains intact between accesses of the function in which it is defined and used. extern – Usually in header files, this means that the variable definition exists in another files. This is not a declaration type. It is a statement that the declaration exists elsewhere. All modules can used the defined variable. volatile – Two or more threads are permitted to change the value of this variable. It is truly global.
17
Also called Variable Declaration Modifiers
(a type of compiler directive, limits the SCOPE of the variable) EXAMPLES: volatile unsigned int temp; extern volatile unsigned int temp;
18
volatile The variable scope is available to all functions and all files in the project The variable can be changed anywhere in the project
19
extern The variable can be used in the current file, but is declared in another file
20
static The variable can be used by multiple C functions within a single .c file, but not across files. Holds the last value assigned to it when the process is acting outside of the processes defined in the file. static int x; function() { int y; y = y+1; x = y; } function2() { x = 2*x -1; }
21
const The variable will have a single value at runtime and cannot be changed in the program
22
Global Variables Global variables are defined BEFORE a function and may be volatile, static, extern, etc.
23
Local Variables Declared inside functions
Are not preserved when the execution returns form the function void delay_cycles(unsigned int x) { unsigned int a; a = x; while (a >= 0) a--; } return;
24
# Compiler Directives #include #define #pragma #ifdef #ifndef #typedef
25
#define #define – Declares a constant and may or may not give it a value. Can also be used to label a macro. #define RED 0x11
26
#include #include – A compiler directive that adds the contents of an external file to the current file definition. #include “stdio.h” // has to be between quotes #include “3567.h”
27
#pragma #pragma – Tells the compiler to include something once if it is included in multiple files.
28
Constant Declarations
There are “constant” variables: const int x = 0x1234; Constant numbers are usually defined with a the compiler directive: #define RED 0x11
29
Constant Declarations
There are “constant” variables: const int x = 0x1234; Constant numbers are usually defined with a the compiler directive: #define RED 0x11
30
#ifdef / #ifndef #ifdef – Conditional compilation based on a definition #endif #ifndef – If this hasn’t already been defined NOTE: does the same thig as #pragma, but is much more common and preferred. Many compilers don’t support #pragma #define __MSP_HAS_AES256__ #ifdef __MSP430_HAS_AES256__ conditional code goes here #endif // __MSP430_HAS_AES256__
31
#typedef #typedef – Defines an alias name or a new type The typedefs for abbreviated variable types are defined in stdint.h typedef signed char int8_t; typedef unsigned char uint8_t; typedef int int16_t; typedef unsigned int uint16_t; typedef long int32_t; typedef unsigned long uint32_t; typedef long int64_t; typedef unsigned long uint64_t; const uint16_t BaseAddress; // Base Address of EUSCI_A port const uint8_t TxPort;// GPIO Port settings for TX pin
34
C
35
Used EXTENSIVELY to set and clear specific bits
37
Ternary Operators
38
Bit Operators ~ Can be used to set and clear multiple bits
39
If and If-else Statements
40
Switch Statements
41
If-else-if is similar to Switch Statements
But compares Boolean expressions instead of integer values
42
Switch or If-Else?
43
For Loops Use nested to to step through multi-dimensional variables
44
Do-while Loops One more iteration than a while loop
45
While Loops Most common – used for delay until an event
46
Loop Control
47
Functions in C
48
Function Prototypes void funct1(); void funct1(void);
void funct2(unsigned int); unsigned int funct3(void); unsigned int funct4(char); unsigned int funct5(char, unsigned int);
49
Functions Called by Value
Functions operate on a COPY if the variable in the argument, not the variable
50
Functions Called by Reference
What is call by reference? The major difference between call by value and call by reference is that in call by value a copy of actual arguments is passed to respective formal arguments. While, in call by reference the location (address) of actual arguments is passed to formal arguments, hence any change made to formal arguments will also reflect in actual arguments. NOTE: C DOES NOT SUPPORT CALL BY REFERENCE, ONLY C ++ HOWEVER: You can do the same thing by passing pointers as arguments
51
EXAMPLE of “Call by Reference” equivalent in C
52
Function with Local Variables
53
TI Macros and Structures
54
TI Macros and Structures
55
Example of main ()
56
Example of main ()
57
Embedded C It is C, but does NOT conform to common software practice due to all the # compiler directives and header files. It relies extensively on #defines and #typedefs located in included HEADER files (.h) in order to make low level manipulation of the hardware much easier than assembly language. NOTE: this also makes the C program look “funny” to software engineers. It is hardware dependent, particularly constant and variable sizes. If unsure of a constant or variable size use sizeof() to find out!
58
Embedded C REAL WORLD ADVICE ON EMBEDDED PROGRAMMING:
NEVER write from the ground up. COPY and MODIFY working source code. Be careful of COPYWRITES. ALWAYS use the HEADER files from the MCU manufacturer. Use an Evaluation Board for development if possible. If not, these usually come with schematics that give you a good idea of what is required to get your MCU “off the ground”. UNDERSTAND THE HARDWARE! Read the Datasheet, at least for the modules that you are using. Initialization is MORE THAN HALF of the project. Write the initialization for each MCU sub-device (module) as a SEPARATE function.
59
Embedded C REAL WORLD ADVICE ON EMBEDDED PROGRAMMING:
The main.c should consist of the following, in this order: A DETAILED DESCRIPTION in comment format #includes (esp. xxx.h files) and compiler directives Calls to INITIALIZATION FUNCTIONS The MAIN LOOP consisting almost entirely of function calls Interrupt Service Routines if applicable
60
C Coding Styles Fast Response Accuracy Reliability Low Power
Driven by the Application Fast Response Accuracy Reliability Low Power Minimize code length Minimize cost Minimize size Examples Deep space applications Medical implants
61
C FOR SPEED! Techniques To Transform Your Turtle Code BY JOHN ALLEN
62
C FOR SPEED! Watch Your Algorithms The algorithmic methods of code optimization can be used in any language, and you probably already use them to some extent. Here's an example: for (i=0; i<(1000; i++){ a=b+c; x[i]= a*i; } The values stored in the variables b and c do not change, therefore the value stored in the variable a does not change after the first iteration. Consequently, the variable a only needs to be set once: a = b+c; for (i=0; i<1000; i+ +){ x[i]=a*i; } Be careful not to use unnecessary variables. For example: j=0; for (i=0; <1000; i++){ x[i]=i; y[i]=j++; } Why increment two variables each loop iteration when one will do? Try this instead: for (i=0;i<1000; i++) x[i]=i; y[i]=i }
63
C FOR SPEED! Another technique is to eliminate common sub-expressions, for example: a =(x*y)+1; b = 2*(x*y)+3; c = a+b+(x*y); See how x and y are multiplied each time. Looking up the value of a variable is faster than re-multiplying! Temp1 = x*y; a = templ+1; b = 2*temp1+3; c = a+b+temp1;
64
C FOR SPEED! Also, beware of using variables which are really constants. a = 1; b = c+a; d = a+1; Use a constant instead. a = 1; b = c+l; d=2; This rule also applies for the following: a = 1+2; which, of course should he a = 3; Most compilers will catch this simple case, but you might as well do it yourself, just in case the compiler doesn't.
65
C FOR SPEED! Another thing to look for is code like this: #define MAXSIZE 5 a = MAXSIZE+b-1; It would be faster to do: #define MAXSIZE 5 #define MAXSIZEMINUSONE 4 a = MAXSIZEMINUSONE+b; However, this makes it harder to modify later, because if MAXSIZE is changed you also must change MAXSIZEMINUSONE. It would probably not he a good trade-off if that line is executed only once, but if it is inside a loop that exeutes 10,000 times, it can really make a difference.
66
C FOR SPEED! Unrolling Loops If you are trying to squeeze every last ounce of speed, you can unroll your loops, but this may cost you development or modification time if the number of times the loop is executed has to change. Also, it is more difficult to do this when the loop is controlled by user input. Here is an example of unrolling: for (i=0; i<3; i++) a[i] =i becomes a[0]=0; a[1]= 1; a[2] = 2; This eliminates the increment for each loop iteration, and substitutes an explicit operation for the memory accesses on the variable i. If unrolling of loops is not practical, perhaps you can combine two or more loops like this: for (i=0; i<l000; i++) a[i] =i for (i=0; i<1000; i++) b[i]=i; into a single loop: for (i=0; <1000; i++) a[i] = b[i]=i; You just eliminated one increment and one test instruction for each loop iteration, All of the optimizations I have shown so far are programming techniques that, for the most part, work in any language. Now we will explore two C-specific techniques, which may also he adapted, with some effort, to other languages.
67
Loop Unrolling in C Example - Fast
do { if(response==1) break; If(response==1) break; }while(1); do { nop; }while(response != 1);
68
Loop Unrolling in C with Watchdog Timer (Fast and Reliable)
WDTCTL = (WDTCTL & 0x00FF) | WDTPW | WDTCNTCL do { if(response==1) break; break; }while(1); WDTCTL = WDTPW + WDTCNTCL; WDTCTL = WDTPW + WDTCNTCH; // Initialize Watchdog //Refreshes Watchdog. If no response, resets // Multiple sequential tests eliminate loop delay.
69
Using Macros In many cases a piece of code is broken up into functions to eliminate typing duplicate code and to make things easily modifiable and readable. However, the use of macros will do the same thing, and, since they arc resolved at compile time, they require absolutely no run-time overhead! There is one thing to remember about using #define macros. When passing arguments to macros, whatever you give as an argument will be literally replaced when the macro is expanded. For example: #define squared(x) (x*x) a = 2; b = squared(a++); is replaced with a = 2; b = a++*a++; This may not be what you intended! The most common use of macros is single-line macros, but C does not limit you to that. Lines that end with the backslash character (\) are combined with the next line, then both the backslash character and the following line feed are deleted during pre-processing: #define test_macro(a,b,c,array) \ inti; \ for (i=0; i<c; i++) \ a+ = array[i,b]; \ will become #define test_macro(a,b,c,array) int i for (i=0; i<c; i++) = array[i,b]; Remember that macros are substituted directly into the source file by the pre-processor. You cannot call a macro from itself, or from another macro that it calls, because recursion is not possible. Read your favorite C reference book a couple of times to familiarize yourself with #define macros. Once you understand how macro substitution works, you can avoid the run-time overhead of some function calls. C FOR SPEED!
70
C FOR SPEED! Shifty Bits Another way to speed up execution is to take a trick from assembly programmers and fake your powers-of-two division and multiplication: 5*2 = 10 is, in binary, 0101* 0010 = 1010; 12/2 = 6 is, in binary, 1100 / 0010 = 0110. Notice that 10 in binary (1010) is just five shifted to the left by one bit (010l). Also, six is just 12 shifted to the right by one bit. A shift operation is very fast compared to mathematical operations such as multiplication and division. of course, this only works for powers of two, but it :an still help considerably. Here is a straightforward way to divide or multiply integers by two: #define divide_2(a) (a>>1) #define mult_2(a) (a<<1)
71
C FOR SPEED! For division or multiplication by four it would be: #define divide_4(a) (a>>2) #define mult_4(a) (a<<2) In a more general fashion, you can write macros to divide or multiply by powers of two, where the first argument is the number and the second is the divisor (2. 4, 8, etc.): #define divide_2p(a,p) (a>>p) #define mulL_2p(a,p) (a<<p) If you divide an odd integer such as five by two using mathematical division, you will get the real number 2.5. If you use the shifting method you will get the integer two; the remainder is lost. This is the same as the DIV operation in Pascal. Remember that shifting numbers in lieu of multiplication or division only works for integers, not for floating point numbers. Power Tools These tools can he very powerful when used properly but like all tools, they are only as efficient as the person using them. Being efficient takes some skill and a lot of practice - so practice! This is certainly not a complete discussion on how to optimize programs; the art of programming is too complex to allow such a thing in one article. But that same complexity leaves a lot of room for improvement, and I hope that the presentation of these techniques encourages you to further develop your own skills. John Allen started programming in C when he got an original 520ST with TOS-on-disk back in 1985.
72
Lab 1/ Quiz 1: Code Composer, Project Creation, GPIO, and Interrupts
IN TWO WEEKS: Lab 1/ Quiz 1: Code Composer, Project Creation, GPIO, and Interrupts
73
Lab Instructions (General):
1. Watch the Video for the Lab at u.osu.edu/ece3567labs BEFORE coming to the Lab session.
74
Lab Instructions (General):
2. Take the Quiz (in class, closed note) to verify that you have watched and understood the pre- lab video.
75
Lab Instructions (General):
3. Perform the Lab, referring to the video and/or instructions slides
76
Lab Instructions (General):
For Lab 1 you will have detailed instructions and only 1 lab session to complete the lab. DO NOT simply copy the slides. You must understand what each step does, or you will not be able to troubleshoot your project. All other labs will require you to write your own code, and you are given 2 weeks to complete the Lab.
77
Lab Instructions (General):
4. Demonstrate the working Lab project to the Lab Staff to earn the Lab CHECKPOINTS The list of lab checkpoints will be included in the pre-lab material NOTE: The best approach is to get one checkpoint at a time to verify that your design is functioning at that point, prior to moving to the next step in the lab.
78
Lab Instructions (General):
5. VERIFY that you received your Checkpoints in writing by signing off on the Checkpoint Sheet before leaving the laboratory.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.