Help with ATTiny13 and Infra Red - Page 1
Forum section: electronics / general

author: CyberBob
author_rank: Contributor
timestamp: September 14, 2020, 11:24:12 am
content: I'm trying to read the codes from an IR remote control and display them on the Serial Monitor of an Arduino IDE. I've seen an example here:https://blog.podkalicki.com/attiny13-ir-receiver-nec-proto-analyzer/This program is written in "C" but I need a program that the Arduino IDE is happy with. I've tried to modify the code mentioned above but I can't get it to compile. Here's what I have so far:#include "ir.h"const int IRPin = PB4; // physical pin 3void setup() {IR_init();Serial.begin(57600);pinMode(IRPin, OUTPUT);uint8_t addr, cmd;}void loop() {uint8_t addr, cmd;while (1)if (IR_read(&addr, &cmd) == IR_SUCCESS){Serial.print("addr= ");Serial.print(addr);Serial.print (',');Serial.print("cmd= ");Serial.print (cmd);Serial.println (' ');}}I get the following errors:undefined reference to `IR_init()'undefined reference to `IR_read(unsigned char*, unsigned char*)Any help would be greatly appreciatedThanks

author: HwAoRrDk
author_rank: Super Contributor
timestamp: September 14, 2020, 12:52:00 pm
content: The Arduino IDE treats sketch files as C++, not C. This is what is causing the compiler not to recognise the functions declared in ir.h, even though you have it included correctly.You need to modify ir.h to add the following:Code:#ifdef __cplusplusextern "C" {#endif/*** existing contents of ir.h here ***/#ifdef __cplusplus}#endifOr, you could do this in your main sketch:Code:extern "C" {#include "ir.h"}

author: Psi
author_rank: Super Contributor
timestamp: September 14, 2020, 12:53:08 pm
content: You'll probably want to use on of the arduino IR libraries instead of ir.h

author: madires
author_rank: Super Contributor
timestamp: September 14, 2020, 01:16:06 pm
content: I'd recommend to use the avr-gcc, make and avrdude tool chain directly. My guess is that the Arduino IDE doesn't support the ATtiny13 anyway. BTW, the ATtiny13 has just 1kB flash and 64 Bytes RAM.

author: Ian.M
author_rank: Super Contributor
timestamp: September 14, 2020, 01:46:03 pm
content: First you'll need to add ATiny13 support to your Arduino IDE. See:https://github.com/MCUdude/MicroCoreand jump down to 'Boards Manager Installation'. Once installed read the page I linked *properly*!You can force the Arduino IDE's code munger (that's supposed to make it easy to use by auto-generating function prototypes) to *NOT* parse your C/C++ source during build and ruin it!Here's what should be in your sketch:Code:/*** Dummy .ino file for C/C++ compatibility.* See: http://www.gammon.com.au/forum/?id=12625** N.B. DO *NOT* call any file in the project 'main.cpp' - it clashes with an* Arduino one. Also, no .c or .cpp file can have the same name as the sketch.** This file should be empty apart from documentation comments and a list of* *LIBRARY* headers (i.e. extra Arduino libraries #included as <...>, not part* of the core) to tell IDE what to build with. eg:** #include <Servo.h> // Arduino Servo library**//*** Copyright (c) 2016, Łukasz Marcin Podkalicki <lpodkalicki@gmail.com>* ATtiny13/013* NEC proto analyzer. Example of monblocking IR signal reader (38kHz, TSOPxxx) and NEC protocol decoder.* Settings:* FUSE_L=0x7A* FUSE_H=0xFF* F_CPU=9600000*/I suggest saving the sketch asIRtiny13. You *MUST* then close it.You'll need to copy all the source files and headers from Łukasz's Github repository:ir.h,uart.h,ir.c,main.c,uart.cto your sketch folder. You can now reopen the sketch.You'll need somewhere to put global #defines as the Arduino IDE doesn't let you set them to be passed to GCC on a per-sketch basis, so add a new tab (file) to the sketch calledIRtiny13.hand paste the following into it:Code:/*** Project global #defines** The Arduino IDE desn't let you pass global defines to GCC so we* must #include them in *all* .c and .cpp files in the project as:** #include "IRtiny13.h" // Project global #defines**/#define F_CPU (9600000UL) // 9.6MHz internal oscillator#define UART_BAUDRATE (115200UL) // Min. baud rate at this F_CPU is 57600Finally, go to each .c tab and add the line:Code:#include "IRtiny13.h" // Project global #definesbefore any other #include lines.It should now build cleanly. Its up to you to figure out how the fuses need to be set (See:https://www.engbedded.com/fusecalc) and how to program an ATtiny13 from the Arduino IDE.

author: westfw
author_rank: Super Contributor
timestamp: September 15, 2020, 02:10:46 am
content: If you're creating an Arduino sketch, rename your "ir.c" file tab to "ir.cpp"that will (probably) make it compatible with having its functions called from your "sketch" main program, and it's simpler than modifiying the .h files to handle both C and C++, or coming up with an alternate build procedure.

author: CyberBob
author_rank: Contributor
timestamp: September 15, 2020, 09:26:36 am
content: Thanks for your reply PsiUnfortunately I've tried this to no avail as the Arduino IR library references two clocks and the ATTiny 13 only has one.ThanksRob

author: CyberBob
author_rank: Contributor
timestamp: September 15, 2020, 09:33:07 am
content: Thank you all for your replies. I'm (literally) overwhelmed. I'll try and work through all your suggestions.The example that I quoted in my original post apparently works. It uses ir.h and UART. I am just after a sketch that will use the ir.h library with Serial output using the Arduino IDE. I'm okay using the Arduino as an ISP to upload to the ATTiny13A but need the original sketch adapted for the Arduino IDE. I'm using the ATTiny core by James Sleeman which I have used extensively for Serial communication without any problems.Thank you all againRob