Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rowan Hall 238A April 02, 2006 Sophomore Clinic ENGR 01-202 5, CRN 20686 Introduction to PIC Programming in C.

Similar presentations

Presentation on theme: "Rowan Hall 238A April 02, 2006 Sophomore Clinic ENGR 01-202 5, CRN 20686 Introduction to PIC Programming in C."— Presentation transcript:

1 Rowan Hall 238A April 02, 2006 Sophomore Clinic ENGR 01-202 5, CRN 20686 Introduction to PIC Programming in C James K. Beard, Ph.D.

2 Slide 2 Sophomore Clinic April 02, 2007 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?

3 Slide 3 Sophomore Clinic April 02, 2007 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

4 Slide 4 Sophomore Clinic April 02, 2007 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 dont need for SC Just right for our project

5 Slide 5 Sophomore Clinic April 02, 2007 The PIC 16F876A

6 Slide 6 Sophomore Clinic April 02, 2007 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

7 Slide 7 Sophomore Clinic April 02, 2007 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

8 Slide 8 Sophomore Clinic April 02, 2007 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

9 Slide 9 Sophomore Clinic April 02, 2007 How Its 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

10 Slide 10 Sophomore Clinic April 02, 2007 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, 2 nd 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.

11 Slide 11 Sophomore Clinic April 02, 2007 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

12 Slide 12 Sophomore Clinic April 02, 2007 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

13 Slide 13 Sophomore Clinic April 02, 2007 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 2 10 = 1024 or greater

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

15 Slide 15 Sophomore Clinic April 02, 2007 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

16 Slide 16 Sophomore Clinic April 02, 2007 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

17 Slide 17 Sophomore Clinic April 02, 2007 Structure of the Program Context #include #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?

18 Slide 18 Sophomore Clinic April 02, 2007 Pin-out of PIC 16F876A Pins Used in Project Shown in Red

19 Slide 19 Sophomore Clinic April 02, 2007 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

20 Slide 20 Sophomore Clinic April 02, 2007 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.

21 Slide 21 Sophomore Clinic April 02, 2007 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

22 Slide 22 Sophomore Clinic April 02, 2007 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

23 Slide 23 Sophomore Clinic April 02, 2007 Environment #include #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

24 Slide 24 Sophomore Clinic April 02, 2007 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

25 Slide 25 Sophomore Clinic April 02, 2007 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

26 Slide 26 Sophomore Clinic April 02, 2007 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

27 Slide 27 Sophomore Clinic April 02, 2007 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

28 Slide 28 Sophomore Clinic April 02, 2007 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

29 Slide 29 Sophomore Clinic April 02, 2007 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

30 Slide 30 Sophomore Clinic April 02, 2007 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

31 Slide 31 Sophomore Clinic April 02, 2007 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

32 Slide 32 Sophomore Clinic April 02, 2007 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

33 Slide 33 Sophomore Clinic April 02, 2007 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

Download ppt "Rowan Hall 238A April 02, 2006 Sophomore Clinic ENGR 01-202 5, CRN 20686 Introduction to PIC Programming in C."

Similar presentations

Ads by Google