First 'C' program - Question

Go To Last Post
73 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Meteor: When the hell do you have time to search old magazines to find that! I love it. Going to print an 8 x 10 and put it over my primary pc as an inspiration piece. :)
Well I have Assembler under control, so now it's a matter of the A,B,......errr

@ Cliff

Quote:
int uart_putchar(char c, FILE *stream);
Going to have to do my homework and figure out the FILE *stream thingie
Quote:
FILE uart_str = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
Another homework assignment
Quote:
stdout = &uart_str;
I think this will be answered when I figure out quote 2.

Yes, this works and I understand it as well to some degree, I also stumbled upon the article you posted as well earlier so I will add that to my evenings read.

Thanks! Some progress

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
I love it. Going to print an 8 x 10 and put it over my primary pc as an inspiration piece. :)
Glad to be a source of inspiration, however minor and/or frivolous. :)
jgmdesign wrote:
When the hell do you have time to search old magazines to find that!
I was (finally!) cleaning up some very old (back to 1986 :shock:) articles I'd saved on PC video (CGA, EGA, VGA, etc) and ran across that ad on the backside of one. I immediately thought of you. Be either flattered or offended -- your choice. ;) :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Going to have to do my homework and figure out the FILE *stream thingie

Well here's the thing. Once again, like that UART_putstring() implementation with all it's *str++ nonsense this file stream stuff is a real black art - pretty advanced stuff for a beginner so for the time being do NOT try to understand how it works. Just take the code as a template then feel free to use printf() and friends inside you main() function and don't concern yourself (for the time being) with how it's actually working as you'll get bogged down in that and not learn the simple bits of C which having printf() working should make a lot easier.

If you have it working I guess you already spotted that the UBRR= value needed adapting to whatever F_CPU and desired baud you use.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am confused.

Surely everyone has a requirement to "print a character" to some device. Whether you use ASM, C, Basic, ... you still have the same need.

So you look up the documentation for CALL, putchar(), PRINT, ... and see both the syntax and examples.

You don't debate the traffic rules every time you drive your car. You just drive on the left side of the road. (or whichever is customary in your country)

I can fully understand people having problems with the semantics of a computer language. However if you understand ASM you should understand variables, parameters, addresses etc.

Many C or Basic programs can be written with the simplest construction. You progress from Blinky to Hello World to Flight Simulator.

Oh, at some stage you may want to look under the hood. Or perhaps you don't. You can still drive the car or write a working program.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, suppose OP is very good at atmel's assembly (and AVR) -I noticed it-. He is entitled to find C/ Basic very strange (even if things work, and an optimiser can make things often smaller/faster than what an ordinary man could do with assembly : having things already working is not very stimulating).

There is a thing which is often forgotten with C : as it is highly portable, parts of an AVR program (say, dealing with date arithmetics/ conversions/ general purpose algorithms) can be easily tested ... on a PC (most of the people have such things, to try to program their AVRs, say).

And, if one wants to change, pieces of code can be ported to ARMs , and one "just" has to concentrate on ones new hardware specificities.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It can be simpler. C has been around since the 70s and its workings are well known and standardized. On a computer with a file system, printf generates calls to putchar which output to a file, usually stdout, usually the console. On a micro with no file system, it sort of makes sense to have printf call putchar, which has a default to write to uart0, but it could also write to an lcd etc. Imagecraft provides a default putchar, but once you have a putchar that writes a char to where you want it to go, you just call printf and everything is cool. I have a 'fancy' putchar that writes to one of the four uarts on a mega1280 based on a global variable that I put a 0,1,2 or 3 in. Just call printf, and it calls putchar. Too simple.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes. And an ASM programmer is used to the concept of passing parameters to a subroutine. And that subroutine passing other parameters to an output subroutine.

I just accept that my PC will be eventually writing bits to my LCD screen. No doubt I could disassemble the BIOS or write my own operating system, but I might be dead before I ever get there.

Of course an AVR application is a lot simpler. Not only can you read the complete ASM code, you may even understand the individual gates inside the Silicon.

I find it easier to read library documentation and follow instructions. I might have a specific interest in one tiny component, and drill down to a 'lower level'.

Think about it. Common tasks like 'output character' have been perfected since the beginning of time. Just use the prior knowledge. Gain experience and confidence with the new language first. Broaden your knowledge at a later stage.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Yes. And an ASM programmer is used to the concept of passing parameters to a subroutine. And that subroutine passing other parameters to an output subroutine.

