ECE 3567 Microcontroller Lab

Slides:



Advertisements
Similar presentations
The 8051 Microcontroller and Embedded Systems
Advertisements

UBI >> Contents Chapter 7 Timers Laboratories MSP430 Teaching Materials Texas Instruments Incorporated University of Beira Interior (PT) Pedro Dinis Gaspar,
Interrupts, Low Power Modes and Timer A (Chapters 6 & 8)
Chung-Ta King National Tsing Hua University
Lab7: Introduction to Arduino
Chung-Ta King National Tsing Hua University
Chung-Ta King National Tsing Hua University
ECE 447 Fall 2009 Lecture 9: TI MSP430 Interrupts & Low Power Modes.
Butterfly I/O Ports CS-212 Dick Steflik. I/O for our labs To get data into and out of our Butterfly its a little trickier than using printf and scanf.
Railway Foundation Electronic, Electrical and Processor Engineering.
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
1 ECE 263 Embedded System Design Lessons 2, 3 68HC12 Hardware Overview, Subsystems, and memory System.
Railway Foundation Electronic, Electrical and Processor Engineering.
Chung-Ta King National Tsing Hua University
LAB 7: WDT+ and Low-Power Optimization
T IMERS - 2. O UTPUT U NIT Each capture/compare block contains an output unit. The output unit is used to generate output signals such as PWM signals.
Development. Development Environment Editor Assembler or compiler Embedded emulator/debugger IAR Embedded Workbench Kickstart Code Composer Essentials.
A Simple Tour of the MSP430. Light LEDs in C LEDs can be connected in two standard ways. Active high circuit, the LED illuminates if the pin is driven.
System Clocks.
MSP430 Mixed Signal Microcontroller – Parte 2 Afonso Ferreira Miguel Source: slau056d – Texas instruments.
1 ARM University Program Copyright © ARM Ltd 2013 General Purpose I/O.
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
Lecture 11: TI MSP430 Timers Compare Modes
LAB 8: Program Design Pattern and Software Architecture
Lecturers: Professor John Devlin Mr Robert Ross
IO Subsystem IV Ports and peripherals. IO Subsystem (1) All devices connected to the system buses, other than memory and CPU – Input and output ports.
Network and Systems Laboratory nslab.ee.ntu.edu.tw.
Chapter 5 - Interrupts.
Lecture 4 General-Purpose Input/Output NCHUEE 720A Lab Prof. Jichiang Tsai.
ECE 382 Lesson 32 Lesson Outline Lab 6 Introduction Pulse Width Modulation Capture / Compare Example Lab 6 Tips Admin Lab#6 “prelab” due BOC lesson 33.
INTRODUCTION TO ROBOTICS Part 5: Programming
Introduction Why low power?
CS4101 嵌入式系統概論 General Purpose IO
ECE 3430 – Intro to Microcomputer Systems
Lecture 8: TI MSP430 Interrupts, ISRs
CS4101 嵌入式系統概論 Interrupts Prof. Chung-Ta King
Lesson Outline Interrupts Admin Assignment #9 due next lesson
PWM and DC Motor Control
CS4101 Introduction to Embedded Systems Lab 6: Low-Power Optimization
Homework Reading Machine Projects Labs
Timer and Interrupts.
Microcontroller Applications
Chapter 6 General Purpose Input/Output
CS4101 Introduction to Embedded Systems Lab 1: General Purpose IO
Lesson Outline Peripherals Memory-Mapped IO Ports GPIO Multiplexing
Prof. Chung-Ta King Department of Computer Science
CS4101 嵌入式系統概論 General Purpose IO
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Blinking an LED Using MSP430ware to Control GPIO
ECE 3430 – Intro to Microcomputer Systems
CS4101 Introduction to Embedded Systems Lab 4: Interrupt
CENG2400 Revision Q1a A system has an ARM processor with a 32-bit General Purpose Input Output (GPIO) module. Two on/off switches are connected to bit-3.
A Play Core Timer Interrupts
Interrupts in C Programs
ECE 3567 Microcontrollers Dr. Gregg J Chapman
ECE 3567 Microcontroller Lab
Lecture 9: TI MSP430 Interrupts & Low Power Modes
Microcontroller Labs Lab 1 – The LED Test Mode Dr. Gregg Chapman
A Play Lab. 2 Task 8 Core Timer Interrupts
8253 – PROGRAMMABLE INTERVAL TIMER (PIT). What is a Timer? Timer is a specialized type of device that is used to measure timing intervals. Timers can.
ECE 3430 – Intro to Microcomputer Systems
CS4101 Introduction to Embedded Systems Lab 2: Basic IO and Timer
Prof. Chung-Ta King Department of Computer Science
MSP430 Clock System and Timer
ECE 3567 Microcontrollers Syllabus – Autumn 2019
ECE 3567 Microcontroller Lab
ECE 3567 Microcontrollers Lab
ECE 3567 Microcontrollers Lab
ECE 3567 Microcontrollers Lab
Presentation transcript:

