New version page

# MSU ECE 3724 - Timers

Documents in this Course

35 pages

13 pages

53 pages

14 pages

16 pages

24 pages

7 pages

22 pages

6 pages

11 pages

43 pages

8 pages

38 pages

18 pages

6 pages

9 pages

20 pages

8 pages

2 pages

4 pages

2 pages

5 pages

6 pages

14 pages

37 pages

2 pages

2 pages

6 pages

53 pages

63 pages

7 pages

20 pages

15 pages

36 pages

13 pages

15 pages

6 pages

22 pages

16 pages

9 pages

10 pages

7 pages

10 pages

162 pages

7 pages

11 pages

3 pages

5 pages

16 pages

12 pages

17 pages

38 pages

17 pages

9 pages

11 pages

11 pages

7 pages

13 pages

26 pages

39 pages

38 pages

42 pages

9 pages

8 pages

7 pages

38 pages

7 pages

13 pages

5 pages

4 pages

3 pages

6 pages

34 pages

10 pages

## This preview shows page 1-2-3-4-25-26-27-51-52-53-54 out of 54 pages.

View Full Document

End of preview. Want to read all 54 pages?

View Full Document
Unformatted text preview:

Timers• Recall that a timer on a μC is simply a counterBasic equations that we have used:Basic equations that we have used:General:Time = Ticks * Clock period of counterTicks = Time / Clock period of counterPIC24 ifiPIC24 specific:Time = Ticks * timer_prescale / FCYTicks = Time * FCY / timer prescaleTicks = Time * FCY / timer_prescaleV 0.9 1Period RegisterRecall that the timer period register controls the amount of time for setting the TxIF flag (controls the Timer roll-over time):TxIF period = (PRx + 1) * Prescale / FCYTo generate a periodic interrupt ofYmilliseconds we have done:To generate a periodic interrupt of Ymilliseconds, we have done:PRx = msToU16Ticks(Y, getTimerPrescale(TxCONbits)) – 1;The msToU16Ticks function converts Y milliseconds to Timer ticks; the decrement by 1 is needed because rollover time is PRx + 1.V 0.9 2Input Capture, Output Compare Modules• The Input Capture and Output Compare modules are peripherals that use Timer2 or Timer3 for time-related functions• The Output Compare module can generate pulses of a specified pulse width and periodTh I C d l i d f i•The Input Capture module is used for measuring pulse width and period (elapsed time)V 0.9 3Output Compare ModulePulses are generated on the OCx pin. The PIC24HJ32GP202 has two Output Compare modules (OC1, OC2). The OCxRS, OCxR registers V 0.9 4control when the output pin is affected by comparing against either Timer2 or Timer3 values.Output Compare ModesV 0.9 5Generating a Square Wave using Toggle Modeusing Toggle ModeSteps (assume using Timer2, and OC1)a. Configure Timer2 for a period that is greater than ½ period of the square wave.bIitOC1R itb. Init OC1R register OC1R = TimerTicks_onehalfSquareWavePeriod c Each match of OC1R register generates an OC1 interruptc. Each match of OC1R register generates an OC1 interrupt, toggles the OC1 pin.d In OC1 ISR assign new OC1R register value as:d. In OC1 ISR, assign new OC1R register value as:OC1R = OC1R + TimerTicks_onehalfSquareWavePeriodV 0.9 6Toggle ModeToggle Mode, OC1 square waveV 0.9 7Pulse Width Modulation (PWM)High pulse width varies (i.e., duty cycle varies)Current to LED is proportional to V 0.9 8period is fixedhigh pulse widthLED PWM CodeV 0.9 9LED PWM Code main()()While(1) loop just prints debugging information, work is actually done by Timer2 ISR that updates pulse width fromV 0.9 10actually done by Timer2 ISR that updates pulse width from ADC converted value.DC Motor Speed ControlpV 0.9 11Half-bridge DrivergIntegrates MOSFET/BJT drivers, protection diodes, switches.V 0.9 12g,p,ServosSidld dlildServos, widely used to steer model cars, airplanes, and boats, consist of a motor with gearing to reduce the output speed and increase output torque and a control circuit which ppqspins the motor until the motor’s position measured by the potentiometer matches the desired position specified by a pulse supplied to the servopulse supplied to the servo.=Cable:red=power (4 8 V–60V)V 0.9 13Cable:red power (4.8 V –6.0 V),black = ground,yellow = desired positionControlling servos (HS-311)g()The high time of a pulse gives the desired position of the servos. The pulse width must b 20 Ahi hti f15 thbe 20 ms. A high time of 1.5 ms moves the servo to its center position; smaller or larger values moves it clockwise or counter-From www.servocity.comclockwise from the center position. CCWMidCW0 ms 0.6 ms 1.5 ms2.4 ms20 msV 0.9 14Not all servos can cover this entire range!Changes to LED PWM Code to convert Potentiometer input to control servo#define PWM PERIOD 20000 // desired period in us#define PWM_PERIOD 20000 // desired period, in us#define MIN_PW 600 //minimum pulse width, in us#define MAX_PW 2400 //maximum pulse width, in usuint16 u16_minPWTicks, u16_maxPWTicks;void configOutputCapture1(void) {void configOutputCapture1(void) {u16_minPWTicks = usToU16Ticks(MIN_PW, getTimerPrescale(T2CONbits));u16_maxPWTicks = usToU16Ticks(MAX_PW, getTimerPrescale(T2CONbits));...rest of the function is the same...}}void _ISR _T2Interrupt(void) {uint32 u32_temp;_T2IF = 0; //clear the timer interrupt bit//update the PWM duty cycle from the ADC value//update the PWM duty cycle from the ADC valueu32_temp = ADC1BUF0; //use 32-bit value for range//compute new pulse width using ADC value// ((max - min) * ADC)/4096 + minu32_temp = ((u32_temp * (u16_maxPWTicks-u16_minPWTicks))>> 12) + 16 i PWTi k // >>12 i di id /4096u16_minPWTicks; // >>12 is same as divide/4096OC1RS = u32_temp; //update pulse width value AD1CON1bits.SAMP = 1; //start next ADC conversion for next interrupt }V 0.9 15Map the potentiometer voltage to equivalent pulse-width range of the servo.Controlling Multiple ServosThe PIC24HJ32GP202 has two output compare modules. How do you control more than two servos?control more than two servos? Solution:Do not dedicate an OCx output per servo, use just one Output CompareDo not dedicate an OCx output per servo, use just one Output Compare module. Use RBx pins to control the servos, and use the OCx ISR to update the RBx pins.V 0.9 16Multiple Servo Control Code#define PWM_PERIOD 20000 //in microseconds #define NUM SERVOS 4#define NUM_SERVOS 4#define SERVO0 _LATB2#define SERVO1 _LATB3#define SERVO2 _LATB13#define SERVO3 _LATB14_#define MIN_PW 600 //minimum pulse width, in us#define MAX_PW 2400 //minimum pulse width, in us#define SLOT_WIDTH 2800 //slot width, in usvolatile uint16 au16_servoPWidths[NUM_SERVOS];volatile uint8 u8 currentServo =0;volatile uint8 u8_currentServo =0; volatile uint8 u8_servoEdge = 1; //1 = RISING, 0 = FALLINGvolatile uint16 u16_slotWidthTicks = 0;void initServos(void) {uint8 u8_i;uint16 u16_initPW;CONFIG_RB2_AS_DIG_OUTPUT(); CONFIG_RB3_AS_DIG_OUTPUT();CONFIG_RB13_AS_DIG_OUTPUT(); CONFIG_RB14_AS_DIG_OUTPUT();u16_initPW = usToU16Ticks(MIN_PW + (MAX_PW-MIN_PW)/2, getTimerPrescale(T2CONbits));get e esca e( CO b ts));//config all servos for half maximum pulse widthfor (u8_i=0; u8_i<NUM_SERVOS; u8_i++) au16_servoPWidths[u8_i]=u16_initPW;SERVO0 = 0; //all servo outputs low initiallySERVO1 = 0; SERVO2 = 0; SERVO3 = 0; //outputs initially low16 l tWidthTi k T U16Ti k (SLOT WIDTH tTi P l (T2CONbit ))V 0.9 17u16_slotWidthTicks = usToU16Ticks(SLOT_WIDTH, getTimerPrescale(T2CONbits));}Multiple Servo Control Code (cont.)p()void configTimer2(void) {T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF| T2_32BIT_MODE_OFF |T2SOURCEINT| T2_SOURCE_INT| T2_PS_1_256 ; //1 tick = 1.6 us at FCY=40 MHz PR2 =

View Full Document
Unlocking...