没有合适的资源?快使用搜索试试~ 我知道了~
Modular Mid-Range PICmicro® KEELOQ® Decoder in C
需积分: 10 8 下载量 108 浏览量
2011-08-04
17:20:16
上传
评论
收藏 292KB PDF 举报
温馨提示
试读
28页
Modular Mid-Range PICmicro® KEELOQ® Decoder in C
资源推荐
资源详情
资源评论
2001 Microchip Technology Inc. Preliminary DS00744A-page 1
M
AN744
OVERVIEW
This application note describes a KEELOQ code hop-
ping decoder implemented on a Microchip Mid-range
Enhanced FLASH MCU (PIC16F872). The software
has been designed as a group of independent modules
(standard C source files "C" ).
For clarity and ease of maintenance, each module cov-
ers a single function. Each module can be modified to
accommodate a different behavior, support a different
MCU, and/or a different set of peripherals (memories,
timers, etc.).
KEY FEATURES
The set of modules presented in this application note
implement the following features:
• Source compatible with HITECH and CCS C
compilers
• Pin out compatible with PICDEM-2 board
• Normal Learn mode
• Learn up to 8 transmitters, using the internal
EEPROM memory of PIC16F872
• Interrupt driven Radio Receive (PWM) routine
• Compatible with all existing K
EELOQ hopping code
encoders with PWM transmission format
selected, operating in "slow mode" (T
E = 400 µs)
• Automatic synchronization during receive, using a
4 MHz RC oscillator
FIGURE 1: DECODER PIN OUT
TABLE 1: FUNCTIONAL INPUTS AND
OUTPUTS
Notice:
This is a non-restricted version of Application Note AN745 which is available under the K
EELOQ License
Agreement. The license agreement can be ordered from the Microchip Literature Center as DS40149.
Author: Lucio Di Jasio
Microchip Technology Inc.
Pin
Name
Pin
Number
Input/
Output
Function
RFIN 3 I Demodulated
PWM signal from
RF receiver
LEARN 6 I Input to enter learn
mode
LEARN-
OUT
25 O Output to show the
status of the learn
process
OUT0..3 21,22,2
3, 24
O Function outputs,
correspond to
encoder input pin
V
LOW 26 O Low Battery indica-
tor, as transmitted
by the encoder
VDD 20 PWR 5V power supply
VSS 19, 8 GND Common ground
Note: All NU pins are tristate
1
2
3
4
5
6
7
8
9
28
27
26
25
24
23
22
21
20
MCLR
LRNOUT
RF
IN
NU
NU
LEARN
NU
V
SS
OSCIN
NU
NU
V
LOW
LEARNOUT
OUT3
OUT2
OUT1
OUT0
V
DD
10
11
12
13
14
19
16
17
18
15
OSCOUT
NU
NU
NU
NU
V
SS
NU
NU
NU
NU
Modular Mid-Range PICmicro
®
KEELOQ
®
Decoder in C
AN744
DS00744A-page 2 Preliminary 2001 Microchip Technology Inc.
DESIGN OBJECTIVES
Each module has been designed for maximum simplic-
ity and maintainability. Whenever possible, we favored
clarity of design over efficiency in order to show the
basic concepts of the design of a K
EELOQ decoder with-
out the constraints of previous PIC16C5X implementa-
tions such as limited RAM, STACK, or other resources.
To achieve maximum ease in maintenance, we
adopted "modern" C language programming tech-
niques, specifically:
• All pin assignments are mapped through
#define directives. This results in almost com-
plete code independence from the specific pin out
chosen
• Drivers to peripherals that are specific to a given
processor type (such as PIC16F872) have been
encapsulated in more generic modules
• Function input and output values are documented
• Pseudo-graphical representation of the data
structures used and program flow is commented
whenever possible
Although the code can be compiled in a set of indepen-
dent object files and then linked together to build the
actual application, we kept all the modules included in
line with the main module to retain compatibility with
compilers that have no linker such as CCS PIC C.
MODULES OVERVIEW
The code presented in this application note is com-
posed of the following basic modules:
RXI.C interrupt driven receiver
KEYGEN.C K
EELOQ key generation routines imple-
menting Normal Mode
FASTDEC.C K
EELOQ decrypt routine
MEM-87X.C PIC16F87X EEPROM driver
TABLE.C transmitters table memory manage-
ment (linear list)
MAIN.C the actual initialization and main loop
FIGURE 2: MODULES OVERVIEW
Timer0
RXI.C
1st Buffer
RF_FULL
Flag
Radio Receiver
Receive Buffer
MAIN.C
Main Loop
Interrupt
Learn
Out S0
Out S3
Learnout
V
LOW
KEYGEN.C
- Normal KEYGEN
- Load Manufacturer Code
FASTDEC.C
- Decrypt
TABLE.C
- Insert
- Search
MEM-87X.C
- RDword
- WRWord
EEPROM
K
EELOQ
®
- IDwrite
- HopUpdate
- ClearMem
- DecCHK
- HopCHK
X
X
PIC16F872
2001 Microchip Technology Inc. Preliminary DS00744A-page 3
AN744
RECEIVER MODULE
The receiver module has been developed around a fast
and independent Interrupt Service Routine (ISR). The
whole receiving routine is implemented as a simple
state machine that operates on a fixed time base. This
can be used to produce a number of virtual timers. The
operation of this routine is completely transparent to
the main program and similar to a UART. In fact, the
interrupt routine consumes only 30% of the computa-
tional power of the MCU working in the background .
After a complete code-word of 66 bits has been prop-
erly received and stored in a 9 bytes buffer, a status flag
(
RF_FULL) is set and the receiver becomes idle.
It is the responsibility of the main program to make use
of the data in the buffer and to clear the flag to enable
the receiving of a new code-word.
In order to be compatible with all K
EELOQ encoders,
with or without oscillator tuning capabilities, the
receiver routine constantly attempts to resynchronize
with the first rising edge of every bit in the incoming
code-word. This allows the decoder to operate from an
inexpensive (uncalibrated) RC clock. In doing so, the
last rising edge/bit of every code-word is lost (resulting
in an effective receive buffer capacity of 65-bit).
For HCS20X and HCS30X encoders this implies that
the REPEAT bit (being the 66th) cannot be captured.
While for Advanced Encoders like the HCS36X or
HCS4XX, the reader can easily modify the definition of
the constant
BIT_NUM to 68 to receive all bits trans-
mitted with exception of the last queue bit Q1 (being the
69th), again rarely used.
The only resource/peripheral used by this routine is
Timer0 and the associated Overflow Interrupt. This is
available on every mid-range PICmicro microcontroller.
Timer0 is reloaded on overflow, creating a time base (of
about 1/3 T
E = 138 µs). The same interrupt service rou-
tine also provides a virtual 16-bit timer, derived from the
same base period, called
XTMR.
FIGURE 3: CODE-WORD TRANSMISSION FORMAT
FIGURE 4: CODE-WORD ORGANIZATION
LOGIC ‘0’
LOGIC ‘1’
Bit
Period
Preamble
Header
Encrypted Portion
of Transmission
Fixed Portion of
Transmission
Guard
Time
TP
TH
THOP
TFIX
TG
TE
16-bit
Discrimination
bits
(10 bits)
Overflow
bits
(2 bits)
Button
Status
(4 bits)
Transmission Direction
32 bits of Encrypted Data
Encrypted using
B
LOCK CIPHER
Algorithm
Encrypted Code Data
Sync Value
28-bit Serial Number
Button
Status
(4 bits)
+
+
VLOW and
Repeat Status
(2 bits)
Serial Number and Button
2 bits
of Status
Fixed Code Data
Status (32 bits)
AN744
DS00744A-page 4 Preliminary 2001 Microchip Technology Inc.
Since the radio input is polled (for 1 µs) on multiples of
the base period (138 µs), the chance of a glitch (short
noise pulse) disturbing the receiver is reduced.
Further, since the time base produced is constant, the
same interrupt service routine could easily be extended
to implement a second UART as a separate state
machine for full duplex asynchronous communication
up to 1,200 baud at 4 MHz.
Note: This would also require the main oscillator
to be crystal based.
Other implementations of the same receiver module
can be obtained using other peripherals and detection
techniques. These include:
• Using the INT pin and selectable edge interrupt
source
• Using the Timer1 and CCP module in capture
mode
• Using comparator inputs interrupt
All of these techniques pose different constraints on the
pin out, or the PICmicro MCU, that can be used. This
would lead to different performances in terms of
achievable immunity from noise and or CPU overhead,
etc.
FAST DECRYPTION MODULE
This module contains an implementation of the KEELOQ
decryption algorithm that has been optimized for speed
on a mid-range PICmicro microcontroller. It allows fast
decryption times for maximum responsiveness of the
system even at 4 MHz clock.
The decryption function is also used in all learning
schemes and represents the fundamental building
block of all K
EELOQ decoders.
KEY GENERATION MODULE
This module shows a simple and linear implementation
of the Normal Learn Key Generation .
This module uses the KEELOQ Decrypt routine from the
Fast Decryption module to generate the key at every
received code-word instead of generating it during the
learn phase and storing it in memory. The advantage is
a smaller Transmitter Record of 8 bytes instead of 16
bytes (see Table 2). This translates in a double number
of transmitters that can be learned using the 64 byte
internal EEPROM available inside the PIC16F872.
This space reduction comes at the expense of more
computational power required to process every code-
word. When a new code-word is received, the key gen-
eration algorithm is applied (Normal Learn) and the
resulting Description key is placed in the array
DKEY[
0..7]. During a continous transmission (the
user is holding the button on the transmitter), the key
generation is not repeated, to save time, the last com-
puted Decryption Key value is used safely instead (the
serial number being the same).
Due to double buffering of the receiver and the
PICmicro MCU execution speed and efficiency (even
running at 4 MHz only), it is possible to receive and
decrypt, at the same time, each and every incoming
code-word.
For an overview of some of the different security levels
that can be obtained through the use of different key
generation/management schemes, refer to the "Secure
Data Products Handbook" [DS40168] (Section 1,
K
EELOQ Comparison Chart, Security Level Summary).
A detailed description of the Normal Learn key gener-
ation scheme can be found in Technical Brief TB003
"An Introduction To K
EELOQ Code Hopping"
[DS91002].
More advanced Key Generation Schemes can be
implemented replacing this module with the techniques
described in Technical Brief TB001 "Secure Learning
RKE Systems Using K
EELOQ Encoders" [DS91000].
TABLE MODULE
One of the major tasks of a decoder is to properly main-
tain a database that contains all the unique ID’s (serial
numbers) of the learned transmitters. In most cases,
the database can be as simple as a single table, which
associates those serial numbers to the synchronization
counters (that are at the heart of the hopping code
technology).
This module implements the easiest of all methods, a
simple "linear list" of records.
Each transmitter learned is assigned a record of 8
bytes (shown in Table 2), where all the relevant infor-
mation is stored and regularly updated.
TABLE 2: TRANSMITTER RECORD
Offset Data Description
+0
FCODE
Function code (4 bits) and
upper 4 Serial Number bits
[24..28]
+1
IDLo
Serial Number bits [0..7]
+2
IDHi
Serial Number bits [8..15]
+3
IDMi
Serial Number bits [16..23]
+4
SYNCH
Sync Counter 8 MSB
+5
SYNCL
Sync Counter 8 LSB
+6
SYNCH2
Second copy of SYNCH
+7
SYNCL2
Second copy of SYNCL
2001 Microchip Technology Inc. Preliminary DS00744A-page 5
AN744
The 16-bit synchronization counter value is stored in
memory twice because it is the most valuable piece of
information in this record. It is continuously updated at
every button press on the remote. When reading the
two stored synchronous values, the decoder should
verify that the two copies match. If not, it can adopt any
safe resync or disable technique required depending
on the desired system security level .
The current implementation limits the maximum num-
ber of transmitters that can be learned to eight. This is
due to the size of the internal EEPROM of the
PIC16F872.
This number can be changed to accommodate different
PICmicro models and memory sizes by modifying the
value of the constant
MAX_USER.
The simple "linear list" method employed can be scaled
up to some tens of users. But due to its simplicity, the
time required to recognize a learned transmitter grows
linearly with the length of the table.
It is possible to reach table sizes of thousands of trans-
mitters by replacing this module with another module
that implements a more sophisticated data structure
like a “Hash Table” or other indexing algorithms.
Again due to the simplicity of the current solution, it is
not possible to selectively delete a transmitter from
memory. The only delete function available is a Bulk
Erase (complete erase of all the memory contents) that
happens when the user presses the Learn button for up
to 10 seconds. (The LED will switch off. At the release
of the button, it will flash once to acknowledge the
delete command). To allow for selective transmitter
removal from memory, more sophisticated techniques
will be analyzed in future application notes, by simply
replacing/updating this module.
MEM-87X MODULE
This module is optimized to drive the internal EEPROM
of the PIC16F87X device.
The module make the memory generically accessible
by means of two routines RDword and WRword that
respectively read and write a 16-bit value out of an
even address specified in parameter IND.
Replacing this module with the appropriate drivers,
(and adapting the pin out) make possible the use of any
kind of nonvolatile memory. This includes internal and
external serial EEPROMs (Microwire
®
, SPI
™
or I
2
C
™
bus) of any size up to 64 Kbytes.
THE MAIN PROGRAM
The main program is reduced to a few pages of code.
The behavior is designed to mimic the basic behavior
of the HCS512 integrated decoder, although just the
parallel output is provided (no serial interface).
Most of the time, the main loop goes idle waiting for the
receiver to complete reception a full code-word.
Double buffering of the receiver is done in RAM, in
order to immediately re-enable the reception of new
codes and increase responsiveness and perceived
range.
CONCLUSION
The C language source increases the readability of the
program structure and eases the maintenance. This
benefit has come at the cost of the program size. That
in terms of memory words, has considerably increased
over the equivalent code written in assembly (more
than 30% larger).
Selecting a FLASH PICmicro microcontroller from the
mid-range family as the target MCU allows us to make
the code simpler and cleaner. It also provides larger
RAM memory space and a deeper hardware stack.
Interrupts have been used to "virtualize" the receiving
routine as a software peripheral and to free the design
of the hard real time constraint that it usually poses.
Still, many of the resources available on the
PIC16F872 are left unused and available to the
designer. These include:
• Timer1, a 16-bit timer
• Timer1 oscillator, a low power oscillator for real
time clock
• CCP module, capable of capture, compare and
PWM generation
• Timer2, an 8-bit timer, with auto reload
• 10-bit A/D converter with a 5 channel input
multiplexer
We resisted introducing extra features and optimiza-
tions in favor of clarity. For example:
• Speed optimizations and code compacting
• More complex key generation schemes
• Multiple manufacturer codes
• Co-processor functionality
• Advanced user entry and deletion commands
• Large memory tables (up to 8,000 users)
• Serial interface to PDAs and/or terminals for
memory management and logging
These are left as exercises to the advanced reader/
designer or as suggestions for further application
notes.
剩余27页未读,继续阅读
资源评论
bestwishes001
- 粉丝: 0
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功