Term 8 Electrical and Computer Engineering Project Course January 2002 Mark Simms Steven Taylor PIC Development, The Easy ...
Overview <ul><li>Basic PIC Circuits </li></ul><ul><li>Development in C using CCS </li></ul><ul><li>Typical Tasks (A/D, PWM...
Basic PIC Circuits <ul><li>All designs are based around the PIC16F877, a mid-range microcontroller that supports: </li></u...
Basic PIC Circuits <ul><li>The most basic circuit consists of: </li></ul><ul><ul><li>The microcontroller </li></ul></ul><u...
Programming in C <ul><li>Programming the PIC in C offers several advantages: </li></ul><ul><ul><li>Higher level language –...
PIC – Common Tasks with CCS <ul><li>Program Template.  Starting point for just about everything </li></ul>#define <16F877....
PIC – Common Tasks with CCS <ul><li>Digital I/O </li></ul><ul><ul><li>Standard I/O vs. Fast I/O </li></ul></ul><ul><ul><li...
PIC – Common Tasks with CCS <ul><li>Analog Input </li></ul><ul><ul><li>Initialization: </li></ul></ul><ul><ul><ul><li>setu...
PIC – Common Tasks with CCS <ul><li>Using PWM </li></ul><ul><ul><li>Initialization: </li></ul></ul><ul><ul><ul><li>setup_t...
PIC – Tips for Software Design <ul><li>Design the program as a state machine </li></ul><ul><ul><li>A main() loop, with: </...
PIC – Tips for Debugging <ul><li>Use a protoboard with RS232 support and lots of print statements.  Example: </li></ul><ul...
PIC – Tips for Debugging <ul><li>Some_function() </li></ul><ul><li>{ </li></ul><ul><li>int1 pushed = FALSE, last_pushed = ...
PIC – Tips for Debugging <ul><li>Can also use conditionals to print out different types of debugging messages.  Say we hav...
PIC – In-Circuit Programming <ul><li>The PIC16F877 has on-board FLASH memory  </li></ul><ul><ul><li>No burner needed to re...
PIC – In-Circuit Programming <ul><li>The PIC16F877 has on-board FLASH memory  </li></ul><ul><ul><li>No burner needed to re...
PIC – Mad2/Pumpkin <ul><li>PIC16F877-based Prototyping Board </li></ul><ul><ul><li>PIC16F877 microcontroller with PWR/GND ...
Sample Application – Analog Sampling <ul><li>PC Application will do the following: </li></ul><ul><ul><li>Present a graphic...
Mplab – Setting up for CCS <ul><li>Project->New (call it  main.prj ) </li></ul><ul><ul><li>Development Mode:  Editor/16F87...
Step 1: Basic Template <ul><li>Basic Template Code is: </li></ul><ul><ul><li>Include the header file for the appropriate P...
Step 2: Initialize the PIC functions <ul><li>Need to initialize (if using): </li></ul><ul><ul><li>Analog to Digital Conver...
Step 2: Initialize the PIC functions <ul><li>Need to initialize (if using): </li></ul><ul><ul><li>Analog to Digital Conver...
Step 3: State Machine and Interrupts <ul><li>Set up the state machine </li></ul><ul><ul><li>Define the allowable states wi...
Step 3: State Machine and Interrupts <ul><li>Three States </li></ul><ul><ul><li>IDLE – do nothing </li></ul></ul><ul><ul><...
Step 3: State Machine and Interrupts <ul><li>The first state machine is composed with this master “on/off” state machine. ...
Step 4: Handle ANALOG_DATA state <ul><li>Declare variables to store analog input and PWM output </li></ul><ul><li>In the s...
Step 5: Handle Serial Input <ul><li>Declare variables to store string data from the user </li></ul><ul><li>Copy in the get...
References and Links <ul><li>Presentation, Notes and Code Archive </li></ul><ul><ul><li>http://www.engr.mun.ca/~msimms/pic...
Upcoming SlideShare
Loading in …5
×

Centre for Instrumentation, Control and Automation (INCA)

771 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
771
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Centre for Instrumentation, Control and Automation (INCA)

  1. 1. Term 8 Electrical and Computer Engineering Project Course January 2002 Mark Simms Steven Taylor PIC Development, The Easy Way
  2. 2. Overview <ul><li>Basic PIC Circuits </li></ul><ul><li>Development in C using CCS </li></ul><ul><li>Typical Tasks (A/D, PWM, timing) </li></ul><ul><li>How to Layout a Program </li></ul><ul><li>Debugging Tips </li></ul><ul><li>The Pumpkin protoboard </li></ul>
  3. 3. Basic PIC Circuits <ul><li>All designs are based around the PIC16F877, a mid-range microcontroller that supports: </li></ul><ul><ul><li>8kB of flash program memory </li></ul></ul><ul><ul><li>Interrupts </li></ul></ul><ul><ul><li>In-circuit programming </li></ul></ul><ul><ul><li>Hardware timers </li></ul></ul><ul><ul><li>Capture/Compare/PWM modules </li></ul></ul><ul><ul><li>10-bit A/D conversion (up to 8 channels) </li></ul></ul><ul><ul><li>Built-in USART for serial communication </li></ul></ul><ul><ul><li>Lots of Digital I/O </li></ul></ul>
  4. 4. Basic PIC Circuits <ul><li>The most basic circuit consists of: </li></ul><ul><ul><li>The microcontroller </li></ul></ul><ul><ul><li>Power and GND (+5V) </li></ul></ul><ul><ul><li>Oscillator with Caps </li></ul></ul><ul><li>Typical development circuit adds: </li></ul><ul><ul><li>RS232 interface (typically with a MAX2.. Chip) </li></ul></ul><ul><ul><li>LED’s / switches / etc </li></ul></ul><ul><li>Schematics available on INCA web site (resources at end) </li></ul>
  5. 5. Programming in C <ul><li>Programming the PIC in C offers several advantages: </li></ul><ul><ul><li>Higher level language – developer is insulated from details of the chip </li></ul></ul><ul><ul><li>Library support for common tasks (string manipulation, serial communication) </li></ul></ul><ul><li>We use the CCS compiler ( http://www. ccsinfo .com/ ) which don’t suck. All examples will use CCS code </li></ul>
  6. 6. PIC – Common Tasks with CCS <ul><li>Program Template. Starting point for just about everything </li></ul>#define <16F877.h> // Define the type of chip you’re using. // Makes it easier to switch chips #use delay(clock=20000000) // 20Mhz oscillator void main() { /* Initialization Code goes here */ while (TRUE) { /* Program Code goes here */ } }
  7. 7. PIC – Common Tasks with CCS <ul><li>Digital I/O </li></ul><ul><ul><li>Standard I/O vs. Fast I/O </li></ul></ul><ul><ul><li>Using (standard I/O): </li></ul></ul><ul><ul><ul><li>// Output a high on PIN_D1, low on PIN_D2 </li></ul></ul></ul><ul><ul><ul><li>// Wait 50 us and invert </li></ul></ul></ul><ul><ul><ul><li>output_high(PIN_D1); </li></ul></ul></ul><ul><ul><ul><li>output_low(PIN_D2); </li></ul></ul></ul><ul><ul><ul><li>delay_us(50); </li></ul></ul></ul><ul><ul><ul><li>output_low(PIN_D1); </li></ul></ul></ul><ul><ul><ul><li>output_high(PIN_D2); </li></ul></ul></ul>
  8. 8. PIC – Common Tasks with CCS <ul><li>Analog Input </li></ul><ul><ul><li>Initialization: </li></ul></ul><ul><ul><ul><li>setup_adc_ports(ALL_ANALOG); </li></ul></ul></ul><ul><ul><ul><li>setup_adc(ADC_CLOCK_DIV_2); </li></ul></ul></ul><ul><ul><li>Picking a channel: </li></ul></ul><ul><ul><ul><li>set_adc_channel(0); // Note: must wait between changing </li></ul></ul></ul><ul><ul><ul><li>// input channels (~ 10us) </li></ul></ul></ul><ul><ul><li>Inputting Data: </li></ul></ul><ul><ul><ul><li>unsigned int16 data; // Declare a 16-bit integer </li></ul></ul></ul><ul><ul><ul><li>data = read_adc(); // Read a 10-bit value from the // selected channel </li></ul></ul></ul>
  9. 9. PIC – Common Tasks with CCS <ul><li>Using PWM </li></ul><ul><ul><li>Initialization: </li></ul></ul><ul><ul><ul><li>setup_timer_2(T2_DIV_BY_1,249,1); // Setup the PWM period </li></ul></ul></ul><ul><ul><ul><li>setup_ccp1(CCP_PWM); // Set CCP1 for PWM </li></ul></ul></ul><ul><ul><li>Setting the Duty Cycle: </li></ul></ul><ul><ul><ul><li>set_pwm1_duty(500); // See the CCS examples for the formula // for setting the PWM period and duty // cycle </li></ul></ul></ul>
  10. 10. PIC – Tips for Software Design <ul><li>Design the program as a state machine </li></ul><ul><ul><li>A main() loop, with: </li></ul></ul><ul><ul><ul><li>A switch() statement that jumps to a function() which represents the actions that occur in that state </li></ul></ul></ul><ul><ul><ul><li>Each state function() has an output section and a transition section (which can change the current state variable) </li></ul></ul></ul><ul><ul><li>Interrupts are very useful (for example: interrupt when data received on serial port), but can cause problems. </li></ul></ul><ul><ul><ul><li>I.e. if you change state during an interrupt (such as an E-stop), return from the interrupt service routine, then change the state variable again (during the transition section) the interrupt change is lost. </li></ul></ul></ul><ul><ul><li>Design with tuning and debugging in mind </li></ul></ul><ul><ul><ul><li>Programmer time is more important than machine time – the PIC16F877 is plenty fast </li></ul></ul></ul>
  11. 11. PIC – Tips for Debugging <ul><li>Use a protoboard with RS232 support and lots of print statements. Example: </li></ul><ul><ul><li>program waits for a switch press </li></ul></ul><ul><ul><li>reads an analog voltage </li></ul></ul><ul><ul><li>changes the PWM cycle accordingly </li></ul></ul>
  12. 12. PIC – Tips for Debugging <ul><li>Some_function() </li></ul><ul><li>{ </li></ul><ul><li>int1 pushed = FALSE, last_pushed = FALSE; </li></ul><ul><li>int16 analog_value; </li></ul><ul><li>float volts; </li></ul><ul><li>pushed = input(PIN_D3); </li></ul><ul><li>if (pushed && !last_pushed) { </li></ul><ul><li>puts(“Button Pushed!”); </li></ul><ul><li>analog_value = read_adc(); /* 10-bit analog input value is * between 0-1023 0-5V range */ </li></ul><ul><li>volts = 5.0 * (analog_value / 1024.0); </li></ul><ul><li>printf(“Button pushed! Analog value is %f volts, PWM to %i , volts, analog_value); </li></ul><ul><li>set_pwm1_duty(analog_value); </li></ul><ul><li>/* We’ve pre-configured PWM channel 1 – the set_pwm1_duty cycle function accepts </li></ul><ul><li> a 10-bit number and adjusts the cycle accordingly */ </li></ul><ul><li>} </li></ul>
  13. 13. PIC – Tips for Debugging <ul><li>Can also use conditionals to print out different types of debugging messages. Say we have a type of message, INFO that we only want to be displayed when testing certain things. We could define a MACRO: </li></ul><ul><li>#ifdef SHOW_INFO </li></ul><ul><li>#define INFO(A) puts(A); </li></ul><ul><li>#else </li></ul><ul><li>#define INFO(A) /* A */ </li></ul><ul><li>#endif </li></ul><ul><li>Then, at an appropriate point in the code: </li></ul><ul><li>INFO(“Button Pushed”); </li></ul>
  14. 14. PIC – In-Circuit Programming <ul><li>The PIC16F877 has on-board FLASH memory </li></ul><ul><ul><li>No burner needed to reprogram the PIC </li></ul></ul><ul><ul><li>No need to remove PIC from circuit </li></ul></ul><ul><li>Using a bootloader on the PIC, and a bootload utility on the PC the PIC can be reprogrammed in seconds over a serial link. </li></ul><ul><ul><li>Burn the bootloader code onto the PIC </li></ul></ul><ul><ul><li>When writing your program in C tell the compiler not to use the top 255 bytes of flash memory </li></ul></ul><ul><ul><li>Connect the PIC circuit to the PC via a serial link. Run the bootloader code from the PC and download your code to the circuit in seconds </li></ul></ul><ul><li>This technique is VITAL to preserving sanity </li></ul>
  15. 15. PIC – In-Circuit Programming <ul><li>The PIC16F877 has on-board FLASH memory </li></ul><ul><ul><li>No burner needed to reprogram the PIC </li></ul></ul><ul><ul><li>No need to remove PIC from circuit </li></ul></ul><ul><li>Using a bootloader on the PIC, and a bootload utility on the PC the PIC can be reprogrammed in seconds over a serial link. </li></ul><ul><ul><li>Burn the bootloader code onto the PIC </li></ul></ul><ul><ul><li>When writing your program in C tell the compiler not to use the top 255 bytes of flash memory </li></ul></ul><ul><ul><li>Connect the PIC circuit to the PC via a serial link. Run the bootloader code from the PC and download your code to the circuit in seconds </li></ul></ul><ul><li>This technique is VITAL to preserving sanity </li></ul>
  16. 16. PIC – Mad2/Pumpkin <ul><li>PIC16F877-based Prototyping Board </li></ul><ul><ul><li>PIC16F877 microcontroller with PWR/GND connected, 20Mhz oscillator </li></ul></ul><ul><ul><li>8 digital I/O points </li></ul></ul><ul><ul><li>8 LED’s (switchable to DI/O) </li></ul></ul><ul><ul><li>8 Analog Input ports (also usable as DI/O) </li></ul></ul><ul><ul><li>2 PWM channels </li></ul></ul><ul><ul><li>RS232 interface </li></ul></ul>
  17. 17. Sample Application – Analog Sampling <ul><li>PC Application will do the following: </li></ul><ul><ul><li>Present a graphical front end to the user </li></ul></ul><ul><ul><li>Have a “sample” button that will send a character to the PIC over the serial port </li></ul></ul><ul><ul><li>Will read back a number in hex format, reformat into decimal and display on the screen </li></ul></ul><ul><li>PIC Application will do the following: </li></ul><ul><ul><li>Poll the serial port </li></ul></ul><ul><ul><li>If a character is received, sample analog channel 0 (A0), and print the value to the serial port as a hex number, followed by a newline/return ( ) </li></ul></ul><ul><ul><li>Use the value read from the analog input channel as the PWM duty cycle on channel 1 </li></ul></ul>
  18. 18. Mplab – Setting up for CCS <ul><li>Project->New (call it main.prj ) </li></ul><ul><ul><li>Development Mode: Editor/16F877 </li></ul></ul><ul><ul><li>Language Tool Suite: CCS </li></ul></ul><ul><ul><li>Click on main.hex, Node Properties </li></ul></ul><ul><ul><li>Click on PCM </li></ul></ul><ul><ul><li>File->New </li></ul></ul><ul><ul><li>File->Save As, main.c </li></ul></ul><ul><ul><li>Add Node, main.c </li></ul></ul><ul><li>Ready to start building the application </li></ul>
  19. 19. Step 1: Basic Template <ul><li>Basic Template Code is: </li></ul><ul><ul><li>Include the header file for the appropriate PIC </li></ul></ul><ul><ul><ul><li>Note: I typically use a custom 16F877.H header file with 10-bit data acquisition turned on </li></ul></ul></ul><ul><ul><li>Set the clock speed </li></ul></ul><ul><ul><li>Set the fuses </li></ul></ul><ul><ul><li>Set up serial communication </li></ul></ul><ul><ul><li>Reserve memory for the bootloader </li></ul></ul><ul><ul><li>Main function and debug/status message </li></ul></ul>
  20. 20. Step 2: Initialize the PIC functions <ul><li>Need to initialize (if using): </li></ul><ul><ul><li>Analog to Digital Conversion </li></ul></ul><ul><ul><li>Counters and Timers </li></ul></ul><ul><ul><li>PWM output / capture </li></ul></ul><ul><ul><li>Interrupts </li></ul></ul><ul><ul><ul><li>Serial </li></ul></ul></ul><ul><ul><ul><li>Timer </li></ul></ul></ul><ul><ul><ul><li>Global </li></ul></ul></ul><ul><ul><ul><li>Etc </li></ul></ul></ul><ul><li>I’ve included an LED test to show the card has reset </li></ul>
  21. 21. Step 2: Initialize the PIC functions <ul><li>Need to initialize (if using): </li></ul><ul><ul><li>Analog to Digital Conversion </li></ul></ul><ul><ul><li>Counters and Timers </li></ul></ul><ul><ul><li>PWM output / capture </li></ul></ul><ul><ul><li>Interrupts </li></ul></ul><ul><ul><ul><li>Serial </li></ul></ul></ul><ul><ul><ul><li>Timer </li></ul></ul></ul><ul><ul><ul><li>Global </li></ul></ul></ul><ul><ul><ul><li>Etc </li></ul></ul></ul><ul><li>I’ve included an LED test to show the card has reset </li></ul>
  22. 22. Step 3: State Machine and Interrupts <ul><li>Set up the state machine </li></ul><ul><ul><li>Define the allowable states with enum’s </li></ul></ul><ul><ul><li>Define the state variables </li></ul></ul><ul><ul><li>ALWAYS INITIALIZE EVERY VARIABLE </li></ul></ul><ul><ul><li>Enter the infinite loop and check for state </li></ul></ul><ul><li>Set up the interrupt handler </li></ul><ul><ul><li>Serial #INT_RDA </li></ul></ul><ul><ul><li>Timer #INT_TIMER1 </li></ul></ul>
  23. 23. Step 3: State Machine and Interrupts <ul><li>Three States </li></ul><ul><ul><li>IDLE – do nothing </li></ul></ul><ul><ul><li>RECV_DATA </li></ul></ul><ul><ul><ul><li>Enter: when serial interrupt received </li></ul></ul></ul><ul><ul><ul><li>Exit: when serial data handled </li></ul></ul></ul><ul><ul><li>READ_ANALOG </li></ul></ul><ul><ul><ul><li>Enter: when timer1 overflows (every 100 ms) </li></ul></ul></ul><ul><ul><ul><li>Exit: when analog data is read and PWM updated </li></ul></ul></ul>
  24. 24. Step 3: State Machine and Interrupts <ul><li>The first state machine is composed with this master “on/off” state machine. </li></ul><ul><li>STOP: user sends a “stop” command </li></ul><ul><li>START: user sends a “start” command </li></ul>
  25. 25. Step 4: Handle ANALOG_DATA state <ul><li>Declare variables to store analog input and PWM output </li></ul><ul><li>In the state handler: </li></ul><ul><ul><li>Read in the analog voltage (remember it’s a 10-bit number, so we’ll need a 16-bit integer) </li></ul></ul><ul><ul><li>Convert to PWM rate (divide by 2, ceil to 500) </li></ul></ul><ul><ul><li>Convert to actual voltage (0-5V) </li></ul></ul><ul><ul><li>Print on the serial port </li></ul></ul><ul><ul><li>Return to IDLE state </li></ul></ul>
  26. 26. Step 5: Handle Serial Input <ul><li>Declare variables to store string data from the user </li></ul><ul><li>Copy in the get_string() function from input.c </li></ul><ul><li>In the state handler: </li></ul><ul><ul><li>Disable interrupts </li></ul></ul><ul><ul><li>Read in a string </li></ul></ul><ul><ul><li>Check to see if it matches “start” or “stop” </li></ul></ul><ul><ul><li>Change state if necessary </li></ul></ul><ul><ul><li>Re-enable interrupts </li></ul></ul><ul><ul><li>Change state to IDLE </li></ul></ul>
  27. 27. References and Links <ul><li>Presentation, Notes and Code Archive </li></ul><ul><ul><li>http://www.engr.mun.ca/~msimms/pic/ </li></ul></ul><ul><li>CCS PIC C Compiler </li></ul><ul><ul><li>http://www.ccsinfo.com/ </li></ul></ul><ul><li>CCS PIC C Compiler Manual </li></ul><ul><ul><li>http://www.ccsinfo.com/piccmanual3.zip </li></ul></ul><ul><li>WorkingTex Web Site (lots of examples!) </li></ul><ul><ul><li>http://www.workingtex.com/htpic/ </li></ul></ul><ul><li>Bootloader Code (that resides on the PIC) </li></ul><ul><ul><li>http://www.workingtex.com/htpic/PIC16F87x_and_PIC16F7x_bootloader_v7-40.zip </li></ul></ul><ul><li>Bootloader Program (that resides on the PC) </li></ul><ul><ul><li>http://www.ehl.cz/pic/ </li></ul></ul>

×