ECE 3567 Microcontroller Lab Laboratory Background and Project #1 Autumn 2018 Dr. Gregg Chapman

Module 1: The C Programming Language

Switch Statements

If-else Statements

While loops int main() { Initialize(); while (1) Do_Stuff(); Do_More_Stuff(); } return 0;

For loops

Functions Called by Value

Functions Called by Reference

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

Function with Local Variables

Data Types (Processor Dependent) MSP430 Series Microcontrollers

Data Types C Programming Language

Compiler Directives #include #define #pragma #ifdef #ifndef #typedef

#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”

#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

#pragma #pragma – Tells the compiler to include something once if it is included in multiple files.

#ifdef / #ifndef #ifdef – Conditional compilation based on a definition #endif #ifndef – If this hasn’t already been defined #define __MSP_HAS_AES256__ #ifdef __MSP430_HAS_AES256__ conditional code goes here #endif // __MSP430_HAS_AES256__

Data Types #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

Variable Declaration Modifiers const volatile extern static extern volatile unsigned int temp;

volatile The variable scope is available to all functions and all files in the project The variable can be changed anywhere in the project

const The variable will have a single value at runtime and cannot be changed in the program

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; }

extern The variable can be used in the current file, but is declared in another file

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;

Constant Declarations There are “constant” variables: const int x = 0x1234; Constant numbers are usually defined with a the compiler directive: #define RED 0x11

Global Variables Global variables are defined BEFORE a function and may be volatile, static, extern, etc.

Module 2: NUMBERS and REGISTERS

The Hexadecimal Number System.

Registers Each 4-bit nibble can be converted to a single Hexadecimal character. Register contents are almost always expressed in hexadecimal values.

Registers – Bit numbering WE USE: 7 6 5 4 3 2 1 0

Hexadecimal Values of Bit Positions 1 0x08 0x04 0x02 0x01 0x10 0x20 0x40 0x80

Hexadecimal Values of Bit Positions 1 0x0F 0xC7 0x03 0xC0 0xF0 0x00 0x55 0xAA 0xFF

Registers 0000 0100 1110 0000 0xE , or Eh 0x0 , or 0h 0x0 , or 0h 0x4 , or 4h Each 4-bit nibble can be converted to a single Hexadecimal character. The register value shown is: 0x04E0

HOW TO SET A BIT IN A REGISTER In C programming, the | character is a BITWISE OR NOTE THAT: 0 | 0 = 0 0 | 1 = 1 1 | 0 = 1 1 | 1 = 1 By BITWISE ORing a register with 1s in the bits you want to SET, and 0s in the bits that you want to preserve, YOU ONLY SET THE BITS AT LOCATIONS THAT THERE ARE A ONE

HOW TO SET A BIT IN A REGISTER EXAMPLE 1: RESIGTER is: 0000 0000 SET Bit 4 REGISTER |= 0x10; 0x10 = 0001 0000 (0000 0000) | (0001 0000) = (0001 0000)

HOW TO SET A BIT IN A REGISTER EXAMPLE 2: RESIGTER is: 1010 1010 SET Bit 2 REGISTER |= 0x04; 0x04 = 0000 0100 (1010 1010) | (0000 0100) = (1010 1110)