In my experience (mainly from posts here) the C concepts Asm programmers do have a problem with when trying to migrate are variable types and stack frames. I know the Asm programmer already had a "foo: .dw 12345" and might have treated it as either 0..65535 or -32768..+32767 but they didn't call this "unsigned int "or "signed int" but just knew what range the held value would represent without a type. As for stack frames and automatics - that simply doesn't have an equivalent in the Asm world. A called function either used some number of the 32 registers for its "locals" or they farmed them out to named locations in SRAM. The idea of dropping the stack and pointing an index register at its base and indexing off that for variables is an "odd" concept. (it's also, on the whole, what mainly makes it tricky to disassemble C code).

Oh and type promotion. If an Asm programmer wants a 24bit value passed to a function he picks 3 registers to hold it. The idea of the value first being padded out to 4 registers (especially if "signed") and then passed is an anathema.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to post a new reply to this and when I include code and quotes I get PAGE NOT FOUND??

Edit:
I had to look up "ANATHEMA". Cliff I assume the dictionary term of LOATHING/DESPISE is what you were driving at and not the English band from Liverpool bearing the same name? :)

AS my header states I can not post code or quotes else I get the HTTP ERROR:400 Page not found

I got Cliffs usart code running. When I inserted it into my code the compile was successful, but the build notes that the code takes up 2300+ bytes of space with CODE+DATA+BOOTLOADER. I never asked for a bootloader.

Now if I comment out the part where pi, and a few other tidbits are printed I get CODE+DATA+BOOTLOADER equals 746 bytes which is still high, but not unreasonable. Whats with the BOOTLOADER?

Can anyone explain this while I go over the assembler code?

Thanks

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

CODE+DATA+BOOTLOADER. I never asked for a bootloader.

No, it's saying that if you had put something into .bootloader it would be added into that figure (but usually it's 0).

If FP is causing that much bloat make sure you link with libm.a (I'm about to eat my kebab so I'll leave others to explain more if necessary).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looked at my makefile and see this:

MATH_LIB = -lm

From this post:
https://www.avrfreaks.net/index.p...

references to it.

So does this mean that I am in fact looking at libm.a?

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK as long as it is being used. Obviously there is a cost for using float but if you stick to just +-*/ then (with libm.a) it generally only "costs" about 700 bytes. If you start using library functions like sin()/cos()/tan() then the cost goes up and could be several K.

Unless you are generating sound waves or something I'm guessing an embedded engineer probably doesn't need to use much more than +-*/ in floats and as an interesting thread the other day showed you can usually replace that with integer ratios anyway.

Of course if you have a "big micro" the few K that things like float or printf/scanf support cost probably don't matter in the great scheme of things. 1-2K here or there matters little in 64K or 128K but perhaps is a little more important in 2K-4K micros!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The compiler organizes code and data in segments. For historical reasons (from Unix) the "ordinary" code segment is called "text". The data, global variables', is in the segments "bss" (for variables that are not explicitly initialized by you in the code, and thus initialized to zero by the compiler) and "data" (for global variables that are explicitly initialized by you in the code).

The contents of the latter has to be kept somewhere so that it can be loaded into RAM when execution starts, and for AVRs this somewhere is in flash. Thus the initialization values will have a flash cost also.

To report the total flash consumption the tool chain thus has to sum the code segment ("text"), the "data" segment, and also any code segment for bootloader code. The text "CODE + DATA + BOOTLOADER" is used at all times, even if there is no bootloader.

For more on data segments see the Wikipedia article on "Data_segment".

HTH!

I have stopped referring to other threads here at AVRfreaks, or to any external web pages, by actually giving the URL. I refuse to fill in a stupidly dysfunctional CAPTCHA when referring to threads here at AVRfreaks, or to absolutely legitimate sites such as Wikipedia, Savannah etc. It should not take someone at Atmel long to fix this, but the writing on the wall is that their web department is also dysfunctional.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"I have stopped referring to other threads here at AVRfreaks, or to any external web pages, by actually giving the URL. I refuse to fill in a stupidly dysfunctional CAPTCHA when referring to threads here at AVRfreaks, or to absolutely legitimate sites such as Wikipedia, Savannah etc. It should not take someone at Atmel long to fix this, but the writing on the wall is that their web department is also dysfunctional. "

