ASM to Arduino - Page 1
Forum section: electronics / beginners

author: metrologist
author_rank: Super Contributor
timestamp: January 16, 2019, 05:11:11 pm
content: There are some analogues to the PICDIV for Arduino, but if I recall correctly, the code is in .asm files. How do I use these?

author: brucehoult
author_rank: Super Contributor
timestamp: January 16, 2019, 05:51:34 pm
content: You could tryhttp://www.ulrich-bangert.de/PPSDividerMega8.zipThat simultaneously provides 8 outputs (a complete output port) with frequencies 100 kHz, 10 kHz, 1 kHz, 100 Hz, 10 Hz, 1 Hz, 0.1 Hz and 0.01 Hz assuming a 10 MHz input clock.

author: fsr
author_rank: Regular Contributor
timestamp: January 16, 2019, 05:58:17 pm
content: I have taken a look at the PicDIV website, and it seems to be an assembler project for very high precision frequency division with pic devices. Doesn't seems to be a way to add that to another project, much less arduino (obviously). This will convert the pic device into a precision frequency divider, and that's it. If you need to do something else, you need another microcontroller (well,maybein assembler you could stuff some other functionality there, but that would require analyzing the code and adding code to it, trying to avoid upsetting the divider, if at all possible)..ASM is the source code for assembler..HEX is the file to upload to the microcontroller with a programmer.I would expect the same to apply to the AVR version.

author: metrologist
author_rank: Super Contributor
timestamp: January 16, 2019, 06:35:26 pm
content: Quote from: fsr on January 16, 2019, 05:58:17 pmThere is an analog to picdiv, called ppsdiv and it is written for Arduino. From this site:http://www.ulrich-bangert.de/html/downloads.htmlhttp://www.ulrich-bangert.de/PPSDividerMega8.zipThere is a hex file in the zip package as well and maybe that can be burned to the chip. I've not done that either.

author: fsr
author_rank: Regular Contributor
timestamp: January 16, 2019, 11:27:50 pm
content: Quote from: metrologist on January 16, 2019, 06:35:26 pmAre you looking for something that you can load in the Arduino gui and use inside an Arduino sketch, or are you looking for anything that will run inside an AVR microcontroller? Those two are different requirements.

author: brucehoult
author_rank: Super Contributor
timestamp: January 17, 2019, 06:37:22 am
content: Not *very* different, as you can happily put assembly language source code into the Arduino IDE, call it from your setup() function, and never return.

author: hamster_nz
author_rank: Super Contributor
timestamp: January 17, 2019, 07:45:21 am
content: Quote from: brucehoult on January 17, 2019, 06:37:22 amFor precise timing just remember to disable the interrupts.Yes, I am looking at you, the timer that updates the millisecond counter...

author: fsr
author_rank: Regular Contributor
timestamp: January 17, 2019, 03:47:37 pm
content: Quote from: brucehoult on January 17, 2019, 06:37:22 amWell, if the Arduino ide happily accepts the code, and you disable all interrupts, and all the instructions take the same amount of cycles than in the mega8, and the arduino board has a real crystal oscillator, and it's frequency if the right one, i suppose it could work, but then the Arduino can't do anything else.

author: metrologist
author_rank: Super Contributor
timestamp: January 17, 2019, 03:51:04 pm
content: Arduino IDE is not happy with the ASM code.fsr, I simply want to load the PPSDiv code to arduino. I think it works "out of the box" so I do not expect to have to modify it, I just want to get the code onto a 328p chip. I think there is a way to burn the hex file which is included. I recall burning the bootloader onto bare chips so I may review how I did that, so many years ago... But to take a step back, I don't really need this code, but any code that will do the same thing. I could also just order some pic chips and try the picdiv code, but I don't have those chips and I have plenty of Arduinos.This is the ASM code I tried to compile in Arduino IDE.Code:#include <m32def.inc>void setup(){init:LDIR16,0xFFOUTDDRA,R16CLRR15CLRR14CLRR13CLRR12CLRR11CLRR10CLRR9CLRR8void loop(){LDIR16,0x0AINCR15CPR15,R16BRNEnd1CLRR15nd1:BRNEnd1wINCR14nd1w:CPR14,R16BRNEnd2CLRR14nd2:BRNEnd2wINCR13nd2w:CPR13,R16BRNEnd3CLRR13nd3:BRNEnd3wINCR12nd3w:CPR12,R16BRNEnd4CLRR12nd4:BRNEnd4wINCR11nd4w:CPR11,R16BRNEnd5CLRR11nd5:BRNEnd5wINCR10nd5w:CPR10,R16BRNEnd6CLRR10nd6:BRNEnd6wINCR9nd6w:CPR9,R16BRNEcreadyCLRR9cready: CLTBLDR8,0OUTPORTA,R8LDIR17,08WLP0:DECR17BRNEWLP0CLRR8LDIR16,0x05SETBLDR8,0CPR15,R16BRCSd2BLDR8,1d2:CPR14,R16BRCSd3BLDR8,2d3:CPR13,R16BRCSd4BLDR8,3d4:CPR12,R16BRCSd5BLDR8,4d5:CPR11,R16BRCSd6BLDR8,5d6:CPR10,R16BRCSd7BLDR8,6d7:CPR9,R16BRCSokBLDR8,7ok:OUTPORTA,R8LDI R17,03WLP1: DEC R17BRNE WLP1NOPRJMP}

