HT66F018 PTM IR Carrier Function Application Example: D/N HA0 326E

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

HT66F018 PTM IR Carrier Function Application Example

HT66F018 PTM IR Carrier Function Application Example

D/N:HA0326E

Introduction
The HT66F018 PTM has rich functionality. After a simple setup procedure, the PTM PWM
function can be selected to output a variety of IR carrier signals, such as 38K or 40K
frequency. When coupled with a timing program, it can send out IR code waveforms, for
example to emulate an NEC format waveform. The following content will explain how to
use the PTM combined with the CTM to achieve the NEC IR coding.

PTM Output IR Carrier Period & Duty Setup


HT66F018 PTM Rrelevant Registers

The HT66F018 PTM relevant registers are shown in Table 1. TMnC0 and TMnC1 control
the PTM on/off function, operation mode selection, output function and other settings.
TMnDL and TMDH are read-only registers to store the internal counter current value;
TMnAL and TMnAH configure the CCRA value while the TMnRPL and TMnRPH registers
configure the CCRP value. For more details, refer to the datasheet content.
Table 1

1
HT66F018 PTM IR Carrier Function Application Example

PTM TM1 Settings for the 38kHz Carrier output


 Set the T1CK2~T1CK0 bits in the TM1C0 register to 001 to select fSYS to be the PTM
Counter clock. The system clock source is 8MHz in this example

 Select the PWM Output Mode


Set the T1M1~T1M0 bits in the TM1C1 register to10 to select the "PWM Mode or
Single Pulse Output Mode"

 Output Mode setting


Set the T1IO1~T1IO0 bits in the TM1C1 register to 10 to select the "PWM output".
Setting considerations
 The T1IO1~T1IO0 bits have four options in the PWM/Single Pulse Output Mode:
00:PWM Output Inactive State
01:PWM Output Active State
10:PWM Output
11:Single Pulse Output
When these bits are 00 or 01, the PWM pin (PA7) outputs a fixed level. The active
state is the output pin level when TM1 is on but when a compare match has not
occurred from comparator A. In contrast, the inactive state is the pin level after a
compare match occurs. The active state level is controlled by the T1OC, T1POL and
PA7 bits.
 When T1POL and PA7 are 0 and T1OC is 1, the active state is a high level while the
inactive state is a low level. In this condition, if PA7 does not change but the T1POL
bit is set high, the active state level is inverted. Now if PA7 is also set high, the active
state level is reversed once again. Of course the inactive state level also changes
along with it.
MOV A,00010000B
MOV TM1C0,A
MOV A,10101000B
MOV TM1C1,A

2
HT66F018 PTM IR Carrier Function Application Example

 Carrier Period setting(CCRP)


 1/fSYS  CCRP => CCRP/fSYS => 1/(8MHz)  CCRP =1/(38kHz) = 26.3s,
CCRP = 210 = D2H
 Because of the HIRC deviation in the simulation, the example programs the CCRP
value as CCH rather than the default D2H, to allow the frequency to be closer to
38kHz
 Set TM1RPL to CCH and TM1RPH to 00H
MOV A,0CCH
MOV TM1RPL, A ; set carrier 38K
MOV A,00H
MOV TM1RPH, A
 Carrier Duty Cycle setting (CCRA)
 1/3 duty : CCRP / 3 = CCH / 3 = 44H, so TM1AL = 44H, TM1AH = 00H
 1/2 duty : CCRP / 2 = CCH / 2 = 66H, so TM1AL = 66H, TM1AH = 00H
MOV A,TM1AL_value
MOV TM1AL,A ;set 1/2 or 1/3 duty
MOV A,00H
MOV TM1AH,A
 TM1 output setting: set T1CP bit high in the TMPC register to enable the TP1 (PWM)
output
 Carrier output on/off control
 On: T1ON=1
 Off: T1ON=0, PA7 outputs low (CLR PA7, CLR PAC7)

IR Code Output Implementation


