implementing an rc5 infrared transmitter using the ir ... · pdf fileimplementing an rc5...
TRANSCRIPT
June 2010 Doc ID 15550 Rev 2 1/21
AN2957Application note
Implementing an RC5 infrared transmitterusing the IR timer modulator of the STM8L10x microcontroller
IntroductionIR (infrared) light is a popular means of wireless communication for remote control applications. Many different protocol standards such as RC5, RC5x, RC6,… exist for transmitting commands via an infrared wireless interface.
This application note describes the widely-used RC5 coding scheme from Philips and presents a full working solution that implements a remote control device.
This application uses the STM8L10x microcontroller, it contains a powerful hardware modulator called IRTIM that combines signals from two internal timers to drive the IR interface. This feature makes the microcontroller especially well suited for applications that require IR signal generation capability.
www.st.com
Contents AN2957
2/21 Doc ID 15550 Rev 2
Contents
1 STM8L10x IR interface description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 RC5 coding scheme . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1 Frame header description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Device addresses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2.3 Device instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 Bit descriptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5 RC5 frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.6 RC5 carrier frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3 Software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.2 RC5 functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.3 RC5 flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.4 Application flowcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4 Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5 Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
6 Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
AN2957 STM8L10x IR interface description
Doc ID 15550 Rev 2 3/21
1 STM8L10x IR interface description
The IR digital interface is designed to output a digital signal to an infrared diode driver circuit.
It can output a signal using any of the existing modulation styles, the modulation style is dependent on the software algorithm.
The IR interface is very easy to configure and it uses 2 signals provided by two of the STM8L10x timers (TIM2 and TIM3).
TIM2 is used to provide the Carrier Frequency and TIM3 provides the actual signal to be sent.
Figure 1. IRTIM input and output signals
TIM2_CH1(Carrier frequency)
TIM3_CH1 (Main signal)
IRTIMIRTIM_Output
RC5 coding scheme AN2957
4/21 Doc ID 15550 Rev 2
2 RC5 coding scheme
The RC5 coding scheme from Philips is a standard in infrared wireless command transmission.
The RC5 coding scheme can generate 2048 different commands organized on 32 groups, each group has 64 different commands.
The RC5 frame is a 14-bit word, composed of 3 parts:
● Header (3 bits)
● Device Address (5 bits)
● Device Instruction (6 bits)
The bits are transmitted in bi-phase code (also known as Manchester code) as shown in Figure 3.
The RC5 carrier frequency is 36 kHz.
Figure 2. RC5 frame format
Figure 3. Manchester coding
Header (3 bits) Device address (5 bits) Device instruction (6 bits)
Logic 0 Logic 1
AN2957 RC5 coding scheme
Doc ID 15550 Rev 2 5/21
2.1 Frame header descriptionThe frame header is composed of three bits: The two first bits are start bits, which always have the value "1". The next bit is a control bit, which is toggled every time a button is pressed on the remote control transmitter (to determine whether a button is pressed and held down, or pressed and released continuously).
Figure 4 and Figure 5 show examples of RC5 frame headers:
Figure 4. Frame header 1
Figure 5. Frame header 2
1 1 0
Head1 (1st press)
1 1 1
Head2 (2nd press)
RC5 coding scheme AN2957
6/21 Doc ID 15550 Rev 2
2.2 Device addressesThe device address sequence is five bits long, allowing the control of up to 32 devices (25 = 32 addresses). Table 1 gives the addresses of the controlled devices.
Table 1. RC5 device addresses
Address Device
0 TV1
1 TV2
2 Videotext
3 Extension for TV1 and TV2
4 Laser vision player
5 VCR1
6 VCR2
7 Reserved
8 SAT1
9 Extension for VCR1 and VCR2
10 SAT2
11 Reserved
12 CD-video
13 Reserved
14 CD-photo
15 Reserved
16 Audio1 preamplifier
17 Tuner
18 Analogical magneto cassette
19 Audio2 preamplifier
20 CD
21 Rack audio
22 Satellite receiver
23 DCC Magneto
24 Reserved
25 Reserved
26 CD writable
27 to 31 Reserved (for experimental use)
AN2957 RC5 coding scheme
Doc ID 15550 Rev 2 7/21
2.3 Device instructionsThe instruction sequence is six bits long, allowing up to 64 (26) different instructions per address.
There are two groups of instructions, the first group is common for all devices, the second group is reserved for television devices (TV1 and TV2).
Common instructions are given in Table 2 and TV instructions are given in Table 3.
Table 2. RC5 common instructions
Common instruction Description
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
16 Volume +
17 Volume -
18 Brightness +
19 Brightness -
20 Color saturation +
21 Color saturation -
22 Bass +
23 Bass -
24 Treble +
25 Treble -
26 Balance right
27 Balance left
63 System select
RC5 coding scheme AN2957
8/21 Doc ID 15550 Rev 2
Table 3. RC5 TV instructions
TV Instruction Description
10 1/2/3 digits / 10
11 Freq./prog./ch./11
12 Standby
13 Mute/de-mute
14 Personal pref.
15 Display
28 Contrast +
29 Contrast -
30 Search +
31 Tint/hue -
32 Ch./Prog. +
33 Ch./Prog. -
36 Spatial stereo
37 Stereo/mono
38 Sleep timer
39 Tint/hue. +
40 RF switch
41 Store/execute/vote
42 Time
43 Scan fwd./increment.
44 Decrement
46 Sec con/menu
47 Show clock
48 Pause
49 Erase/correct
50 Rewind
51 Go to
52 Wind
53 Play
54 Stop
55 Record
56 External 1
57 External 2
59 Advance
60 TXT sub-mode/12
AN2957 RC5 coding scheme
Doc ID 15550 Rev 2 9/21
61 Sys. Standby
62 Crispener
Table 3. RC5 TV instructions (continued)
TV Instruction Description
RC5 coding scheme AN2957
10/21 Doc ID 15550 Rev 2
2.4 Bit descriptionsRC5 bits are Manchester coded, this means that a bit is composed of 2 alternated half bits. A low to high transition means that the bit is "1", a high to low transition means "0".
The frequency used to code a bit is 562.429 Hz (1778 µs), half bits have the same width (889 µs each).
Figure 6. RC5 bit description
2.5 RC5 frames RC5 frames are composed of 14 bits, the width of a frame is 24.892 ms (14 x 1.778 ms = 24.892 ms)
To avoid frame collisions, an idle time is inserted between two frames with a specific width. The idle time is defined as 50 bits wide. So the periodicity of a frame is 64 x 1 bit width: 64 x 1.778 = 113.792 ms (exactly 113.788 ms).
Figure 7. RC5 frame description
"0"
889 µs
1778 µs
14 bits50 bits14 bits
Frame 1 Idle time Frame 2
64 bits = 113.788 ms
AN2957 RC5 coding scheme
Doc ID 15550 Rev 2 11/21
2.6 RC5 carrier frequencyThe RC5 carrier frequency is 36 kHz. To minimize the power consumption, the duty cycle used for a pulse is fixed at 25%, so the width of a pulse is 6.94 µs.
Figure 8. RC5 carrier frequency description
1 1 0 0
6.94 μs 20.82 μs
Carrier frequencyFrequency = 36 kHzDuty Cycle = 25%
Main Signal to send:Manchester coded
Software description AN2957
12/21 Doc ID 15550 Rev 2
3 Software description
3.1 IntroductionThe software provided with this application note enables the user to send all RC5 instructions to all RC5 receiver devices. It contains an RC5 driver with functions and software flags.
The software is designed to avoid frame collisions; an idle time is inserted between two frames with a specific width (50 bits).
The application uses 5 peripherals:
● IRTIM: (IR interface with timers) generates the IR signal using TIM2 and TIM3
● TIM2: (Timer2) provides the Carrier signal with a frequency of 36 kHz
● TIM3: (Timer3) provides the main signal to be sent (RC5 Frame)
● GPIO: (General purpose I/O) provides the I/O to be connected to the buttons of the the remote control
● CLK: (Clock controller) enables the clocks and provides the correct clock frequency for the timers
AN2957 Software description
Doc ID 15550 Rev 2 13/21
Figure 9. Application architecture and description
main c
ApplicationLayer
RC5 softwareLayerrc5.h
rc5.c
stm8l10x.h
stm8l10x_conf.hstm8l10x_it.h
stm8l10x_it.c
stm8l10x_tim3.c
stm8l10x_tim3.h
stm8l10x_tim2.
stm8l10x_tim2.h stm8l10x_irtim.h
stm8l10x_irtim.c
stm8l10x_clk.h
stm8l10x_clk.c stm8l10x_gpio.c
stm8l10x_gpio.h
API Layer
FW Drivers
HardwareLayer
STM8L 10xmicrocontroller
Software description AN2957
14/21 Doc ID 15550 Rev 2
3.2 RC5 functionsRC5_BinFrameGeneration function
Generates and returns a binary format of the RC5 frame: the flowchart of this function is described below
uint16_t RC5_BinFrameGeneration ( RC5_Address_TypeDef RC5_Address,
RC5_Instruction_TypeDef RC5_Instruction,
RC5_Ctrl_TypeDef RC5_Ctrl)
RC5_ManchesterConvert function
Converts an RC5 binary format Frame to a RC5 Manchester format frame.
uint32_t RC5_ManchesterConvert (uint16_t RC5_BinaryFrameFormat)
RC5_SendFrame function
Sends a Manchester Format RC5 Frame.
void RC5_SendFrame(uint32_t RC5_ManchesterFrameFormat)
RC5_CtrlBitReverse function
Reverses the state of the RC5 Control bit (can be used to determine whether a button is pressed and held down, or pressed and released continuously).
RC5_Ctrl_TypeDef RC5_CtrlBitReverse(RC5_Ctrl_TypeDef RC5_Ctrl);
3.3 RC5 flags
Table 4. RC5 flags
Flag name Flag description
Send_Operation_Completed Flag 0: The RC5 frame is not completely sent
1: The RC5 frame is completely sent
Send_Operation_Ready Flag 0: The RC5 frame is not yet ready to be send
1: The RC5 frame is ready to be send
AN2957 Software description
Doc ID 15550 Rev 2 15/21
3.4 Application flowcharts
Figure 10. Main loop flowchart
Identify which key is pressedand send the corresponding
RC5 data
If key is pressed
Enable and configure IRTIMto generate IR signal
Enable and configure TIM3 to generate a periodic interrupt T = 889 µs
and enable the TIM3 Update Interrupt
Enable and configure TIM2 to generate the carrier signal
Frequency = 36 kHzPWM Duty cycle = 25%
Start
No
Yes
INTERRUPT
Software description AN2957
16/21 Doc ID 15550 Rev 2
Figure 11. Send frame function flowchart
End
Enable Interrupts
Set the Send_Operation_Ready flag to indicate that the frame is ready to be sent
Convert the frame to the Manchester format
Disable Interrupts
Generate a binary format of the Frame
Begin SendFrame
AN2957 Software description
Doc ID 15550 Rev 2 17/21
Figure 12. RC5 SendFrame function flowchart (this function is called during the TIM3 Update Interrupt routine)
BeginRC5_SendFrame
If Flag Send Operation Ready =1
Yes
No
No
NoYes
Yes
If all bits of the Manchesterformat frame are sent
Clear FlagSend_operation_completed = 0
If the bit to be sent is 1
Force the TIM3Ch1 Output to 1
Force the TIM3Ch1 Output to 0
Increment BitsSent_Counter(BitsSent_Counter++)
Set FlagSend_Operation_Completed = 1
Reset BitsSent_Counter(BitsSent_Counter = 0)
Force the TIM3 Ch1 Output to 0
End
Application description AN2957
18/21 Doc ID 15550 Rev 2
4 Application description
This Application note is provided with a basic application in which only two keys are used for remote control:
● Button 1 (PA5) enables user to send "VolumeUp" RC5 Instruction to "TV1" (Key button on STM8L1X-EVAL Board).
● Button 2 (PD7) enables user to send "VolumeDown" RC5 Instruction to "TV1" (Joystick Select button on STM8L1X-EVAL Board).
However, user can implement a bigger remote control to fit its application needs.
Figure 13. Simplified RC5 transmitter
STM8L10x
Button 2 Button 1
PD7 PA5
PA0NOT
IR Led
GND
AN2957 Hardware description
Doc ID 15550 Rev 2 19/21
5 Hardware description
Figure 14. RC5 transmitter
RC5 remote control
GND
IR Led
R2=100
VCC
R1=10K
Port B Port D
STM8L 10x
Revision history AN2957
20/21 Doc ID 15550 Rev 2
6 Revision history
Table 5. Document revision history
Date Revision Changes
23-Apr-2009 1 Initial release.
04-Jun-2010 2
Removed several common instructions (from 71 to 124) in Table 2: RC5 common instructions on page 7Removed several TV instructions (from 70 to 127) in Table 3: RC5 TV instructions on page 8
AN2957
Doc ID 15550 Rev 2 21/21
Please Read Carefully:
Information in this document is provided solely in connection with ST products. STMicroelectronics NV and its subsidiaries (“ST”) reserve theright to make changes, corrections, modifications or improvements, to this document, and the products and services described herein at anytime, without notice.
All ST products are sold pursuant to ST’s terms and conditions of sale.
Purchasers are solely responsible for the choice, selection and use of the ST products and services described herein, and ST assumes noliability whatsoever relating to the choice, selection or use of the ST products and services described herein.
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of thisdocument refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party productsor services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of suchthird party products or services or any intellectual property contained therein.
UNLESS OTHERWISE SET FORTH IN ST’S TERMS AND CONDITIONS OF SALE ST DISCLAIMS ANY EXPRESS OR IMPLIEDWARRANTY WITH RESPECT TO THE USE AND/OR SALE OF ST PRODUCTS INCLUDING WITHOUT LIMITATION IMPLIEDWARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE (AND THEIR EQUIVALENTS UNDER THE LAWSOF ANY JURISDICTION), OR INFRINGEMENT OF ANY PATENT, COPYRIGHT OR OTHER INTELLECTUAL PROPERTY RIGHT.
UNLESS EXPRESSLY APPROVED IN WRITING BY AN AUTHORIZED ST REPRESENTATIVE, ST PRODUCTS ARE NOTRECOMMENDED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, AIR CRAFT, SPACE, LIFE SAVING, OR LIFE SUSTAININGAPPLICATIONS, NOR IN PRODUCTS OR SYSTEMS WHERE FAILURE OR MALFUNCTION MAY RESULT IN PERSONAL INJURY,DEATH, OR SEVERE PROPERTY OR ENVIRONMENTAL DAMAGE. ST PRODUCTS WHICH ARE NOT SPECIFIED AS "AUTOMOTIVEGRADE" MAY ONLY BE USED IN AUTOMOTIVE APPLICATIONS AT USER’S OWN RISK.
Resale of ST products with provisions different from the statements and/or technical features set forth in this document shall immediately voidany warranty granted by ST for the ST product or service described herein and shall not create or extend in any manner whatsoever, anyliability of ST.
ST and the ST logo are trademarks or registered trademarks of ST in various countries.
Information in this document supersedes and replaces all information previously supplied.
The ST logo is a registered trademark of STMicroelectronics. All other names are the property of their respective owners.
© 2010 STMicroelectronics - All rights reserved
STMicroelectronics group of companies
Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan - Malaysia - Malta - Morocco - Philippines - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America
www.st.com