Code_help

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

Hello,

I'm writing a program to display a number using 7-segment display. I'm also trying to set the decimal point for the same.

The decimal point will be set if I make the value of the variable equal to 1. This is done inside an infinite while loop.

But the problem is that the statements inside the loop doesn't get executed at all.

I have attached my C code for the same.

Please help me out.

Attachment(s): 

Gaurav Gawade

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

gauravg20 wrote:
The decimal point will be set if I make the value of the variable equal to 1. This is done inside an infinite while loop. But the problem is that the statements inside the loop doesn't get executed at all.

...
    num2digit(123);

    toggle=1;
    while(1);

}

What statements are you talking about?

 

 

Stefan Ernst

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

You have code in .h files - that's a pretty bad idea before you even start!

 

Anyway that code enables 3 interrupts sources and also uses sei() but does not provide ISR() routines to catch the interrupts. (and why 3 anyway?).

 

Also the code makes no sense what soever. How on earth do you think writes to disp[] are actually going to be reflected in the hardware anyway. Also you appear to be writing ASCII digits ('0' is a much better idea than 48 !) but while here is a case_select() function (terrible name) that may do ASCII to 7seg conversion I don't see it invoked?

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

I am sorry. I didn't give the correct code. I'm reattaching the code again for your reference.

Attachment(s): 

Gaurav Gawade

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

First of all, see, I am enabling only two interrupts of T1 & T2, please check. I have only written the function of timer 0 for future use, but it is not enabled.

Second, please suggest me where to write code, instead of .h files.

Third, regarding disp[], you may check that it displays the characters on the 7-segment display because I have used Timer-2 for displaying data using ISR of Timer 2 & the case_select() function gets invoked over there.

Gaurav Gawade

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

gauravg20 wrote:
Second, please suggest me where to write code, instead of .h files.

In C files. It is perfectly normal, not to say it is the norm, to have code in .c files and declarations in a .h file of the same name. The .h file is a sort of "contract" between the implementor and the user of the functionality in the .c file.

 

For more and details, go to the Tutorials subforum here and locate a tutorial named "Managing large projects" (or something similar).

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]

Last Edited: Mon. Jul 24, 2017 - 07:16 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gauravg20 wrote:
. I'm reattaching the code again for your reference.
Still meaningless. All my previous comments apply. You appear to have made no attempt to fix any of the faults I mentioned. Also you have:

 

		if(toggle==1)
		{
			c++;
			num2digit(c);
		}

but nothing in that code ever sets toggle to 1. So the main contents of the while(1) will never execute.

 

Can I suggest you simply throw this all away.

 

Don't start writing C. Start by writing a DESIGN for what you want to achieve. What is the program supposed to do. What "building blocks" do you think you need to achieve that? If you plan to use interrupts how many will you use and what will it/they be doing?

 

When you have a clear design in your mind (better still sketched out somewhere) then you begin to write the C code that will realise that design.

 

This will make for a much clearer and well thought out sequence to your code.

 

(and yes, if it helps, you might be able to re-use something of what you have written already (though actually I doubt it)).

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

JohanEkdahl wrote:
go to the Tutorials subforum here and locate a tutorial named "Managing large projects" (or something similar).

+1

 

Here: https://www.avrfreaks.net/forum/tut-modularizing-c-code-managing-large-projects

 

See also: http://c-faq.com/decl/decldef.html

 

And here are some other 'C' learning & reference materials for you: http://blog.antronics.co.uk/2011/08/08/so-youre-thinking-of-starting-with-c/

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...