To make the pin output an IR Coded waveform 38kHz carrier there are three common
methods as the following shows:
1. Use delay instructions: after the 38kHz carrier outputs a start or end, use a delay
subroutine to form the carrier output mark/space ratio to implement a complete IR
waveform output. This method only uses the PTM, but has a low code execution
efficiency.
2. Timer method: this method requires two TMs, one is used to generate the 38kHz
Carrier and the other is used to setup the mark/space ratio to implement a complete
IR waveform output.
3. Count the carrier cycle interrupts: by counting of the carrier cycle interrupts, decide
when to output the carrier to implement a complete IR waveform output. This method
only uses the PTM.
The application example here uses the second method and implements the NEC-IR
coding using TM2, which is the CTM, for the coding waveform timing.

3
HT66F018 PTM IR Carrier Function Application Example

CTM TM2 Settings for NEC Coding Waveform Output


1. Set the T2CK2~T2CK0 bits in the TM2C0 register to 000 to select fSYS/4 to be the
CTM clock source, the system clock source is 8MHz in the example.
2. Select TM2 to operate in the Timer/Counter Mode: set the T2M1~T2M0 bits in the
TM2C register to 111 to select the “Timer/Counter Mode”.
3. Set the T2CCLR bit in the TM2C1 register to 1 to select: “TM2 Comparator A match”.
4. Use CCRA for the minimum timing time width setting

Figure 1. NEC IR Coding Format

Figure 2. NEC IR coding “Logic 0" and "Logic 1”

;CTM setting - IR NEC code


;============================================================================
MOV A,00000000B
MOV TM2C0,A
MOV A,11000001B
MOV TM2C1,A
MOV A,60H
MOV TM2AL,A
MOV A,04H
MOV TM2AH,A
SET MF1E
SET T2AE

(1) As shown in Figure 1 and Figure 2, the NEC IR coding requires several time widths:
9ms, 4.5ms, 0.56ms, 2.24ms and 108ms. Taking 560s as the minimum time
width, then the other times will be multiples of this value.
(2) 1/(fSYS/4)  CCRA= 1/(2MHz)  CCRA = 560us, CCRA = 1120 = 460H。
(3) Set TM2Al to 60H and TM2AH to 04H.

5. TM2 interrupt setting: Enable MF1E, T2AE and the global interrupt EMI to enable the
TM2 interrupt.
Setup Note: when a TM2 interrupt occurs, the system will automatically clear the
Multi-Function request flag MF1F. But the CCRA Compare Interrupt flag T2AF will not
be automatically cleared and needs to be manually cleared by the application
program.

4
HT66F018 PTM IR Carrier Function Application Example

6. NEC IR coding transmission


Taking into account the code implementation efficiency and the code transmission
timings, the NEC IR coding timing procedures and the code transmission are
executed in the TM2 Interrupt service routine. As a result two things need to be
managed in the TM2 interrupt subroutine:

(1) The NEC IR coding timing procedure: each time a time-out flag occurs, when the
time is up, the flag will be set high. Because the TM2 has set the minimum time as
560s, then to achieve times of 9ms, 4.5ms, 2.24ms and 108ms, these require 16,
8, 4 and 193 timer cycles respectively.
(2) NEC IR coding waveform transmission:
As the key is pressed, PA7 will output the NEC IR coded waveforms. When
sending the NEC IR coding high level part, it will output the 38kHz carrier, in which
case TM1 and TM2 will be switched on simultaneously. When sending the NEC IR
coded low level part, TM1 will stop generating the 38kHz carrier, and only TM2
continues to execute timing operations.

; TM2 ISR with the time flags

;TM2 ISR (time flag)


;============================================================================
ISR_TM2:
CLR WDT
MOV ISR_ACC,A
MOV A,STATUS
MOV ISR_STATUS,A
SET f_560us ; 0.56ms
ISR_9ms:
SNZ f_9ms_start
JMP ISR_4500us
INC r_timer_count ; 9ms
MOV A,r_timer_count
SUB A,16
SNZ C
JMP ISR_108ms
CLR r_timer_count
SET f_9ms
ISR_4500us:
SNZ f_4500us_start
JMP ISR_2240us
INC r_timer_count ; 4.5ms
MOV A,r_timer_count
SUB A,8
SNZ C
JMP ISR_108ms
CLR r_timer_count
SET f_4500us