HOW TO SET A BIT IN A REGISTER EXAMPLE 3: RESIGTER is: ???? ???? SET Bit 7 REGISTER |= 0x80; 0x80 = 1000 0000 (???? ????) | (1000 0000) = (1??? ????) You have insured that Bit 7 is SET

HOW TO SET A BIT IN A REGISTER EXAMPLE 4: RESIGTER is: 1111 0000 SET Bits 0 and 1 REGISTER |= 0x03; 0x03 = 0000 0011 (1111 0000) | (0000 0011) = (1111 0011) You have SET bits 0 and 1.

HOW TO CLEAR A BIT IN A REGISTER In C programming, the & character is a BITWISE AND NOTE THAT: 0 & 1 = 0 1 & 1 = 1 By BITWISE ANDing a register with 1s in the bits you want to preserve, and 0s in the bits that you want to clear, YOU ONLY CLEAR THE BITS AT LOCATIONS THAT THERE ARE A ZERO

HOW TO CLEAR A BIT IN A REGISTER EXAMPLE 1: RESIGTER is: 1111 1111 Clear Bit 4 REGISTER &= 0xEF; 0xEF = 1110 1111 (1111 1111) & (1110 1111) = (1110 1111)

HOW TO CLEAR A BIT IN A REGISTER EXAMPLE 2: RESIGTER is: 1010 1010 Clear Bit 3 REGISTER &= 0xF7; 0xF7 = 1111 0111 (1010 1010) & (1111 0111) = (1010 0010)

HOW TO CLEAR A BIT IN A REGISTER EXAMPLE 3: RESIGTER is: 0000 0000 Clear Bit 0 REGISTER &= 0xFE; 0xFE = 1111 1110 (0000 0000) & (1111 1110) = (0000 0000) You have insured that Bit 0 is cleared

HOW TO CLEAR A BIT IN A REGISTER EXAMPLE 4: RESIGTER is: ???? ???? Clear Bit 7 REGISTER &= 0x7F; 0x7F = 0111 1111 (???? ????) & (0111 1111) = (0??? ????) You have insured that bit 7 is cleared

HOW TO CLEAR A BIT IN A REGISTER EXAMPLE 5: RESIGTER is: 1111 0000 Clear Bits 6 and 5 REGISTER &= 0x9F; 0x9F = 1001 1111 (1111 0000) & (1001 1111) = (1001 0000) You have cleared bits 6 and 7

Other Ways to Express the Bit Patterns 0000 1000 = 0x08 = BIT3 1111 0111 = 0xF7 = ~0x08 = ~BIT3 Because a #included header file msp430fr6989.h contains: This is HEX not Binary #define BIT0 (0x0001) #define BIT1 (0x0002) #define BIT2 (0x0004) #define BIT3 (0x0008) #define BIT4 (0x0010) #define BIT5 (0x0020) #define BIT6 (0x0040) #define BIT7 (0x0080) #define BIT8 (0x0100) #define BIT9 (0x0200) #define BITA (0x0400) #define BITB (0x0800) #define BITC (0x1000) #define BITD (0x2000) #define BITE (0x4000) #define BITF (0x8000)

TI Method of Setting BIT Fields – On Quiz DEFINES THE FIRST BIT OF THE FIELD TO BE USED 2*0x1000u #define BIT0 (0x0001) #define BIT1 (0x0002) #define BIT2 (0x0004) #define BIT3 (0x0008) #define BIT4 (0x0010) #define BIT5 (0x0020) #define BIT6 (0x0040) #define BIT7 (0x0080) #define BIT8 (0x0100) #define BIT9 (0x0200) #define BIT10 (0x0400) #define BIT11 (0x0800) #define BIT12 (0x1000) #define BIT13 (0x2000) #define BIT14 (0x4000) #define BIT15 (0x8000) STEPS: 1. Convert this number to BINARY 2. Place the number in the register with the Least Significant Bit defined by this value (See Table) 0010 0000 0000 0000 9*0x0020u: 0000 0001 0010 0000 7*0x2000u: 1110 0000 0000 0000 3*0x0040u: 0000 0000 1100 0000

