本帖最后由 S1213 于 2013-9-4 15:21 编辑
http://ubiyubix.wordpress.com/20 ... -to-the-attiny4313/
Porting the Arduino IRremote library to the ATtiny4313 移植Arduino的ATtiny4313 IRremote图书馆 Posted on May 5, 2012 by ubiyubix 5月5日公布,2012年由ubiyubix Note: This covers the IR receive and decode part only. I may do the IR send part at some other time. 注意:这个涵盖了红外接收和解码部分只。我可能做的红外发送部分在其他一些时间。
Ken Shirriff wrote a popular multi-protocol IR library for the Arduino. It’s easy to use and works really well. When I wanted to add an IR receiver to a project based on the ATtiny2313, I turned to this library to see whether it could be ported to run on an ATtiny. It turned out to be next to impossible to squeeze the code into the 2K of flash memory the ATtiny2313 has, but I succeeded using the pin-compatible ATtiny4313. The ’4313 is more or less identical to the ’2313, but has twice the amount of ram and flash memory. 肯Shirriff写了一个流行的多协议为Arduino红外图书馆。它很容易使用和工作真的非常好。当我想要添加一个红外接收机项目基于ATtiny2313,我转到这个库是否可以移植到运行在一个ATtiny。它原来是几乎不可能挤到2 k的代码的ATtiny2313的闪存,但我成功使用销兼容ATtiny4313。4313是或多或少相同的2313,但有2倍的内存和闪存。
Step 1 步骤1
I started from Ken Shirriff’s code in http://arcfn.com/files/IRremote.zip. The Arduino code is C++, so the very first thing to do was to rename the files and switch from C++ classes to C structs and functions.
IRremote. cpp -> irrecv.c IRremote。cpp - > irrecv.c IRremote. h -> irrecv.h IRremote。irrecv.h h - > IRremoteInt. h -> irrecvint.h IRremoteInt。irrecvint.h h - > The IR receive code uses a timer interrupt function called every 50 microseconds. The original code used TIMER2 which does not exist in the ’4313. This chip has an 8-bit TIMER0 and a 16-bit TIMER1. Since I wanted to keep TIMER0 free for other uses, I changed to use TIMER1. 红外接收代码使用一个定时器中断函数称为每50微秒。原始的代码使用,不存在TIMER2在4313。这个芯片有一个8位和16位TIMER1 TIMER0。因为我想保持TIMER0免费用于其它用途,我改为使用TIMER1。
Everything compiled fine and I timed the interrupt function to clock in every 50us using an oscilloscope. A skeleton program using the IR receive library would look something like this: 一切都好,我的编译时间中断功能,时钟在每50我们使用示波器。一个骨架程序使用红外接收图书馆看起来像这样:
#include "irrecv.h" # include“irrecv.h”
void process(decode_results *r) { /* do cool stuff here */ } 无效过程(解码结果* r){ / *做很酷的东西在这里* / }
int main(void) int main(void) { { decode_results r; 解码结果r; setup_irrecv(); setup_irrecv(); for (;;) { (,,){ if (irrecv_decode(&r)) { 如果(irrecv_decode(描述)){ process(&r); 过程(描述); irrecv_resume(); irrecv_resume(); } } } } return 0; 返回0; } } But, there is a problem: 但是,有一个问题:
avr-size -t main. o irrecv.o avr大小- t主要。irrecv.o啊 text data bss dec hex filename 文本数据bss 12月六角文件名 64 0 0 64 40 main.o 64 0 0 64 40主啊 1612 0 0 1612 64c irrecv.o 1612 0 0 1612 64c irrecv.o 1676 0 0 1676 68c (TOTALS) 1676 0 0 1676 68c(总数) avr-size main.elf avr大小主要精灵 text data bss dec hex filename 文本数据bss 12月六角文件名 4676 264 140 5080 13d8 main.elf 4676 264 140 5080 13d8主要精灵 The size of the object code of the library and the minimal main program is way less than 2K, but the resulting binary has about 5k. This won’t fit in the 4K available on the ’4313. 对象的大小码的图书馆和最小主程序是大大低于2 k,但由此产生的二进制有大约5公里。这个不适合在4 k可在4313。
Step 2 步骤2
The additional code must come from the C library the avr-gcc links to. So, let’s disassemble the . hex file to see what’s in there. 额外的代码必须来自C库链接的avr gcc。所以,让我们拆开。十六进制文件,看看里面有什么。
avr-objdump -d main.elf avr objdump - d主要精灵 Whoa! What’s the deal with all those floating point functions? I do not recall seeing any float variables in the IR code. 哇!有什么处理所有浮点功能呢?我不记得看到任何浮动变量红外代码。
000006dc <_fpadd_parts>: 000006直流< _fpadd_parts >: 0000096c <__addsf3>: 0000096 c < __addsf3 >: 000009ba <__subsf3>: 000009 ba < __subsf3 >: 00000a10 <__mulsf3>: 00000 a10 < __mulsf3 >: 000006dc <_fpadd_parts>: 000006直流< _fpadd_parts >: [...] […] The code allows the timing of the incoming IR signals to be off be a certain percentage. The percentage is first calculated using floating point, but the result will be converted to an integer in the end: 这个代码允许定时的红外信号不再是某个百分比。这个比例是第一个使用浮点数计算,但其结果将被转换成一个整数最后:
#define TOLERANCE 25 // percent tolerance in measurements #定义公差25 / /百分比公差在测量 #define LTOL (1.0 - TOLERANCE/100.) #定义LTOL(1.0 -公差/ 100)。 #define UTOL (1.0 + TOLERANCE/100.) #定义UTOL(1.0 +公差/ 100)。 /* ... */ / *……* / #define TICKS_LOW(us) (int) (((us)*LTOL/USECPERTICK)) #定义蜱低(美国)(int)(((美国)* LTOL / USECPERTICK)) #define TICKS_HIGH(us) (int) (((us)*UTOL/USECPERTICK + 1)) #定义蜱高(美国)(int)(((美国)* UTOL / USECPERTICK + 1)) I changed this to to get rid of the intermediate floating point arithmetic. 我改变了这摆脱中间浮点运算。
#define LTOL (100 - TOLERANCE) #定义LTOL(100 -公差) #define UTOL (100 + TOLERANCE) #定义UTOL(100 +公差) /* ... */ / *……* / #define TICKS_LOW(us) (int) (( (long) (us) * LTOL / (USECPERTICK * 100) )) #定义蜱低(美国)(int)(((长)(美国)* LTOL /(USECPERTICK * 100))) #define TICKS_HIGH(us) (int) (( (long) (us) * UTOL / (USECPERTICK * 100) + 1)) #定义蜱高(美国)(int)(((长)(美国)* UTOL /(USECPERTICK * 100)+ 1)) This results in a much reduced code size: 这个结果在一个更减少了代码大小:
avr-size main.elf avr大小主要精灵 text data bss dec hex filename 文本数据bss 12月六角文件名 1950 0 140 2090 82a main.elf 1950 0 140 2090 82a主要精灵 Step 3 – Putting it all together 第三步,把它们结合在一起
I tested this using a modified Larson Scanner circuit from Evil Mad Science. The schematics are here and this is what I have changed: 我测试了这个使用修改后的拉森扫描电路从邪恶疯狂科学。图在这里,这就是我改变了:
Used ’4313 instead of ’2312 用“4313年而不是2312年 Replaced the button at port PB4 with an IR detector. I used a Vishay TSOP 4838. 取代了按钮和一个红外探测器PB4港。我使用一个威世TSOP 4838。 Added an activity indicator LED at port PB5. 添加一个活动指示器领导PB5港。 The remote control is a cheap RC5 universal remote from dx.com. 远程控制是一种廉价的从dx.com RC5万能遥控器。
The code repository is on github. 代码存储库是在github上。
git clone git://github.com/alohr/avr.git git克隆git:/ / github.com/alohr/avr.git cd avr/irrecv cd / irrecv avr # edit Makefile according to your programmer and clock frequency #编辑Makefile根据你的程序员和时钟频率
# update flash *and* change AVR fuses: #更新闪光*和*改变AVR融合: make install 使安装
# flash only: # flash只有: make flash 使闪光 Clock frequencies I tried were 8 Mhz using the avr’s internal oscillator and 16 Mhz with an external crystal. 时钟频率我试着8 Mhz使用avr的内部振荡器和16兆赫与外部晶体。
Some links I found useful during this project 一些链接我发现有用的在这个项目
The 4313 has landed (evilmadscientist.com) 4313年已登陆(evilmadscientist.com) Newbie’s Guide to AVR Timers (avrfreaks.net) 新手指南AVR计时器(avrfreaks.net) Philips RC-5 Protocol (sbprojects.com) 飞利浦rc 5协议(sbprojects.com) http://www.ladyada.net/learn/sensors/ir.html (ladyada.net) http://www.ladyada.net/learn/sensors/ir.html(ladyada.net)
|