C Program Doesnt work with custom function

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey guys,

 

I have a problem with my avr c programs, it works as long as I only use the main function, but when I add my own function it doesn't work anymore.

 

This works (my LED turns on and stays on)

#include <avr/io.h>

int main (void) {
    DDRA = 0xFF;
        PORTA = 1 << PA0;
    return 0;
}

And this doesnt (my LED stays off)

#include <avr/io.h>

void add(){
}

int main (void) {
    DDRA = 0xFF;
        PORTA = 1 << PA0;
    return 0;
}

This is the script I use for compiling and flashing

compile: 'avr-gcc -mmcu=atmega32a -Wall -Os -c main.c -o main.o',
link: 'avr-gcc main.o -o main.elf',
hex: 'avr-objcopy -O ihex -j .text -j .data main.elf main.hex',
flash: 'avrdude -p m32 -c usbtiny -F -e -U flash:w:main.hex'

 

 

Does anyone know where the problem is, am I compiling it wrong so that it doesnt the errors?

This topic has a solution.
Last Edited: Thu. Oct 26, 2017 - 08:19 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

danielr1996 wrote:
when I add my own function it doesn't work anymore.

"doesen't  work" can mean anything. We can not guess what you're observing.

 

Tell us what you observe with the working variant. Tell us what you observe with the variant that "doesen't work".

 


 

Apart from that: The name is "blinky" suggesting it should make a LED on port A pin 1 blink.

 

But even the code you claim work does not do that. it simply alters the state of the pin once, and then the program ends.

 

If your LED is actually blinking then that suggests something else is going on, like the AVR constantly resetting itself.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"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

You have to add -mmcu=atmega32a to the linking command line too.

Stefan Ernst

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

JohanEkdahl wrote:

danielr1996 wrote:
when I add my own function it doesn't work anymore.

"doesen't  work" can mean anything. We can not guess what you're observing.

 

Tell us what you observe with the working variant. Tell us what you observe with the variant that "doesen't work".

 


 

Apart from that: The name is "blinky" suggesting it should make a LED on port A pin 1 blink.

 

But even the code you claim work does not do that. it simply alters the state of the pin once, and then the program ends.

 

If your LED is actually blinking then that suggests something else is going on, like the AVR constantly resetting itself.

 

I updated my question.

Does work means my led turns on and stays on.

Doesnt work means my led stays off

 

originally it should blink, but I changed the code to the easiest so I can see it is a logic error

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sternst wrote:

You have to add -mmcu=atmega32a to the linking command line too.

 

A first test shows that it works know, can you tell me why it doens't work without this?

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

danielr1996 wrote:
I updated my question.

Don't do that!

 

Now we have answers (#2 and #3) which refer to the original question, but we can no longer see the original question - so those answers no longer make sense!

 

So add or update stuff, do it in a reply.

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

danielr1996 wrote:
return 0;
 

To where, exactly, do you think that this is going to "return" ?

 

The programs in microcontrollers like this don't "return" - because there is nowhere for them to "return" to!

 

The programs in microcontrollers like this just keep running until the power dies.

 

 

 

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

@awneil: While what you say is generally sound advice, it can also be noted that the avr-libc run-time actually has this code to handle returning from main() :

 

0000007a <_exit>:
  7a:	f8 94       	cli

0000007c <__stop_program>:
  7c:	ff cf       	rjmp	.-2      	; 0x7c <__stop_program>

So.. The net effect of executing a return from main() is

i) Interrupts are disabled

ii) the program goes into an endless loop

 

I've used this on occasion for a firmware that was "one-shot". The OPs example of just lighting up an LED is such a one-shot.

 

Still, the cost of inserting ones own

while (1);

at the end of main() is negligible, so one might just as well get used to that habit.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"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

Good point.