HOW TO CLEAR A BIT IN A REGISTER EXAMPLE 6: RESIGTER is: 1111 0000 Clear Bits 6 and 5 REGISTER &= ~(BIT6 + BIT5); (BIT6 & BIT5) = (0100 0000) + (0010 0000) = 0110 0000 ~(BIT6 and BIT5) = 1001 1111 (1111 0000) & (1001 1111) = (1001 0000) You have cleared bits 6 and 7

Module 3: The MSP430FR6989 Microcontroller

The MSP430FR6989 Microcontroller - Overview

I/O Port Registers Input Registers (PxIN) Output Registers (PxOUT) Direction Registers (PxDIR) Pullup or Pulldown Resistor Enable Registers (PxREN) Function Select Registers (PxSEL0, PxSEL1) Interrupt Settings (PxIFG, PxIES, PxIE) NOTE: x – substitute the number of the PORT

I/O Ports – Multi-purpose Pins Primary Secondary Tertiary

I/O Ports I/O Primary Secondary Tertiary

I/O Ports Port Number and Bit Number TI Conventions This is the number of the BIT in the PORT This is the number of the PORT P3.6 - Bit 6 of Port3

I/O Ports

I/O Ports

I/O Ports

I/O Ports

I/O Ports

I/O Ports

I/O Ports (and possibly P3 and P4)

I/O Ports – Putting It All Together Suppose that you wanted to configure BIT 2 of PORT 2 as a Timer B0.4 output for a PWM application Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 X OUT 1 P2SEL0 P2SEL1 P2DIR P2OUT P2REN

Control Registers Every MODULE in a Microcontroller has one or more CONTROL REGISTERS to configure the functions of the module. Each CONTROL REGISTER is divided into FIELDS Each FIELD sets one PARAMETER of the MODULE to a specific function The number of options for the PARAMETER determines the number of BITS in the FIELD Each BIT has a Power-up DEFAULT value

The Timers There are FIVE 16-bit Timer Modules in the MSP430FR6989 Microcontroller TB0 Has 7 Capture/Compare Registers TA0 Has 3 Capture/Compare Registers TA1 TA2 Has 2 Capture/Compare Registers (internal only) TA3 Has 5 Capture/Compare Registers (internal only)

Timer Hierarchy CAPTURE or COMPARE Modes Capture Compare TIMER Counting Modes Stop Up Continuous Up/Down OUTPUT Modes Output Set Toggle/Reset Set/Reset Toggle Reset Toggle/Set Reset/Set Pulse Width Modulation

CAPTURE Mode

COMPARE Mode

TIMER Modes There FOUR Timer MODES: This describes what the COUNTER is doing (TAxR or TB0R) STOP Mode UP Mode Continuous Mode UP/DOWN Mode

UP Mode

Continuous Mode

Up/Down Mode

UP Mode

OUTPUT Modes There EIGHT OUTPUT MODES: This describes what the OUTPUT SIGNAL is doing (OUT1 – OUT6 on TBO e.g.) Compare Registers are used to SET or CLEAR the OUTPUT signal

OUTPUT Modes There EIGHT OUTPUT MODES:

Output Examples “Pulse Width Modulation”

Interrupt Service Routines Require a #pragma vector= NAME_OF_VECTOR There is USUALLY an unused_interrupt file with all of the #pragmas for all possible interrupts. The vector you wish to use must be commented out in the unused_interrupt source file. ISR must be preceded by the reserved for implementation name of : __interrupt

Clock Modules Four Internal Clocks (Can be linked to CLK sources and adjusted) Typical Values MCLK Master Clocks SMCLK Subsystem Master Clock MODCLK Module Clock ACLK Auxiliary Clock 32.768 KHz Two External Clock Sources LFXTCLK (Low Frequency XTALS) 32.768 KHz HFXTC (High Frequency XTALS) 4 – 24 MHz Internal Clock Sources DCOCLK Digitally Controlled Oscillator 2.7-24 MHZ (16MHz typ.) VLOCLK Very Low Power Clock 10 KHz MODCLK Module Clock 5 MHz

