Microcontrollers Basics Tips and Tricks with PIC MCUs
Microcontrollers, Basics Tips and Tricks with PIC MCUs 20 January 2012 Jonathan A. Titus
General Tips & Tricks I • Keep your lab notebook up to date so you know what worked and what didn’t. • Always insert your name, time, date, version in your code listings. • In large projects, use version-control programs. • Do not obfuscate code; keep it clear and readable. • Adopt a coding style and use it. • Use comments liberally--they take NO code space. • If you run into problems, before you do anything else. . . 2
3 General Tips & Tricks II • Check your power supply. Put a ‘scope on the output. • Does the supply voltage match the MCU specs? Is the supply current-limit improperly set? Do you have a short circuit? Have you connected the power supply? Don’t use built-in meters, use a calibrated DVM. 3
General Tips & Tricks III • Check jumpers and connectors. Do all boards have a common ground? Do all boards have power? • Have you used the latest version of the code and the software tools? • Run a simple test program to ensure communication through the debug pod or dev-kit board. • Have you selected the proper MCU for the IDE software? • Is there a power conflict between your power supply and the debug pod? 4
Several Keys at One Input I Microchip AN 512, Implementing Ohmmeter/Temperature Sensor. 5
Several Keys at One Input II 6
Expanded LED Driving Charlieplexing: http: //en. wikipedia. org/wiki/Charlieplexing 7
Resistive-Sensor Interface 8
Don’t Waste Power in Loops • Replace loops that wait for a flag with code that puts the CPU in a low-power mode, then awaken CPU when an event occurs. • Don’t do this: • while(!ADCInterrupt. Flag); • Do this: • while(!ADCInterrupt. Flag) • { • Idle(); //wake on ADC interrupt • } 9
Use Internal Pull-Up Resistors 10
11 LEDs • 1. Use a PWM output to drive an LED and vary brightness. • 2. Use a high-brightness LED at low power--you’ll still get good brightness at lower power. • 3. If you have an LED to use for diagnostics, connect to an I/O pin with a jumper to free the pin for other uses. 11
Terminate or Set I/O Pins • 1. To reduce power, leave unused I/O pins unconnected, but drive them to a logic 1 or 0. • 2. If you have an unused input, connect it to either V+ or ground through a 10 K resistor. You lower power use, but add a component. • For more power-saving tips, read: Microchip Application Note AN 1416, "Low-Power Design Guide. " 12
Comparator Detects Low Battery 13
Debounce a Switch Interrupt I Not Good: Switch bounce could cause a second interrupt. 14
Debounce a Switch Interrupt II Better: Clear flag after switch bounce ends. 15
Filter PWM Output to Get a Voltage 16
Miscellaneous Tips I • Use an internal R-C clock instead of external R-C components. Depends on frequency accuracy you need. • Use different clock sources to reduce power. • Operate at low clock frequencies • Switch to higher-frequency clocks for critical tasks • Turn off unused peripherals, if possible. • Disable watch-dog timer, if not needed. • Look for fast wake-up times, which minimize power use. • Include an LED for each power source. • 17
Miscellaneous Tips II • Check MCU vendor Web sites and forums for more tips and tricks. • Find additional tips, tricks, and sample code on independent sites and discussion groups. • For more "Tips and Tricks, " visit: http: //ww 1. microchip. com/downloads/en/Device. Doc/0114 6 B. pdf. 18
Purchase from Reputable Vendors • Ebay looks like it has some “bargains” in development hardware, but don’t trust your career to unknown suppliers. • Problems with Chinese AVR JTAG-ICE “knock offs. ” • Software might not update cloned hardware. • Difficult to spot cloned equipment. 19
- Slides: 19