author: darrellg
author_rank: Unknown
timestamp: January 17, 2019, 04:26:07 pm
content: Quote from: metrologist on January 17, 2019, 03:51:04 pmThat's not how you do it. A ten second search on Google will show you the right way to use asm in the Arduino IDE:https://ucexperiment.wordpress.com/2016/03/04/arduino-inline-assembly-tutorial-1/

author: metrologist
author_rank: Super Contributor
timestamp: January 17, 2019, 06:07:26 pm
content: Quote from: darrellg on January 17, 2019, 04:26:07 pmYes, knowing that something exists and finding it takes 10 seconds.QuoteI tried working with the tutorial and here is where I ended up.Code:#include <m32def.inc>void setup(){asm ("init: \n""LDIR16,0xFF \n""OUTDDRA,R16 \n""CLRR15 \n""CLRR14 \n""CLRR13 \n""CLRR12 \n""CLRR11 \n""CLRR10 \n""CLRR9 \n""CLRR8 \n""loop: \n""LDIR16,0x0A \n""INCR15 \n""CPR15,R16 \n""BRNEnd1 \n""CLRR15 \n""nd1:BRNEnd1w \n""INCR14 \n""nd1w:CPR14,R16 \n""BRNEnd2 \n""CLRR14 \n""nd2:BRNEnd2w \n""INCR13 \n""nd2w:CPR13,R16 \n""BRNEnd3 \n""CLRR13 \n""nd3:BRNEnd3w \n""INCR12 \n""nd3w:CPR12,R16 \n""BRNEnd4 \n""CLRR12 \n""nd4:BRNEnd4w \n""INCR11 \n""nd4w:CPR11,R16 \n""BRNEnd5 \n""CLRR11 \n""nd5:BRNEnd5w \n""INCR10 \n""nd5w:CPR10,R16 \n""BRNEnd6 \n""CLRR10 \n""nd6:BRNEnd6w \n""INCR9 \n""nd6w:CPR9,R16 \n""BRNEcready \n""CLRR9 \n""cready: CLT \n""BLDR8,0 \n""OUTPORTA,R8 \n""LDIR17,08 \n""WLP0:DECR17 \n""BRNEWLP0 \n""CLRR8 \n""LDIR16,0x05 \n""SET \n""BLDR8,0 \n""CPR15,R16 \n""BRCSd2 \n""BLDR8,1 \n""d2:CPR14,R16 \n""BRCSd3 \n""BLDR8,2 \n""d3:CPR13,R16 \n""BRCSd4 \n""BLDR8,3 \n""d4:CPR12,R16 \n""BRCSd5 \n""BLDR8,4 \n""d5:CPR11,R16 \n""BRCSd6 \n""BLDR8,5 \n""d6:CPR10,R16 \n""BRCSd7 \n""BLDR8,6 \n""d7:CPR9,R16 \n""BRCSok \n""BLDR8,7 \n""ok:OUTPORTA,R8 \n""LDI R17,03 \n""WLP1: DEC \n""BRNE WLP1 \n""NOP \n""RJMP \n");}void loop(){}==========================Error compiling:C:\Users\\AppData\Local\Temp/ccYDtlBP.s: Assembler messages:C:\Users\\AppData\Local\Temp/ccYDtlBP.s:31: Error: constant value requiredC:\Users\\AppData\Local\Temp/ccYDtlBP.s:78: Error: constant value requiredC:\Users\\AppData\Local\Temp/ccYDtlBP.s:79: Error: garbage at end of lineC:\Users\\AppData\Local\Temp/ccYDtlBP.s:107: Error: constant value requiredC:\Users\\AppData\Local\Temp/ccYDtlBP.s:109: Error: constant value requiredC:\Users\\AppData\Local\Temp/ccYDtlBP.s:112: Error: missing operandC:\Users\\AppData\Local\Temp/ccYDtlBP.s:124: Error: symbol `loop' is already definedC:\Users\\AppData\Local\Temp/ccYDtlBP.s:133: Error: operation combines symbols in different segmentsMy favorite error is the "garbage at the end of line"My objective, however, is not to learn asm. And as a matter of fact, I have resigned myself from any sort of programming expectations. I just don't seem to be able to do it.

