HobbyKing.com New Products Flash Sale
Reply
Thread Tools
Old Feb 12, 2013, 04:30 PM
RC beginner
New York
Joined Oct 2008
5,975 Posts
Quote:
Originally Posted by RJKIRK View Post
What chip are you running that on?

man! you dont even like to read the replies to your own posts. i highly recommened at least reviewing the few relevant to this subject before commenting further. theres only a handfull and it should take less than a minute.

Quote:
Originally Posted by dave1993 View Post

com r23
out 23,r23

this is a complete program, not a snip or fragmant. downloaded to a mega128 or mega8 it will flash the led continuosly at about the same rate as midelics code.
dave1993 is offline Find More Posts by dave1993
Reply With Quote
Sign up now
to remove ads between posts
Old Feb 12, 2013, 04:37 PM
RC beginner
New York
Joined Oct 2008
5,975 Posts
Quote:
Originally Posted by midelic View Post
Dave I have some doubts.Can I ask you about some asm snippet code regarding the loading the A7105 control registers,........... if I wrote code correctly skipping the (oxff) values from the table?Are you in a good mood for that?It is only a sample not all code.
you know im never really in a GOOD mood. and rarely enjoy hunting and pecking through other peoples code. some people are good at reading and copying existing code but its all greek to me. i will always just start from scratch and do it myself. even reading your blinky and the ones from rj was excruciatingly painful. you are lucky to be able to take advantage on thierrys upcoming tx code. not an option for me.

