1 / 81

Chapter 8: 68HC11 Timer Functions

Chapter 8: 68HC11 Timer Functions. The 68HC11 Microcontroller. Han-Way Huang. Minnesota State University, Mankato. Applications that Requires a Dedicated Timer System - time delay creation and measurement - period measurement - event counting - time-of-day tracking

pabla
Télécharger la présentation

Chapter 8: 68HC11 Timer Functions

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. Chapter 8: 68HC11 Timer Functions The 68HC11 Microcontroller Han-Way Huang Minnesota State University, Mankato

  2. Applications that Requires a Dedicated Timer System - time delay creation and measurement - period measurement - event counting - time-of-day tracking - periodic interrupt generation to remind the processor to perform routine tasks - waveform generation - etc. • A Summary of the 68HC11 Timer Functions • 1. Main timer • 16-bit non-stop timer • read-only after reset • 2. Input capture function • three channels -- 1 to 3 • each channel has a 16-bit latch, edge-detection logic, flag bit, and interrupt logic • will load the current main timer value into the input capture register when the selected • signal edge is detected • can be used to measure the signal frequency, period, and pulse width and as time reference

  3. 3. Output compare functions • A (E) series members have five (four/five) channels (OC1…OC5) • each channel has a 16-bit comparator, 16-bit register, action pin, interrupt request circuit, • forced-compare function • continuously compare the value of the 16-bit compare register with that of the main timer • and may optionally trigger an action on a pin, generate an interrupt • is often used to create a time delay and generate a waveform • 4. Real-time interrupt • generates periodic interrupts when enabled • interrupt period is programmable • 5. Computer operating properly (COP) • discussed in Chapter 6 • 6. Pulse accumulator • has an 8-bit counter • has two operation modes • can be used to measure events, frequency, or the duration of a pulse width

  4. The Free-Running Main Timer (TCNT) - The main timer is cleared to 0 on reset and is read-only except in test mode. - The timer counter register is meant to be read by a 16-bit read instruction such as LDD or LDX. - The block diagram is shown in Figure 8.1.

  5. Registers related to Main Timer • Timer counter: TCNT • Timer flag register 2: TFLG2 • Timer mask register 2: TMSK2 • Those bits related to main timer operation in TFLG2 and TMSK2 are in boldface. • Timer counter is meant to be read using a double-byte read instruction such as LDD or LDX. • If the user accesses TCNT with two 8-bit reads, the result might not be correct, because the • lower byte of TCNT would be incremented when the upper byte is accessed.

  6. The prescale factor for the main timer is selected by bits 1 and 0 of the timer mask register 2 as shown in Table 8.1. Example 7.1 What values will be in A and B after execution of the following three instructions if TCNT contains $5EFE when the upper byte is accessed ? Assume the bits PR1 and PR0 of TMSK2 are 00. regbas equ $1000 TCNTH equ $0E TCNTL equ $0F ldx #regbas ldaa TCNTH,X ; read the upper byte of TCNT ldaa TCNTL,X ; read the lower byte of TCNT

  7. Solution: - The main timer prescale factor is 1 and hence the E clock is the clock input to TCNT. - The instruction LDAA TCNTH,X loads the upper byte (value is $5E) of TCNT into A. - The instruction LDAB TCNTL,L takes 4 E clock cycles to execute. Therefore, TCNT will have been incremented by 4 to $5F02. The accumulator B will receive the value $02. This is not what we expect. If the instruction LDD TCNT,X is executed, then A and B will contain $5E and $FE respectively.

  8. Input Capture Functions - Physical time is often represented by the contents of the main timer. - The occurrence of an event is represented by a signal edge (rising or falling edge). - The time when an event occurs can be recorded by latching the count of the main timer when a signal arrives. - The 68HC11 has three input capture channels (IC1, IC2, & IC3) to implement this operation. - Each input capture channel has a 16-bit input capture register, a flag, edge-detection logic, and interrupt request circuit.

  9. - The edge to be captured is selected by programming the register TCTL2. For example, the following instruction sequence captures the rising edge of the signal applied at PA0 (IC3): regbas equ $1000 TCTL2 equ $21 ; offset of TCTL2 from regbas ldx #regbas bclr TCTL2,X %00000010 ; clear bit 1 to 0 bset TCTL2,X %00000001 ; set bit 0 to 1

  10. 1. timer mask register 1 (TMSK1): 7 6 5 4 3 2 1 0 OC1I OC2I OC3I OC4I OC5I IC1I IC2I IC3I 0 0 0 0 0 0 0 0 Registers related to input capture - the lowest three bits (bits 2 to 0) of this register enable/disable the interrupt from the proper input capture channel - the upper five bits (bits 7 to 3) of this register enable/disable the interrupt from the corresponding output compare channels 2. timer flag register 1 (TFLG1): - the lowest three bits (bits 2 to 0) of this register are input capture flags - the arrival of a signal edge will set one of the input capture flags - the upper five bits (bits 7 to 3) of this register are output compare flags

  11. How to clear a timer flag bit? write a 1 to the flag bit to be cleared Method 1. use the BCLR instruction with a 0 at the bit position (s) corresponding to the flag (s) to be cleared. For example, BCLR TFLG1,X $FE will clear the IC3F flag. (Assume the index register contains $1000 and TFLG1 = $23) Method 2. load an accumulator with a mask that has a 1 (or 1s) in the bit (s) corresponding to the flag (s) to be cleared; then write this value to TFLG1 or TFLG2. For example, LDAA #$01 STAA TFLG1,X will clear the IC3F flag.

  12. Applications of Input Capture function - Event arrival time recording - Period measurement: the input capture function captures the main timer values corresponding to two consecutive rising or falling edges - Pulse width measurement: capture the rising and falling edges

  13. - Interrupt generation: three input capture functions can be used as three edge-sensitive interrupt sources. - Event counting: by counting the number of signal edges arrived during a period - Time reference: often used in combination with an output compare function

  14. Duty Cycle Measurement Phase Difference Measurement

  15. Example 8.3 Use the input capture channel IC1 to measure the period of an unknown • signal. The period is known to be shorter than 32 ms. Write a program to set up IC1 • to measure its period. • Solution: • two versions are available. • The polling method is shown • in Figure 8.15.

  16. Assembly Program for Period Measurement (Polling Method) REGBAS EQU $1000 TLFG1 EQU $23 TIC1 EQU $10 TCTL2 EQU $21 IC1rise EQU $10 ORG $00 edge1 RMB 2 period RMB 2 ORG $C000 LDX #REGBAS BCLR TFLG1,X $FB ; clear IC1F flag LDAA #IC1rise STAA TCTL2,X ; capture rising edge BRCLR TFLG1,X $04 * ; wait for the first rising edge LDD TIC1,X STD edge1 ; save the first edge BCLR TFLG1,X $FB ; clear IC1F flag BRCLR TFLG1,X $04 * ; wait for the second edge LDD TIC1,X SUBD edge1 STD period ; save the period END

  17. C Program for Period Measurement (Polling method) #include <hc11.h> #include <stdio.h> main ( ) { unsigned int edge1, period; TFLG1 = 0x04; /* clear IC1F flag TCTL2 = 0x10; /* configure to capture rising edge */ while (!(TFLG1 & 0x04)); /* wait for the arrival of the first rising edge */ edge1 = TIC1; /* save the arrival time of the first rising edge */ TFLG1 = 0x04; while (!(TFLG1 & 0x04)); /* wait for the arrival of the second rising edge */ period = TIC1 – edge1; printf(“\n The period of the signal is %d E clock cycles. \n”, period); return 0; }

  18. Interrupt-driven Method for Period Measurement

  19. REGBAS EQU $1000 ; base address of the I/O register block TFLG1 EQU $23 ; offset of TFLG1 from regbas TMSK1 EQU $22 ; offset of TMSK1 from regbas TIC1 EQU $10 ; offset of TIC1 from regbas TCTL2 EQU $21 ; offset of TCTL2 from regbas IC1rise EQU $10 ; value to select the rising edge of IC1 to capture IC1I EQU $04 ; mask to select the IC1 bit in TMSK1 IC1FM EQU $FB ; mask to clear IC1F using the BCLR instruction ORG $0000 edge_cnt RMB 1 ; edge count edge1 RMB 2 ; captured first edge period RMB 2 ; period in number of E clock cycles ORG $E8 ; IC1 interrupt jump table entry on the EVB JMP IC1_ISR ; “ ORG $C000 ; starting address of the main program LDS #$DFFF ; set up stack pointer LDX #REGBAS LDAA #IC1rise ; select to capture the rising edge of IC1 STAA TCTL2,X ; “ BCLR TFLG1,X IC1FM ; clear the IC1F flag LDAA #2

  20. STAA edge_cnt ; initialize edge count to 2 BSET TMSK1,X IC1I ; enable IC1 interrupt CLI ; enable interrupt to the 68HC11 wait TST edge_cnt ; edge_cnt = 0? BNE wait LDD TIC1,X ; get the second edge time SUBD edge1 ; take the difference of edge 1 and 2 STD period ; save the period . . . # IC1 interrupt service routine in the following IC1_ISR LDX #regbas BCLR TFLG1,X IC1FM ; clear the IC1F flag DEC edge_cnt BEQ skip ; is this the second edge? LDD TIC1,X ; save the first edge time in memory STD edge1 ; “ skip RTI END

  21. C Language Program for Period Measurement (Interrupt-Driven Method) #include <hc11.h> #include <stdio.h> int edge_cnt unsigned int edge1, period; void IC1_ISR ( ); main ( ) { *(unsigned char *)0xe8 = 0x7E; /* $7E is the opcode of JMP */ *(void (**)())0xe9 = ICI_ISR; /* set up pseudo vector entry of IC1 */ TFLG1 = 0x04; /* clear IC1F flag edge_cnt = 2; TCTL2 = 0x10; /* prepare to capture the rising edge */ TMSK1 |= 0x04; /* enable IC1 interrupt locally */ INTR_ON ( ); /* enable interrupt globally */ while (edge_cnt); period = TIC1 – edge1; printf(“\n The period is %d E clock cycles. \n”, period); return 0; }

  22. #pragma interrupt_handler IC1_ISR ( ) void IC1_ISR ( ) { TFLG1 = 0x04; /* clear IC1F flag */ if (edge_cnt == 2) edge1 = TIC1; /* save the first edge */ -- edge_cnt; }

  23. Example 8.4 Write a subroutine to measure the pulse width of an unknown signal connected to the IC1 pin. Return the pulse width in D. The main timer prescale factor is 1. The pulse width of the unknown signal is known to be shorter than 32.67 ms. Solution: - capture the rising edge on the IC1 pin - capture the falling edge on the IC1 pin - take the difference of two captured values regbas EQU $1000 ; base address of the I/O register block TFLG1 EQU $23 ; offset of TFLG1 from regbas TIC1 EQU $10 ; offset of TIC1 from regbas TCTL2 EQU $21 ; offset of TCTL2 from regbas IC1rise EQU $10 ; value to select the rising edge of IC1 IC1fall EQU $20 ; value to select the falling edge of IC1 IC1F EQU $04 ; a mask to select the IC1F flag temp EQU $00 ; offset of temp from the top of the stack pul_width PSHX PSHY DES ; allocate two bytes for local variable temp DES TSY LDX #regbas

  24. LDAA #IC1rise ; configure TCTL2 to capture the rising edge of IC1 STAA TCTL2,X ; “ LDAA #IC1F ; clear IC1F flag STAA TFLG1,X ; “ rise BRCLR TFLG1,X IC1F rise ; wait for the arrival of the rising edge LDD TIC1,X ; save the first edge STD temp,Y ; “ LDAA #IC1fall ; configure to capture the falling edge of IC1 STAA TCTL2,X ; “ LDAA #IC1F ; clear IC1F flag STAA TFLG1,X ; “ fall BRCLR TFLG1,X IC1F fall ; wait for the arrival of the falling edge LDD TIC1,X ; get the captured time of the second edge SUBD temp,Y INS INS PULY PULX RTS

  25. Measuring the period or pulse width of a slow signal (longer than 32.76 ms) We need to keep track of the number of times that main timer overflows. Let ovcnt = main timer overflow count diff = difference of two edges edge1 = the captured time of the first edge edge2 = the captured time of the second edge Case 1: edge2 ³ edge1 period (or pulse width) = ovcnt × 216 + diff Case 2: edge2 < edge1 period (or pulse width) = (ovcnt - 1) × 216 + diff The main timer overflows at least once in this case.

  26. Example 8.5 Write a program to measure the period of an unknown signal, which may be longer than 216 E cycles using the IC1 input capture channel. Solution: The logic flow of the program is shown in Figure 8.17.

  27. regbas EQU $1000 ; base address of I/O register block TFLG1 EQU $23 ; offset of TFLG1 from regbas TIC1 EQU $10 ; offset of TIC1 from regbas TCTL2 EQU $21 ; offset of TCTL2 from regbas TMSK1 EQU $22 ; offset of TMSK1 from regbas TMSK2 EQU $24 ; offset of TMSK2 from regbas IC1rise EQU $10 ; value to select the rising edge of IC1 ORG $0000 edge1 RMB 2 ; captured time of the first edge ov_cnt RMB 2 ; main timer overflow count period RMB 2 ; period of the unknown signal ORG $00D0 ; setup timer overflow interrupt vector jump table entry JMP tov_ISR ; on EVB ORG $C000 LDS #$DFFF ; set up stack pointer SEI ; disable all maskable interrupts to the 68HC11 CLR ov_cnt ; initialize overflow count to 0 CLR ov_cnt+1 ; “ LDX #regbas

  28. LDAA #IC1rise ; select to capture the rising edge STAA TCTL2,X ; “ BCLR TFLG1,X $FB ; clear IC1F flag BCLR TMSK1,X $FF ; disable all input capture and output compare interrupts BRCLR TFLG1,X $FB * ; wait for the arrival of the first edge BCLR TFLG2,X $7F ; clear TOF flag BSET TMSK2,X $80 ; enable timer overflow interrupt CLI ; enable interrupt to the 68HC11 LDD TIC1,X ; save the captured time of the first edge STD edge1 ; “ BCLR TFLG1,X $FB ; clear IC1F flag BRCLR TFLG1,X $04 * ; wait for the arrival of the second edge LDD TIC1,X ; compute the difference of edge2 and SUBD edge1 ; edge1

  29. STD period ; “ BCC next ; is second edge smaller? LDD ov_cnt ; decrement overflow count if second edge is smaller SUBD #1 ; “ STD ov_cnt ; “ next … tov_ISR LDX #regbas BCLR TFLG2,X $7F ; clear TOF flag LDD ov_cnt ; increment timer overflow count ADDD #1 ; “ STD ov_cnt ; “ RTI END

  30. C Program for Measuring the Period of a Slow Signal #include <hc11.h> #include <stdio.h> unsigned edge1, overflow; unsigned long period; void TOV_ISR ( ); main ( ) { *(unsigned char *)0xd0 = 0x7E; *(void(**) ( ))0xd1 = TOV_ISR; /* set up TOV pseudo vector entry */ INTR_OFF ( ); overflow = 0; TFLG1 = 0xFF; /* clear all output-compare and input-capture flags */ TFLG2 = 0x80; /* clear TOF flag */ TCTL2 = 0x10; /* configure to capture IC1’s rising edge */ TMSK1 = 0x00; /* disable all input capture and output compare interrupts */ while (!(TFLG1 & 0x04)); /* wait for the arrival of first rising edge on IC1 */ TFLG1 = 0x04; edge1 = TIC1; /* save the first rising edge */

  31. TMSK2 = 0x80; /* enable timer overflow interrupt */ INTR_ON ( ); /* “ */ while (!(TFLG1 & 0x04)); /* wait for the second rising edge */ if (TIC1 < edge1) /* if the second edge is smaller, then overflow --; /* decrement the overflow count */ period = overflow * 65536; /* combine the result */ period +=TIC1 - edge1; /* “ */ printf(“\n The period is %d E clock cycles. \n”, period); return 0; } #pragma interrupt_handler TOC_ISR ( ) void TOV_ISR ( ) { TFLG2 = 0x80; /* clear TOF flag */ overflow ++; }

  32. Output Compare Functions - five output compare channels: OC1-OC5 - port A pins PA7-PA3 are associated with output compare channels OC1-OC5 respectively - Each output compare channel consists of 1. a 16-bit comparator 2. a 16-bit compare register (TOCx, x = 1,…,5) 3. an output action pin 4. an interrupt request circuit 5. a forced-compare function (FOCx, x = 1,…,5) 6. control logic

  33. - To use an output compare function, 1. make a copy of the main timer 2. add to this copy a value equal to the desired delay 3. store the sum onto an output-compare register - The actions that can be activated on an output compare pin include 1. pull up to high 2. pull down to low 3. toggle The action is determined by the timer control register 1 (TCTL1):

  34. Example 8.6 Generate a 1KHz digital waveform with 40% duty cycle from output compare pin OC2. Use the polling method to check the success of the compare operation. The frequency of the E clock is 2 MHz and the prescale factor to the main timer is 1. Solution: A 1KHz digital with 40% duty cycle has 400 ms high and 600 ms low in one period. The flowchart to generate this waveform is shown in Figure 8.20.

  35. regbas equ $1000 ; base address of I/O register block PORTA equ $00 ; offset of PORTA from regbas TOC2 equ $18 ; offset of TOC2 from regbas TCNT equ $0E ; offset of TCNT from regbas TFLG1 equ $23 ; offset of TFLG1 from regbas toggle equ $40 ; value to select the toggle action lotime equ 1200 ; value to set low time to 600 ms hitime equ 800 ; value to set high time to 400 ms org $C000 ldx #regbas bset PORTA,X $40 ; set OC2 pint to high bclr TFLG1,X $BF ; clear OC2F flag ldaa #toggle ; select output compare action to be staa TCTL1,X ; toggle ldd TCNT,X ; start an OC2 operation which toggles the OC2 pin addd #hitime ; with a delay of 800 E clock cycles std TOC2,X ; “ high brclr TFLG1,X $40 high ; wait until OC2F is set to 1 bclr TFLG1,X $BF ; clear OC2F flag ldd TOC2,X ; start another OC2 operation which toggles the OC2 pin addd #lotime ; with a delay of 1200 E cycles std TOC2,X ; “

  36. low brclr TFLG1,X $40 low ; wait until OC2F is set to 1 bclr TFLG1,X $BF ; clear OC2F flag ldd TOC2,X ; start another OC2 operation which toggles the OC2 pin addd #hitime ; with a delay of 800 E cycles std TOC2,X ; “ bra high end

  37. In C language, #include <hc11.h> main ( ) { PORTA |= 0x40; /* set OC2 pint to high */ TCTL1 = 0x40; /* select toggle as the OC2 pin action */ TOC2 = TCNT + 800; /* start an OC2 operation with 800 E cycles as the delay */ TFLG1 = 0x40; /* clear OC2F flag */ while (1) { while (!(TFLG1 & 0x40)); /* wait for 400 ms */ TFLG1 = 0x40; TOC2 += 1200; /* start next OC2 operation with 1200 E cycles as delay */ while (!(TFLG1 & 0x40)); /* wait for 600 ms */ TFLG1 = 0x40; TOC2 += 800; } return 0; }

  38. Example 8.7 Write a function to generate one second delay using the OC2 function. The E clock is 2 MHz and the prescale factor to the main timer is 1. Solution: A one-second delay can be created by performing 40 OC2 output compare operations. Each OC2 compare operation creates 25 ms delay. A memory location is allocated to keep track of the number of OC2 operations that have been performed.

  39. regbas EQU $1000 ; base address of I/O register block TOC2 EQU $18 ; offset of TOC2 from regbas TCNT EQU $0E ; offset of TCNT from regbas TFLG1 EQU $23 ; offset of TFLG1 from regbas dly25ms EQU 50000 ; the number of E cycles to generate 25 ms delay onesec EQU 40 ; number of OC2 operations to be performed oc2_cnt EQU 0 ; offset of oc2_cnt from the top of the stack delay_1s PSHX PSHY DES TSY LDX #regbas BCLR TFLG1,X $BF ; clear OC2F flag LDAA #40 STAA oc2_cnt,Y ; initialize oc2_cnt LDD TCNT,X wait ADDD #dly25ms STD TOC2,X ; start an OC2 operation with 25 ms delay BRCLR TFLG1,X $40 * ; wait until OC2F flag is set BCLR TFLG1,X $BF ; clear OC2F flag DEC oc2_cnt,Y

  40. BEQ exit LDD TOC2,X BRA wait exit INS PULY PULX RTS C function to generate one second delay, void delay_1s ( ) { unsigned char oc2_cnt; oc2_cnt = 100; /* prepare to perform 100 OC2 operation */ TFLG1 = 0x40; /* clear OC2F flag */ TOC2 = TCNT + 20000; /* start an OC2 operation with 20000 E cycles as the delay */ while (oc2_cnt) { while(!(TFLG1 & 0x40)); /* wait for 10 ms */ TFLG1 = 0x40; -- oc2_cnt; TOC2 += 20000; /* start the next OC2 operation */ } }

  41. Example 8.8 Suppose an alarm device is already connected properly and the subroutine to turn on the alarm is also available. Write a program to implement the alarm timer--it should call the given alarm subroutine when the alarm time is reached. Solution: - Use OC2 to create the delay. - Perform OC2 operations with a delay of 20 ms - Perform 3000 such operations to create a delay of 1 minute. - Check the alarm time every second. - Call the alarm routine if the alarm time is reached. - Enable OC2 interrupt

  42. regbas equ $1000 TOC2 equ $18 TCNT equ $0E TFLG1 equ $23 TMSK1 equ $22 dly20ms equ 40000 ; number of E cycles equivalent to 20 ms delay one_min equ 3000 ; number of OC2 operations to be created to * ; generate 1 minute delay org $0000 hours rmb 1 minutes rmb 1 ticks rmb 2 alarm rmb 2 routine fdb start_alarm ; starting address of the alarm routine org $00DC ; interrupt jump table entry for OC2 on EVB jmp oc2_ISR org $C000 lds #$DFFF ; set up stack pointer sei ; disable interrupt before setup is done ldd #one_min ; initialize the OC2 count to generate std ticks ; one minute delay

  43. ldx #regbas bclr TFLG1,X $BF ; clear OC2F flag bset TMSK1,X $04 ; enable OC2 interrupt ldd TCNT,X ; start an OC2 operation with addd #dly20ms ; 20 ms delay std TOC2,X ; “ cli ; enable interrupt to the 68HC11 forever bra forever ; loop forever to wait for interrupt oc2_ISR ldx #regbas bclr TFLG1,X $BF ; clear OC2 flag ldd TOC2,X ; start the next OC2 operation with addd #dly20ms ; 20 ms delay std TOC2,X ; “ ldy ticks ; decrement the minute count dey ; “ sty ticks ; “ bne case2 ; is one minute expired? ldd #one_min ; reinitialize the one-minute counter std ticks ; “ ldd hours ; load the hours and minutes

  44. INCB CMPB #60 ; is it time to increment the hour? BNE case1 ; no need to update hour digits yet CLRB ; reset minutes to 0 INCA ; increment the hour CMPA #24 ; it is time to reset hours to 0? BNE case1 ; no need to reset hour yet CLRA ; reset hours to 00 case1 STD hours ; save the current time in memory CPD alarm ; reaches alarm time? BNE case2 LDX routine JSR 0,X ; call the alarm routine case2 RTI END

  45. Example 8.9 LED Flashing. Connect 8 LEDs to port B and flash these LEDs in the following way: 1. Light all LEDs for ¼ seconds and turn them off for ¼ seconds—repeat this pattern 4 times. 2. Light one LED at a time for one second—from the LED controlled by the most significant output port pin to the LED controlled by the least significant port pin. 3. Reverse the order of display in step 2. 4. Turn off all of the LEDs.

  46. unsigned char flas_tab [25][2] = {{0xFF, 25}, {0x00, 25}, {0xFF, 25} {0x00, 25}, {0xFF, 25}, {0x00, 25}, {0xFF, 25}, {0x00, 25}, {0x80, 100}, {0x40, 100}, {0x20, 100}, {0x10, 100}, {0x08,100}, {0x04, 100}, {0x02, 100}, {0x01, 100}, {0x01, 100}, {0x02, 100}, {0x04, 100}, {0x08, 100}, {0x10, 100}, {0x20, 100}, {0x40, 100}, {0x80, 100}, {0x00,100}}; void delay (char k); void flash ( ) { int i; for (i = 0; i < 25; i++) { PORTB = flash_tab [i][0]; delay (flash_tab[i][1]); } } void delay (char k) { TFLG1 = 0x40; /* clear OC2F flag */ TOC2 = TCNT + 20000; /* start an OC2 operation */ while (k) { while (!(TFLG1 & 0x40)); TFLG1 = 0x40; -- k; TOC2 += 20000; /* start a new OC2 operation */ } }

  47. Using OC1 to Control Multiple OC Functions - OC1 can control up to five OC pins - Specify the OC pins to be controlled by OC1 using the register OC1M. - Specify the value that any OCx (x = 1,…,5) pin to assume when the value of TOC1 equals TCNT using the OC1D register. - When a successful OC1 compare is made, each affected pin assumes the value of the corresponding bit of OC1D. - The OC1 (PA7) pin is bidirectional. For this pin to be controlled by OC1 function, it must be configured for output. The direction of PA7 pin is controlled by the bit 7 of the PACTL register. Set bit 7 of PACTL to 1 to configure PA7 pin for output.

  48. Example 8.9 Write values into OC1M and OC1D so that OC2 and OC3 pins will assume the values of 0 and 1 when the OC1 compare operation succeeds. Solution: - Set bits 6 and 5 of OC1M to 11 - Set bits 6 and 5 of OC1D to 01 regbas EQU $1000 OC1M EQU $0C OC1D EQU $0D LDX #regbas LDAA #%01100000 STAA OC1M,X LDAA #%00100000 STD OC1D … In C language OC1M = 0x60; OC1D = 0x20;

  49. Example 8.11 An application requires control of five valves. The first, third, fifth valves should be opened for five seconds and then closed for five seconds. When these three valves are closed, the second and fourth valves are opened, and vise versa. This process is repeated forever. Pins OC1,…,OC5 are used to control these five valves. When the OCx pin is high, the corresponding valve will be opened. Write a program to perform the operation. Solution: - The OC1 pin (same as PA7) is bi-directional, to use it to control a valve, it must be configured for output. Set the bit 7 of the PACTL register to 1. - Write the value %11111000 into OC1M so that OC1 function can control all five OC pins. - Write the value %10101000 into OC1D to open only valves 1,3,and 5. - Write the value %01010000 into OC1D to open only valves 2 and 4. - Perform 200 OC1 output compare operations with each operation creating 25 ms delay. regbas EQU $1000 PACTL EQU $26 OC1D EQU $0D OC1M EQU $0C TOC1 EQU $16 TCNT EQU $0E TFLG1 EQU $23 oc1m_in EQU $F8 ; value to initialize OC1M oc1d_in1 EQU $A8 ; value to initialize OC1D to open valves 1, 3, and 5

More Related