Author Topic: Help with ATTiny13 and Infra Red  (Read 1388 times)

0 Members and 1 Guest are viewing this topic.

Offline CyberBobTopic starter

  • Contributor
  • Posts: 20
  • Country: gb
Help with ATTiny13 and Infra Red
« on: September 14, 2020, 11:24:12 am »
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 3

void 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 appreciated

Thanks
 

Offline HwAoRrDk

  • Super Contributor
  • ***
  • Posts: 1478
  • Country: gb
Re: Help with ATTiny13 and Infra Red
« Reply #1 on: September 14, 2020, 12:52:00 pm »
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: [Select]
#ifdef __cplusplus
extern "C" {
#endif

/*** existing contents of ir.h here ***/

#ifdef __cplusplus
}
#endif

Or, you could do this in your main sketch:

Code: [Select]
extern "C" {
#include "ir.h"
}
« Last Edit: September 14, 2020, 12:53:34 pm by HwAoRrDk »
 
The following users thanked this post: CyberBob

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9953
  • Country: nz
Re: Help with ATTiny13 and Infra Red
« Reply #2 on: September 14, 2020, 12:53:08 pm »
You'll probably want to use on of the arduino IR libraries instead of ir.h
Greek letter 'Psi' (not Pounds per Square Inch)
 
The following users thanked this post: CyberBob

Offline madires

  • Super Contributor
  • ***
  • Posts: 7765
  • Country: de
  • A qualified hobbyist ;)
Re: Help with ATTiny13 and Infra Red
« Reply #3 on: September 14, 2020, 01:16:06 pm »
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.
 
The following users thanked this post: CyberBob

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12862
Re: Help with ATTiny13 and Infra Red
« Reply #4 on: September 14, 2020, 01:46:03 pm »
First you'll need to add ATiny13 support to your Arduino IDE.  See: https://github.com/MCUdude/MicroCore and 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: [Select]
/**
 * 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 as IRtiny13.  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.c
to 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 called IRtiny13.h and paste the following into it:
Code: [Select]
/**
 * 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 57600
Finally, go to each .c tab and add the line:
Code: [Select]
#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.
 
The following users thanked this post: CyberBob

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Help with ATTiny13 and Infra Red
« Reply #5 on: September 15, 2020, 02:10:46 am »
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.

 
The following users thanked this post: CyberBob

Offline CyberBobTopic starter

  • Contributor
  • Posts: 20
  • Country: gb
Re: Help with ATTiny13 and Infra Red
« Reply #6 on: September 15, 2020, 09:26:36 am »
Thanks for your reply Psi

Unfortunately I've tried this to no avail as the Arduino IR library references two clocks and the ATTiny 13 only has one.

Thanks

Rob
« Last Edit: September 15, 2020, 09:37:26 am by CyberBob »
 

Offline CyberBobTopic starter

  • Contributor
  • Posts: 20
  • Country: gb
Re: Help with ATTiny13 and Infra Red
« Reply #7 on: September 15, 2020, 09:33:07 am »
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 again

Rob
« Last Edit: September 15, 2020, 09:38:08 am by CyberBob »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf