Understanding Microcontrollers and Embedded C Programming

ece 3567 microcontrollers lab ece 3567 n.w
1 / 69
Embed
Share

Explore the world of microcontrollers and embedded C programming in this detailed lecture, covering topics such as hexadecimal number systems, register bit numbering, variable declaration modifiers, and more. Get insights into data types in the C programming language and discover how they are processor-dependent on MSP430 series microcontrollers.

  • Microcontrollers
  • Embedded Programming
  • Hexadecimal
  • Register Bit
  • Data Types

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. ECE 3567 Microcontrollers Lab ECE 3567 Microcontrollers Lab Lecture #2 The Microcontroller & Embedded C Programming Dr. Gregg Chapman Spring 2025 1

  2. Numbering

  3. The Hexadecimal Number System.

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

  5. Hexadecimal Values of Bit Positions Don t get Bit by Bit Numbering Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0x01 0 0 0 0 0 0 0 1 0x02 0 0 0 0 0 0 1 0 0x04 0 0 0 0 0 1 0 0 0x08 0 0 0 0 1 0 0 0 0x10 0 0 0 1 0 0 0 0 0x20 0 0 1 0 0 0 0 0 0x40 0 1 0 0 0 0 0 0 0x80 1 0 0 0 0 0 0 0

  6. Hexadecimal Values of Bit Positions Examples Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0 0xC0 1 1 0 0 0 0 0 0 0x03 0 0 0 0 0 0 1 1 0xC7 1 1 0 0 0 1 1 1 0x0F 0 0 0 0 1 1 1 1 0xF0 1 1 1 1 0 0 0 0 0x00 0 0 0 0 0 0 0 0 0x55 0 1 0 1 0 1 0 1 0xAA 1 0 1 0 1 0 1 0 0xFF 1 1 1 1 1 1 1 1

  7. Registers 0000 0100 1110 0000 0x0 0xE 0x0 0x4 Example of 4-bit binary values converted to a single Hexadecimal character. This 16-bit register value would be shown as: 0x04E0

  8. Register Terminology Note that each 4-bit nibble can be converted to a single Hexadecimal character. Register contents are always expressed in hexadecimal values.

  9. Data Types C Programming Language ! ! ! ! X X X X X

  10. Data Types Processor Dependent!! MSP430 Series Microcontrollers ! !

  11. Variable Declaration Modifiers const single value at runtime and cannot be altered volatile value can be altered anywhere in code (global) extern declaration is in another file, but can be used in current file static value can be altered only within the current file Examples: volatile unsigned int delay; extern volatile unsigned char i, j, k; static int counter; const int x = 123;

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

  13. C Operations (for reference)

  14. C Operations

  15. C Operations

  16. C Operations MOST Important Operators in Embedded C Programming ! Used to CLEAR Bits Used to TOGGLE Bits Used to SET Bits Used to INVERT ALL Bits

  17. 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 WHERE THERE ARE A ONES IN THE BYTE USED TO OPERATE ON THE REGISTER

  18. 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) You have SET Bit 4

  19. 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) You have SET Bit 2

  20. 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 regardless of the other bit values.

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

  22. 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 WHERE THERE ARE A ZEROS IN THE BYTE USED TO OPERATE ON THE REGISTER

  23. HOW TO CLEAR A BIT IN A REGISTER EXAMPLE 1: RESIGTER is: 1111 1111 Clear Bit 4 REGISTER &= 0xEF; SAME AS REGISTER &= ~0x10 0xEF = 1110 1111 (1111 1111) & (1110 1111) = (1110 1111) You have CLEARED Bit 4

  24. HOW TO CLEAR A BIT IN A REGISTER EXAMPLE 2: RESIGTER is: 1010 1010 Clear Bit 3 REGISTER &= 0xF7; SAME AS REGISTER &= ~0x08 0xF7 = 1111 0111 (1010 1010) & (1111 0111) = (1010 0010) You have CLEARED Bit 3

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

  26. 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, regardless of the other bit values

  27. 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 5

  28. Other Ways to Express the Bit Patterns 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) 0000 1000 = 0x08 = BIT3 1111 0111 = 0xF7 = ~0x08 = ~BIT3 NOTE: msp430fr6989.h is #included in driverlib.h

  29. HOW TO CLEAR A BIT IN A REGISTER EXAMPLE 6: Alternate Notation: 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 5

  30. TI Method of Setting BIT Fields of Different Sizes 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 of the field defined by this value (See Table) EXAMPLES: 0010 0000 0000 0000 9*0x0020u: 0000 0001 0010 0000 7*0x2000u: 1110 0000 0000 0000 3*0x0040u: 0000 0000 1100 0000

  31. Toggling Bits Compliment Bit 0 of register P1OUT P1OUT ^= BIT0; // P1OUT XOR 0x0001 inverts BIT0 only (NOTE: This is the Bitwise Exclusive OR operator: A ZERO retains the bit value of all the positions that are zero. A ONE INVERTS the positions that are 1. Since BIT0 is #defined as 0x0001 it is the only bit complimented. Pretty Cool (not to mention useful). a b XOR 0 0 0 0 1 1 1 0 1 1 1 0 31

  32. Programming

  33. Compiler Directives (from Lecture #1) #include append contents of the external file #define text substitution for a constant, can assign a numeric value #pragma directs compiler to ignore multiple declarations of the same entity #ifdef begin conditional compilation, paired with #endif #ifndef define only if it hasn t already been defined #typedef defines an alias name or new type

  34. 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); NOTE: I put all function prototypes in a separate header file called 3567.h

  35. Functions Called by Value

  36. Functions Called by Reference 2

  37. Local Variables Declared inside functions Are not preserved when the execution returns form the function unless returned. void delay_cycles(unsigned int x) { unsigned int a; a = x; while (a >= 0) { a--; } return; }

  38. Function with Local Variables NOTE: You can return a local variable if the function prototype has a returned parameter

  39. Loops in Embedded C Use while loops if possible FOR LOOP WHILE LOOP Also a do-while which tests at end. Not used much.

  40. if-else if or switch ?

  41. if-else if or switch ? Use switch statements, for more than 3 conditions AND the values are constants. The cross-compiler will make a jump table (which is way more efficient than multiple tests). Use if-else if if the cases are Boolean or logical expressions. In general, most cross-compilers are more efficient with switch. switch is much more readable (easy to understand), IMHO For a low number of cases, the difference is negligible.

  42. How to handle external delays of unknown duration NEVER just wait: (assumes no Watchdog Timer) TAOCTL0 |= CCIE; // Enable process while(CCIF == 0); // CODE HANGS HERE if no event Value = TAOR; // Read Result

  43. How to handle external delays of unknown duration Method 1: Rely on a Watchdog Timeout TAOCTL0 |= CCIE; // Enable process WDTCTL = WDTPW+WDTCNTCL; //Refresh the Watchdog timer while(CCIF == 0); // Watchdog timer could timeout if enabled Value = TAOR; // Read Result

  44. How to handle external delays of unknown duration Method 2: Use a down-counter to prevent hanging: timeout = 0x100; while (timeout > 0) { timeout--; if (flag == 1) //external event occurred break; }

  45. How to handle external delays of unknown duration Method 3b: MEASURE the worst-case latency and add NOPs (short, fast process) Method 3a: MEASURE the worst-case latency and add as delay count (slow process) // enable process TAOCTL0 |= CCIE; __no_operation(); // TI NOP macro __no_operation(); // TI NOP macro __no_operation(); // TI NOP macro __no_operation(); // TI NOP macro __no_operation(); // TI NOP macro Value = TAOR; // enable process TAOCTL0 |= CCIE; delay(2000); if (CCIF == 1) // flag checking { Value = TAOR; }

  46. Every Cross-Compiler has Gotchas Example from Code Composer Studio: void delay(unsigned long delay_count) { void delay(unsigned long delay_count) { while(delay_count > 1) { delay_count--; } while(delay_count > 1) { delay_count--; __no_operation(); // TI NOP macro } return; } return; } Never put a return directly after a loop. The above code is optimized out. The NOP fixes the problem

  47. Registers

  48. Registers Control Registers (xxxCTLx) Module level. Use to configure functions. Count Registers (xxxR) Up or down counter Capture/Compare Registers (xxxCCR) Work in conjunction with Counters to take action at a certain count. Capture/Compare Control Registers (xxxCCTLx) Used to configure what happens when the CCR matches R (counter). I/O Registers (Ports) Input/Output ports can have 1 of 4 functions.

  49. 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, usually 0.

  50. Control Registers

More Related Content