Skip to main content
11 votes

Different ways (and the fastest) to compute sines (and cosines) in Arduino

The two basic methods are mathematical calculation (with polynomials) and lookup tables. The Arduino's math library (libm, part of avr-libc) uses the former. It is optimised for the AVR in that it ...
Majenko's user avatar
  • 105k
10 votes

How to use assembly in Arduino?

The Arduino IDE is quite idiosyncratic. It won't let you work on a project that doesn't have a file with the extension “.ino”. On the other hand, it doesn't care about how much code is in that file, ...
Edgar Bonet's user avatar
  • 43.3k
8 votes

Different ways (and the fastest) to compute sines (and cosines) in Arduino

There are some good answers here but I wanted to add a method which hasn't been mentioned yet, one very well suited to computing trigonometric functions on embedded systems, and that's the CORDIC ...
Halzephron's user avatar
7 votes

Different ways (and the fastest) to compute sines (and cosines) in Arduino

I have been playing a bit with computing sines and cosines on the Arduino using fixed-point polynomial approximations. Here are my measurements of average execution time and worst case error, compared ...
Edgar Bonet's user avatar
  • 43.3k
5 votes
Accepted

Building an AtMega328p from Transistors

Yes, it is possible - after all, the Arduino is essentially just a bunch of incredibly tiny transistors. Early computers, before ICs were invented, were built from transistors. Before that it was ...
Majenko's user avatar
  • 105k
5 votes
Accepted

Does the bootloader use some SRAM?

You did not specify what type of Arduino you are using. My answer is for the AVR-based ones. The bootloader does use RAM while it is running. However, once it handles control to your program, the ...
Edgar Bonet's user avatar
  • 43.3k
5 votes
Accepted

Arduino Uno R3 assembly code to push a button and turn on a LED

This line: ldi r18, 0b00100000 ;pushed button does not match the definition of BUTTON. You are setting bit 5 of the register, whereas the button is on bit 3. In order to avoid this ...
Edgar Bonet's user avatar
  • 43.3k
4 votes

Different ways (and the fastest) to compute sines (and cosines) in Arduino

I looked for other people that had approximated cos() and sin() and I came across this answer: dtb's answer to "Fast Sin/Cos using a pre computed translation array" Basically he computed that the ...
sa_leinad's user avatar
  • 3,188
4 votes

Different ways (and the fastest) to compute sines (and cosines) in Arduino

You could create a couple of functions that uses linear approximation to determine the sin() and cos() of a particular angle. I am thinking something like this: For each I have broken the graphical ...
sa_leinad's user avatar
  • 3,188
3 votes
Accepted

How to use arduino IDE to compile .s files