Analog to Digital Converter 32 single ended or 16 Differential Channels 12-bit Resolution 200 Kilo-Samples per Second Sample and Hold circuitry Internal Programmable Voltage Reference (1.2V, 2.0V, 2.5V) Internal Conversions on Internal Sensors (E.G. Temperature Sensor) Selectable Clocks Single, Repeated Single, Auto-scan Sequence, and Repeated Sequence Modes Separate Interrupt Vectors Separate Memory Buffers Input Voltage Comparator Windows for low signal monitoring (MUCH more on the ADC12 Set-up at a later date)

Module 4: MSP430FR6989 Project #1

MSP430FR6989 HARDWARE PUSHBUTTONS

MSP430FR6989 HARDWARE LEDS 2 0f 73 P1.0 P9.7

MSP430FR6989 Project You will create a simple microcontroller project Follow the Steps in the next slide to create a project DON’T SKIP ANY STEPS!

Project Set-up 4 0f 73 Open Code Composer Studio version 7.2.0 Select a Workspace on your U: drive Select File  New  CCS Project In the CCS Project window set Target to MSP430FRxxx and select MSP430FR6989 In the Project Templates and Examples window, scroll down to MPS430 DriverLib and select the Empty Project with DriverLib Source beneath that level. (See next slide.) Enter the Project Name and click Finish Select File  New  Source File Enter Code Save the File Select Project  Rebuild Project At this point it is essential to connect the hardware Make sure that the Project is selected as [Active – Debug] Select the Debug ICON Once the GREEN ARROW comes up you can run the code Halt execution with the RED SQUARE

Project Set-up 5 0f 73

MSP430FR6989 Project 6 0f 73 The project software shall run an Interrupt Service Routine whenever the S1 pushbutton is pressed

MSP430FR6989 Project 7 0f 73 The Interrupt Service Routine for the Pushbutton will switch the flashing LED from the RED LED (LED1) to the GREEN LED (LED2) whenever it executes

MSP430FR6989 Project 8 0f 73 You shall configure Timer A0 to generate another Interrupt every 62.5 milliseconds. To do this, you must configure the following registers: TA0CTL – Timer A0 Control Register TA0CCTL0 – Compare 0 Control Register TA0CCTL1 – Compare 1 Control Register You must also write compare values to the following registers TA0CCR0 – Compare 0 Register TA0CCR1 – Compare 1 Register

MSP430FR6989 Project 9 0f 73 The Timer A0 Interrupt Service Routine shall toggle the state of the ACTIVE LED (either LED1 or LED2) every 500 milliseconds.

MSP430FR6989 Project 1. Include: driverlib.h stdio.h string.h Can be <driverlib.h> or “driverlib.h”

MSP430FR6989 Project 2. Add the following function prototype: void delay_cycles(unsigned int); This will be used to debounce the pushbutton presses which will eliminate multiple interrupts from one press.

MSP430FR6989 Project 3. Create the main function: void main (void) { }

MSP430FR6989 Project 13 0f 73 4. Stop the Watchdog Timer using the following TI macro (inside the main function). WDT_A_hold(__MSP430_BASEADDRESS_WDT_A__);

MSP430FR6989 Project 14 0f 73 5. Disable the power-on high impedance mode to release all pins for configuration (do this inside main) using the following TI macro: PMM_unlockLPM5();

MSP430FR6989 Project LED Initialization 6. Set Bit 0 of Port 1 as an OUTPUT You will use the P1DIR register to do this. This pin is connected to the RED LED.

MSP430FR6989 Project LED Initialization 7. Set Bit 7 of Port 9 as an OUTPUT You will use the P9DIR register to do this. This pin is connected to the GREEN LED

MSP430FR6989 Project LED Initialization 8. Turn OFF the RED LED You will use the P1OUT register to do this. The RED LED is on Bit 0.

MSP430FR6989 Project LED Initialization 9. Turn ON the GREEN LED You will use the P9OUT register to do this. The GREEN LED is on Bit 7.

MSP430FR6989 Project Pushbutton Initialization 10. CLEAR Bit 1 of PORT 1 as an INPUT You will use P1DIR register to do this. Use &= to CLEAR the Bit 1.