author: fsr
author_rank: Regular Contributor
timestamp: January 17, 2019, 07:35:02 pm
content: Quote from: metrologist on January 17, 2019, 03:51:04 pmI see, so you want to get the .HEX into an atmega328p. I suppose that you don't have an AVR programmer, but you can see how to use one Arduino as a programmer for an stand-alone AVR in this link:https://learn.sparkfun.com/tutorials/installing-an-arduino-bootloader/allOr you could buy a cheap-ass usbasp programmer to do the same. I use one of them, and the commands for avrdude that i use most often are the following:This line sets the fuses (normally you only need to do this once). Check the attached libreoffice document with details about the fuses settings (those are for 5v operation with full-swing crystal oscillator, and some other miscellaneous options, YMMV):avrdude -p m328p -c usbasp -U lfuse:w:0xf7:m -U hfuse:w:0xd9:m -U efuse:w:0xfc:mAnd this line is to upload the hex file:avrdude -p m328p -c usbasp -U flash:w:YOURHEXFILE.hexNote that the AVR won't function as an Arduino anymore after doing that. If you want to make it work again, you possibly will need to reprogram the fuses, and upload another HEX file with the Arduino bootloader to the AVR.

author: metrologist
author_rank: Super Contributor
timestamp: January 17, 2019, 09:04:08 pm
content: Oh yeah, that's right, I used an Arduino to program the bootloader. I also bought one of those AVRASP devices but could never get it to work in windows.I also had AVR studio set up and used it a couple times, but it seemed to have so much overhead that I stopped using it.I'll breadboard up a chip and if I get it working I'll make this a dedicated divider module.

author: fsr
author_rank: Regular Contributor
timestamp: January 18, 2019, 01:55:55 am
content: Did you use avrdude with the usbasp? I used it quite a lot of times under windows, and no problems. Should work.

author: metrologist
author_rank: Super Contributor
timestamp: January 18, 2019, 03:22:02 am
content: I think it was that Windows did not see the ASP device. That might be another topic.Because I was reading about it and that I found a vendor 10 miles away, I ordered 10 of PIC12F675. This seems a bit better for my application, and I have a pickit2 that needs to be used at least twice.Thanks

author: fsr
author_rank: Regular Contributor
timestamp: January 18, 2019, 09:56:41 pm
content: You're welcome.Makes sense to have the PICs, then you can just program them easily and use them with the original project.

author: westfw
author_rank: Super Contributor
timestamp: January 19, 2019, 10:21:47 am
content: QuoteIt's not "Arduino code", it's an "Atmel Studio" "Project", written for a bare AVR chip.That means:the .ASM files are written for the Atmel Assembler.The won't get compiled from the Arduino IDE, which only knows about the Gnu Assembler.If you somehow compiled them with the Atmel Assembler, they still wouldn't be linkable into an Arduino "sketch", because the Atmel Assembler doesn't produce linkable object files.Your best bet is to build the project with Studio, and upload it to your Arduino via the bootloader.(actually, the idea of converting it all to inline ASM isn't awful, since it's quite short. You'll need to mess with the options for handling symbols in inline ASM, though. And don't use symbols that are already defined ("loop", "init"))hExcept that if it's capable of dividing 10MHz, it's probably expecting the "input frequency" on the clock pin, which is not compatible with Arduino hardware (that already has a 16MHz resonator there.)Quoteinline assembly isn't the only way, nor even the best way, to include ASM in an Arduino sketch. As of recent versions (not THAT recent), it will happily accept .S files in the sketch directory.)

