7-segment Display

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

I have written code for 7-segment display using timer 2 in interrupt mode.

The problem with it is that the code gets stuck in the ISR of Timer-2 & statements in while loop never gets executed.

Please suggest me any corrections/modifications. 

Please find attached code for reference.

Attachment(s): 

Gaurav Gawade

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

For starters you do  not put functions in a .h file.  They go in a .c file.

 

Your code appears incomplete as there is nothing in either file for configuring the timers, or the timer ISR's.

 

JIm

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 code isn't incomplete as the configuration of timers is done in main function itself.

Please check. Also this is just a trial code for testing purpose only.

 

Gaurav Gawade

Last Edited: Tue. Jul 25, 2017 - 02:51 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How is this different to your last post - you were advised to clean up your code. You can see we're having trouble making sense of it - I looked at it and my immediate reaction would be to rewrite it so I could understand it. Once again, don't put code in .h files.

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

I have modified the code once again. Please suggest me any modifications if any.

Also note that the statements inside the main doesn't get executed at all.

Please help.

Attachment(s): 

Gaurav Gawade

Last Edited: Tue. Jul 25, 2017 - 03:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You don't #include C files either.

 

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

I'm surprise that you can compile and run your code as I got 3 errors with that.

And you can found:

gauravg20 wrote:
note that the statements inside the main doesn't get executed at all

 

Make display.h file for your prototype and macro defining and the function inside display.c.

You don't attach your schematic so how could someone know your wiring?

 

 

 

MG

I don't know why I'm still doing this hobby

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

I have modified the code.

Even now, the statements inside while loop doesn't get executed.

Please suggest any corrections for the same.

Attachment(s): 

Gaurav Gawade

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

Does this thread/code have ANYTHING to do with your other thread on 7 segment displays?

 

Jim

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

Thread/Code?

Gaurav Gawade

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

Here's what I did:

 

Timer 1 setup:

OCR1A=0x0A12; <- 0x7A12 is too big

Timer 1 ISR

ISR(TIMER1_COMPA_vect)
{
  if(++c > 9) c=0; //count to 10 only for display test
}

Timer 2 ISR:

ISR(TIMER2_COMP_vect)		
{
  clrbit(PORTB,pin++);	        

  ASCII_to_7segment(disp[pin]); <- 
                                  | I swap this 2 from your code
  if(pin==4) pin=0;             <- 

  setbit(PORTB,pin);		
}

In main:

int main()		
{
    
    DDRA=0xFF;			// Segments
    DDRB=0xFF;			// Control Lines

    timer1_init();
    timer2_init();

    //num2digit(c); <- what is this for anyway ?
    
    sei(); <- put this just before loop after setup
    
    while(1)
    {
     num2digit(c);
    }

}

Without schematic I can only guess the wiring.

Good luck

 

 

 

MG

I don't know why I'm still doing this hobby

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

See, num2digit() is only to split a number into individual digits on the 7-segment display using array disp[].

Regarding value of OCR1A, it is the value for 1 s delay at 8 MHz on-chip oscillator frequency.

Also, I want to change the value of the variable c if toggle is set to one in ISR of Timer 1 after 1 s delay.

The above logic doesn't work as desired i.e. The statements inside the while loop doesn't get executed at all.

Please check.

Also suggest any modifications for the same. 

 

Gaurav Gawade

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

What have you done to debug the problem? Have you run the code in the Atmel Studio simulator?
Have you determined if enabling interrupts is causing a problem.

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

Others have told you not to put code in .h files but when I look at your latest set of code (#8) the display.h still has:

unsigned char pin,disp[5]={"PAGE"},toggle;

Apart from "don't put code in .h" the other rule is "don't put variables in .h either". If there's some reason these variables need to be "seen" outside of display.c then you can DECLARE them in the .h as extern's such as:

extern unsigned char pin,disp[5],toggle;

Now, another .c that does a #include of display.h will be able to make a reference to any of these even though they are actually created in display.c

 

Anyway this just seems identical to your previous thread. All the same old mistakes. Your num2digit() (as shown in display.c from #5) just does:

void num2digit(int no)
{
	unsigned char i=3;
	
	while(i!=255)
	{
		disp[i]=no%10;
		disp[i]+='0';
		no/=10;
		i--;
	}

	if(disp[0]=='0')
		disp[0]='x';
	if(disp[0]=='x' && disp[1]=='0')
		disp[1]='x';
	if(disp[1]=='x' && disp[2]=='0')
		disp[2]='x';
}

Apart from things like the whacky use of 'i' (did you ever hear of for() loops ?) all this function does is write ASCII characters to disp[]. Nothing in this does anything about converting those ASCII to 7seg values or outputting them to a port do what exactly is this test program trying to achieve?

 

Oh and if you do want "int no" as 3 ASCII digits isn't sprintf() a whole heap easier? (one of your comments say ATmega16 so it's not like you don't have the flash space).

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

clawson wrote:
Others have told you not to put code in .h files

Including clawson himself - in your other thread on, apparently, the same topic:

 

https://www.avrfreaks.net/comment...

 

 

Apart from "don't put code in .h" the other rule is "don't put variables in .h either".

Specifically, don't put variable definitions in .h files (declarations are fine, as Cliff went on to say).

 

Again, this has all been pointed out to you already in your other thread: https://www.avrfreaks.net/comment...

 

Anyway this just seems identical to your previous thread. All the same old mistakes.

Indeed - that's what Jim was asking about in #9

 

did you ever hear of for() loops ?

Again, check out those 'C' learning & reference materials posted in your other thread: https://www.avrfreaks.net/comment...

 

 

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...
Last Edited: Tue. Jul 25, 2017 - 08:45 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you try the changed code I posted above? I already tested it. It gave me counting display from 0 to 9 loops test.

 

gauravg20 wrote:
Regarding value of OCR1A, it is the value for 1 s delay at 8 MHz on-chip oscillator frequency.

I'm aware of that. 

I changed it to 82ms ISR to get rapid changing of 5 x 82ms displays to see that it can display the loop as expected, instead of waiting for 10 sec.

I forgot that I'm using simulator to test. Ignore that.

 

gauravg20 wrote:
Also, I want to change the value of the variable c if toggle is set to one in ISR of Timer 1 after 1 s delay.

The code I posted above did that.

 

gauravg20 wrote:
The statements inside the while loop doesn't get executed at all.

Wrong! It did executed! The way you do it is wrong. That's why I made those changes above.

 

gauravg20 wrote:
Please check.

I already done it for you and you don't bother to check it don't you?

 

But I don't see the section of separating value you want to display to those 5 display. They all will show the same value.

 

I guess you are working with 4 displays ? But since I don't know it for sure so I make it to 5.

If you do use 4 displays, ignore the swap in timer 2 ISR and use your own setup.

 

 

 

MG

 

I don't know why I'm still doing this hobby

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

Thanks everybody for your kind suggestions.

Gaurav Gawade