MSP430FR6989 Project Pushbutton Initialization 11. Enable the Pull-up Resistor on P1.1 by setting Bit 1 in P1REN register You will use P1REN register to do this Remember that P1.1 means Bit 1 on Port 1 Remember that |= can be used to SET a bit

Pushbutton Initialization MSP430FR6989 Project Pushbutton Initialization 21 0f 73 12. Set Bit 1 in P1OUT to complete the Pull-up Resistor configuration for P1.1 You will use the P1OUT register to do this Remember that P1.1 means Bit 1 on Port 1 Remember that |= can be used to SET a bit

Pushbutton Initialization MSP430FR6989 Project Pushbutton Initialization 22 0f 73 13. Configure the PORT 1 Interrupt for Bit 1 to trigger on a high-to-low transition by setting BIT 1 in the P1IES register. You will use the P1IES register to do this Remember that |= can be used to SET a bit

Pushbutton Initialization MSP430FR6989 Project Pushbutton Initialization 23 0f 73 14. Clear any pending interrupts on the PORT1 Bit 1 Interrupt Flag You will use the P1IFG register to do this Clear BIT 1 to clear any pending interrupt. Remember that &= can be used to CLEAR a bit

Pushbutton Initialization MSP430FR6989 Project Pushbutton Initialization 24 0f 73 15.ENABLE the PORT 1 Interrupt for Bit 1 by setting BIT 1 in the P1IE register. You will use the P1IE register to do this Remember that |= can be used to SET a bit

MSP430FR6989 Project Timer A0 Initialization 16. You will configure the TA0 Control Register (TA0CTL) to set up the Timer A0 to generate an interrupt every 62.5 milliseconds. Calculate the HEXADECIMAL value for the entire register and write it with one command TO FIND OUT HOW TO DO SO GO TO THE NEXT SLIDE(S) Please refer to the next slides before writing the final command

MSP430FR6989 Project Timer A0 Initialization – Clock Source 17. Find the TASSEL field of 2 bits. Select the correct bits to use the ACLK. These bits will go in the TASSEL 2 bit field Please refer to the next slide before writing the final command

MSP430FR6989 Project Timer A0 Initialization- Clock Divider 18. Find the ID field of 2 bits. Select the correct bits to divide the ACLK by 1 These bits will go in the ID 2 bit field Please refer to the next slide before writing the final command

MSP430FR6989 Project Timer A0 Initialization – Timer Mode 19. Find the MC field of 2 bits. Select the correct bits to Put the Timer A0 in UP MODE These bits will go in the MC 2 bit field Please refer to the next slide before writing the final command

MSP430FR6989 Project Timer A0 Initialization – Other Settings 20. Let Bits 2, 1, and 0 remain 0 for now Assume Bits 15, 14, 13, 12, 11, and 10, and 3 are 0s Convert the 16 bit BINARY sequence of numbers that you derived in slides 22-26 into a 4 character HEXADECIMAL VALUE

MSP430FR6989 Project Timer A0 Initialization- Initialize the Register 21. Set the TA0CTL to the HEXADECIMAL value that you derived in Slides 22-26. Write the value to the TA0CTL register with a single instruction. DO NOT USE BINARY NUMBERS TO CONFIGURE THE REGISTERS

MSP430FR6989 Project Timer A0 Initialization - NOTE: Interrupts are enabled in the TA0CCTL0 Register, not the TA0CCTL1 Register 22. Enable the COMPARATOR 0 Interrupt for Timer A0 by SETTING the CCIE BIT in the TA0CCTL0 Control Register

MSP430FR6989 Project Timer A0 Initialization 23. Next Step. Configure the TA0 Compare Control Register for COMPARATOR NUMBER 1 (TA0CCTL1) Please refer to the next slide before writing the final command

MSP430FR6989 Project Timer A0 Initialization – Output Mode 24. Find the TA0CCTL1, 3 bit field for OUTMOD Select the Bits for RESET/SET Assume all other Bits are 0s

