ECE 3567 Microcontrollers Syllabus Autumn 2019 ECE 3567
ECE 3567 Microcontrollers Syllabus – Autumn 2019
ECE 3567 Microcontrollers Syllabus – Autumn 2019
ECE 3567 Microcontrollers Syllabus – Autumn 2019
ECE 3567 Microcontrollers Syllabus – Autumn 2019
ECE 3567 Microcontrollers
ECE 3567 Microcontrollers
ECE 3567 Microcontrollers Lab Reports • • Purpose – To verify that you have understood the lab Uses OSU standard format from Freshman Engineering There is a Template on the website. The grading rubric is published on the website.
ECE 3567 Microcontrollers Lab Reports - Rubric
The Laboratories
ECE 3567 Microcontrollers Labs • Lab 1 – Blinking LEDs (1 week) Learn to create a project in Code Composer Studio Get a project working • Lab 2 - GPIO & Interrupts (2 weeks) Learn to use Interrupt Service Routines and Timers Control process timing • Lab 3 - Pulse Width Modulation (2 weeks) PWM, More Timers, Multicolored LED Change the LED to any color • Lab 4 - Analog to Digital Converter (2 weeks) A-to-D Converter, Measure Voltage Generate an analog DC voltage, then measure it digitally • Lab 5 – Serial Communications (2 weeks) UART, Command Structure Set up a custom command structure over a serial port • Lab 6 – Closed Loop Control (2 weeks) Feedback Control, Control Voltage Implement a closed loop controller for voltage setpoint. Indicate voltage range with LED color.
ECE 3567 Microcontrollers Embedded Hardware Microprocessor (μP) Micro-Controller (MCU)
ECE 3567 Microcontrollers
ECE 3567 Microcontrollers
ECE 3567 Microcontrollers
ECE 3567 Microcontrollers
ECE 3567 Microcontrollers
The Development Platform
ECE 3567 Microcontrollers Code Composer Studio
ECE 3567 Microcontrollers Code Composer Studio 20
Embedded C Programming – Cross Compiler Inputs: . c, . h Pre-processor output files Source files (MCU specific) Object files: . o, . obj Assembly: . asm, . a Executable: . hex, . bin Debugging, . lst is an ASM file
ECE 3567 Microcontrollers TI Launch. Pad (MSP 430 FR 6989)
P 2. 1 RC PWM output RC charging circuit ECE 3567 Microcontrollers Custom Designed Booster. Pack RGB PWMs to LED ADC – A 5 fcutoff = 6 Hz A 5 input on ADC 12
Timers: TA 0 – ISR, Command, USCI_A 0 TA 1 – ADC 12, USCI_A 1 TB 0 – PWMs The MSP 430 FR 6989 Microcontroller Architecture
Why Should You Use Embedded C Programming
ECE 3567 Microcontrollers Embedded C Programming – C / Embedded C / ASM C Programming Language Embedded C Programming • Structure oriented • Platform specific (processor) • Compiler used to generate machine code • Assumes FAST processor • Generalized applications • Requires an OS interface • • • • Assembly Language • Fastest Speed VERY different coding style • Smallest Code Size Subset of C • Too difficult for large scale projects Can be faster and more efficient than “C” Hardware oriented Cross-compiler used to generate ASM, HEX, and Machine Code Microcontroller specific Smaller code size Speed limited by MCU Can be used to interface environment through sensors and actuators Lower cost than computer Lower power than computer More reliable that OS based systems Can provide better security
ECE 3567 Microcontrollers Embedded C Programming -Value Programming Overall
ECE 3567 Microcontrollers Embedded C Programming -Value Embedded Programming – 2010 to 2013
ECE 3567 Microcontrollers Embedded C Programming -Value Embedded Programming – 2017
ECE 3567 Microcontrollers Embedded C Programming -Value Embedded Programming – 2019, Overseas
Embedded C Programming
Embedded C Programming - Distinctives • Embedded C is a SUBSET of the C Programming Language • Like assembly language, it is written to operate directly on HARDWARE • ALL features of unique to C++ are nearly useless at the embedded level. • High level software engineers NEARLY ALWAYS look down their nose at the embedded coding style. They consider it “sophomoric”, because it CANNOT take advantage of many aspects of high-level languages. • (Don’t be one of those people. They only look foolish when the get to industry).
Embedded C Programming - Distinctives • Almost all embedded code is contained in HEADER files, both from the manufacturer and the person writing the code. • Embedded code is MICROCONTROLLER SPECIFIC, and only portable to another application of the same MCU. • Almost ALL embedded code has re-used files written for the same MCU Max Headroom
Embedded C Programming - Distinctives
Embedded C Programming - Distinctives • Trying to write embedded code without using the manufacturer’s header files is PURE INSANITY. Bela Lugosi
Take a 10 Minute Break
Documentation
ECE 3567 Microcontrollers Embedded C Programming – Documentation 1 – Create a documentation standard 2 – Use a consistent coding style 3 – Use a documentation template 4 – Use a file header 5 – Comment before coding 6 – Document as you go not afterwards 7 – Update comments when making coding changes 8 – Consistent use of commenting brackets and location 9 – Write non-obvious comments – Explain the why, NOT the how 10 – Provide example uses to increase clarity
ECE 3567 Microcontrollers Embedded C Programming – File headers (. c)
ECE 3567 Microcontrollers Embedded C Programming – Comments
Compiler directives and. h files
ECE 3567 Microcontrollers Embedded C Programming – Compiler Directives • #define – Declares a constant and may or may not give it a value. Can also be used to label a macro. #define RED 0 x 11 • #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 or carrots <> #include “ 3567. h” Usage is reserved • #pragma – Tells the compiler to include something once if it is included in multiple files. • #ifdef – Conditional compilation based on a definition #endif #define __MSP 430 FR 6989 #ifdef __MSP 430 FR 6989__ #include <msp 430 fr 6989. h> #else #include <msp 430. h> #endif // __MSP 430 FR 6989__
#typedef – Defines an alias name or a new type The typedefs for abbreviated variable types are defined in stdint. h typedef typedef signed char int 8_t; unsigned char uint 8_t; int 16_t; unsigned int uint 16_t; long int 32_t; unsigned long uint 32_t; long int 64_t; unsigned long uint 64_t; const uint 16_t Base. Address; // Base Address of EUSCI_A port const uint 8_t Tx. Port; // GPIO Port settings for TX pin
ECE 3567 Microcontrollers Embedded C Programming – TI header files (. h) #include "inc/hw_memmap. h" #include "adc 12_b. h" #include "aes 256. h" #include "comp_e. h" #include "crc 32. h" #include "cs. h" #include "dma. h" #include "esi. h" #include "eusci_a_spi. h" #include "eusci_a_uart. h" #include "eusci_b_i 2 c. h" #include "eusci_b_spi. h" #include "framctl. h" #include "gpio. h" #include "lcd_c. h" #include "mpu. h" #include "mpy 32. h" #include "pmm. h" #include "ram. h" #include "ref_a. h" #include "rtc_b. h" #include "rtc_c. h" #include "sfr. h" #include "sysctl. h" #include "timer_a. h" #include "timer_b. h" #include "tlv. h" #include "wdt_a. h" Structures Compiler Directives #define ADC 12_B_START_AT_ADC 12 MEM 0 #define ADC 12_B_START_AT_ADC 12 MEM 1 #define ADC 12_B_START_AT_ADC 12 MEM 2 #define ADC 12_B_START_AT_ADC 12 MEM 3 #define ADC 12_B_START_AT_ADC 12 MEM 4 #define ADC 12_B_START_AT_ADC 12 MEM 5 #define ADC 12_B_START_AT_ADC 12 MEM 6 #define ADC 12_B_START_AT_ADC 12 MEM 7 #define ADC 12_B_START_AT_ADC 12 MEM 8 #define ADC 12_B_START_AT_ADC 12 MEM 9 #define ADC 12_B_START_AT_ADC 12 MEM 10 #define ADC 12_B_START_AT_ADC 12 MEM 11 #define ADC 12_B_START_AT_ADC 12 MEM 12 #define ADC 12_B_START_AT_ADC 12 MEM 13 #define ADC 12_B_START_AT_ADC 12 MEM 14 #define ADC 12_B_START_AT_ADC 12 MEM 15 #define ADC 12_B_START_AT_ADC 12 MEM 16 typedef struct ADC 12_B_init. Param (ADC 12 CSTARTADD_0) (ADC 12 CSTARTADD_1) (ADC 12 CSTARTADD_2) (ADC 12 CSTARTADD_3) (ADC 12 CSTARTADD_4) (ADC 12 CSTARTADD_5) (ADC 12 CSTARTADD_6) (ADC 12 CSTARTADD_7) (ADC 12 CSTARTADD_8) (ADC 12 CSTARTADD_9) (ADC 12 CSTARTADD_10) (ADC 12 CSTARTADD_11) (ADC 12 CSTARTADD_12) (ADC 12 CSTARTADD_13) (ADC 12 CSTARTADD_14) (ADC 12 CSTARTADD_15) (ADC 12 CSTARTADD_16) { //! Is the signal that will trigger a sample-and-hold for an input signal //! to be converted. uint 16_t sample. Hold. Signal. Source. Select; //! Selects the clock that will be used by the ADC 12 B core, and the uint 8_t clock. Source. Select; //! Selects the amount that the clock will be divided. uint 16_t clock. Source. Divider; //! Selects the amount that the clock will be predivided. uint 16_t clock. Source. Predivider; //! Selects what internal channel to map for ADC input channels uint 16_t internal. Channel. Map; } ADC 12_B_init. Param; TI “macro” functions or prototypes extern void ADC 12_B_set. Adc. Power. Mode(uint 16_t base. Address, uint 8_t power. Mode);
ECE 3567 Microcontrollers Embedded C Programming – Custom Header files (. h)
Embedded C Programming - Examples
ECE 3567 Microcontrollers Embedded C Programming – Micro. Chip (PICC) /********** Microchip Inc. PICC Process Code *************/ bit Count. Enabled; #include <pic. h> bit Filling; #define XTAL_FREQ 4 MHZ bit TMRHIGH; #include "delay. h" //******************************** #include "delay. c" //main #define TIMEOUT 8 //-- Minutes to timeout //******************************** #define MAXTIME (TIMEOUT * 60 * 1000000)/256 //-- us divided by 256 void main (void) #define SECOND (1000000/256) { //-- counts per second #define OFF 1 //-- OFF State of output char Hour. Not. Up=0, Minute, Second; #define ON 0 //-- ON State of output OPTION=0 x. D 8; //- 11011000 Set up the option Register #define TIMEOUT_ON 1 //-- ON State for Timeout LED GPIO=0 x. FF; //-- All Outputs HIGH #define TIMEOUT_OFF 0 //-- OFF State for Timeout LED TIMEOUTPIN=TIMEOUT_OFF; //-- No Timeout yet so turn LED OFF OUTPUT=OFF; //-- Don't Fill so Output is OFF TRIS=0 x 0 F; //-- Set Up 0 to 3 as input rest as output #define TRUE 1 #define FALSE 0 #define BITNUM(adr, bit) static bit DEAD static bit EMPTY static bit FULL static bit OUTPUT ((unsigned)(&adr)*8+(bit)) @ BITNUM(GPIO, 0); //- Dangerously LOW @ BITNUM(GPIO, 1); //- Lower Limit @ BITNUM(GPIO, 2); //- Upper Limit @ BITNUM(GPIO, 4); //- Output Control Pin static bit TIMEOUTPIN @ BITNUM(GPIO, 5); //- Timeout Indicator. unsigned long Counter; Count. Enabled=0; Counter=0; Filling=0; TMRHIGH=0; while(1) {
ECE 3567 Microcontrollers Embedded C Programming – Motorola /********* ********* *****/ void Tilt_ctl(void) { int i; unsigned int counter; TMSK 1 &= 0 x. CF; counter = TCNT; TOC 3 = counter + 2000; TCTL 1 = (TCTL 1 & 0 x. CF) | 0 x 30; TOC 4 = counter + 2000; TCTL 1 = (TCTL 1 & 0 x. F 3) | 0 x 0 C; TFLG 1 = 0 x 30; while ((TFLG 1 & 0 x 20)==0){ i=i; } TFLG 1 = 0 x 30; TOC 3 = counter + 4000; TCTL 1 = (TCTL 1 & 0 x. CF) | 0 x 20; TOC 4 = counter + 14000; TFLG 1 = 0 x 30; while ((TFLG 1 & 0 x 10)==0){ i=i; } adc_read(60, 1); adc_read(61, 1); TFLG 1 = 0 x 30; TOC 3 = counter + 24000; TCTL 1 = (TCTL 1 & 0 x. CF) | 0 x 30; TOC 4 = counter + 24000; TCTL 1 = (TCTL 1 & 0 x. F 3) | 0 x 08; while ((TFLG 1 & 0 x 10)==0){ i=i; } TFLG 1 = 0 x 30; TOC 4 = counter + 44000; TCTL 1 = (TCTL 1 & 0 x. F 3) | 0 x 0 C; while ((TFLG 1 & 0 x 10)==0){ i=i; } /* /* /* /* /* /* /* disable timer 3, 4 interrupt */ get the current counter value */ set timer 3 to clock at +1 ms */ set timer 3 to go HIGH then */ set timer 4 to clock at +1 ms */ set timer 4 to go HIGH then */ clear clocks on timers 3, 4 */ and wait until timers 3 fires */ clear clocks on timers 3, 4 */ set timer 3 to clock at +2 ms */ set timer 3 to go LOW then */ set timer 4 to clock at +7 ms */ clear clocks on timers 3, 4 */ and wait until timers 4 fires */ read the left TILT meter side */ read the right TILT meter side */ clear clocks on timers 3, 4 */ set timer 3 to clock at +12 ms */ set timer 3 to go HIGH then */ set timer 4 to clock in +12 ms */ set timer 4 to go LOW then */ and wait until timers 4 fires */ clear clocks on timers 3, 4 */ set timer 4 to clock in +22 ms */ set timer 4 to go HIGH then */ and wait until timers 4 fires */ } /********* ********* *****/
ECE 3567 Microcontrollers Embedded C Programming – Texas Instruments /**** TI Code for MPS 430 Power Management Module ****/ // pmm. c - Driver for the pmm Module. //**************************** void PMM_unlock. LPM 5 (void) //! addtogroup pmm_api pmm { //**************************** //Direct register access to avoid compiler warning - #10420 -D #include "inc/hw_memmap. h" //For FRAM devices, at start up, the GPO power-on default #ifdef __MSP 430_HAS_PMM_FRAM__ //high-impedance mode needs to be disabled to activate previously #include "pmm. h" //configured port settings. This can be done by clearing the LOCKLPM 5 #include <assert. h> //bit in PM 5 CTL 0 register void PMM_enable. SVSH (void) PM 5 CTL 0 &= ~LOCKLPM 5; } { HWREG 8(PMM_BASE + OFS_PMMCTL 0_H) = PMMPW_H; HWREG 8(PMM_BASE + OFS_PMMCTL 0_L) |= SVSHE; HWREG 8(PMM_BASE + OFS_PMMCTL 0_H) = 0 x 00; }
- Slides: 49