ISR_2240us:
SNZ f_2240us_start
JMP ISR_108ms
INC r_timer_count ; 2.24ms
MOV A,r_timer_count
SUB A,4

5
HT66F018 PTM IR Carrier Function Application Example

SNZ C
JMP ISR_108ms
CLR r_timer_count
SET f_2240us
ISR_108ms:
SNZ f_108ms_start
JMP ISR_sendcode
INC r_108ms_COUNT
MOV A,r_108ms_COUNT ; 108ms
SUB A,193
SNZ C
JMP ISR_sendcode
CLR r_108ms_COUNT
SET f_108ms

ISR_sendcode:
MOV A,r_INDEX
XOR A,1
SNZ Z
JMP ISR_addr1_send
SNZ f_9ms
JMP ISR_EXIT
CLR f_9ms
CLR T1ON
CLR PA7 ; stop IR carrier generator
CLR PAC7
CLR f_9ms_start
SNZ f_longpress
JMP ISR_4500us_send
CLR f_longpress
SET f_2240us_start
CLR r_timer_count
MOV A,6
MOV r_INDEX,A
JMP ISR_EXIT
ISR_4500us_send:
SET f_4500us_start
CLR r_timer_count
MOV A,2
MOV r_INDEX,A
JMP ISR_EXIT
ISR_addr1_send:
MOV A,r_INDEX
XOR A,2
SNZ Z
JMP ISR_logic_judge
SNZ f_4500us
JMP ISR_EXIT
CLR f_4500us
CLR f_4500us_start
INC r_DATA_COUNT
CLR f_560us
SET T1ON
MOV A,ADDR_1
MOV r_SENDCODE,A
MOV A,3
MOV r_INDEX,A
JMP ISR_EXIT
ISR_logic_judge:
MOV A,r_INDEX
XOR A,3

6
HT66F018 PTM IR Carrier Function Application Example

SNZ Z
JMP ISR_logic_send
SNZ f_560us
JMP ISR_EXIT
CLR f_560us
CLR T1ON
CLR PA7
CLR PAC7
MOV A,r_SENDCODE
CLR C
RRC ACC
SNZ C
JMP LOGIC_0_LOOP
MOV r_SENDCODE,A
SET f_2240us_start
CLRr_timer_count
SET f_logic_1
JMP LOGIC_EXIT
LOGIC_0_LOOP:
MOV r_SENDCODE,A
CLR f_560us
SET f_logic_0
LOGIC_EXIT:
MOV A,4
MOV r_INDEX,A
JMP ISR_EXIT
ISR_logic_send:
MOV A,r_INDEX
XOR A,4
SNZ Z
JMP ISR_108ms_JUDGE
SNZ f_logic_1
JMP LOGIC_0
SNZ f_2240us
JMP ISR_EXIT
CLR f_2240us_start
CLR f_2240us
CLR f_logic_1
JMP ISR_logic_send_EXIT
LOGIC_0:
SNZ f_logic_0
JMP ISR_logic_send_EXIT
SNZ f_560us
JMP ISR_EXIT
CLR f_560us
CLR f_logic_0
ISR_logic_send_EXIT:
SDZ r_COUNT
JMP ISR_DATALOAD_UNFINISHED
INC r_DATA_COUNT
MOV A,r_DATA_COUNT
XOR A,2
SNZ Z
JMP ISR_DATA_LOAD
MOV A,ADDR_2
MOV r_SENDCODE,A
JMP ISR_DATALOAD_FINISHED
ISR_DATA_LOAD:
MOV A,r_DATA_COUNT
XOR A,3
SNZ Z

7
HT66F018 PTM IR Carrier Function Application Example