MSP430FR6989 Project Timer A0 Initialization – Initialize the Register 25. Set the TA0CCTL1 Register Value to the HEXADECIMAL value that you derived in Slide 30. Write the value to the TA0CCTL1 register with a single instruction. DO NOT USE BINARY NUMBERS TO CONFIGURE THE REGISTERS

MSP430FR6989 Project Timer A0 Initialization – Timer Period 26. Set the TA0CCR0 Register to 0x07FF (Comparator for the Period) This COMPARATOR 0 value sets the OUT1 signal when the Timer A0 counts to 2047. This also Resets the Timer A0 Counter (TA0R) Since the ACLK is 32768 Hz, this value is 2047/32768 SECONDS. The resulting ISR occurs every 62.469 milliseconds ( 1/16th second) The OUT1 Signal used to generate an interrupt has a frequency of 16 Hertz DO NOT USE BINARY NUMBERS TO CONFIGURE THE REGISTERS

MSP430FR6989 Project Timer A0 Initialization- Timer Duty Cycle 27. Set the TA0CCR1 Register to 0x03FF This COMPARATOR 1 value clears the OUT0 signal at a count of 1023 This gives the OUT1 signal a 50% Duty Cycle DO NOT USE BINARY NUMBERS TO CONFIGURE THE REGISTERS

MSP430FR6989 Project Timer A0 Initialization Enable All Interrupts 28. Use the TI macro to enable all configured interrupts simultaneously: __enable_interrupt();

MSP430FR6989 Project CONGRATULATIONS! You are done with Initialization

MSP430FR6989 Project Timer A0 ISR Write the Timer A0 interrupt Service Routine This occurs at 16 Hz or every 62.6 milliseconds

MSP430FR6989 Project Timer A0 ISR Add the following variables BEFORE the main() function: volatile unsigned char LED_Flag; volatile unsigned int ISR_Counter; volatile unsigned char ISR_Flag = 0;

MSP430FR6989 Project Timer A0 ISR Add unused_interrupts.c to your project DOWNLOAD IT FROM THE PROJECT 1 LAB on the ECE 3567 website.

MSP430FR6989 Project Timer A0 ISR In unused_interrupts.c comment out the #pragma vector = TIMER0_A0_VECTOR

MSP430FR6989 Project Timer A0 ISR Back in main.c, AFTER the main() function, Create the Timer A0 Interrupt Service Routine: USE THE FOLLOWING FORMAT: #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A0(void) { return; }

MSP430FR6989 Project Timer A0 ISR Inside the Timer_A0 ISR: 1. Set the ISR_Flag = 1; 2. Increment the ISR_Counter++;

MSP430FR6989 Project Timer A0 ISR Inside the Timer_A0 ISR: If the ISR_Counter is greater than or equal to 8: Compliment Bit 0 of P1OUT IF the LED_Flag equals 1 Use a new operator to COMPLIMENT BIT 0 P1OUT ^= BIT0; (NOTE: This is the Bitwise Exclusive OR operator: A ZERO retains the bit value. A ONE INVERTS the VALUE. Pretty Cool (not to mention useful). a b XOR 0 0 0 0 1 1 1 0 1 1 1 0

MSP430FR6989 Project Timer A0 ISR Inside the Timer_A0 ISR: If the ISR_Counter is greater than or equal to 8: Compliment Bit 7 of P9OUT IF the LED_Flag is NOT equal to 1 (use an else)

MSP430FR6989 Project Timer A0 ISR Inside the Timer_A0 ISR: Reset the ISR_Counter to 0 LAST THING INSIDE THE IF statement for ISR_Counter >= 8 before the } for the end of the if.

MSP430FR6989 Project 48 0f 73 NOTE: Since the ISR_Counter must count to 8 before any changes take place, The lights are toggled every 8/16 or ½ SECOND. This means that the LEDs flash at 1 Hertz.

MSP430FR6989 Project Pushbutton ISR Write the Port 1 interrupt Service Routine This runs whenever the S1 Pushbutton is pressed

MSP430FR6989 Project Pushbutton ISR Add the PB_Flag variable BEFORE main(): volatile unsigned int PB_Flag = 0;

