Thread: A little help, please?

  1. #1
    Registered User
    Join Date
    May 2009
    Location
    Ireland
    Posts
    13

    A little help, please?

    Hello,

    I was wondering if I could get a little help with a problem I have. I am trying to program a counter on an AVR microcontroller, and the code I have is below, but all I am getting out is zero's on the 4 seven segment displays.

    I am pretty new to C programming, so if anyone can spot any errors, that would be great!

    Code:
    #include <avr/io.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <avr/interrupt.h>
    
    void delay (unsigned int size)
    {
    	unsigned int i;
    	for (i = 0; i < size; i = i + 1);
    }
    
    void setup_hardware (void)
    {
    	DDRA = 0xFF;
    	DDRB = 0xFF;
    	TCCR0 = 0x03;
    	TIFR = 0x00;
    	TIMSK = 0x02;
    	//SREG = SREG | 0x80;
    	sei();
    }
    
    const unsigned char enable [4] = {0x10, 0x20, 0x40, 0x80};
    const unsigned char pattern [10] =
    	{0x7E, 0x0C, 0xB6, 0xCC, 0xDA, 
    			0xFA, 0xFE, 0xFE, 0xCE};
    	
    #define DISPLAY_SIZE 4
    unsigned char my_display [DISPLAY_SIZE];
    unsigned char led_counter = 0;
    
    #define POSITION_0 0
    #define POSITION_1 1
    #define POSITION_2 2 
    #define POSITION_3 3
    
    int main (void)
    {
    	unsigned int count = 0;
    	setup_hardware ();
    
    	unsigned char mins_units = 0, mins_tens = 0, 
    		hrs_units = 0, hrs_tens = 0;
    
    	//unsigned char hours = 0;
    	//unsigned int mins = 0;
    
    	mins_units = ((count%3600)/60)%10;
    	mins_tens = ((count%3600)/60)/10;
    	hrs_units = (count/3600)/10;
    	hrs_tens = (count/3600)%10;
    
    	my_display[POSITION_0] = pattern[hrs_tens];
    	my_display[POSITION_1] = pattern[hrs_units];
    	my_display[POSITION_2] = pattern[mins_tens];
    	my_display[POSITION_3] = pattern[mins_units];
    
    	while(1)
    	{
    		count = count + 1;
    		if (count == 43199)
    		{
    			count = 0;
    		}
    
    //		load_display (count);
    
    		delay(1000);
    	}
    }
    
    ISR(TIMER0_OVF_vect) 
    {
        //clear interrup flag???...done by hardware
    
    	//refresh();
    	PORTB = 0x00;						//Turns of all LEDs
    	PORTA = my_display [led_counter]; 	//Sets segments for the LED
    
    	PORTB = enable [led_counter];		//Turn the LED on
    	led_counter = led_counter + 1;		//Move onto the next LED
    
    	if (led_counter == DISPLAY_SIZE)
    	{
    		led_counter = 0;
    	}
    }
    Thanks for any help pr advice.

    Sean

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Problem need not revovle around C alone.....
    check out the link...
    Index :: AVR Freaks

  3. #3
    Registered User
    Join Date
    May 2009
    Location
    Ireland
    Posts
    13
    I know the AVR freaks site, thanks

    The ports ect are set up fine, I have used the same setup on different programs, and they are fine, so I figured the problem was in the coding somewhere, and I just don't see it.

    But thanks for your reply

    Sean

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your array has only 9 initialisers.

    > mins_tens = ((count%3600)/60)/10;
    And this is nowhere near being between 0 and 9

    You also don't update the digits array inside the loop, so it won't change anyway.


    Have you tested really basic stuff, like lighting specific segments without the complications of say ISR's ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by Salem View Post
    > mins_tens = ((count%3600)/60)/10;
    And this is nowhere near being between 0 and 9
    Yes it is. It is in fact between 0 and 5.

    Besides the array not defining 10 values, the patterns seem to be all wrong. Better double check which segments you want illuminated.

  6. #6
    Registered User
    Join Date
    May 2009
    Location
    Ireland
    Posts
    13
    Quote Originally Posted by nonoob View Post
    Yes it is. It is in fact between 0 and 5.

    Besides the array not defining 10 values, the patterns seem to be all wrong. Better double check which segments you want illuminated.
    Hmm, I don't know how I missed one of the digits in the array! Silly!!

    Also, when you say the pattern is wrong, do you mean the hex numbers in the array? I know they are a bit weird, but they are actually right - its got to do with the pins used in the microcontroller.

    The {0x10, 0x20, 0x40, 0x08}; picks the segment to be displayed.

    As for the actual maths bit that gets the numbers, that is still confusing me. I need to get that sorted in my head

    Thanks for the help
    Last edited by feely; 05-15-2009 at 03:03 PM.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No, I meant the

    pattern [10] = {0x7E, 0x0C, 0xB6, 0xCC, 0xDA, 0xFA, 0xFE, 0xFE, 0xCE}

    doesn't appear to light up any sensible segments no matter how I order them. Oh well, you'll see once it gets displayed.

    Feel free to PM me if you want more detailed explanation of the math part.

  8. #8
    Registered User
    Join Date
    May 2009
    Location
    Ireland
    Posts
    13
    I know, it is a weird patten, but it does actually work, took me ages to get it right . The pattern is off because one of the pins on port b sets a buzzer, to keep the buzzer off, I need to keep that pin at a zero. This changes the pattern

    Also, thanks for the offer of help. I will more than likely PM you tomorrow when I get to work.

    Sean

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by feely View Post
    I know, it is a weird patten, but it does actually work, took me ages to get it right . The pattern is off because one of the pins on port b sets a buzzer, to keep the buzzer off, I need to keep that pin at a zero. This changes the pattern

    Also, thanks for the offer of help. I will more than likely PM you tomorrow when I get to work.

    Sean
    I don't believe it is correct either.
    I'm familiar with the concept of one of the bits controlling a buzzer or other device. I've worked on such a product. I assure you, that table is wrong. The first three entries do look like they could correctly correspond to 0, 1 and 2 (they have the right number of bits set). However:

    The entry for 3 only has 4 bits set when it would need to be 5.
    The entry for 4 has 5 bits set when it should be only 4.
    The entry for 5 only has 6 bits set when it would need to be 5.
    The entry for 6 only has 7 bits set when it would need to be 6. Not only that but the segments for a six are IDENTICAL to that of a seven, which if nothing else does, this proves beyond the shadow of a doubt that it is wrong.
    The entry for 8 only has 5 bits set when it would need to be 7.
    The nine has no segments whatsoever on. Heck you aren't even specifying that element of the array, so the compiler will be padding it with a zero.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    May 2009
    Location
    Ireland
    Posts
    13
    Okay, maybe I have screwed up somewhere ....I will check through it all this morning.

    Thanks

    EDIT:

    Okay, the array is

    Code:
     {0x7E, 0x0C, 0xB6, 0x9E, 0xCC, 0xDA, 0xFA, 0x4E, 0xFE, 0xCE};
    And I have attached a copy of how I worked out the segments.
    Last edited by feely; 05-18-2009 at 01:59 AM. Reason: Adding information

  11. #11
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yeah that looks good.

  12. #12
    Registered User
    Join Date
    May 2009
    Location
    Ireland
    Posts
    13
    I still am just getting the zeros on the seven segment ...I can't figure it out

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Perhaps you mean for these lines of code to be inside the while loop as well:
    Code:
    	mins_units = ((count%3600)/60)%10;
    	mins_tens = ((count%3600)/60)/10;
    	hrs_units = (count/3600)/10;
    	hrs_tens = (count/3600)%10;
    
    	my_display[POSITION_0] = pattern[hrs_tens];
    	my_display[POSITION_1] = pattern[hrs_units];
    	my_display[POSITION_2] = pattern[mins_tens];
    	my_display[POSITION_3] = pattern[mins_units];
    Or do you mean to uncomment load_display?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    May 2009
    Location
    Ireland
    Posts
    13
    Quote Originally Posted by iMalc View Post
    Perhaps you mean for these lines of code to be inside the while loop as well:
    Code:
    	mins_units = ((count%3600)/60)%10;
    	mins_tens = ((count%3600)/60)/10;
    	hrs_units = (count/3600)/10;
    	hrs_tens = (count/3600)%10;
    
    	my_display[POSITION_0] = pattern[hrs_tens];
    	my_display[POSITION_1] = pattern[hrs_units];
    	my_display[POSITION_2] = pattern[mins_tens];
    	my_display[POSITION_3] = pattern[mins_units];
    Okay, I will give that a go when I get back in on Wednesday Thanks ....I guess it would make sense for this to be in the loop!

    On a side note, I can't view anyones profile, to PM them ...are profiles always blocked?

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I tried to initiate a PM with you, feely, but the system said you are unable or refuse to receive PMs. So I tried to add you to my contact list, whatever that does. Sorry this forum is not quite working the way I expected.

    Sorry you're still having trouble with your 7-segment display project. Have you not made a simple test program first to check lighting up segments? As the previous poster said, your loops may be wrong. That's quite a basic flaw I didn't even think of. I thought perhaps your hardware was malfunctioning.

Popular pages Recent additions subscribe to a feed