AN913 STMICROELECTRONICS | Alldatasheet
Document overview
- Manufacturer or author: Provided By ALLDATASHEET.COM(FREE DATASHEET DOWNLOAD SITE)
- PDF pages: 12
Technical content
PWM GENERATION WITH THE ST62 16-BIT AUTO-RELOAD TIMER by 8-bit Micro Application Team INTRODUCTION This note presents how to use the ST62 16-bit Auto-Reload Timer (ARTimer) for generating a DTMF signal (Dual-Tone Multiple Frequency) with the PWM. In the example shown, the PWM output pin generates a DTMF to dial a telephone number. 1 16-BIT AUTO-RELOAD TIMER DESCRIPTION This timer is a 16-bit downcounter timer with prescaler (see Figure 1.). It includes auto-reload PWM, capture and compare capability with two input (CP1, CP2) and two output pins (OVF, PWM). It is controlled by the following registers: – Status control registers (8-bit) SCR1, SCR2, SCR3, SCR4 – CP Capture register pair (16-bits total): CPH and CPL – MASK register pair (16-bits total): MASKH and MASKL – TC 16-bit downcounter register – CMP Compare register pair (16-bits total): CMPH and CMPL – RLCP Reload/Capture register pair (16-bits total): RLCPH and RLCPL
The prescaler ratio can be programmed to choose the timer input frequency fint(see Table 1 ). Figure 1. 16-bit Auto-Reload Timer Block Diagram
2 PULSE WIDTH MODULATION (PWM) GENERATION
pare register CMP (see Figure 2.). and is reset or set or toggled when MASK&TC=MASK&CMP . CMP register value should not be equal to zero. prescaler ratio as low as possible to achieve maximum resolution. Table 1. Prescaler Programming Ratio
Figure 2. PWM Timer Operation with PWMPOL=0, MASK=FFFFh and RELOAD mode The PWM is selected with the set/reset mode. This frequency is the highest one in order to have 115 CPU cycles to calculate the tone. To calculate the sinus wave with only 8 bits, the sinus table gives data between 1 and 7Fh.
36 cycles are enough to load the CMP register after the overflow. This program dials a telephone number, for example 012345678 and loops when it is finished. 5319/2=2659.5Hz. (toggle mode divide the 5319Hz by 2). filter is enough (see Figure 3.). 10/1633=0.61% (1633 is the maximum DTMF frequency used). Figure 3. Connection between modules
PWM GENERATION WITH THE ST62 16-BIT AUTO-RELOAD TIMER
3 PROGRAM EXAMPLE
.vers "st6230" .romsize 8 .input "623x.asm" Msb_inc1.def 084h ; Msb of the increment tone 1 Lsb_inc1.def 085h ; Lsb of the increment tone 1 Msb_inc2.def 086h ; Msb of the increment tone 2 Lsb_inc2.def 087h ; Lsb of the increment tone 2 Msb_ton1.def 088h ; Msb of the tone 1 Lsb_ton1.def 089h ; Lsb of the tone 1 Msb_ton2.def 08Ah ; Msb of the tone 2 Lsb_ton2.def 08Bh ; Lsb of the tone 2 WaitTone.def 08Ch ; even -> tone, odd -> break NToSend.def 08Dh ; Number to dial from the telephone number Tel_Num.def 08Eh ; Telephone number to dial OVFFLG .def 2 ; Overflow flag bit position tmz .equ 7 ; This bit must be cleared by user software ; before starting a new count. .org 800h sinus: .byte 040h,046h,04ch,052h,058h,05eh,063h,068h .byte 06dh,071h,074h,078h,07ah,07ch,07eh,07fh .byte 07fh,07fh,07eh,07ch,07ah,078h,074h,071h .byte 06dh,068h,063h,05eh,058h,052h,04ch,046h .byte 040h,03ah,034h,02eh,028h,022h,01dh,018h .byte 013h,0fh,0ch,08h,06h,04h,02h,01h .byte 01h,01h,02h,04h,06h,08h,0ch,0fh .byte 013h,018h,01dh,022h,028h,02eh,034h,03ah num_dial: ; FREQ1 FREQ2 ; Msb1 Lsb1 Msb2 Lsb2 .byte 0bh, 53h, 10h, 13h;number 0 .byte 08h, 63h, 0eh, 8ch;number 1 .byte 08h, 63h, 10h, 13h;number 2 .byte 08h, 63h, 11h,0c6H;number 3 .byte 09h, 44h, 0eh, 8ch;number 4 .byte 09h, 44h, 10h, 13h;number 5 .byte 09h, 44h, 11h,0c6h;number 6
PWM GENERATION WITH THE ST62 16-BIT AUTO-RELOAD TIMER .byte 0ah, 40h, 0eh, 8ch;number 7 .byte 0ah, 40h, 10h, 13h;number 8 .byte 0ah, 40h, 11h,0c6h;number 9 .byte 08h, 63h, 13h,0a6h;number A .byte 09h, 44h, 13h,0a6h;number B .byte 0ah, 40h, 13h,0a6h;number C .byte 0bh, 53h, 13h,0a6h;number D .byte 0bh, 53h, 0eh, 8ch;number * .byte 0bh, 53h, 11h,0c6h;number # reset reti ldi drwr,20h ; Rom window with sinus ldi Lsb_ton1,0h ; Initialize the tone for break ldi Msb_ton1,0h ldi Lsb_ton2,0h ldi Msb_ton2,0h ldi Lsb_inc1,0h ldi Msb_inc1,0h ldi Lsb_inc2,0h ldi Msb_inc2,0h ldi x,#Tel_Num ; The Telephone number is 01234567 ldi a,0 ld Tel_Num,a ldi a,1 ld Tel_Num+1,a ldi a,2 ld Tel_Num+2,a ldi a,3 ld Tel_Num+3,a ldi a,4 ld Tel_Num+4,a ldi a,5 ld Tel_Num+5,a ldi a,6 ld Tel_Num+6,a ldi a,7 ld Tel_Num+7,a ldi a,7 ld Tel_Num+8,a ldi a,8 ld Tel_Num+9,a ldi WaitTone,0 ; WaitTone is even to start with tone ; instead of break ldi NToSend,0 ; First number to dial
PWM GENERATION WITH THE ST62 16-BIT AUTO-RELOAD TIMER ldi tcr,0ffh ; Load the counter ldi tscr,01001000b ; Restart the timer with input tone ; from input pin which is connected to the ; OVFFLG of ARTIMER. ; The prescaler is 1 ;* PortA initialisation for PWM and OVF output ldi ddra,0ch ldi ora,0ch ldi SCR2,00h ; CP1 input interrupt disable ldi SCR3,00h ; CP2 and ZEROFLG interrupt disable ; PWM output is run in SET/RESET mode ldi SCR4,0Fh ; OVFPOL=1, Overflow enable, PWMPOL=1, ; PWM enable ldi RLCPH,001h ; 100h for 8 bit of PWM, 77h to have time to ldi RLCPL,077h ; load the CMP register, and to have with ; 8MHz and a prescaler of 1/4 a ; Digital Analog Converter ; sample tone equivalent to 5319hz. ldi MASKH,0FFh ; MASK = 0FFFFh ldi MASKL,0FFh ldi SCR1,0B2h ; prescal by 4 to have a sample tone of 2MHz ; Reload mode ; Runres =1 to start the counter ; No interrupt with overflow ; Toggle mode to give 2000 Hz to the timer ; (with toggle mode it's not necessary to ; reset the OVFFLAG) ldi ior,10h ; Enable all interrupts. DTMF: ;* calculation of the first tone *** ld a,Lsb_ton1 ; load tone Lsb add a,Lsb_inc1 ; add it to the increment Lsb ld Lsb_ton1,a ; save the result ldi a,0 ; initiate a to 0 for no carry jrnc nocarry1 ; test carry of the previous result ldi a,1 ; if carry a=1 to add 1 to the Msb addition nocarry1: add a,Msb_ton1 ; add the carry to the tone Msb
PWM GENERATION WITH THE ST62 16-BIT AUTO-RELOAD TIMER add a,Msb_inc1 ; add it to the increment Lsb ld Msb_ton1,a ; save the result ;*** calculation of sin(2*PI*f) with look up table * andi a,03fh ; mask the Msb tone to call the look up table addi a,040h ; add the result to the address table ld x,a ;use the indirect pointer x to read the data ;* calculation of the second tone * ld a,Lsb_ton2 ; load tone Lsb add a,Lsb_inc2 ; add it to the increment Lsb ld Lsb_ton2,a ; save the result ldi a,0 ; initiate a to 0 for no carry jrnc nocarry2 ; test carry of the previous result ldi a,1 ; if carry a=1 to add 1 to the Msb addition nocarry2: add a,Msb_ton2 ; add the carry to the tone Msb add a,Msb_inc2 ; add it to the increment Lsb ld Msb_ton2,a ; save the result ;* calculation of sin(2*PI*f) with look up table * andi a,03fh ; mask the Msb tone to call the look up table addi a,040h ; add the result to the address table ld y,a ; use the indirect pointer y to read the data ld a,(y) ; read the data add a,(x) ; add to the first tone wait_ovf: jrr OVFFLG,SCR1,wait_ovf res OVFFLG,SCR1 ; reset the overflow ;* load compare register with a new value * ld CMPL,a jp DTMF ;* Input with accumulator a for the number to make InitTone: ldi drwr,21h ; Rom window with num_dial sla a sla a addi a,40h ld x,a ld a,(x) ld Msb_inc1,a; First tone
PWM GENERATION WITH THE ST62 16-BIT AUTO-RELOAD TIMER inc x ld a,(x) ld Lsb_inc1,a inc x ld a,(x) ld Msb_inc2,a ; Second tone inc x ld a,(x) ld Lsb_inc2,a ldi drwr,20h ; Rom window with sinus ret it_timer jrr 0,WaitTone,Break ld a,NToSend jrr 3,NToSend,TelNotFinish TelLoop: jrs 1,NToSend,TelLoop; infinit loop for this application note ; after dialing 10 numbers TelNotFinish: addi a,Tel_Num ld x,a ld a,(x) call InitTone ; Initialize the increment tone ldi tcr,0ffh ; Reload the counter for 96ms inc NToSend JP end_tim Break: ldi Lsb_inc1,0 ; All data to 0 for no sound. ldi Msb_inc1,0 ; For other applications, the time break ldi Lsb_inc2,0 ; could be used for process instead of ldi Msb_inc2,0 ; calculate nothing ldi Lsb_ton1,0 ldi Msb_ton1,0 ldi Lsb_ton2,0 ldi Msb_ton2,0 ldi tcr,06ah ; Reload the counter for 40ms end_tim inc WaitTone res tmz,tscr ; Restart the timer. reti
PWM GENERATION WITH THE ST62 16-BIT AUTO-RELOAD TIMER ;* Restart and interrupt Vectors * .org 0ff0h timer jp it_timer ;FF0h reti ;FF2h reti reti ;FF4h reti reti ;FF6h reti .org 0ffch nmi nop reti res jp reset
PWM GENERATION WITH THE ST62 16-BIT AUTO-RELOAD TIMER THE PRESENT NOTE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH A NOTE AND/OR THE USE MADE BY CUSTOMERS OF THE INFORMATION CONTAINED HEREIN IN CONNEXION WITH THEIR PRODUCTS. Information furnished is believed to be accurate and reliable. However, STMicroelectronics assumes no responsibility for the consequences of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. No license is granted by implication or otherwise under any patent or patent rights of STMicroelectronics. Specifications mentioned in this publication are subject to change without notice. This publication supersedes and replaces all information previously supplied. STMicroelectronics products are not authorized for use as critical components in life support devices or systems without the express written approval of STMicroelectronics. The ST logo is a registered trademark of STMicroelectronics 2003 STMicroelectronics - All Rights Reserved. Purchase of I2C Components by STMicroelectronics conveys a license under the Philips I2C Patent. Rights to use these components in an I2C system is granted provided that the system conforms to the I2C Standard Specification as defined by Philips. STMicroelectronics Group of Companies Australia - Brazil - Canada - China - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - U.S.A. http://www.st.com