MSP430FR6989 Project Pushbutton ISR In unused_interrupts.c comment out the #pragma vector = PORT1_VECTOR

MSP430FR6989 Project Pushbutton ISR Back in main.c, AFTER the main() function, Create the Port 1 Interrupt Service Routine: USE THE FOLLOWING FORMAT: #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { return; }

MSP430FR6989 Project Pushbutton ISR Inside the Port_1 ISR: (BUT BEFORE THE return;) Disable the interrupt from PORT 1 by CLEARING BIT 1 in the P1IE register

MSP430FR6989 Project Pushbutton ISR Inside the Port_1 ISR: (BUT BEFORE THE return;) SET the PB_Flag

MSP430FR6989 Project Pushbutton Debounce Add the volatile unsigned int BEFORE main() called delay and initialize it to 2000: volatile unsigned int delay = 2000;

MSP430FR6989 Project Pushbutton Debounce Create a function to delay a fixed amount of time called delay_cycles. do this in main.c Begin by adding the FUNCTION PROTOTYPE BEFORE the main() void delay_cycles ( unsigned int);

MSP430FR6989 Project Pushbutton Debounce Create a function to delay a fixed amount of time called delay_cycles. do this in main.c file BUT NOT IN THE MAIN FUNCTION. Write the function: void delay_cycles(unsigned int x) { unsigned int a; a = x; while (a >= 1) a--; } return;

MSP430FR6989 Project The Main Loop Program the infinite WHILE LOOP in main() It should follow the initializations: while(1) { }

MSP430FR6989 Project Main Loop - PB handler Inside the while(1) loop, test for the PB_Flag using an if statement if(PB_Flag == 1) { }

MSP430FR6989 Project Main Loop - PB handler Inside the PB_Flag IF: 1. Turn OFF both LEDS

MSP430FR6989 Project Main Loop - PB handler Inside the PB_Flag IF: 2. Call the debounce delay function delay_cycles(delay);

MSP430FR6989 Project Main Loop - PB handler Inside the PB_Flag IF: 3. Follow the delay with a re-reading of the pushbutton and test to see if it is still pressed: if ((P1IN & BIT1) == 0) { } This is a NESTED if

MSP430FR6989 Project Main Loop - PB handler Inside the NESTED IF just created: 4. Compliment the LED_Flag using the ! operator LED_Flag = !LED_Flag;

MSP430FR6989 Project Main Loop - PB handler Inside the NESTED IF just created: 5. Clear the PB_Flag

MSP430FR6989 Project Main Loop - PB handler AFTER the NESTED IF: WAIT until the pushbutton is released: while((P1IN & BIT1) == 0);

MSP430FR6989 Project Main Loop - PB handler AFTER the pushbutton release call the debounce delay again: delay_cycles(delay);

MSP430FR6989 Project Main Loop - PB handler Clear the Pending Interrupts for the PORT 1 ISR: Use &= to CLEAR BIT 1 in the P1IFG register P1IFG &= ~BIT1;

MSP430FR6989 Project Main Loop - PB handler Re-enable the BIT 1 Interrupt for the PORT 1 ISR: Use |= to SET BIT 1 in the P1IE register P1IE |= BIT1;

MSP430FR6989 Project Main Loop - PB handler This is the end of the if(PB_Flag) statement

MSP430FR6989 Project Main Loop – Timer A0 Handler Add the if(ISR_Flag) to process the Timer A0 function IN THE MAIN LOOP BUT AFTER the end of if(PB_Flag==1) section: if(ISR_Flag ==1) { ISR_Flag = 0; }

MSP430FR6989 Project Main Loop This is the end of the main() function

MSP430FR6989 Project Compile and Program Project #1 Demonstrate the Project to the Lab Monitor One LED should flash at a time. The GREEN LED should be the default after initialization The LED should flash at 1 Hz The Pushbutton should switch between the 2 LEDs The Pushbutton should be free of glitches from button bounce. Include your COMMENTED code in the Progress Report

This is the end of Project #1 MSP430FR6989 Project 73 0f 73 This is the end of Project #1