Home Code Listings Contact

Embedded Software Development

A comprehensive and practical guide to embedded systems development

binary.h. Definitions of 8-bit binary numbers of the form “b00101011”

Listing 1. Calculation in 64-bit integer arithmetic.

Listing 2. Calculation in floating point arithmetic.

Listing 3. Example of machine code and assembly language.

Listing 4. Clearing leftmost bit of a memory address.

Listing 5. JMP instruction.

Listing 6. Conditional jump with no CMP required.

Listing 7. Use of NEC 75X skip instruction.

Listing 8. Relative branch instruction.

Listing 9. Compiler output of a sprintf() function for the Renesas H8/300.

Listing 10. Converting endianness of multi-byte quantities.

Listing 11. Bit endianness swapping function.

Listing 12. C bit endianness swapping function.

Listing 13. Button – LED interrupt example code.

Listing 14. Making setting of flags in a bitfield atomic.

Listing 15. Re-entrant ISR.

Listing 16. Re-entrant ISR with calling depth control.

Listing 17. Variable volatility.

Listing 18. Temporary volatile access to variables.

Listing 19. Protecting multiple byte reads from interrupts.

Listing 20. Example of unused interrupt diagnostic logging.

Listing 21. Sample of an Intel Hex format executable object file.

Listing 22. Sample of a Motorola S-Record format executable object file.

Listing 23. Example list file output.

Listing 24. Initialising variables in C startup code.

Listing 25. Segment map from MAP file.

Listing 26. Stack frame example.

Listing 27. A peripheral register implementation in C.

Listing 28. Round robin sequencer.

Listing 29. Timed sequencer.

Listing 30. Two-tier task structure sequencer.

Listing 31. Using a flag to control a background task from an ISR task.

Listing 32. Time flag triggered sequencer.

Listing 33. Time & task flag triggered sequencer.

Listing 34. A simple Event Queue Manager.

Listing 35. Example use of the EQM.

Listing 36. An implementation of Figure 83's state machine.

Listing 37. A data driven state table.

Listing 38. Example needing a state machine.

Listing 39. A state machine implementation of Listing 38.

Listing 40. Some example GPIO settings on the SiLabs EFR32.

Listing 41. Parallel to serial conversion.

Listing 42. Soft-switching an output high.

Listing 43. Example code to implement repeat samples debouncing.

Listing 44. Button debounce code using a shift register.

Listing 45. Scanning the keypad of Figure 128.

Listing 46. Pseudocode to read the switches of Figure 132.

Listing 47. Pseudocode for Figure 133.

Listing 48. Writing the LCD control registers a field at a time.

Listing 49. Writing the LCD control registers directly.

Listing 50. MAXQ2010 code to write a number onto the display of Figure 162.

Listing 51. Software driven LCD driver.

Listing 52. Definitions for digits 0 to 9 of our LCD character set.

Listing 53. Using two timers to increase the number of output compares.

Listing 54. Pseudo-code for measuring a pulse width.

Listing 55. Measuring PWM using a counter with input capture.

Listing 56. Example function to set PPM position of Figure 192.

Listing 57. A watchdog that requires all tasks to kick.

Listing 58. Example of a watchdog task.

Listing 59. Setting up and kicking the RX IWDT watchdog.

Listing 60. Code to use the Renesas RL78 RTC module.

Listing 61. Converting epoch time format to tm struct format.

Listing 62. Date time structure RTC increment code.

Listing 63. Converting date time structure format to seconds since 1970 format.

Listing 64. Fast code to find the day of the week.

Listing 65. Code to determine whether we are in DST or not.

Listing 66. IsLeapYear() function.

Listing 67. RTC crystal temperature compensation.

Listing 68. Fast 8-bit parity calculation.

Listing 69. Low processor usage I²C driver.

Listing 70. Manchester decoder.

Listing 71. 16-bit XOR checksum function.

Listing 72. 16-bit 2's complement additive checksum function.

Listing 73. 16-bit 1's complement checksum function.

Listing 74. 16-bit Fletcher checksum function.

Listing 75. 16-bit Adler checksum function.

Listing 76. Implementation of a Berger Code checksum on 32-bit data.

Listing 77. 2 of 5 encoding and decoding.

Listing 78. 8b/10b encode and decode functions.

Listing 79. 4B5B encode and decode.

Listing 80. 8 × 8 block rectangular code encoder.

Listing 81. Linear Block Code encoder and checker.

Listing 82. Code to generate a Hadamard matrix.

Listing 83. Source code for Walsh-Hadamard coding.

Listing 84. Implementation of Golay(24,12) code.

Listing 85. Interpolation function.

Listing 86. RAM buffering of an EEPROM.

Listing 87. A safe EEPROM write function, with power-up checking.

Listing 88. Copy a function from a flash array to RAM and call it.

Listing 89. Using RAM-based functions with Position Independent Code.

Listing 90. Example BSL Interrupt Service Routine.

Listing 91. Example Microchip PIC24 PTG "program".

Listing 92. Using NOP instructions for very short delays.

Listing 93. Using a timer to create a very long delay.

Listing 94. Example Free Running Timer functions.

Listing 95. Huffman coding example for the English alphabet characters.

Listing 96. Huffman encoding and packing.

Listing 97. Huffman unpacking and decoding.

Listing 98. Code to generate a diff file.

Listing 99. Decoder function for diff buffer.

Listing 100. Function to find the Highest Bit Set.

Listing 101. Function to Bit Reverse an integer.

Listing 102. log2 with fixed point output.

Listing 103. Simple moving average filter.

Listing 104. Moving average filter with resetting.

Listing 105. Implementation in C of the filter of Figure 404.

Listing 106. FIR filter implemented on an MC56000 DSP.

Listing 107. FFT implementation on the NXP DSP56300.

Listing 108. PID control algorithm.

Listing 109. A CStartup module from the IAR H8 compiler.

Listing 110. Example of an inline function.

Listing 111. Example of identical code blocks for optimisation.

Listing 112. Example structure.

Listing 113. Reformatted structure for better memory use.

Listing 114. Bad example of determinism.

Listing 115. Deterministic version of Listing 114.

Listing 116. Using bitfields to store boolean variables.

Listing 117. Bitfield structure to define a peripheral register.

Listing 118. Example of a questionable use of bitfields.

Listing 119. Using arrays and shifts instead of bitfields.

Listing 120. Masking macros for boolean variables.

Listing 121. Display function using vsprintf().

Listing 122. Table look-up and interpolation example in C.

Listing 123. Example of a C++ class.

Listing 124. Table look-up and interpolation example in Ada.

Listing 125. Table look-up and interpolation example in Ada.

Listing 126. Table look-up and interpolation example in Java.

Listing 127. Table look-up and interpolation example in Forth.

Listing 128. Table look-up and interpolation example in Pascal.

Listing 129. Integrated assembly language in C.

Listing 130. Table look-up and interpolation example in ARM Cortex M4 assembly language.

Listing 131. Reading comms buffer data into local variables.

Listing 132. Endian-independent version of Listing 131.

Listing 133. Snippet of IAR linker map file.

Listing 134. Overriding values in autotest builds.

Code listings

All the listings in the book have been tested on the IAR Embedded Workbench compiler suite for various microcontroller targets, apart from the listings in languages other than C in chapter 38. Most were written with a test harness to ensure the functions listed were working properly, but the test harness is not included in the listing, since you wouldn’t use that if you are using the code in your project. If you have any difficulties with the code not compiling or working then please contact me. You are free to use any of the software listings in this book for your own projects, commercial or otherwise.

All code listings in the book can be found on the accompanying website named according to their captions.