It is one of the least stupidly dysfunctional CAPTCHAs I could see (try posting here http://rroms.blogspot.fr/ without getting crazy : on AVRfreaks, there remains a little tiny hope) ... Things might be WORSE....
(do they detect locked caps?)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Unfortunately I have been banned from putting quotes, and code into replies, bothe NEW REPLY and quick reply so I cannot post what is confusing me as to why the printf statement that has pi, a charachter, and a negative number takes up so much space in CODE that the Tiny is over its limit, but if I take the line out then I only use 700bytes

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The % (percent sign) is known to cause problems here.

The easiest workaround is to add a space after each percent sign. The usual suspects will understand why your format strings look like that (e.g. "% d").

If your pi is a float and that is the only float you use, that could account for the code size increase as libraries to handle floats will be included.

Sid

Life... is a state of mind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Post an attached image with a screenshot showing the line in question.

Note:

1) that floating point math has two potential "costs". One is about 700 bytes to get +-*/ and another is >1K to also get trig and other floating point functions.

2) printf actually has three costs. There is a "min" version that can only print simple formats including integers but not floats. It only costs a few hundred bytes. There is a "normal" version that costs about 1K or more that can do pretty much every format EXCEPT %f for float. Then there is a "float" version that costs the most and can do pretty much everything including floats.

I imagine your code is trying to use libprintf_flt.a and a minmum of +-*/ from the fp lib for a total cost of a bit over 2K?

As I said previously this "heavy stuff" (printf and also float) don't matter one jot in a 16/32/64/128K micro but you'll really feel the squeeze in a 1/2/4K micro. Only use them when appropriate (the right chip and the right requirement that really needs them). Often in a 1/2/4K micro you can give the appearance of float support (say a 0..1023 ADC reading shown on an LCD as 0.00V..5.00V) using scaled integer at the cost of only a few hundred bytes.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This built for mega16:

#include 
#include 
#include 

char buff[20];

int main(void) {
	sprintf(buff, "2pi = %f", 2 * M_PI);
}
	Program Memory Usage 	:	2096 bytes   1.5 % Full

But it just prints ?" in place of %f

Using libprintf_min.a:

	Program Memory Usage 	:	1744 bytes   1.3 % Full

this too just prints ?.

Finally with libprintf_flt.a:

	Program Memory Usage 	:	3646 bytes   2.6 % Full

and the output is:

2pi = 6.283185

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Cliff,
The code snippet is what you posted in the third page of this thread.
https://www.avrfreaks.net/index.p...

About half way down. THe PRINTF statement with the pi, the charachter and the negative number.

I'll try to post a screenshot in the morning if you don't see it

Thanks

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ah you mean:

  printf("pi = %f, char = %c, int = %d", 3.1415926, 'A', -12345); 

Well that's almost identical to the examples I quoted above (where I realised afterwards that the build target was set to xmega128a1 so the sizes are a bit bloated because of the huge interrupt table)

If you want %f support it requires the big printf library (libprintf_flt.a) and that is going to cost about 3K.

As I keep saying - this doesn't matter in a >=16K chip but does in a <=4K chip. So just accept the fact that you aren't going be able to use floats and print them in small AVRs. Either use a big chip in which case you don't care or come up with a solution that does not involve using and printing floats.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

So just accept the fact that you aren't going be able to use floats and print them in small AVRs. Either use a big chip in which case you don't care or come up with a solution that does not involve using and printing floats.

Well, there are more options. We found recently that integer and FP "scaling" have about the same cost:
https://www.avrfreaks.net/index.p...
This is in line with what I've found in the past: If you need to e.g. convert A/D counts to arbitrary [pressure or whatever] units, a single FP multiply has about the same cost as an elaborate integer scaling.

Now, on to the output: No-one says that (s)printf is the only way to convert this FP value to a displayable string. ftoa() comes to mind as a straightforward approach. (Remember that we've been through this exhaustively with integers and long integers and printf family and itoa/ltoa and homegrown solutions.) With my toochain, ftoa() costs 878 bytes and big (s)printf() costs 2183 bytes.

In my past work I referred to, the destination was 7-seg displays. So I indeed wanted BCD and an implied decimal point. I found a posted project here on 'Freaks that broke up a FP number into the pieces useful for such a display. I don't remember the cost but it was less than printf or ftoa. If I had to guess, perhaps half?

I guess I'm just telling my experience. I use very little FP in my apps. "Normal" work starts with small integer numbers such as A/D counts or timer counts. Given 8/10/16 bit inputs there is no need to calculate e.g PID values to 5 decimal places.

Thus, it is very ironic that where I >>have<< used FP is on Mega48 apps, resource constrained, where I found that it was efficient and straightforward. But remember, that was >>a single FP multiply<<, and perhaps the to/from FP routines. Throw in + and - and / and a different ballgame.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

meteor wrote:
bobgardner wrote:
Is that the Source Safe?
This is Microsoft we're talking about here, so their idea of "safe source" would look a lot more like this: :lol:
'Tain't orange, but I have one of those safes.

Moderation in all things. -- ancient proverb

Pages