Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 3567 Microcontroller Lab

Similar presentations


Presentation on theme: "ECE 3567 Microcontroller Lab"— Presentation transcript:

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

2 Module 1: The C Programming Language

3 Switch Statements

4 If-else Statements

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

6 For loops

7 Functions Called by Value

8 Functions Called by Reference

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

10 Function with Local Variables

11 Data Types (Processor Dependent)
MSP430 Series Microcontrollers

12 Data Types C Programming Language

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

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

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

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

17 #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__

18 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

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

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

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

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

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

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

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

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

27 Module 2: NUMBERS and REGISTERS

28 The Hexadecimal Number System.

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

30 Registers – Bit numbering
WE USE:

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

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

33 Registers 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: x04E0

34 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

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

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

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

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

39 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

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

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

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

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

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

45 Other Ways to Express the Bit Patterns
= 0x08 = BIT3 = 0xF7 = ~0x08 = ~BIT3 Because a #included header file msp430fr6989.h contains: This is HEX not Binary #define BIT (0x0001) #define BIT (0x0002) #define BIT (0x0004) #define BIT (0x0008) #define BIT (0x0010) #define BIT (0x0020) #define BIT (0x0040) #define BIT (0x0080) #define BIT (0x0100) #define BIT (0x0200) #define BITA (0x0400) #define BITB (0x0800) #define BITC (0x1000) #define BITD (0x2000) #define BITE (0x4000) #define BITF (0x8000)

46 TI Method of Setting BIT Fields – On Quiz
DEFINES THE FIRST BIT OF THE FIELD TO BE USED 2*0x1000u #define BIT (0x0001) #define BIT (0x0002) #define BIT (0x0004) #define BIT (0x0008) #define BIT (0x0010) #define BIT (0x0020) #define BIT (0x0040) #define BIT (0x0080) #define BIT (0x0100) #define BIT (0x0200) #define BIT (0x0400) #define BIT (0x0800) #define BIT (0x1000) #define BIT (0x2000) #define BIT (0x4000) #define BIT (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) 9*0x0020u: 7*0x2000u: 3*0x0040u:

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

48 Module 3: The MSP430FR6989 Microcontroller

49 The MSP430FR6989 Microcontroller - Overview

50

51 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

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

53 I/O Ports I/O Primary Secondary Tertiary

54 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 P Bit 6 of Port3

55 I/O Ports

56 I/O Ports

57 I/O Ports

58 I/O Ports

59 I/O Ports

60 I/O Ports

61 I/O Ports (and possibly P3 and P4)

62 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

63 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

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

65 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

66 CAPTURE Mode

67 COMPARE Mode

68 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

69 UP Mode

70 Continuous Mode

71 Up/Down Mode

72 UP Mode

73 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

74 OUTPUT Modes There EIGHT OUTPUT MODES:

75 Output Examples “Pulse Width Modulation”

76 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

77 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 KHz Two External Clock Sources LFXTCLK (Low Frequency XTALS) KHz HFXTC (High Frequency XTALS) 4 – 24 MHz Internal Clock Sources DCOCLK Digitally Controlled Oscillator MHZ (16MHz typ.) VLOCLK Very Low Power Clock KHz MODCLK Module Clock MHz

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

79 Module 4: MSP430FR6989 Project #1

80 MSP430FR6989 HARDWARE PUSHBUTTONS

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

82 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!

83 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

84 Project Set-up 5 0f 73

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

86 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

87 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

88 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.

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

90 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.

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

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

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

94 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.

95 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

96 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.

97 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.

98 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.

99 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

100 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

101 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

102 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

103 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

104 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

105 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

106 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

107 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

108 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 into a 4 character HEXADECIMAL VALUE

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

110 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

111 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

112 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

113 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

114 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 This also Resets the Timer A0 Counter (TA0R) Since the ACLK is Hz, this value is 2047/32768 SECONDS. The resulting ISR occurs every 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

115 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

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

117 MSP430FR6989 Project CONGRATULATIONS! You are done with Initialization

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

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

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

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

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

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

124 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

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

126 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.

127 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.

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

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

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

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

132 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

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

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

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

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

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

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

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

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

141 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

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

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

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

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

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

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

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

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

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

151 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

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


Download ppt "ECE 3567 Microcontroller Lab"

Similar presentations


Ads by Google