1 / 33

Sophomore Clinic ENGR 01-202 5, CRN 20686 Introduction to PIC Programming in C

Sophomore Clinic ENGR 01-202 5, CRN 20686 Introduction to PIC Programming in C. James K. Beard, Ph.D. Topics. Requirements What do we need to do? What do we have to accomplish this? PIC 16F876A Capabilities The Program Issues The PWM The rest of the program The C Program Code Issues

laszlo
Download Presentation

Sophomore Clinic ENGR 01-202 5, CRN 20686 Introduction to PIC Programming in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sophomore ClinicENGR 01-202 5, CRN 20686Introduction to PIC Programming in C James K. Beard, Ph.D.

  2. Topics • Requirements • What do we need to do? • What do we have to accomplish this? • PIC 16F876A Capabilities • The Program Issues • The PWM • The rest of the program • The C Program Code • Issues • What do you do? • What can you do to improve your project once it is all working? Sophomore Clinic

  3. Basic Requirements • Notional Functions (Your design may differ) • Drive H-Bridges for Two bi-directional DC motors • Drive Electromagnet On-Off • Controls on HMI • Two potentiometers on thumbwheels • Three pushbuttons • Use a PIC 16F876A • Three buses • A Bus for up to five analog signals • B and C Bus are eight bits bi-directional • 4 MHz clock • C Code for the PIC using CCS PIC C Sophomore Clinic

  4. PIC 16F876A Capabilities • Mid-range PIC • 28 Pins, low power, RISC instruction set, slow arithmetic • Low end is 18 pins and smaller • High end is 40 pins, fast arithmetic • Five A to D Converters • Two Pulse Width Modulators (PWMs) • 23 Programmable I/O Pins • Much other stuff that we don’t need for SC • Just right for our project Sophomore Clinic

  5. The PIC 16F876A Sophomore Clinic

  6. PIC 16F876A Buses • Bus A • Six individually programmable I/O lines • Analog or digital inputs and digital outputs • Up to 5 ADC inputs • Programmable pull-ups for switched inputs • Bus B • Eight individually programmable digital I/O lines • Programmable pull-ups • Bus C – eight individually programmable I/O lines Sophomore Clinic

  7. Devices in the PIC 16F876A • Processor • Memory • 8 K of 14-bit instruction flash memory • 368 bytes of program memory • 256 bytes of EEPROM • Five 10-bit ADCs • Some configurable with references • One configurable with both high and low reference • Two oscillators • Backup 2.5 MHz R-C clock oscillator • Quartz clock up to 10 MHz Sophomore Clinic

  8. Devices (Continued) • RS-232 mapped to C I/O commands like printf • Computer on null modem cable is a PIC terminal • Uses only two pins, C6 and C7 • Called the Master Synchronous Serial Port (MSSP) • Coupled with memory-mapped UART • Three timers • 14 interrupts • Two capture/compare/PWM (CCP) modules Sophomore Clinic

  9. How It’s Done • The hardest part is the PWM setup • The PWM uses a counter, Timer 2, to set a PWM period • Timer 2 counts the processor clock pulses • The output pulse is ON for a specified number of clock pulses • The duty cycle is the ratio of the number of ON pulses to the total period set by Timer 2 • The rest is easy • ADC is 10 bit, from any of 5 pins of bus A • Pushbuttons are read from three bits of bus C • H-bridge word is made up from • PWM outputs • Bits that tell us forward-backward, up-down, toggled by pushbuttons • Magnet drive is logic output to bit of bus C, toggled on-off by pushbutton Sophomore Clinic

  10. Your Resources • The CCS PIC C Compiler MCU • Only for mid-range PIC microcontrollers • Other compilers for high-end PIC microcontrollers • The file 16F876A.h in your compiler • The PIC Project Board Programmer-Debugger • Available in room 237 • Power supplies and lab equipment help you integrate your project • Books • The PIC MCU C Compiler Reference Manual, comes with the compiler • The C Programming Language, 2nd Edition Kernighan & Ritchie, Prentice Hall (1988), ISBN-10: 0131103628, ISBN-13: 978-0131103627, about $40 from Amazon • PIC micro MCU C, Nigel Gardner, about $15 from Microchip, Inc. Sophomore Clinic

  11. The PIC PWM • Based on Timer 2 • Three timer stages • First stage divides main clock by 1, 4, or 16 • Second stage divides by user-specified number • Third stage divides by 1-16 and resets timer • Total PWM period (1/frequency) is total count • PWM operates by turning on an output pin for a user-specified number of main clock ticks Sophomore Clinic

  12. Timer 2 Setup • User call in CCS C setup_timer_2(T2_DIV_BY_nn, period, postscale) • The nn may be 1, 4, or 16 • The period is 0 to 255 • The postscale is 1 to 16 Sophomore Clinic

  13. How the PWM Controls Power • The PWM has a cycle of T2_Ticks clocks • Use a C call set_pwmn_duty(d_clocks) • The number n may be 1 or 2 • Each cycle is ON for n clocks • The PIC 16F876A ADC is 10 bits • Scale ADC output so that full scale is T2_Ticks • Thus T2_Ticks must be 210 = 1024 or greater Sophomore Clinic

  14. One Way for Timer 2 Usage Processor Clock of 4 MHz, period is 255, postscale is 1 Sophomore Clinic

  15. The H-Bridge Driver • PWM output is read back into the processor • PWM output is the drive signals for two of the four H-bridge MOSFETs • The other two signals are zero • Which are used depends on direction of motor • PWM bits are put into proper position in control byte with shifts • Combine to form 8-bit H-bridge drive word • Output on pins B0 through B7 Sophomore Clinic

  16. The Scratchy Button Contact • A pushbutton can give a scratchy waveform • One solution • Keep track of what the last pushbutton signal was • Log a button push only when you see it change from unpushed to pushed on a pass through the program • Hardware H-bridge allows a loop delay • Build the H-bridge drive word with external hardware to decouple the processing loop speed from the PWM waveform • Add a delay of a few milliseconds at the end of the loop • Will see only one button push as the button makes contact • Use a Schmidt trigger on each pushbutton • Keep a longer track record for pushbutton bounce logic Sophomore Clinic

  17. Structure of the Program • Context • #include <16F876A.h> • #define, #device and #use statements • Setup • Calls to PIC-specific functions in CCS PIC C • Set up ADC,s PWM, I/O ports • Loop • Read the thumbwheels • Set the PWM duty cycles • Read the pushbuttons and toggle forward-back, up-down, magnet • Make the H-bridge word and write it • Pause? Sophomore Clinic

  18. Pin-out of PIC 16F876A Pins Used in Project Shown in Red Sophomore Clinic

  19. Things to Do to Make It Work • Implement hardware H-bridge drive, OR: • Work out the pin-outs to pushbuttons, H-bridge as connected • Make the initial toggles what you expect • Forward-backward • Up-down • Magnet on-off • Change the program, not the wires • Software H-bridge drive may be used for a prototype; may be smooth enough to use Sophomore Clinic

  20. The Initial State • What is the crane doing when the power is applied? • Forward-back is forward • Up-down is down • Magnet is off • What about the thumbwheels? • If they are turned up, the crane and lift will move • You can add logic to keep things off until both thumbwheels are zero. Sophomore Clinic

  21. What About Pushbutton Bounce? • Some elementary logic is already there • Toggles forward-back, etc. only when pushbutton transitions from un-pushed to pushed between loops • If the loop is fast and the button is slow, this can happen more than once • One solution: Add a delay in the loop • The controls only need to be read 10 to100 times a second • Experimentation may give you a good delay number that provides robust key bounce performance with the pushbuttons • Another solution • Keep a history of several pushbutton outputs • Average them or perform logic to provide robust determination of when to toggle the bits Sophomore Clinic

  22. The C Program Code • Environment • Include the processor definitions for the 16F876A • Define the constants • Specific compiler directives • Initialization • Declare all the variables and initialize them • Set up ADCs, PWM, and I/O • Processing loop • Read the thumbwheels and pushbuttons • Formulate the H-bridge driver outputs • Output the H-bridge and magnet drive outputs • Repeat Sophomore Clinic

  23. Environment #include <16F876A.h> #fuses HS,NOWDT,NOPROTECT,NOLVP #device ADC=10 //10 bits right justified in a 16 bit word #use delay(clock=10000000) //Put your clock rate here; 4 MHz == 4000000 //#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) //RS-232 not used #define scale_shift 4 //log2(T2_Ticks/2^(ADC bits)); see below #define speedpos 0 //Propulsion is bottom 4 bits #define liftpos 4 //Lift is top 4 bits Sophomore Clinic

  24. Initialization: Variable Declarations void main() { long int speed, lift, adc_out; //long int is 16 bits in CCS PIC C MCU compiler int speed_Hbridge, lift_Hbridge, Hbridge, lsb; //8 bits short forward, lift_up, magnet_on; //One bit short pbp, pbp_state, pbl, pbl_state, pbm, pbm_state; //Anti-bounce logic Sophomore Clinic

  25. Initialization: Set Up I/O, ADCs //SETUP SET_TRIS_B(0b00000000); //B pins are H-bridge driver -- all outputs SET_TRIS_C(0b11100000); //Buttons input on C7, C6, C5, magnet drive on C4 setup_adc_ports( ALL_ANALOG ); //Inputs are A0 A1 A2 A3 A5, ref is 5 V setup_adc( ADC_CLOCK_INTERNAL ); //Internal clock, or box crystal if there Sophomore Clinic

  26. Initialization: PWM Setup //Set up PWM clock, which is always timer 2 //PWM frequency determination //We will try 244 Hz. If it is too low and motor buzzes, try 977 Hz setup_timer_2(T2_DIV_BY_16, 255, 1); //The ADC output must be scaled so that 2^10=1024 is scaled to the PWM period //See "#define scale_shift 4" above setup_ccp1(CCP_PWM); //Configure CCP1 as a PWM setup_ccp2(CCP_PWM); //Configure CCP2 as a PWM Sophomore Clinic

  27. Initialization: Variable Initialization //VARIABLE INITIALIZATIONS pbp=1; //Initialize all pushbuttons as off, with pullups pbl=1; pbm=1; pbp_state=1; pbl_state=1; pbm_state=1; forward=1; //Initially, move forward lift_up=0; //Initially, hook moves down magnet_on=0; //Initially, magnet is off Sophomore Clinic

  28. Processing Loop: Read Thumbwheels do { //Read speed thumbwheel set_adc_channel(0); //Propulsion thumbwheel is port A0 delay_us(10); //A small delyay is required before a read adc_out=Read_ADC(); lsb=bit_test(adc_out,0);//Extend LSB on scaling shift speed = ((adc_out+lsb)<<scale_shift)-lsb; set_pwm1_duty(speed); //Output thumbwheel data to PWM for propulsion speed_Hbridge=input_state(PIN_C2); //Set drive for first MOSFET speed_Hbridge|=(speed_Hbridge)<<1; //Second MOSFET Propulsion thumbwheel shown; lift thumbwheel similar Sophomore Clinic

  29. Processing Loop: Read Pushbuttons //Check push button for propulsion direction reversal pbp=input_state(PIN_C7); //Read propulsion pushbutton (0 == pushed) forward^=(!pbp & pbp_state); //Toggle if transition from 1 to 0 pbp_state=pbp; output_bit(PIN_C3,forward); //Put propulsion toggle on pin C3 //Check push button for crane lift direction reversal pbl=input_state(PIN_C6); lift_up^=(!pbl & pbl_state); pbl_state=pbl; output_bit(PIN_C0,lift_up); //Put lift toggle on pin C0 //Check magnet push-on, push-off button pbm=input_state(PIN_C5); magnet_on^=(!pbm & pbm_state); pbm_state=pbm; output_bit(PIN_C4,magnet_on); //Put magnet toggle on pin C4 Sophomore Clinic

  30. Processing Loop: Build H-Bridge Control, Output Drive Signals //Build and output H-bridge output word //This is probably too slow for smooth motor speed control //because the waveform update granularity is the loop time, //not the Timer 2 comparator, so a hardware solution is best. speed_Hbridge = (!forward) ? speed_Hbridge : speed_Hbridge<<2; lift_Hbridge = (!lift_up) ? lift_Hbridge : lift_Hbridge<<2; Hbridge = (speed_Hbridge << speedpos) | (lift_Hbridge << liftpos); output_B(Hbridge); //Output the H-bridge bits to bus B } while (TRUE); } Far too slow for 10 bit accuracy Sophomore Clinic

  31. Issues • Use of processing loop to update PWM waveform • Slower than Timer 2 granularity • 10 bits accuracy will not be achieved • Accuracy of 5 bits is about 3% and may be enough if that is achieved • Data is available to provide a hardware solution • Key bounce logic is just a start • Will probably need more • Hardware solution for H-bridge drive will allow adding a delay at the end of the loop • Software solution will require keeping track of the last several pushbutton inputs and taking an average, or similar logic Sophomore Clinic

  32. Other Things You Can Do • Add a pushbutton that stops everything • Add logic to program • Reset the PIC microcontroller • Make the forward-back and up-down pushbuttons up-stop-down instead • Add a button that is pushed when the crane gets to the end of the rails that stops the propulsion, or reverses it • Make the relationship between the thumbwheel and the motors something other than linear to improve control and feel • Whatever you can think of Sophomore Clinic

  33. Summary • You have your project ready to go • Hand unit • Crane motors • PIC and H-bridge boards • Electromagnet • Put your PIC board on a programmer-debugger in room 237 • Run the program and wring out the glitches • Use your own ideas to improve the program and your project Sophomore Clinic

More Related