JMP ISR_DATA_COM_LOAD
MOV A,DATA
MOV r_SENDCODE,A
JMP ISR_DATALOAD_FINISHED
ISR_DATA_COM_LOAD:
MOV A,r_DATA_COUNT
XOR A,4
SNZ Z
JMP ISR_LOAD_EXIT
MOV A,DATA
XOR A,0FFH
MOV r_SENDCODE,A
JMP ISR_DATALOAD_FINISHED
ISR_LOAD_EXIT:
MOV A,r_DATA_COUNT
XOR A,5
SNZ Z
JMP ISR_EXIT
CLR r_DATA_COUNT
MOV A,5
MOV r_INDEX,A
JMP ISR_EXIT
ISR_DATALOAD_FINISHED:
CLR f_560us
SET T1ON
MOV A,8
MOV r_COUNT,A
MOV A,3
MOV r_INDEX,A
JMP ISR_EXIT
ISR_DATALOAD_UNFINISHED:
CLR f_560us
SET T1ON
MOV A,3
MOV r_INDEX,A
JMP ISR_EXIT
ISR_108ms_JUDGE:
MOV A,r_INDEX
XOR A,5
SNZ Z
JMP ISR_SIMPLECODE_2240us
SNZ f_108ms
JMP ISR_EXIT
CLR f_108ms
CLR f_108ms_start
SET f_keycheck
MOV A,1
MOV r_INDEX,A
JMP ISR_EXIT
ISR_SIMPLECODE_2240us:
MOV A,r_INDEX
XOR A,6
SNZ Z
JMP ISR_SIMPLECODE_560us
SNZ f_2240us
JMP ISR_EXIT
CLR f_2240us_start
CLR f_2240us
CLR f_560us
SET T1ON
MOV A,7

8
HT66F018 PTM IR Carrier Function Application Example

MOV r_INDEX,A
ISR_SIMPLECODE_560us:
MOV A,r_INDEX
XOR A,7
SNZ Z
JMP ISR_EXIT
SNZ f_560us
JMP ISR_EXIT
CLR f_560us
CLR T1ON
CLR PA7
CLR PAC7
MOV A,5
MOV r_INDEX,A
ISR_EXIT:
CLR T2AF
MOV A,ISR_STATUS
MOV STATUS,A
MOV A,ISR_ACC
RETI

Program Description
 A key is connected to the HT66F018 PA1 pin via a 1K resistor to VSS.
 PA7 is setup as a HT66F018 PTM PWM output.
 When the key is pressed, PA7 outputs an NEC IR coded waveform; when released,
PA7 outputs a low.

 TM2 is used to setup the times required by NEC IR coding protocol.


 The TM1AL_value is used to select the 38kHz carrier duty 1/3 or 1/2, by setting
TM1AL_value EQU 44H or 66H.

9
HT66F018 PTM IR Carrier Function Application Example

Program Example
The following example is part of the setup procedure for the PTM IR carrier.

MOV A,00010000B ; PTM clock source:fsys


MOV TM1C0,A
MOV A,10101000B ; PWM mode,PWM Active High
MOV TM1C1,A
MOV A,TM1AL_value
MOV TM1AL,A
MOV A,00H
MOV TM1AH,A; Configure CCRA for duty
MOV A,0CCH
MOV TM1RPL,A
MOV A,00H
MOV TM1RPH,A ; Configure CCRP for Period
MOV A,00000010B
MOV TMPC,A ; enable PA7 for PWM output

When T1ON = 1, PA7 outputs a 38kHz IR carrier. When T1ON = 0, PA7 will cease to
output the carrier signal. However it is necessary to set the PA7 output low and shut down
the IR emitting diodes driven by an NPN transistor. More details are provided in the
attached file.

Conclusion
In this document, the example provided was used to illustrate how to use the HT66F018
PTM function to generate a 38kHz carrier and achieve NEC IR coded waveform output.

Accessory
Users can consult the Holtek website for the program attachment.

10

You might also like