- department of mechanical engineering

20
bootcamp.doc Page 1 of 20 2/8/2010 Smart Products Boot Camp 1. Intro The outcome objective of the boot camp is that you exit knowing how to wire a PIC chip circuit, know how to power a PIC chip circuit, know how to connect the programming pod to the circuit and to the PC, know how to create a C program for the PIC chip, know how to compile the C program using the CCS C compiler, know how to burn the code onto the PIC chip, and know how to run the program. 2. Required tools Breadboard, 22 g solid wire, 9V battery, battery snap, components. DMM (digital multi-meter) Windows PC with USB port(s) CCS C compiler, ver. 4.066 or later Microchip PICkit 2 programmer hardware PICkit 2 software, ver 2.40b or later 3. Hardware Steps, LED Flasher Project 1. Wire the PIC LED circuit shown on the PIC LED schematic.. Keep wires short, never have wires cross over the top of a chip and keep wires as far from the chip as possible. Leave room on the board for additional chips. IC pinouts are elsewhere in this document. See samples for general layout techniques. 2. For the Programming Connection (J1), use 5 adjacent holes. Connect to the PICkit 2 programming pod using a right angle header. (Depending on the header, you may have to push the plastic retaining bar on the header to expose more of the pin. 3. For power, use a 9V battery and a battery snap. Do not connect the battery. (Optional: Off- line, solder jumper wires to a power jack so that a 9V wall wart can be used for power.) 4. Power the circuit by connecting the battery. While connecting, place finger tips on all the integrated circuits. If anything gets warm, pull the power…..fast. 5. Using a DMM, check that circuit supply voltage is at +5V and that all ICs are getting the correct supply voltage. Disconnect the battery 4. Software Steps, LED Flasher Project 1. Make a new folder for each project. 2. Place a shortcut to the PICkit 2 software in the folder 3. Create a new text file named flash_led.c. (Or, copy in a .c file that will be used as the starting point for the new project and change the filename to suit the project.) 4. The icon for the .c file should be the CCS compiler icon. Double click on the file to open the compiler IDE. 5. Type in (do not copy) the flash_led.c code shown elsewhere in this document. As you go along, examine the annotated version of the code (on the following page) to get a feel for what you are typing in. Normally, one would be starting with a template or example source file rather than typing from scratch. 6. Compile the file with F9. Correct any errors. Look in the project directory to see the generated files. The .hex file is the one that will be loaded onto the microcontroller.

Upload: others

Post on 09-Feb-2022

4 views

Category:

Documents


0 download

TRANSCRIPT

bootcamp.doc Page 1 of 20 2/8/2010

Smart Products Boot Camp

1. Intro The outcome objective of the boot camp is that you exit knowing how to wire a PIC chip circuit, know how to power a PIC chip circuit, know how to connect the programming pod to the circuit and to the PC, know how to create a C program for the PIC chip, know how to compile the C program using the CCS C compiler, know how to burn the code onto the PIC chip, and know how to run the program.

2. Required tools Breadboard, 22 g solid wire, 9V battery, battery snap, components. DMM (digital multi-meter) Windows PC with USB port(s) CCS C compiler, ver. 4.066 or later Microchip PICkit 2 programmer hardware PICkit 2 software, ver 2.40b or later

3. Hardware Steps, LED Flasher Project 1. Wire the PIC LED circuit shown on the PIC LED schematic.. Keep wires short, never have

wires cross over the top of a chip and keep wires as far from the chip as possible. Leave room on the board for additional chips. IC pinouts are elsewhere in this document. See samples for general layout techniques.

2. For the Programming Connection (J1), use 5 adjacent holes. Connect to the PICkit 2 programming pod using a right angle header. (Depending on the header, you may have to push the plastic retaining bar on the header to expose more of the pin.

3. For power, use a 9V battery and a battery snap. Do not connect the battery. (Optional: Off-line, solder jumper wires to a power jack so that a 9V wall wart can be used for power.)

4. Power the circuit by connecting the battery. While connecting, place finger tips on all the integrated circuits. If anything gets warm, pull the power…..fast.

5. Using a DMM, check that circuit supply voltage is at +5V and that all ICs are getting the correct supply voltage. Disconnect the battery

4. Software Steps, LED Flasher Project 1. Make a new folder for each project. 2. Place a shortcut to the PICkit 2 software in the folder 3. Create a new text file named flash_led.c. (Or, copy in a .c file that will be used as the starting

point for the new project and change the filename to suit the project.) 4. The icon for the .c file should be the CCS compiler icon. Double click on the file to open the

compiler IDE. 5. Type in (do not copy) the flash_led.c code shown elsewhere in this document. As you go

along, examine the annotated version of the code (on the following page) to get a feel for what you are typing in. Normally, one would be starting with a template or example source file rather than typing from scratch.

6. Compile the file with F9. Correct any errors. Look in the project directory to see the generated files. The .hex file is the one that will be loaded onto the microcontroller.

bootcamp.doc Page 2 of 20 2/8/2010

5. Burning to the PIC Chip Steps, LED Flasher Project 1. Connect the PICkit 2 programming pod to the computer USB port. 2. Connect the pod to the programming connector on the breadboard. 3. Power the breadboard by connecting the 9V battery. 4. Start the PICkit 2 application. Confirm that PC sees programmer: Tools > Check

Communication. If not, check connections and board power. 5. File > Import Hex (Ctrl-I). Navigate to your project directory and select the .hex file. The

program should download and run on the microcontroller. Once downloaded, it is possible to disconnect the programming pod. Each time the application circuit is powered, the program will run.

6. For subsequent iterations, click the Auto Import Hex + Write Device button and select the .hex file. Now, each time you compile (F9) in the CCS IDE window, the new .hex file will automatically download to the application circuit making code iterations fast and easy. Try it.

You have successfully completed your first embedded microcontroller project when you demo a flashing LED.

6. Moving On The remaining projects in the boot camp are not described in detail. Use the schematics in the pic-generic.sch file and the code in the generic_code folder, available on the ME8243 web site. Along with building and running the project, do the homework assignments to gain a deeper understanding about the circuit.

7. LED-Switch Project This project adds a switch so that pushing the switch turns the LED on and off. Homework: In addition to the recommended texts on electronics, for a quick introduction to electronics, see the electronics lecture notes for course ME 2011, which are accessed from the ME 2011 schedule (http://www.me.umn.edu/courses/me2011/).

8. Potentiometer Project This project adds a potentiometer (variable resistor) control. Use the adc.c code. Homework: Learn about analog-to-digital converters (ADC). The Wikipedia entry (Analog-to-digital converter) is a good launching point. Other resources include: (1) www.hardwaresecrets.com/article/317/1, (2) microchip.com/downloads/en/AppNotes/21841b.pdf. In particular, understand resolution, range, conversion time and noise of ADCs.

9. DC Motor Project This project has three parts. You only have to show the last part to the instructor. For the first part, implement on-off control of a DC motor. Use the PIC-MOTOR schematic found later in this document. For the code, add three lines to the flashing led code that, after the LED flashes, turns on the motor, delays for two seconds, and then turns off the motor. The added code lines will look like this:

bootcamp.doc Page 3 of 20 2/8/2010

output_high(MOTOR); delay_ms(2000); output_low(MOTOR); At the top of the code, define the pin that controls the motor like this: #define MOTOR PIN_B1 // motor Run the program. Try changing the time that the motor is on. Add a tape flag to the motor shaft so that you can see it spin. For the second part, implement speed control of the motor using pulse-width modulation (PWM). For this, disconnect the LED and connect the motor to port B0 (pin 6), including the same resistor to ground that you had when the motor was connected to port B1. Use the pwm.c code. Modify the code to vary the speed ramp up time. For the third part, control the speed of the motor using the pot. You are on your own for this code. Show the result to the instructor. If the motor behaves erratically and you know the wiring and code are good, there may be noise spikes that randomly reset the microcontroller. Noise spikes are generated whenever an inductive load is switched on and off. The motor is an inductive load. See the ME 2011 motor control technical notes for handling noise spikes with a flyback diode and filtering capacitors. In addition, place a 0.1 uF bypass capacitor between the PICmicro power pins (5 and 14 for the F88 chip), getting as close as you can to the pins by straddling the chip with the capacitor. Homework: Review your understanding of DC brushed motors. The Wikipedia entry on brushed DC electric motor is a good place to start. From there, review the motor technical notes on the ME 2011 web site, www.me.umn.edu/courses/me2011/arduino/technotes/dcmotors/.

10. Bi-Directional Motor Project This project provides two-way control of the motor using an H-bridge circuit based on the L293D or SN754410NE driver chip. Use the l293.c code. Homework: Understand the principles of controlling a motor using an H-Bridge. The Wikipedia H-Bridge entry is a good start. For more on motor control, go to www.microchip.com/motor and read the tutorials and application notes on DC brushed motor control.

11. Stepper Motor Project This project controls a unipolar stepper motor. Use the step.c code. One task is to understand the operation of step.c so well that you can teach it to others. Add a tape flag to the motor shaft so that you can see it turn. The stepper you have is a 6-wire unipolar stepper (see references below). Two of the wires are joined to form the common and the remaining four are the four coils, which must be connected in the proper order to the driver chip as shown in the schematic. Finding which wire is which can be an adventure, but the references below can help.

bootcamp.doc Page 4 of 20 2/8/2010

For the small stepper motor from All Electronics, ALL-64, the two brown leads are the common and coil leads 1 to 4, in order, are: red, blue, white, orange. For the Jameco 171601 small stepper motor, red + green are the common and coil leads 1 to 4, in order are: yellow, brown, orange, black. This motor is number PF35T-48L4: 7.5 deg/step, 48 steps/rev, 20 ohms/coil, 250 mA @ 5 V. Solder 22 g solid wire jumpers to the motor leads because stranded wire will break off in and clog up breadboard holes for good. The Vmotor line is the power supply to the motor, which should match the specs on the stepper. Steppers, except for the very smallest models, draw a lot of current, even when they are not moving. For the ALL-65 tiny stepper, can power off 5 V on the breadboard or the 9 V battery or the 9 V power supply brick for the Vmotor line. For the Jameco 171601 stepper, power off the 5 V on the breadboard, but only if using the 7805 voltage regulator that is in the TO-220 package. The 78L05 regulator in the small TO-92 package will burn out because of the high current. After connecting the power, with the motor connected, hold the voltage regulator to check that it is not getting too hot. Feel free to be inventive with the step.c program. Try different rotation speeds and different angular excursions. Monitor the voltage regulator temperature with your finger and disconnect the power when it gets too hot, which it will when the motor is connected for a long time because the motor draws current whenever one of the coils is on. The unlock() command at the end of step.c turns off the coils to save power and to prevent the regulator from getting hot at idle. If the motor vibrates rather than spins, the leads are in the wrong order. If the microcontroller, randomly resets, noise spikes are getting in the way. Try a 10 uF capacitor on the 9 V line, placed close to the stepper Vmotor,(pin 9 of the ULN2003A driver chip) and try a 0.1 uF bypass capacitor on the PICmicro power pins. Homework: Understand how stepper motors work. The Wikipedia entry on stepper motor is a good start. From there, go to the ME 2011 notes on controlling stepper motors with the Arduino, which contain more background information including methods for identifying stepper motor leads, www.me.umn.edu/courses/me2011/arduino/technotes/stepper/index.html.

12. Serial Connection Project This project connects the PICmicro to the PC using a serial connection, going to the PC through a USB-to-serial adaptor cable, which appears as a virtual serial port on the PC. The serial converter chip on the microcontroller side is the DS275. Use the RS-232 Serial schematic. To install and run the driver for the USB-to-serial adapter cable, see /www.me.umn.edu/courses/me2011/arduino/stamp/technotes/usb2serial.html. Note the number of the COM port that the cable appears as.

bootcamp.doc Page 5 of 20 2/8/2010

Build the circuit and run the char-echo.c code and then the char-echo-ints.c code. On the PC side, run a terminal emulator such as Hyper-Term or Tera Term (http://en.sourceforge.jp/projects/ttssh2/releases/ or http://www.ayera.com/teraterm/) , connecting using the COM port number used by the USB-to-serial cable and at the baud rate specified in the Microchip .c code. Homework: Understand the RS-232 serial protocol. The RS-232 entry in Wikipedia is a good place to start. These projects use a 3-wire RS-232 and a protocol of 8 data bits, 1 stop bit and no parity bit. The baud rate is set by the code on the PICmicro.

13. Light Switch Project This project has a VB.NET program on the host PC controlling the LED on the PICmicro with the two computers communicating over a serial connection. The wiring is the same as the serial connection project. On the PICmicroside, run the lightswitch.c code. On the PC host side, run the LightSwitch code. Homework: understand what is going on.

14. Moving Bar Project This project has a potentiometer on the PICmicro controlling the position of a bar on a VB.NET program running on the host computer with the two computers communicating through a serial connection. On the PICmicro side, keep the serial connections and add the potentiometer to the ADC in the same was as the potentiometer project. On the PICmicro side, run the movebar.c code. On the PC host side, run the MoveBar code. Homework: understand what is going on.

15. Encoder Project This project controls the device of your choosing using a mechanical encoder. Use the encoder.c code.

16. Understanding Assembly Language Write a CCS .c source file with this content: #include <16f88.h> void main(void) { int8 i,j,k; i=i+1; i++; i=i*5; i=i*4; i=i/5; i=i/4; k=i+j; k=i*j; k=i/j; }

Compile and view the .lst file. Attempt to understand the generated assembly language code, using a description of the PICmicro instruction set that is in the PICF88 data sheet and provided

bootcamp.doc Page 6 of 20 2/8/2010

in more detail in other documents on the Microchip web site. Pay closer attention to how the math operations are performed. Run again with i,j,k defined as int16 variables. Gain sufficient understanding of the assembly language output that you could explain the code to a classmate. Then, test your understanding by explaining the code generated for the int8 variables to a classmate. You have passed the assignment when the classmate is satisfied that you know what you are talking about.

17. Math Benchmarking Using the flash_led.c program as the template, write a program that demonstrates the length of time it takes the PICF88 chip to do math operations. For this, define several variables and then do repeated math calculations (for example, and add, a subtract a multiply and a divide) in a loop. Use the loop variable in each calculation, otherwise the optimizing complier will take the math outside the loop when generating assembly language. Turn on the LED before entering the loop and turn off after exiting the loop. The number of times in the loop should be high enough that you can see the LED turn on and off. For the first loop, define all the variables as type int8. After a 2 s pause, create a second loop with exactly the same calculations and the same number of loop iterations, but define the variables used in this loop as int16. Add a third loop with the variables defined as int32 and a fourth loop with the variables defined as float32. The result will be a visual indicator of the speed at which the processor can do math operations on variables of different types.

bootcamp.doc Page 7 of 20 2/8/2010

/************************************************** ***** FILE: flash_led.c DESCRIPTION: A first PIC program. Flashes LED attac hed to PIC pin. Designed for 16f88 PIC chip using internal oscillat or. REVISION HISTORY: *************************************************** ****/ //--Includes #include <16f88.h> //--Setup commands #fuses INTRC_IO,NOWDT,NOPROTECT,PUT,NOWRT,NOLVP // internal oscillator #id 0x1234 // 4 digit id #use standard_io(A) #use standard_io(B) //#use fast_io(A) //#use fast_io(B) #use delay(clock=8000000) // declare clock spe ed //--Pin definitions #define LED PIN_B0 // LED /************************************************** ***** Main starts here *************************************************** ****/ void main(void) { int i; setup_oscillator(OSC_8MHZ); //-------Set direction registers // set_tris_a(0xFF); // a is all inputs (for adc ) // set_tris_b(0xC0); // b7,b6 in, rest out //-------Flash debug led a few times for (i=0;i<5;i++) { output_high(LED); delay_ms(100); output_low(LED); delay_ms(300); } //-------That's all folks sleep(); }

bootcamp.doc Page 8 of 20 2/8/2010

/************************************************** ***** FILE: flash_led.c DESCRIPTION: A first PIC program. Flashes LED attac hed to PIC pin. Designed for 16f88 PIC chip using internal oscillat or. REVISION HISTORY: *************************************************** ****/ //--Includes #include <16f88.h> //--Setup commands #fuses INTRC_IO,NOWDT,NOPROTECT,PUT,NOWRT,NOLVP // internal oscillator #id 0x1234 // 4 digit id #use standard_io(A) #use standard_io(B) //#use fast_io(A) //#use fast_io(B) #use delay(clock=8000000) // declare clock speed //--Pin definitions #define LED PIN_B0 // LED for debugging /************************************************** ***** Main starts here *************************************************** ****/ void main(void) { int i; setup_oscillator(OSC_8MHZ); //-------Set direction registers // set_tris_a(0xFF); // a is all inputs (for adc ) // set_tris_b(0xC0); // b7,b6 in, rest out //-------Flash debug led a few times for (i=0;i<5;i++) { output_high(LED); delay_ms(100); output_low(LED); delay_ms(300); } //-------That's all folks sleep(); }

Optional: finishing with the built-in sleep function prevents code from falling into never-never land

Declare all your variables

Required if using internal oscillator

Tris commands deal with setting i/o direction of pins. Not required unless using fast i/o.

Always use #defines to define your pins. Makes code easier to read and much easier to change if you change which

Required for using any timing functions. Equals speed of oscillator

Has to do with how code handles setting i/o directions of pins. See compiler and chip manuals for details. When in doubt, use standard_io. When need every bit of speed, use fast_io. A and B are the pin ports

Required. Defines which type of PIC chip being used

Note comments preceding each section

Defines various setup options for the chip. Generally, can keep the same except for the first parameter (INTRC_IO) which changes depending on oscillator type

For reverse engineering or identifying code once it is burned onto the chip. Not particularly useful

Always start with a generous, standard header, even for practice code snippet

Very handy built-in pause function

Built-in function that sets a pin high (+5V)

Function that sets a pin low (0V)

Read CCS compiler help file to learn about commands that are not part of the standard C language and are specific to PIC chip microcontrollers

bootcamp.doc Page 9 of 20 2/8/2010

bootcamp.doc Page 10 of 20 2/8/2010

bootcamp.doc Page 11 of 20 2/8/2010

bootcamp.doc Page 12 of 20 2/8/2010

bootcamp.doc Page 13 of 20 2/8/2010

bootcamp.doc Page 14 of 20 2/8/2010

bootcamp.doc Page 15 of 20 2/8/2010

bootcamp.doc Page 16 of 20 2/8/2010

bootcamp.doc Page 17 of 20 2/8/2010

bootcamp.doc Page 18 of 20 2/8/2010

bootcamp.doc Page 19 of 20 2/8/2010

bootcamp.doc Page 20 of 20 2/8/2010