but you should also know by now im always glad to answer specific questions. this one might be more appropriate for the protocol thread though.
dave1993 is offline Find More Posts by dave1993
Reply With Quote
Old Feb 12, 2013, 04:44 PM
Registered User
midelic's Avatar
Joined Apr 2012
3,516 Posts
Ok no more code.This is also one doubt from thierry code I could not clarify completely.I need only this specific answer.
When loading data for A7105 control registers table, I need to skip the oxff bytes(-1 in pb table)...right?It is not very clear from the Thierry program, but is logical.
midelic is offline Find More Posts by midelic
RCG Plus Member
Latest blog entry: DIY Multiprotocol TX Module
Reply With Quote
Old Feb 12, 2013, 04:51 PM
RC beginner
New York
Joined Oct 2008
5,975 Posts
yes there are no register values for 0xff so it is a marker to skip that byte. it is very important that you continue to increment both the array and the register pointers or youll be out of sync. btw that method was from the original amicom reference program.
dave1993 is offline Find More Posts by dave1993
Reply With Quote
Old Feb 12, 2013, 04:52 PM
We want... Information!
Bruce Abbott's Avatar
Hastings, New Zealand
Joined Jan 2001
5,173 Posts
Quote:
Originally Posted by RJKIRK View Post
You guys really need to load Atmel Studio 6 C/C++ Compiler(it's free) and give it a whirl.
I downloaded and installed Studio 6. Tried compiling your code, but there seems to be a problem with the function 'delay_ms'. What am I doing wrong?

------ Build started: Project: FlashLED, Configuration: Debug AVR ------
Build started.
Project "FlashLED.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files\Atmel\Atmel Studio 6.0\Vs\Compiler.targets" from project "C:\Documents and Settings\pos\My Documents\Atmel Studio\FlashLED\FlashLED\FlashLED.cproj" (target "Build" depends on it):
Using "RunCompilerTask" task from assembly "C:\Program Files\Atmel\Atmel Studio 6.0\Vs\Compiler.Task.dll".
Task "RunCompilerTask"
C:\Program Files\Atmel\Atmel Studio 6.0\make\make.exe all
Building file: .././FlashLED.c
Invoking: AVR/GNU C Compiler : (AVR_8_bit_GNU_Toolchain_3.4.1_830) 4.6.2
"C:\Program Files\Atmel\Atmel Studio 6.0\extensions\Atmel\AVRGCC\3.4.1.95\AVRToolchain\ bin\avr-gcc.exe" -funsigned-char -funsigned-bitfields -O1 -fpack-struct -fshort-enums -g2 -Wall -c -std=gnu99 -MD -MP -MF "FlashLED.d" -MT"FlashLED.d" -MT"FlashLED.o" -mmcu=atmega8 -o"FlashLED.o" ".././FlashLED.c"
.././FlashLED.c: In function 'main':
C:\Documents and Settings\pos\My Documents\Atmel Studio\FlashLED\FlashLED\FlashLED.c(12,3): implicit declaration of function 'delay_ms' [-Wimplicit-function-declaration]
Finished building: .././FlashLED.c
Building target: FlashLED.elf
Invoking: AVR/GNU Linker : (AVR_8_bit_GNU_Toolchain_3.4.1_830) 4.6.2
"C:\Program Files\Atmel\Atmel Studio 6.0\extensions\Atmel\AVRGCC\3.4.1.95\AVRToolchain\ bin\avr-gcc.exe" -o FlashLED.elf FlashLED.o -Wl,-Map="FlashLED.map" -Wl,--start-group -Wl,-lm -Wl,--end-group -mmcu=atmega8
FlashLED.o: In function `main':
C:\Documents and Settings\pos\My Documents\Atmel Studio\FlashLED\FlashLED\Debug/.././FlashLED.c(12,1): undefined reference to `delay_ms'
collect2: ld returned 1 exit status
make: *** [FlashLED.elf] Error 1
Done executing task "RunCompilerTask" -- FAILED.
Done building target "CoreBuild" in project "FlashLED.cproj" -- FAILED.
Done building project "FlashLED.cproj" -- FAILED.

Build FAILED.
Bruce Abbott is online now Find More Posts by Bruce Abbott
Reply With Quote
Old Feb 12, 2013, 04:57 PM
Registered User
midelic's Avatar
Joined Apr 2012
3,516 Posts
Yes I wrote already the code ,increment the register address and data,tested in avr studio simulator every one of the 51 bytes skipping the oxff byte and its register.It is ok ,wanted only to be sure.
Thanks!


Quote:
Originally Posted by dave1993 View Post
yes there are no register values for 0xff so it is a marker to skip that byte. it is very important that you continue to increment both the array and the register pointers or youll be out of sync. btw that method was from the original amicom reference program.
midelic is offline Find More Posts by midelic
RCG Plus Member
Latest blog entry: DIY Multiprotocol TX Module
Reply With Quote
Old Feb 12, 2013, 04:59 PM
RC beginner
New York
Joined Oct 2008
5,975 Posts
Quote:
Originally Posted by Bruce Abbott View Post
I downloaded and installed Studio 6. Tried compiling your code, but there seems to be a problem with the function 'delay_ms'. What am I doing wrong?
that is understandable because there is little compatibility between c library routines. for example unlikely jakes pic code (even if corrected) would ever work on any avr compiler either and definitely not on his beloved 8051. keil dont even have a delay function. at least not in the freeby.

it has always amused me that c was supposed to solve all the portability problems between platforms when it actually has the opposite effect. kinda like windows was supposed to eliminate the driver issues of dos. lol! its amazing how much time and effort are spent struggling with tool issues on these so called high level lenguages. to me assembler was like the proverbial breath of fresh air.
dave1993 is offline Find More Posts by dave1993
Reply With Quote
Old Feb 12, 2013, 05:11 PM
RC beginner
New York
Joined Oct 2008
5,975 Posts
btw i should mention ive had no problem with delay_ms in raw winavr, arduino winavr, and also the version in as4. but atmel has deviated from the original gcc fork and who knows what they are up to these days.
dave1993 is offline Find More Posts by dave1993
Reply With Quote
Old Feb 12, 2013, 05:14 PM
Registered User
Columbus Ohio.
Joined Aug 2006
151 Posts
Noise???...lol...
OK...Let's do a code walkthrough assuming an ATMega 8.

Here's the code:

com r23
out 23,r23

On chip reset(powerup) all 32 general purpose registers are set to 0x00.
"com" is an instruction that takes a One's Complement of a register.
One's complement reverses all bit values.

So "com r23" takes a One's complement of the original value 0x00...and leaves a 0xFF in
Register 23.

"out 23, r23" takes the value in general purpose register 23 and puts it in I/0 byte 23(0x17) which is DDRB or the direction byte of I/O PortB...in other words you've just set all 8 pins of PortB to output(the output direction).

Then the program counter advances you to the next program instruction which is some random value in flash...and from there it's anyone's guess....

Maybe I just dumb...what am I missing...
RJKIRK is offline Find More Posts by RJKIRK
Last edited by RJKIRK; Feb 12, 2013 at 05:29 PM.
Reply With Quote
Old Feb 12, 2013, 05:28 PM
RC beginner
New York
Joined Oct 2008
5,975 Posts
Quote:
Originally Posted by RJKIRK View Post
On chip reset(powerup) all 32 general purpose registers are set to 0x00.
not true. most io registers are cleared on reset but none of the general purpose file. r0-31 remain exactly the same as before.

Quote:
Originally Posted by RJKIRK View Post
Then the program counter advances you to the next program instruction which is some random value in flash...and from there it's anyone's guess....

Maybe I just dumb...what am I missing...
you no dum but maybe missing one of the biggest benefits of assembler. that is, an understanding of avr archirecture and how these chips work. most asm guys are forced to get a handle on these kind of things while the c girls are out practicing cheerleader drills. jake pretty much summed it up in his post that started all this but amazingly considers learning a drawback. apparently part of the "foolish pride" theory.

as far as anyones guess we who are truly familiar with avr dont have to guess. it turns out ffff ffff is a perfectly valid avr instruction that does little more than harmlessly alter r31 and kindly provides nop functionality for my two word blinky. admittedly the blink rate is very fast on a small avr like t13 but, like with m8, with the right clock selection will do very well.

i was overly conservative with that coding example which is actually 200% bigger than necessary. would anyone like to see the much smaller (source and binary) led blink program i really use to test a new build?
dave1993 is offline Find More Posts by dave1993
Last edited by dave1993; Feb 12, 2013 at 06:41 PM. Reason: how about ONE instruction blinky?
Reply With Quote
Old Feb 12, 2013, 07:40 PM
Registered User
Columbus Ohio.
Joined Aug 2006
151 Posts
Observations from a "C" Girl........(but yes, I do understand ASM too)...

1. Blinking an LED assumes some sort of looping in your code. I see two sequential instructions...but no Jump instruction. How do you execute two instructions in succession without the need of a third JUMP instruction to get you back to the top for a second pass?

2. Every General Purpose I/O pin actually has three registers that control it:
The "DDR Register" Sets the direction of the pin(how is the pin used)..."0"=Input..."1"=Output.
The "PIN Register" is used to read the pin if the pin is set up for "input".
The "PORT Register" is used for pins set up for "output".
To toggle a LED you need to set the DDR pin to "1"(output)...then you need to toggle the PORT pin from "1" to "0" in a loop.

Dave, your two lines of code sets a DDR register but it never toggles a PORT Register.

Assuming you had an AtMega 8 with and LED Attached to Pin 1 on Port B, this is about the shortest your ASM code could be...4 lines:

sbi 0x17, 1 ; This sets bit 1 of DDRB register to a direction of output.
sbi 0x16, 1 ; This sets bit 1 of PORTB Register...to change state of LED.
cbi 0x16, 1 ; This clears bit 1 of PORTB Register...to change state of LED.
rjmp .-6 ; This jumps to the second instruction.

But even that's not good enough, as your LED would be blinking at about a 1MHZ rate. So you would probably see either nothing, or a dim solid light. A definitive blinking LED at this rate is out of the question. I've found that any rate faster than about 5 milliseconds will appear as a solid LED.

This "C" Girl is smart enough to know when someone is trying to blow a bunch of blue smoke up her dress...lol...
RJKIRK is offline Find More Posts by RJKIRK
Last edited by RJKIRK; Feb 12, 2013 at 07:55 PM.
Reply With Quote
Old Feb 12, 2013, 07:50 PM
Registered User
Columbus Ohio.
Joined Aug 2006
151 Posts
Quote:
Originally Posted by Bruce Abbott View Post
I downloaded and installed Studio 6. Tried compiling your code, but there seems to be a problem with the function 'delay_ms'. What am I doing wrong?

------ Build started: Project: FlashLED, Configuration: Debug AVR ------
Build started.
Project "FlashLED.cproj" (default targets):
Target "PreBuildEvent" skipped, due to false condition; ('$(PreBuildEvent)'!='') was evaluated as (''!='').
Target "CoreBuild" in file "C:\Program Files\Atmel\Atmel Studio 6.0\Vs\Compiler.targets" from project "C:\Documents and Settings\pos\My Documents\Atmel Studio\FlashLED\FlashLED\FlashLED.cproj" (target "Build" depends on it):
Using "RunCompilerTask" task from assembly "C:\Program Files\Atmel\Atmel Studio 6.0\Vs\Compiler.Task.dll".
Task "RunCompilerTask"
C:\Program Files\Atmel\Atmel Studio 6.0\make\make.exe all
Building file: .././FlashLED.c
Invoking: AVR/GNU C Compiler : (AVR_8_bit_GNU_Toolchain_3.4.1_830) 4.6.2
"C:\Program Files\Atmel\Atmel Studio 6.0\extensions\Atmel\AVRGCC\3.4.1.95\AVRToolchain\ bin\avr-gcc.exe" -funsigned-char -funsigned-bitfields -O1 -fpack-struct -fshort-enums -g2 -Wall -c -std=gnu99 -MD -MP -MF "FlashLED.d" -MT"FlashLED.d" -MT"FlashLED.o" -mmcu=atmega8 -o"FlashLED.o" ".././FlashLED.c"
.././FlashLED.c: In function 'main':
C:\Documents and Settings\pos\My Documents\Atmel Studio\FlashLED\FlashLED\FlashLED.c(12,3): implicit declaration of function 'delay_ms' [-Wimplicit-function-declaration]
Finished building: .././FlashLED.c
Building target: FlashLED.elf
Invoking: AVR/GNU Linker : (AVR_8_bit_GNU_Toolchain_3.4.1_830) 4.6.2
"C:\Program Files\Atmel\Atmel Studio 6.0\extensions\Atmel\AVRGCC\3.4.1.95\AVRToolchain\ bin\avr-gcc.exe" -o FlashLED.elf FlashLED.o -Wl,-Map="FlashLED.map" -Wl,--start-group -Wl,-lm -Wl,--end-group -mmcu=atmega8
FlashLED.o: In function `main':
C:\Documents and Settings\pos\My Documents\Atmel Studio\FlashLED\FlashLED\Debug/.././FlashLED.c(12,1): undefined reference to `delay_ms'
collect2: ld returned 1 exit status
make: *** [FlashLED.elf] Error 1
Done executing task "RunCompilerTask" -- FAILED.
Done building target "CoreBuild" in project "FlashLED.cproj" -- FAILED.
Done building project "FlashLED.cproj" -- FAILED.

Build FAILED.
Bruce...
What code were you trying to compile...what type of chip.
Show us the source code.
We need more info.

If the board you are flashing has an LED, compiling and flashing a simple blink program is the place to start. Get past that small step before trying to build larger programs.
RJKIRK is offline Find More Posts by RJKIRK
Last edited by RJKIRK; Feb 12, 2013 at 07:56 PM.
Reply With Quote
Old Feb 13, 2013, 12:13 AM
We want... Information!
Bruce Abbott's Avatar
Hastings, New Zealand
Joined Jan 2001
5,173 Posts
Quote:
Originally Posted by RJKIRK View Post
Bruce...
What code were you trying to compile...what type of chip.
Show us the source code.
We need more info.
ATmega8. Your code.
Bruce Abbott is online now Find More Posts by Bruce Abbott
Reply With Quote
Old Feb 13, 2013, 02:54 AM
We want... Information!
Bruce Abbott's Avatar
Hastings, New Zealand
Joined Jan 2001
5,173 Posts
Quote:
Originally Posted by Bruce Abbott View Post
Your code.
Figured it out:- delay_ms should be _delay_ms.

After spending almost 2 hours downloading and installing Atmel Studio 6, I only had 5 minutes left to create and build the project before having to leave for work. So I quickly started a new project, pasted RJKIRK's code into it, found the 'Build' button, and in a few seconds it was done (or at least it would have been - without the typo). Studio 6 takes a while to load, but seems pretty efficient once it gets going.

Now for the $64 question; which language produced the most compact and efficient code - RJKIRK's c source with Studio 6 compiler, or midelic's asm source with AVR Studio 4 asm? Right away I see that the compiled C hex file is 343 bytes, while the asm hex file is only 149 bytes - not what I was expecting! Let's disassemble them and see what's inside...

First the compiled 'c' version,
Code:
+00000000:   C012        RJMP      PC+0x0013 
+00000001:   C02C        RJMP      PC+0x002D 
+00000002:   C02B        RJMP      PC+0x002C 
+00000003:   C02A        RJMP      PC+0x002B 
+00000004:   C029        RJMP      PC+0x002A 
+00000005:   C028        RJMP      PC+0x0029 
+00000006:   C027        RJMP      PC+0x0028 
+00000007:   C026        RJMP      PC+0x0027 
+00000008:   C025        RJMP      PC+0x0026 
+00000009:   C024        RJMP      PC+0x0025 
+0000000A:   C023        RJMP      PC+0x0024 
+0000000B:   C022        RJMP      PC+0x0023 
+0000000C:   C021        RJMP      PC+0x0022 
+0000000D:   C020        RJMP      PC+0x0021 
+0000000E:   C01F        RJMP      PC+0x0020 
+0000000F:   C01E        RJMP      PC+0x001F 
+00000010:   C01D        RJMP      PC+0x001E 
+00000011:   C01C        RJMP      PC+0x001D 
+00000012:   C01B        RJMP      PC+0x001C 
+00000013:   2411        CLR       R1        
+00000014:   BE1F        OUT       0x3F,R1   
+00000015:   E5CF        LDI       R28,0x5F  
+00000016:   E0D4        LDI       R29,0x04  
+00000017:   BFDE        OUT       0x3E,R29  
+00000018:   BFCD        OUT       0x3D,R28  
+00000019:   E010        LDI       R17,0x00  
+0000001A:   E6A0        LDI       R26,0x60  
+0000001B:   E0B0        LDI       R27,0x00  
+0000001C:   E8E0        LDI       R30,0x80  
+0000001D:   E0F0        LDI       R31,0x00  
+0000001E:   C002        RJMP      PC+0x0003 
+0000001F:   9005        LPM       R0,Z+     
+00000020:   920D        ST        X+,R0     
+00000021:   36A0        CPI       R26,0x60  
+00000022:   07B1        CPC       R27,R17   
+00000023:   F7D9        BRNE      PC-0x04   
+00000024:   E010        LDI       R17,0x00  
+00000025:   E6A0        LDI       R26,0x60  
+00000026:   E0B0        LDI       R27,0x00  
+00000027:   C001        RJMP      PC+0x0002 
+00000028:   921D        ST        X+,R1     
+00000029:   36A0        CPI       R26,0x60  
+0000002A:   07B1        CPC       R27,R17   
+0000002B:   F7E1        BRNE      PC-0x03   
+0000002C:   D002        RCALL     PC+0x0003 
+0000002D:   C010        RJMP      PC+0x0011 
+0000002E:   CFD1        RJMP      PC-0x002E 
+0000002F:   9AA3        SBI       0x14,3    
+00000030:   E098        LDI       R25,0x08  
+00000031:   B385        IN        R24,0x15  
+00000032:   2789        EOR       R24,R25   
+00000033:   BB85        OUT       0x15,R24  
+00000034:   EF2F        SER       R18       
+00000035:   E639        LDI       R19,0x69  
+00000036:   E148        LDI       R20,0x18  
+00000037:   5021        SUBI      R18,0x01  
+00000038:   4030        SBCI      R19,0x00  
+00000039:   4040        SBCI      R20,0x00  
+0000003A:   F7E1        BRNE      PC-0x03   
+0000003B:   C000        RJMP      PC+0x0001 
+0000003C:   0000        NOP                 
+0000003D:   CFF3        RJMP      PC-0x000C 
+0000003E:   94F8        CLI                 
+0000003F:   CFFF        RJMP      PC-0x0000
18 redundant interrupt vectors, lots of unnecessary initialization, 47 instructions before it finally gets around to executing our code! And then it leaves a bit of flotsam at the end...

Now the asm version,
Code:
+00000000:   E50F        LDI       R16,0x5F 
+00000001:   BF0D        OUT       0x3D,R16 
+00000002:   E004        LDI       R16,0x04 
+00000003:   BF0E        OUT       0x3E,R16 
+00000004:   9AA3        SBI       0x14,3   
+00000005:   9AAB        SBI       0x15,3   
+00000006:   D003        RCALL     PC+0x0004
+00000007:   98AB        CBI       0x15,3   
+00000008:   D001        RCALL     PC+0x0002
+00000009:   CFFB        RJMP      PC-0x0004
+0000000A:   E70D        LDI       R16,0x7D 
+0000000B:   E71D        LDI       R17,0x7D 
+0000000C:   E72D        LDI       R18,0x7D 
+0000000D:   952A        DEC       R18      
+0000000E:   F7F1        BRNE      PC-0x01  
+0000000F:   951A        DEC       R17      
+00000010:   F7D9        BRNE      PC-0x04  
+00000011:   950A        DEC       R16      
+00000012:   F7C1        BRNE      PC-0x07  
+00000013:   9508        RET
Just what we coded, no more and no less.
Bruce Abbott is online now Find More Posts by Bruce Abbott
Reply With Quote
Old Feb 13, 2013, 03:02 AM
Registered User
United States, ID
Joined Sep 2011
417 Posts
Dave, it seems like everything you've said in the last few pages is wrong. Too much stuff to even cover.

The code I posted works just fine. "__delay_ms()" is a Microchip XC8 compiler macro. Yes it's a macro, and yes it breaks down to NOP instructions. It does very well figuring out the timing. However it decides the best way to structure the NOPs to get the desired delay... I don't care because I don't have to. I have better things to do with my time, like actually coding. Any time wasted on figuring out processor instructions is just that, wasted time.

The code you posted doesn't work, it won't blink a light.

Anyone who really wants to see the difference between asm and C should check out...
http://www.gooligum.com.au/tutorials...IC_Mid_C_1.pdf

They used to have them all online, now only some are free. The link above describes exactly what we're talking about...

Quote:
used an XOR operation to flip the GP1 bit every 500 ms, creating a 1 Hz flash with a 50% duty cycle.
That's our blinky program, so what did they find?

Microchip MPASM = 27 lines of code, 29 program memory words, 4 bytes of data memory, and a 0.15% timing error.

XC8 (free mode, no optimizations) = 11 lines of code, 35 program memory words, 4 bytes of data memory, and a 0.0024% timing error.

So for 15% more memory they got less than half the code length and much better timing accuracy.

All that is with no optimizations on the compiler!

In any case, RJK made some good points also. Unless you're really good at asm, AND memorize the 20-50 processor instructions for every processor you use, AND like recalculating your program timing for every little change you make... an optimizing compiler is going to smoke your ass every time!

Even if you DO have all that going for you... you've wasted a ton more of your time to create a very hard to read and overly long program in an archaic and unpopular language. For what? To save a couple bytes? It's just not worth it.
jakestew is offline Find More Posts by jakestew
Reply With Quote
Reply


Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Mini-HowTo TH9X Turnigy FlySky IMax modify module to SMA and 1W booster DIY FPV rotaryboots Radios 3 Jun 08, 2013 02:32 PM
Sold iMAX/Turnigy/FlySky TX module & RX John Kim Aircraft - General - Radio Equipment (FS/W) 0 Jan 12, 2011 12:33 PM
For Sale iMAX/Turnigy/FlySky TX module & RX John Kim Aircraft - General - Radio Equipment (FS/W) 2 Dec 09, 2010 11:15 AM
For Sale Turnigy/iMAX/FlySky TX module & RX John Kim Aircraft - General - Radio Equipment (FS/W) 0 Oct 27, 2010 11:03 AM