The IDE should be able to accept .S files (not .s - the difference is very important: .S will be preprocessed, .s won't). Just create a new sketch, and create a new tab called whatever.S (obviously ...
Majenko's user avatar
  • 105k
3 votes

Different ways (and the fastest) to compute sines (and cosines) in Arduino

A lookup table will be the fastest way to find sines. And if you're comfortable computing with fixed-point numbers (integers whose binary-point is somewhere other than to the right of bit-0), your ...
JRobert's user avatar
  • 15.3k
3 votes

Different ways (and the fastest) to compute sines (and cosines) in Arduino

Just for the fun of it, and to prove it can be done, I finished an AVR assembly routine to calculate sin(x) results in 24 bits (3 bytes) with one bit of error. The input angle is in degrees with one ...
Wagner Lip's user avatar
3 votes
Accepted

Using Arduino with Assembly on MacOS

You can add source code to the sketch in the Arduino IDE. When adding a new source code file make sure the extension is uppercase .S Files with .S extension will be compiled as Assembly. There is an ...
Visual Micro's user avatar
  • 1,027
3 votes

Make LED blink using AVR assembly

It appears that your intent is to set TCCROB up with a 1024x prescale, and each time it counts up to 128, increment r20, and toggle the LED each time r20 overflows to zero. That sequence ought to ...
James Waldby - jwpat7's user avatar
3 votes

Building an AtMega328p from Transistors

I do not think you could build an ATmega328P out of discrete parts: you would need floating-gate transistors for implementing the flash, and these do not seem to be available as discretes. If you ...
Edgar Bonet's user avatar
  • 43.3k
3 votes
Accepted

Trying to run Timer1 in ATmega2560 with assembly

Here: ldi r20, 0b00000010 ; CTC mode, int clk; sts TCCR1A, r20 ldi r20, 0b000000101 ; prescaler /1024 sts TCCR1B, r20 you are setting the bit WGM11 on TCCR1A. The timer then runs in mode 2 (PWM,...
Edgar Bonet's user avatar
  • 43.3k
3 votes

How do I convert this program to assembly from c++

The obvious solution for converting any program to assembly is... to compile it! You can disassemble the resulting binary with a command like: avr-objdump -S -C the_program.elf > the_program.lss ...
Edgar Bonet's user avatar
  • 43.3k
3 votes
Accepted

Am not able to call C++ function pointers from inline assembly

It seems you got confused by the pointer indirections, which is confounded by the implicit indirections made by the compiler. Here: extern "C" void __attribute__ ((used, noinline)) call(...
Edgar Bonet's user avatar
  • 43.3k
3 votes
Accepted

LED Blink in assembly with timer1 output compare interrupt not working

I am not set up to test your code right now, perhaps later. But: ; Set the prescalar to 256 in the Timer Control 1B register ldi r16, 0b00000111 sts TCCR1B, r16 When set to ones,...
timemage's user avatar
  • 5,299
3 votes
Accepted

Loading code to a mega 2560

Only from bootloader section is it possible to write to flash on ATmega2560. Optiboot 8 has a do_spm function to write to flash. It is accesible over a header file available in examples in the ...
Juraj's user avatar
  • 18k
3 votes

Is it possible to use assembly with arduino?

I suggest the ARV libC project as a starting point to HOW to get assmbler code running. And the AVR Instruction Set Manual to get hold of the actual assembler mnemonics. Maybe also my own AVR Assembly ...
Kwasmich's user avatar
  • 1,513
2 votes

Force compiler to execute instructions sequentially?

You can use the optimize function attribute to change the compiler optimization level for an individual function. That's the closest you'll get. optimize The optimize attribute is used to ...
Majenko's user avatar
  • 105k
2 votes
Accepted

Arduino PWM fading led in assembly

I tried your program, after converting it to GNU-as syntax. It works as expected, as seen on the scope. Your problem may be simply that the difference between 50% ON and 100% ON is not very obvious to ...
Edgar Bonet's user avatar
  • 43.3k
2 votes
Accepted

Working on a reaction test for the Arduino Uno using an led and an lcd shield

Expanding on my comment... I see a few issues in your asm code, not all as serious as the first one: killms is calling itself recursively indefinitely, which should overflow your stack. The last ...
Edgar Bonet's user avatar
  • 43.3k
2 votes
Accepted

Make LED blink using AVR assembly

There is a bug with the logic of your program: once TCNT0 gets larger than 128, it will continue to be larger than 128 for quite some time... during which time you are continuously incrementing your ...
Edgar Bonet's user avatar
  • 43.3k
2 votes

Building an AtMega328p from Transistors

Try to build out the entire thing inside a simulator instead of buying 10s of thousands of transistors and several other components....less messy and gets the job done at almost no expense
user221238's user avatar
2 votes

Different ways (and the fastest) to compute sines (and cosines) in Arduino

I had a simillar question to OP. I wanted to make a LUT table to calculate the first quadrant of the sine function as unsigned 16 bit integers starting from 0x8000 to 0xffff. And i ended up writing ...
Arnadath's user avatar
2 votes

Building an AtMega328p from Transistors

The ATmega128 have 800000 transistors. (+-200000 transistors) So If you want to take some transistors and breadboards and build an ATmega128p first count the money for transistors (if 1 transistor ...
ARISTOS's user avatar
  • 121
2 votes

Different ways (and the fastest) to compute sines (and cosines) in Arduino

As others have mentioned lookup tables are the way to go if you want speed. I've recently been investigating computation of trig functions on an ATtiny85 for use of fast vector averages (wind in my ...
acicuc's user avatar
  • 31
2 votes

I need help programming my arduino in assembly

In the case, that this is a school assignment and you actually have to program directly in assembler, you probably don't want to directly convert your C++ sketch. It will generate way more assembler ...
chrisl's user avatar
  • 16.3k

Only top scored, non community-wiki answers of a minimum length are eligible