author: westfw
author_rank: Super Contributor
timestamp: January 19, 2019, 10:56:44 am
content: Hmm. Your attempt to produced inline ASM was pretty credible.I already mentioned symbol conflicts...QuoteGet rid of that; it's avr assembler, and the equivalent is already included.QuoteBut symbols need to be referenced "specially." Seehttps://www.nongnu.org/avr-libc/user-manual/inline_asm.html(The tutorial you found wasn't very good.)QuoteThere was a type or two - missing argument for DEC, here. And I think one other...QuoteAnd THAT, of course, is the gnu assembler's way of telling you that "08" is not a valid octal number. Obviously.It's a C think that nn is decimal, 0nn is octal, and 0xnn is hex...Try this version:Code:#include <avr/io.h>void setup(){asm ("LDI R16,0xFF \n""OUT %0,R16 \n""CLR R15 \n""CLR R14 \n""CLR R13 \n""CLR R12 \n""CLR R11 \n""CLR R10 \n""CLR R9 \n""CLR R8 \n""pploop: \n""LDI R16,0x0A \n""INC R15 \n""CP R15,R16 \n""BRNE nd1 \n""CLR R15 \n""nd1: BRNE nd1w \n""INC R14 \n""nd1w: CP R14,R16 \n""BRNE nd2 \n""CLR R14 \n""nd2: BRNE nd2w \n""INC R13 \n""nd2w: CP R13,R16 \n""BRNE nd3 \n""CLR R13 \n""nd3: BRNE nd3w \n""INC R12 \n""nd3w: CP R12,R16 \n""BRNE nd4 \n""CLR R12 \n""nd4: BRNE nd4w \n""INC R11 \n""nd4w: CP R11,R16 \n""BRNE nd5 \n""CLR R11 \n""nd5: BRNE nd5w \n""INC R10 \n""nd5w: CP R10,R16 \n""BRNE nd6 \n""CLR R10 \n""nd6: BRNE nd6w \n""INC R9 \n""nd6w: CP R9,R16 \n""BRNE cready \n""CLR R9 \n""cready: CLT \n""BLD R8,0 \n""OUT %1,R8 \n""LDI R17,8 \n""WLP0: DEC R17 \n""BRNE WLP0 \n""CLR R8 \n""LDI R16,0x05 \n""SET \n""BLD R8,0 \n""CP R15,R16 \n""BRCS d2 \n""BLD R8,1 \n""d2: CP R14,R16 \n""BRCS d3 \n""BLD R8,2 \n""d3: CP R13,R16 \n""BRCS d4 \n""BLD R8,3 \n""d4: CP R12,R16 \n""BRCS d5 \n""BLD R8,4 \n""d5: CP R11,R16 \n""BRCS d6 \n""BLD R8,5 \n""d6: CP R10,R16 \n""BRCS d7 \n""BLD R8,6 \n""d7: CP R9,R16 \n""BRCS ok \n""BLD R8,7 \n""ok: OUT %1,R8 \n""LDI R17,3 \n""WLP1: DEC R17 \n""BRNE WLP1 \n""NOP \n""RJMP pploop\n":: "I"(_SFR_IO_ADDR(DDRD)), "I"(_SFR_IO_ADDR(PORTD)));}

author: westfw
author_rank: Super Contributor
timestamp: January 19, 2019, 11:00:26 am
content: (change PORTD and DDRD back to PORTA and DDRA for your m32...)

author: metrologist
author_rank: Super Contributor
timestamp: January 19, 2019, 01:41:58 pm
content: Thanks for looking at this and the code. I added void loop(){} and your code compiled. So now we just need the 10M coax to the clock (there are two clock pins?) and we can find the divided signals on other pins? I do have a handful of bootloaded bare 328p chips.

author: cncjerry
author_rank: Unknown
timestamp: January 19, 2019, 06:39:38 pm
content: I recently built a half dozen of the PICDIV chips. I also duplicated the front end from the TAPR TADD-2 to drive them. You can probably get away without the input transformer but I had them so I used them. The PICDIVs are the way to go as they only have a few ps of jitter. The PIC, I think it is a 12F675? , can't remember, but they are cheap. I bought a tube of them, programmed a bunch of different versions, and have a socket to emplace them when needed. I programed them with a PICIT III, I think it is called, it is the red version, and it takes about a minute.I'd have to look at Ulrich's code but I know he is a time nut so I'm sure the ADEV variance using the Arduino project vs the PICDIV would be low if even measureable. But when you conside how cheap the PICs are and how well the PICDIVs work, I recommend you try some of them as well.Jerry

author: iMo
author_rank: Super Contributor
timestamp: January 19, 2019, 06:54:51 pm
content: Under normal conditions the only measurable jitter you may get could be caused by noisy Vcc only, imho.